AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
missionManagerClass.cpp
1 #include "header/missionManager.h"
2 #include <chrono>
3 
15 int main(int argc, char **argv)
16 {
17  if (argc < 3){
18  std::cout << "[MAIN] Please insert the two robots name"<< std::endl;
19  return -1;
20  }
21 
22  MissionManager missionManagerA(argc, argv);
23  missionManagerA.execute();
24  return 0;
25 }
26 
27 
28 
29 
31 // std::cout << "JACOBIAN " << i << ": \n";
32 // tasks[i]->getJacobian().PrintMtx();
33 // std::cout<< "\n";
34 // std::cout << "REFERENCE " << i << ": \n";
35 // tasks[i]->getReference().PrintMtx() ;
36 // std::cout << "\n";
37 
38 
39 //Q.PrintMtx("Q"); ///DEBUG
40 //yDot_cmat.PrintMtx("yDot");
41 
42 
43 
44 
45 /*****************************************************************************
46  * PRINCIPAL FUNCTION
47  ******************************************************************************/
48 
49 
50 MissionManager::MissionManager(int argc, char **argv)
51 {
52  robotName = argv[1];
53 
54  ros::init(argc, argv, robotName + "_MissionManager");
55  std::cout << "[" << robotName << "][MISSION_MANAGER] Start" << std::endl;
56 
57  if (LOG && (argc > 3)){
58  pathLog = argv[3];
59  }
60 
61 
62 }
63 
64 
65 MissionManager::~MissionManager(){clearTaskList();}
66 
67 
69 
70  setGoals();
71  initrobotInterface();
72  initKdlHelper();
73  setInitialState();
74 
75  //TODO before this the robot must have grasped, here only to see if it is correct
76  if (true){ //TODO if grasped
77  setKdl4Tool();
78  }
79 
81  initController();
82 
83  double ms = 100;
84  boost::asio::io_service io;
85  while(1){
86  controlLoop(io, ms);
87  }
88 
89  return 0;
90 }
91 
92 
93 
94 
99 void MissionManager::setGoals(){
100 
102  double goalLinearVect[] = {-0.287, -0.062, 7.424};
103  Eigen::Matrix4d wTgoalVeh_eigen = Eigen::Matrix4d::Identity();
104  //rot part
105  wTgoalVeh_eigen.topLeftCorner(3,3) = Eigen::Matrix3d::Identity();
106  //trasl part
107  wTgoalVeh_eigen(0, 3) = goalLinearVect[0];
108  wTgoalVeh_eigen(1, 3) = goalLinearVect[1];
109  wTgoalVeh_eigen(2, 3) = goalLinearVect[2];
110 
112  double goalLinearVectEE[] = {-0.27, -0.102, 2.124};
113  Eigen::Matrix4d wTgoalEE_eigen = Eigen::Matrix4d::Identity();
114  //rot part
115  wTgoalEE_eigen.topLeftCorner(3,3) = Eigen::Matrix3d::Identity();
116 // wTgoalEE_eigen.topLeftCorner(3,3) << 0.5147, 0 , -0.8574,
117 // 0 ,1, 0 ,
118 // 0.8573 , 0 , 0.5147;
119  //trasl part
120  wTgoalEE_eigen(0, 3) = goalLinearVectEE[0];
121  wTgoalEE_eigen(1, 3) = goalLinearVectEE[1];
122  wTgoalEE_eigen(2, 3) = goalLinearVectEE[2];
123 
125  double goalLinearVectTool[] = {-0.27, -0.102, 2.124};
126  // (rob and world have downward z)
127  Eigen::Matrix4d wTgoalTool_eigen = Eigen::Matrix4d::Identity();
128  //rot part
129  wTgoalTool_eigen.topLeftCorner(3,3) = Eigen::Matrix3d::Identity();
130 
131  //trasl part
132  wTgoalTool_eigen(0, 3) = goalLinearVectTool[0];
133  wTgoalTool_eigen(1, 3) = goalLinearVectTool[1];
134  wTgoalTool_eigen(2, 3) = goalLinearVectTool[2];
135 
136 
137  //struct transforms to pass them to Controller class
138  robInfo.transforms.wTgoalVeh_eigen = wTgoalVeh_eigen;
139  robInfo.transforms.wTgoalEE_eigen = wTgoalEE_eigen;
140  robInfo.transforms.wTgoalTool_eigen = wTgoalTool_eigen;
141 
142 }
143 
144 void MissionManager::initrobotInterface(){
146  robotInterface(argc, argv, "pipe");
147  robotInterface.init();
148 }
149 
150 void MissionManager::initKdlHelper(){
152  std::string filename = "/home/tori/UWsim/Peg/model/g500ARM5.urdf";
153  std::string vehicle = "base_link";
154  std::string link0 = "part0";
155  std::string endEffector = "end_effector";
156  kdlHelper(filename, link0, endEffector);
157 
158  kdlHelper.setEESolvers();
159 
160  // KDL parse for fixed things (e.g. vehicle and one of his sensor)
161  //TODO Maybe exist an easier method to parse fixed frame from urdf without needed of kdl solver
162  kdlHelper.getFixedFrame(vehicle, link0, &(robInfo.robotStruct.vTlink0));
163 }
164 
165 void MissionManager::setInitialState(){
167  robotInterface.getJointState(&(robInfo.robotState.jState));
168  robotInterface.getwTv(&(robInfo.robotState.wTv_eigen));
169  robotInterface.getwTt(&(robInfo.transforms.wTt_eigen));
170  robotInterface.getOtherRobPos(&(robInfo.exchangedInfo.otherRobPos));
171  //get ee pose RESPECT LINK 0
172  kdlHelper.getEEpose(robInfo.robotState.jState, &(robInfo.robotState.link0Tee_eigen));
173  // useful have vTee: even if it is redunant, everyone use it a lot
174  robInfo.robotState.vTee_eigen = robInfo.robotStruct.vTlink0 * robInfo.robotState.link0Tee_eigen;
175  // get jacobian respect link0
176  kdlHelper.getJacobianEE(robInfo.robotState.jState, &(robInfo.robotState.link0_Jee_man));
177  //get whole jacobian (arm+vehcile and projected on world
178  computeWholeJacobian(&robInfo, ee);
179 }
180 
181 void MissionManager::setKdl4Tool(){
182  Eigen::Matrix4d eeTtool =
183  FRM::invertTransf(robInfo.robotState.wTv_eigen*robInfo.robotState.vTee_eigen)
184  * robInfo.transforms.wTt_eigen;
185  kdlHelper.setToolSolvers(eeTtool);
186  kdlHelper.getJacobianTool(robInfo.robotState.jState, &(robInfo.robotState.link0_Jtool_man));
187  computeWholeJacobian(&robInfo, tool);
188 }
189 
190 void MissionManager::initController(){
191  controller(robotName);
192  setTaskListInit(&tasks, robotName);
193  controller.setTaskList(tasks);
194 }
195 
196 
197 void MissionManager::ControlLoop(boost::asio::io_service io, double ms){
198  //auto start = std::chrono::steady_clock::now();
199 
200  // this must be inside loop
201  boost::asio::deadline_timer loopRater(io, boost::posix_time::milliseconds(ms));
202 
204  robotInterface.getJointState(&(robInfo.robotState.jState));
205  robotInterface.getwTv(&(robInfo.robotState.wTv_eigen));
206  robotInterface.getwTt(&(robInfo.transforms.wTt_eigen));
207  robotInterface.getOtherRobPos(&(robInfo.exchangedInfo.otherRobPos));
208 
209  //get ee pose RESPECT LINK 0
210  kdlHelper.getEEpose(robInfo.robotState.jState, &(robInfo.robotState.link0Tee_eigen));
211  // useful have vTee: even if it is redunant, tasks use it a lot
212  robInfo.robotState.vTee_eigen = robInfo.robotStruct.vTlink0 * robInfo.robotState.link0Tee_eigen;
213  // get jacobian respect link0
214  kdlHelper.getJacobianEE(robInfo.robotState.jState, &(robInfo.robotState.link0_Jee_man));
215  computeWholeJacobian(&robInfo, ee);
216  //if(grasped)
217  kdlHelper.getJacobianTool(robInfo.robotState.jState, &(robInfo.robotState.link0_Jtool_man));
218  computeWholeJacobian(&robInfo, tool);
219 
220 
222  controller.updateTransforms(&robInfo);
223  std::vector<double> yDot = controller.execAlgorithm();
224 
225 
227  robotInterface.sendyDot(yDot);
228  robotInterface.spinOnce(); // actually the spinonce is called here and not in sendyDot
229 
231 // auto end = std::chrono::steady_clock::now();
232 // auto diff = end - start;
233 // std::cout << std::chrono::duration<double, std::milli> (diff).count()
234 // << " ms" << std::endl;
235 
236  loopRater.wait(); //wait for the remaning time until period setted (ms)
237 }
238 
239 
240 
254 void MissionManager::setTaskListInit(){
255 
256  clearTaskList(); //reset, useful when we will change tasks order for change mission phase
258  // note: order of priority at the moment is here
259  bool eqType = true;
260  bool ineqType = false;
261 
262  //tasks->push_back(new VehicleNullVelTask(6, ineqType));
263 
264  tasks->push_back(new JointLimitTask(4, ineqType, robotName));
265  tasks->push_back(new HorizontalAttitudeTask(1, ineqType, robotName));
266 
267  tasks->push_back(new ObstacleAvoidEETask(1, ineqType, robotName));
268  tasks->push_back(new ObstacleAvoidVehicleTask(1, ineqType, robotName));
269 
270 
271  //tasks->push_back(new FovEEToToolTask(1, ineqType, robotName));
272 
273  tasks->push_back(new EndEffectorReachTask(6, eqType, robotName, tool));
274 
275  //tasks->push_back(new VehicleReachTask(6, eqType, robotName));
276 
277  //The "fake task" with all eye and zero matrices, needed as last one for algo
278  tasks->push_back(new LastTask(TOT_DOF, eqType, robotName));
279 
280  createLogFolders(); //if setted so
281 }
282 
283 
289 void MissionManager::clearTaskList(){
290  for (std::vector< Task* >::iterator it = tasks.begin() ; it != tasks.end(); ++it)
291  {
292  delete (*it);
293  }
294  tasks.clear();
295 }
296 
297 
298 void MissionManager::createLogFolders(){
300  //if flag log setted to 1 and path log is given
301  if (pathLog.size() != 0){
302 
303  for (int i =0; i< tasks.size(); ++i){
304  PRT::createDirectory(pathLog +"/" +tasks[i]->getName());
305  }
306  std::cout << "[" << robotName
307  << "][MISSION_MANAGER] Created Log Folders in "
308  << pathLog << std::endl;
309  }
310 }
311 
312 
313 
314 
315 
int getJacobianEE(std::vector< double > jointPos, Eigen::Matrix< double, 6, ARM_DOF > *jacobianEE_eigen)
KDLHelper::getJacobianEE respect base frame of arm (link0)
Definition: kdlHelper.cpp:165
int getFixedFrame(std::string frameOrigin, std::string frameTarget, Eigen::Matrix4d *xTx_eigen)
KDLHelper::getFixedFrame get relative position of things inside the urdf file of the robot that NOT c...
Definition: kdlHelper.cpp:220
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 setToolSolvers(Eigen::Matrix4d eeTtool_eigen)
KDLHelper::setToolSolvers.
Definition: kdlHelper.cpp:87
"Fake" Task needed as last to algorithm, it has all fixed matrices of eye and zeros so no setxx are n...
Definition: lastTask.h:9
The ObstacleAvoidEETask class Usage of this task can be for avoiding with the end effector...
int getJacobianTool(std::vector< double > jointPos, Eigen::Matrix< double, 6, ARM_DOF > *jacobianTool_eigen)
KDLHelper::getJacobianTool respect base frame of arm (link0)
Definition: kdlHelper.cpp:188
MissionManager(int argc, char **argv)
DEBUG.
Task to make the end effector reach a goal (both linear and angular position)
The ObstacleAvoidVehicleTask class (where the obstacle is the other vehicle, at the moment only for t...
Eigen::Matrix< double, 6, ARM_DOF > link0_Jee_man
Jacobians.
Definition: infos.h:52
Eigen::Matrix4d wTv_eigen
Transforms.
Definition: infos.h:34
int setEESolvers()
KDLHelper::setEESolvers Initialize the kinematic solvers (jacob and pose) for the arm End Effector...
Definition: kdlHelper.cpp:55