AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
coordinator_old.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 robotName1 = argv[1];
12  std::string robotName2 = 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 readyRob1 = false;
19  bool readyRob2 = false;
20 
21  ros::init (argc, argv, "Coordinator");
22  std::cout << "[COORDINATOR] Start" << std::endl;
23  ros::NodeHandle nh;
24 
26  //double goalLinearVectTool[] = {-0.27, -0.102, 2.124};
27  double goalLinearVectTool[] = {-0.27, -1.102, 9.000};
28 
29  Eigen::Matrix4d wTgoalTool_eigen = Eigen::Matrix4d::Identity();
30 
31  //rot part
32  wTgoalTool_eigen.topLeftCorner<3,3>() << 0, -1, 0,
33  1, 0, 0,
34  0, 0, 1;
35 
36  //wTgoalTool_eigen.topLeftCorner(3,3) = Eigen::Matrix3d::Identity();
37 
38  //trasl part
39  wTgoalTool_eigen(0, 3) = goalLinearVectTool[0];
40  wTgoalTool_eigen(1, 3) = goalLinearVectTool[1];
41  wTgoalTool_eigen(2, 3) = goalLinearVectTool[2];
42 
43 
44  //TODO put these in interfaces ? this should be a single publisher...
45  std::string topicStart = "/uwsim/Coord/StartStopBoth";
46  ros::Publisher startBothPub = nh.advertise<std_msgs::Bool>(topicStart, 1);
47 
48  std::string topicCoopVel = "/uwsim/Coord/CoopVel";
49  ros::Publisher coopVelPub = nh.advertise<geometry_msgs::TwistStamped>(topicCoopVel, 1);
50 
51 
52 
53  std_msgs::Bool start;
54  start.data = false;
55 
56 
58  // world interface needed to find peg position to calculate barX_t (reference that brings tool in goal)
59  std::string toolName = "pipe";
60  std::string toolName2 = "pipe2";
61  WorldInterface worldInterface("COORDINATOR");
62  worldInterface.waitReady(toolName);
63  worldInterface.waitReady(toolName2);
64 
65 
66  CoordInterfaceCoord coordInterfaceA(nh, robotName1);
67  CoordInterfaceCoord coordInterfaceB(nh, robotName2);
68  Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelA_eigen;
69  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolA_eigen;
70  Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelB_eigen;
71  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolB_eigen;
72  Eigen::Matrix<double, VEHICLE_DOF, 1> refTool;
73  Eigen::Matrix<double, VEHICLE_DOF, 1> coopVelToolFeasible;
74 
75  Eigen::Matrix4d wTt;
76 
78  Logger logger;
79  if (pathLog.size() > 0){
80  logger = Logger("Coordinator", pathLog);
81  logger.createDirectoryForNode();
82  }
83 
84  int ms = 100;
85  boost::asio::io_service io;
86 
87  while(ros::ok()){
88 
89  start.data = false;
90  while(!(readyRob1 && readyRob2)){
91  boost::asio::deadline_timer loopRater(io, boost::posix_time::milliseconds(ms));
92  startBothPub.publish(start);
93 
94  readyRob1 = coordInterfaceA.getReadyRob();
95  readyRob2 = coordInterfaceB.getReadyRob();
96  ros::spinOnce();
97  loopRater.wait();
98  }
99 
100  std::cout << "[COORDINATOR] Now both robot are ready" << std::endl;
101 
102 
103  start.data = true;
104  while(readyRob1 && readyRob2){
105  boost::asio::deadline_timer loopRater(io, boost::posix_time::milliseconds(ms));
106  startBothPub.publish(start); //TODO publish once? but first mess always lost...
107 
108  bool arrived[4] = {false, false, false, false};
109  //if at least one did not arrive, repeat loop, but call each get only once
110  while (arrived[0] == false || arrived[1] == false || arrived[2] == false || arrived[3] == false){
111  if (arrived[0] == false){
112  arrived[0] = (coordInterfaceA.getNonCoopCartVel(&nonCoopCartVelA_eigen) == 0) ? true : false;
113  }
114  if (arrived[1] == false){
115  arrived[1] = (coordInterfaceA.getJJsharp(&admisVelToolA_eigen) == 0) ? true : false;
116  }
117  if (arrived[2] == false){
118  arrived[2] = (coordInterfaceB.getNonCoopCartVel(&nonCoopCartVelB_eigen) == 0) ? true : false;
119  }
120  if (arrived[3] == false){
121  arrived[3] = (coordInterfaceB.getJJsharp(&admisVelToolB_eigen) == 0) ? true : false;
122  }
123 
124  boost::asio::deadline_timer loopRater(io, boost::posix_time::milliseconds(1));
125  ros::spinOnce();
126  loopRater.wait();
127  }
128 
129 
130 // std::cout << nonCoopCartVelA_eigen << "\n\n";
131 // std::cout << admisVelToolA_eigen << "\n\n";
132 // std::cout << nonCoopCartVelB_eigen << "\n\n";
133 // std::cout << admisVelToolB_eigen << "\n\n";
134 
135  worldInterface.getwT(&wTt, toolName);
136  refTool = calculateRefTool(wTgoalTool_eigen, wTt);
137  coopVelToolFeasible = execCoordAlgo(nonCoopCartVelA_eigen, admisVelToolA_eigen,
138  nonCoopCartVelB_eigen, admisVelToolB_eigen,
139  refTool, &logger);
140 
141  std::cout << "Calculated coop vel:\n" << coopVelToolFeasible << "\n\n";
142 
143  publishCoopVel(coopVelPub, coopVelToolFeasible);
144 
145 
147  if (pathLog.size() != 0){
148  Eigen::Matrix4d wTt2;
149  worldInterface.getwT(&wTt2, toolName2);
150  logger.logCartError(wTt, wTt2, "stressTool");
151  }
152 
153  ros::spinOnce();
154  loopRater.wait();
155  }
156 
157  std::cout << "[COORDINATOR] Now one robot is not ready anymore" << std::endl;
158 
159  }
160 
161 }
162 
163 Eigen::Matrix<double, VEHICLE_DOF, 1> execCoordAlgo(
164  Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelA_eigen,
165  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolA_eigen,
166  Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelB_eigen,
167  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolB_eigen,
168  Eigen::Matrix<double, VEHICLE_DOF, 1> refTool,
169  Logger* logger){
170 
171  double muZero = 1; //this is only to not divide by zero?
172 
173  // weights to understand the "difficulty" each robot has in following the ideal reference(refTool)
174  double muA = muZero + ((refTool-nonCoopCartVelA_eigen).norm());
175  double muB = muZero + ((refTool-nonCoopCartVelB_eigen).norm());
176 
177 
178  //xHatDot_tool
179  Eigen::Matrix<double, VEHICLE_DOF, 1> coopVelTool;
180  coopVelTool = (1 / (muA + muB)) *
181  ( (muA * nonCoopCartVelA_eigen) + (muB * nonCoopCartVelB_eigen) );
182 
183  // C
184  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> constraintMat;
185  constraintMat = (admisVelToolA_eigen - admisVelToolB_eigen);
186 
187  //xTildeDot_tool
188  Eigen::Matrix<double, VEHICLE_DOF, 1> coopVelToolFeasible;
189  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> eye =
190  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF>::Identity();
191 
192  coopVelToolFeasible = ( eye - (FRM::pseudoInverse(constraintMat) * constraintMat) )
193  * coopVelTool;
194 
195  if (logger != NULL){
196  logger->logNumbers(muA, "weightA");
197  logger->logNumbers(muB, "weightB");
198  logger->logNumbers(coopVelTool, "notFeasibleCoopVel");
199  logger->logNumbers(nonCoopCartVelA_eigen, "nonCoopVelg500_A");
200  logger->logNumbers(nonCoopCartVelB_eigen, "nonCoopVelg500_B");
201  logger->logNumbers(coopVelToolFeasible, "coopVel");
202  logger->logNumbers(refTool, "idealTool");
203  }
204 
205  return coopVelToolFeasible;
206 }
207 
208 
209 Eigen::Matrix<double, VEHICLE_DOF, 1> calculateRefTool(Eigen::Matrix4d wTgoaltool_eigen,
210  Eigen::Matrix4d wTtool_eigen){
211 
212  CMAT::TransfMatrix wTgoaltool_cmat = CONV::matrix_eigen2cmat(wTgoaltool_eigen);
213  CMAT::TransfMatrix wTtool_cmat = CONV::matrix_eigen2cmat(wTtool_eigen);
214 
215  //double gain = 0.5; GAIN E SATURAZ??
216  CMAT::Vect6 errorSwapped = CMAT::CartError(wTgoaltool_cmat, wTtool_cmat);//ang;lin
217  // ang and lin must be swapped because in yDot and jacob linear part is before
218 
219 
220  Eigen::Matrix<double, VEHICLE_DOF, 1> reference;
221  reference(0) = errorSwapped(4);
222  reference(1) = errorSwapped(5);
223  reference(2) = errorSwapped(6);
224  reference(3) = errorSwapped(1);
225  reference(4) = errorSwapped(2);
226  reference(5) = errorSwapped(3);
227 
228  reference *= 0.05;
229 
230  reference.topRows(3) = FRM::saturateVectorEigen(reference.topRows(3), 0.3);
231  reference.bottomRows(3) = FRM::saturateVectorEigen(reference.bottomRows(3), 0.1);
232 
233 
234  return reference;
235 
236 }
237 
238 void publishCoopVel(ros::Publisher coopVelPub, Eigen::Matrix<double, VEHICLE_DOF, 1> coopVel){
239 
240  geometry_msgs::TwistStamped msg;
241  msg.twist.linear.x = coopVel(0);
242  msg.twist.linear.y = coopVel(1);
243  msg.twist.linear.z = coopVel(2);
244  msg.twist.angular.x = coopVel(3);
245  msg.twist.angular.y = coopVel(4);
246  msg.twist.angular.z = coopVel(5);
247 
248  coopVelPub.publish(msg);
249 
250 }
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