AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
old_coordinator_with_tools.cpp
1 #include "header/coordinator.h"
2 
3 
4 int main(int argc, char **argv){
5 
6  if (argc < 3){
7  std::cout << "[COORDINATOR] Please insert the two robots name"
8  << std::endl;
9  return -1;
10  }
11  std::string robotNameA = argv[1];
12  std::string robotNameB = argv[2];
13  std::string pathLog;
14  if (LOG && (argc > 3)){ //if flag log setted to 1 and path log is given
15  pathLog = argv[3];
16  }
17 
18  bool readyBothRob = false;
19 
20  ros::init (argc, argv, "Coordinator");
21  std::cout << "[COORDINATOR] Start" << std::endl;
22  ros::NodeHandle nh;
23 
25  //double goalLinearVectTool[] = {-0.27, -0.102, 2.124};
26  double goalLinearVectTool[] = {-0.27, -1.102, 9.000};
27 
28  Eigen::Matrix4d wTgoalTool_eigen = Eigen::Matrix4d::Identity();
29 
30  //rot part
31  wTgoalTool_eigen.topLeftCorner<3,3>() << 0, 1, 0,
32  -1, 0, 0,
33  0, 0, 1;
34 
35  //wTgoalTool_eigen.topLeftCorner(3,3) = Eigen::Matrix3d::Identity();
36 
37  //trasl part
38  wTgoalTool_eigen(0, 3) = goalLinearVectTool[0];
39  wTgoalTool_eigen(1, 3) = goalLinearVectTool[1];
40  wTgoalTool_eigen(2, 3) = goalLinearVectTool[2];
41 
42 
43  //TODO put these in interfaces ? this should be a single publisher...
44 
45 
47  // world interface needed to find peg position to calculate barX_t (reference that brings tool in goal)
48  std::string toolName = "pipe";
49  std::string toolName2 = "pipe2";
50  WorldInterface worldInterface("COORDINATOR");
51  worldInterface.waitReady(toolName);
52  worldInterface.waitReady(toolName2);
53 
54 
55  CoordInterfaceCoord coordInterface(nh, robotNameA, robotNameB);
56  Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelA_eigen;
57  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolA_eigen;
58  Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelB_eigen;
59  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolB_eigen;
60  Eigen::Matrix<double, VEHICLE_DOF, 1> refTool;
61  Eigen::Matrix<double, VEHICLE_DOF, 1> coopVelToolFeasible;
62 
63  Eigen::Matrix4d wTt;
64 
66  Logger logger;
67  if (pathLog.size() > 0){
68  logger = Logger("Coordinator", pathLog);
69  logger.createDirectoryForNode();
70  }
71 
72  int ms = 100;
73  boost::asio::io_service io;
74 
75  while(ros::ok()){
76 
77  while(!(readyBothRob)){ //wait until both ready, meanwhile publish false to make no robot start
78  boost::asio::deadline_timer loopRater(io, boost::posix_time::milliseconds(ms));
79  coordInterface.publishStartBoth(false);
80 
81  readyBothRob = coordInterface.getReadyBothRob();
82  ros::spinOnce();
83  loopRater.wait();
84  }
85 
86  std::cout << "[COORDINATOR] Now both robot are ready" << std::endl;
87 
88  while(readyBothRob){
89  boost::asio::deadline_timer loopRater(io, boost::posix_time::milliseconds(ms));
90  coordInterface.publishStartBoth(true);
91 
92  //if at least one did not arrive, repeat loop, but call each get only once (this is done inside methods class interface)
93  int returned = 1;
94  while (returned > 0){
95  returned = coordInterface.getMatricesFromRobs(&nonCoopCartVelA_eigen, &nonCoopCartVelB_eigen, &admisVelToolA_eigen, &admisVelToolB_eigen);
96  if (returned == -1){
97  std::cout << "[COORDINATOR] ERROR in the dimension of mesage arrived from robots" << std::endl;
98  return -1;
99  }
100  if (returned == -2){
101  std::cout << "[COORDINATOR] ERROR in the name of robots" << std::endl;
102  return -1;
103  }
104 
105  boost::asio::deadline_timer loopRater(io, boost::posix_time::milliseconds(1));
106  ros::spinOnce();
107  loopRater.wait();
108  }
109 
110 
111 // std::cout << nonCoopCartVelA_eigen << "\n\n";
112 // std::cout << admisVelToolA_eigen << "\n\n";
113 // std::cout << nonCoopCartVelB_eigen << "\n\n";
114 // std::cout << admisVelToolB_eigen << "\n\n";
115 
116  worldInterface.getwT(&wTt, toolName);
117  refTool = calculateRefTool(wTgoalTool_eigen, wTt);
118  coopVelToolFeasible = execCoordAlgo(nonCoopCartVelA_eigen, admisVelToolA_eigen,
119  nonCoopCartVelB_eigen, admisVelToolB_eigen,
120  refTool, &logger);
121 
122  std::cout << "Calculated coop vel:\n" << coopVelToolFeasible << "\n\n";
123 
124  coordInterface.publishCoopVel(coopVelToolFeasible);
125 
126 
128  if (pathLog.size() != 0){
129  Eigen::Matrix4d wTt2;
130  worldInterface.getwT(&wTt2, toolName2);
131  logger.logCartError(wTt, wTt2, "stressTool");
132  }
133 
134  readyBothRob = coordInterface.getReadyBothRob();
135  ros::spinOnce();
136  loopRater.wait();
137  }
138 
139  std::cout << "[COORDINATOR] Now one robot is not ready anymore" << std::endl;
140 
141  }
142 
143 }
144 
145 Eigen::Matrix<double, VEHICLE_DOF, 1> execCoordAlgo(
146  Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelA_eigen,
147  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolA_eigen,
148  Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelB_eigen,
149  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolB_eigen,
150  Eigen::Matrix<double, VEHICLE_DOF, 1> refTool,
151  Logger* logger){
152 
153  double muZero = 1; //this is only to not divide by zero?
154 
155  // weights to understand the "difficulty" each robot has in following the ideal reference(refTool)
156  double muA = muZero + ((refTool-nonCoopCartVelA_eigen).norm());
157  double muB = muZero + ((refTool-nonCoopCartVelB_eigen).norm());
158 
159 
160  //xHatDot_tool
161  Eigen::Matrix<double, VEHICLE_DOF, 1> coopVelTool;
162  coopVelTool = (1 / (muA + muB)) *
163  ( (muA * nonCoopCartVelA_eigen) + (muB * nonCoopCartVelB_eigen) );
164 
165  // C
166  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> constraintMat;
167  constraintMat = (admisVelToolA_eigen - admisVelToolB_eigen);
168 
169  //xTildeDot_tool
170  Eigen::Matrix<double, VEHICLE_DOF, 1> coopVelToolFeasible;
171  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> eye =
172  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF>::Identity();
173 
174  coopVelToolFeasible = ( eye - (FRM::pseudoInverse(constraintMat) * constraintMat) )
175  * coopVelTool;
176 
177  if (logger != NULL){
178  logger->logNumbers(muA, "weightA");
179  logger->logNumbers(muB, "weightB");
180  logger->logNumbers(coopVelTool, "notFeasibleCoopVel");
181  logger->logNumbers(nonCoopCartVelA_eigen, "nonCoopVelg500_A");
182  logger->logNumbers(nonCoopCartVelB_eigen, "nonCoopVelg500_B");
183  logger->logNumbers(coopVelToolFeasible, "coopVel");
184  logger->logNumbers(refTool, "idealTool");
185  }
186 
187  return coopVelToolFeasible;
188 }
189 
190 
191 Eigen::Matrix<double, VEHICLE_DOF, 1> calculateRefTool(Eigen::Matrix4d wTgoaltool_eigen,
192  Eigen::Matrix4d wTtool_eigen){
193 
194  CMAT::TransfMatrix wTgoaltool_cmat = CONV::matrix_eigen2cmat(wTgoaltool_eigen);
195  CMAT::TransfMatrix wTtool_cmat = CONV::matrix_eigen2cmat(wTtool_eigen);
196 
197  //double gain = 0.5; GAIN E SATURAZ??
198  CMAT::Vect6 errorSwapped = CMAT::CartError(wTgoaltool_cmat, wTtool_cmat);//ang;lin
199  // ang and lin must be swapped because in yDot and jacob linear part is before
200 
201 
202  Eigen::Matrix<double, VEHICLE_DOF, 1> reference;
203  reference(0) = errorSwapped(4);
204  reference(1) = errorSwapped(5);
205  reference(2) = errorSwapped(6);
206  reference(3) = errorSwapped(1);
207  reference(4) = errorSwapped(2);
208  reference(5) = errorSwapped(3);
209 
210  reference *= 0.05;
211 
212  reference.topRows(3) = FRM::saturateVectorEigen(reference.topRows(3), 0.3);
213  reference.bottomRows(3) = FRM::saturateVectorEigen(reference.bottomRows(3), 0.1);
214 
215 
216  return reference;
217 
218 }
Definition: logger.h:16
The CoordInterfaceCoord class.
The WorldInterface class to get position from world to anything else.
void logCartError(Eigen::Matrix4d goal, Eigen::Matrix4d base, std::string fileName)
Logger::logCartError log cartesian error for two frames The result is the error that brings in2 towar...
Definition: logger.cpp:106