AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
controller.cpp
1 #include "header/controller.h"
2 
7 Controller::Controller(std::string robotName) {
8  this->robotName = robotName;
9  std::cout << "[" << robotName<< "][CONTROLLER] Start " << std::endl;
10 }
11 
12 
20 int Controller::updateMultipleTasksMatrices(std::vector<Task*> tasks, struct Infos* const robInfo){
21 
22  int numTasks = tasks.size();
23  for (int i=0; i<(numTasks-1); i++){ //LastTask has everything fixed
24  if (tasks[i]->updated == false){
25  tasks[i]->updateMatrices(robInfo);
26  tasks[i]->updated = true;
27 // std::cout << "[" << robotName<< "][CONTROLLER] Updated transforms for " <<
28 // tasks[i]->getName() << std::endl;
29 
30  }
31  }
32 
33  return 0;
34 }
35 
43 int Controller::updateSingleTaskMatrices(Task* task, struct Infos* const robInfo){
44 
45  if (task->updated == false){
46  task->updateMatrices(robInfo);
47  task->updated = true;
48  std::cout << "[" << robotName<< "][CONTROLLER] Calculated transforms for only the " <<
49  task->getName() <<" task" << std::endl;
50  } else {
51  std::cout << "[" << robotName<< "][CONTROLLER] " <<
52  task->getName() <<" task already updated" << std::endl;
53  }
54  return 0;
55 }
56 
63 int Controller::resetAllUpdatedFlags(std::vector<Task*> tasks){
64 
65  int numTasks = tasks.size();
66  for (int i=0; i<(numTasks-1); i++){ //LastTask has everything fixed
67  tasks[i]->updated = false;
68  }
69 // std::cout << "[" << robotName<< "][CONTROLLER] Resetted Updated Flags for " <<
70 // numTasks-1 <<" (exlcuding the null final task) tasks" << std::endl;
71  return 0;
72 
73 }
74 
79 int Controller::resetAllAlgosFlag(std::vector<Task*> tasks){
80 
81  int numTasks = tasks.size();
82  for (int i=0; i<(numTasks); i++){
83  tasks[i]->setFlag_G(0);
84  tasks[i]->setFlag_W(0);
85  tasks[i]->setMu_G(0.0);
86  tasks[i]->setMu_W(0.0);
87 
88  }
89 // std::cout << "[" << robotName<< "][CONTROLLER] Resetted Updated Flags for " <<
90 // numTasks-1 <<"+1(the null task) tasks" << std::endl;
91  return 0;
92 }
93 
101 std::vector<double> Controller::execAlgorithm(std::vector<Task*> tasks){
102 
104 
105  //initialize yDot and Q for algorithm
106  //yDot = [arm arm arm arm wx wy wz x y z]
107  CMAT::Matrix yDot_cmat = CMAT::Matrix::Zeros(TOT_DOF,1);
108  CMAT::Matrix Q = CMAT::Matrix::Eye(TOT_DOF);
109  for (int i=0; i<tasks.size(); i++){
110 
111  if (tasks[i]->eqType){
112  //std::cout<<tasks[i]->gain<<"\n"; ///DEBUG
113  Controller::equalityIcat(tasks[i], &yDot_cmat, &Q);
114 
115  } else {
116  Controller::inequalityIcat(tasks[i], &yDot_cmat, &Q);
117  }
118 
120 // if (tasks[i]->getName().compare("PIPE_REACHING_GOAL") == 0){
121 // std::cout << "[CONTROLLER] yDot after " << tasks[i]->getName() << ": \n";
122 // yDot_cmat.PrintMtx();
123 // std::cout << "\n";
124 // }
125  }
126 
127  return (CONV::vector_cmat2std(yDot_cmat));
128 
129 }
130 
131 
132 
147 int Controller::equalityIcat(Task* task, CMAT::Matrix* rhop, CMAT::Matrix* Q) {
148 
149  CMAT::Matrix J = task->getJacobian(); //J task jacobian
150  CMAT::Matrix ref = task->getReference(); //task reference
151 
152  CMAT::Matrix I = CMAT::Matrix::Eye(task->getDof());
153  CMAT::Matrix barG, barGtransp, T, W, barGpinv;
154  // barG the actual Jacobian
155  barG = J * (*Q);
156  barGtransp = barG.Transpose();
157 
159  // T is the reg. matrix for the different levels of task priority
160  // takes into account that not all the controls are available
161  T = (I - (*Q)).Transpose() * (I-(*Q));
162 
163  // compute W to solve the problem of discontinuity due to different priority levels
164  W = barG *
165  (barGtransp * barG + T)
166  .RegPseudoInverse(task->getThreshold(), task->getLambda(),
167  task->getMu_W(), task->getFlag_W()) *
168  barGtransp;
169 
171  barGpinv = (barGtransp * barG)
172  .RegPseudoInverse(task->getThreshold(), task->getLambda(),
173  task->getMu_G(), task->getFlag_G());
174  (*rhop) = (*rhop) + (*Q) * barGpinv * barGtransp * W * (ref - J * (*rhop));
175 
177 
178 
180  (*Q) = (*Q) * (I - barGpinv * barGtransp * barG);
181 
182  return 0;
183 
184 }
198 int Controller::inequalityIcat(Task* task, CMAT::Matrix* rhop, CMAT::Matrix* Q) {
199 
200  CMAT::Matrix J = task->getJacobian(); //Jacobiana of task
201  CMAT::Matrix ref = task->getReference(); // Reference of task
202  CMAT::Matrix A = task->getActivation(); // Activation of task
203 
204  CMAT::Matrix I = CMAT::Matrix::Eye(task->getDof());
205  CMAT::Matrix barG, barGtransp, barGtranspAA, T, H, W, barGpinv;
206  // barG the actual Jacobian
207  barG = J * (*Q);
208  barGtransp = barG.Transpose();
209  barGtranspAA = barGtransp * A * A;
210 
212  // T is the reg. matrix for the different levels of task priority
213  // takes into account that not all the controls are available
214  T = (I - (*Q)).Transpose() * (I-(*Q));
215  // H is the reg. matrix for the activation, i.e. A*(I-A)
216  H = barGtransp *
217  (CMAT::Matrix::Eye(task->getDimension()) - A)*
218  A * barG;
219 
220  // compute W to solve the problem of discontinuity due to different priority levels
221  W = barG *
222  (barGtranspAA * barG + T + H)
223  .RegPseudoInverse(task->getThreshold(), task->getLambda(),
224  task->getMu_W(), task->getFlag_W()) *
225  barGtranspAA;
226 
227  barGpinv = (barGtranspAA * barG + H)
228  .RegPseudoInverse(task->getThreshold(), task->getLambda(),
229  task->getMu_G(), task->getFlag_G());
230 
231 
233 
234 
236  (*rhop) = (*rhop) + (*Q) * barGpinv * barGtranspAA * W * (ref - J * (*rhop));
237 
239  (*Q) = (*Q) * (I - barGpinv * barGtranspAA * barG);
240 
241  return 0;
242 }
243 
Controller(std::string robotName)
Controller::Controller constructor.
Definition: controller.cpp:7
int resetAllUpdatedFlags(std::vector< Task * > tasks)
Controller::resetAllUpdatedFlags to be called at the end of the control loop, so next loop all matric...
Definition: controller.cpp:63
std::vector< double > execAlgorithm(std::vector< Task * > tasks)
Controller::execAlgorithm this function calls the step of the algorithm modifying (in the for) each t...
Definition: controller.cpp:101
int & getFlag_G()
Task::getFlag_G Various getter methods.
Definition: task.cpp:73
int & getFlag_W()
Task::getFlag_W Various getter methods.
Definition: task.cpp:57
double & getMu_G()
Task::getMu_G Various getter methods.
Definition: task.cpp:81
double & getMu_W()
Task::getMu_W Various getter methods.
Definition: task.cpp:65
Definition: infos.h:117
int updateSingleTaskMatrices(Task *task, struct Infos *const robInfo)
Controller::updateSingleTaskMatrices This function calls the updateMatrices for a single task...
Definition: controller.cpp:43
ABSTRACT class task. Each task is a derived class of this class. It contains all the variable that th...
Definition: task.h:17
int updateMultipleTasksMatrices(std::vector< Task * > tasks, struct Infos *const robInfo)
Controller::updateAllTaskMatrices This function calls all the updateMatrices of each task...
Definition: controller.cpp:20
int resetAllAlgosFlag(std::vector< Task * > tasks)
Controller::resetAllAlgosFlag Flag_W Mu_W Flag_G Mu_G must be resetted before each TPIK...
Definition: controller.cpp:79