1 #include "header/coordinator.h" 4 int main(
int argc,
char **argv){
7 std::cout <<
"[COORDINATOR] Please insert the two robots name" 11 std::string robotNameA = argv[1];
12 std::string robotNameB = argv[2];
14 if (LOG && (argc > 3)){
18 bool readyBothRob =
false;
20 ros::init (argc, argv,
"Coordinator");
21 std::cout <<
"[COORDINATOR] Start" << std::endl;
25 std::string toolName =
"g500_A/pegHead";
26 std::string toolName2 =
"g500_B/pegHead";
28 worldInterface.waitReady(toolName);
29 worldInterface.waitReady(toolName2);
31 worldInterface.getwT(&wTt, toolName);
37 double goalLinearVectToolTrue[] = {0.9999999999999999, -9.999999999999998, 8.378840300977453};
38 double goalLinearVectTool[] = {0.9999999999999999, -9.999999999999998, 8.378840300977453};
40 std::vector<double> eulRadTrue = {0, 0, -1.443185307179587};
41 std::vector<double> eulRad = {0, 0, -1.443185307179587};
43 Eigen::Matrix4d wTgoalTool_eigen = Eigen::Matrix4d::Identity();
50 wTgoalTool_eigen.topLeftCorner<3,3>() = FRM::eul2Rot(eulRad);
53 Eigen::Vector3d v_inside;
55 v_inside << 0.20, 0, 0;
56 Eigen::Vector3d w_inside = FRM::eul2Rot(eulRadTrue) * v_inside;
59 wTgoalTool_eigen(0, 3) = goalLinearVectTool[0] + w_inside(0);
60 wTgoalTool_eigen(1, 3) = goalLinearVectTool[1] + w_inside(1);
61 wTgoalTool_eigen(2, 3) = goalLinearVectTool[2] + w_inside(2);
63 Eigen::Matrix4d wTgoalTool_ideal;
64 wTgoalTool_ideal.topLeftCorner<3,3>() = FRM::eul2Rot(eulRadTrue);
65 wTgoalTool_ideal(0, 3) = goalLinearVectToolTrue[0] + w_inside(0);
66 wTgoalTool_ideal(1, 3) = goalLinearVectToolTrue[1] + w_inside(1);
67 wTgoalTool_ideal(2, 3) = goalLinearVectToolTrue[2] + w_inside(2);
72 Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelA_eigen;
73 Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolA_eigen;
74 Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelB_eigen;
75 Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolB_eigen;
76 Eigen::Matrix<double, VEHICLE_DOF, 1> refTool;
77 Eigen::Matrix<double, VEHICLE_DOF, 1> coopVelToolFeasible;
78 Eigen::Vector3d forcePegTip;
79 Eigen::Vector3d torquePegTip;
80 double changeMagnitude = 0.0005;
85 if (pathLog.size() > 0){
86 logger =
Logger(
"Coordinator", pathLog);
87 logger.createDirectoryForNode();
95 boost::asio::io_service ioVisio;
96 std::cout <<
"[COORDINATOR] Wating for Vision Robot giving Hole's Pose...\n";
99 while (0 != visionInterface.getHoleTransform(&(wTgoalTool_eigen))){
100 boost::asio::deadline_timer loopRater(ioVisio, boost::posix_time::milliseconds(msVisio));
105 std::cout <<
"[COORDINATOR] arrived wThole: " << wTgoalTool_eigen <<
"\n";
110 wTgoalTool_eigen(0, 3) += w_inside(0);
111 wTgoalTool_eigen(1, 3) += w_inside(1);
112 wTgoalTool_eigen(2, 3) += w_inside(2);
122 int ms = MS_CONTROL_LOOP;
123 boost::asio::io_service io;
127 while(!(readyBothRob)){
128 boost::asio::deadline_timer loopRater(io, boost::posix_time::milliseconds(ms));
129 coordInterface.publishStartBoth(
false);
130 coordInterface.publishUpdatedGoal(wTgoalTool_eigen);
132 readyBothRob = coordInterface.getReadyBothRob();
137 std::cout <<
"[COORDINATOR] Now both robot are ready" << std::endl;
140 boost::asio::deadline_timer loopRater(io, boost::posix_time::milliseconds(ms));
141 coordInterface.publishStartBoth(
true);
143 worldInterface.getwT(&wTt, toolName);
148 coordInterface.getForceTorque(&forcePegTip, &torquePegTip);
151 if (forcePegTip.norm() > 0 || torquePegTip.norm() > 0) {
152 wTgoalTool_eigen = changeGoalLin(changeMagnitude, forcePegTip, wTgoalTool_eigen);
156 coordInterface.publishUpdatedGoal(wTgoalTool_eigen);
168 while (returned > 0){
169 returned = coordInterface.getMatricesFromRobs(&nonCoopCartVelA_eigen, &nonCoopCartVelB_eigen, &admisVelToolA_eigen, &admisVelToolB_eigen);
171 std::cout <<
"[COORDINATOR] ERROR in the dimension of mesage arrived from robots" << std::endl;
175 std::cout <<
"[COORDINATOR] ERROR in the name of robots" << std::endl;
179 boost::asio::deadline_timer loopRater(io, boost::posix_time::milliseconds(1));
190 worldInterface.getwT(&wTt, toolName);
191 refTool = calculateRefTool(wTgoalTool_eigen, wTt);
192 coopVelToolFeasible = execCoordAlgo(nonCoopCartVelA_eigen, admisVelToolA_eigen,
193 nonCoopCartVelB_eigen, admisVelToolB_eigen,
196 std::cout <<
"Cooperative Tool Velocity\n[x y z w_x w_y w_z]:\n" << coopVelToolFeasible <<
"\n";
198 coordInterface.publishCoopVel(coopVelToolFeasible);
203 CMAT::TransfMatrix wTtool_cmat = CONV::matrix_eigen2cmat(wTt);
204 CMAT::TransfMatrix wTgoalTool_ideal_cmat = CONV::matrix_eigen2cmat(wTgoalTool_ideal);
205 CMAT::Vect6 errorSwapped = CMAT::CartError(wTgoalTool_ideal_cmat, wTtool_cmat);
208 Eigen::Matrix<double, VEHICLE_DOF, 1> error;
209 error(0) = errorSwapped(4);
210 error(1) = errorSwapped(5);
211 error(2) = errorSwapped(6);
212 error(3) = errorSwapped(1);
213 error(4) = errorSwapped(2);
214 error(5) = errorSwapped(3);
215 if (pathLog.size() != 0){
216 Eigen::Matrix4d wTt2;
217 worldInterface.getwT(&wTt2, toolName2);
219 logger.logNumbers(wTgoalTool_eigen,
"wTgoal");
220 logger.logNumbers(wTt,
"wTt");
221 logger.logNumbers(error,
"realgoal_Tool_error");
224 readyBothRob = coordInterface.getReadyBothRob();
229 std::cout <<
"[COORDINATOR] Now one robot is not ready anymore" << std::endl;
235 Eigen::Matrix<double, VEHICLE_DOF, 1> execCoordAlgo(
236 Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelA_eigen,
237 Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolA_eigen,
238 Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVelB_eigen,
239 Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelToolB_eigen,
240 Eigen::Matrix<double, VEHICLE_DOF, 1> refTool,
246 double muA = muZero + ((refTool-nonCoopCartVelA_eigen).norm());
247 double muB = muZero + ((refTool-nonCoopCartVelB_eigen).norm());
250 Eigen::Matrix<double, VEHICLE_DOF, 1> coopVelTool;
251 coopVelTool = (1 / (muA + muB)) *
252 ( (muA * nonCoopCartVelA_eigen) + (muB * nonCoopCartVelB_eigen) );
255 Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> constraintMat;
256 constraintMat = (admisVelToolA_eigen - admisVelToolB_eigen);
259 Eigen::Matrix<double, VEHICLE_DOF, 1> coopVelToolFeasible;
260 Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> eye =
261 Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF>::Identity();
263 coopVelToolFeasible = ( eye - (FRM::pseudoInverse(constraintMat) * constraintMat) )
267 logger->logNumbers(muA,
"weightA");
268 logger->logNumbers(muB,
"weightB");
269 logger->logNumbers(coopVelTool,
"notFeasibleCoopVel");
270 logger->logNumbers(nonCoopCartVelA_eigen,
"nonCoopVelg500_A");
271 logger->logNumbers(nonCoopCartVelB_eigen,
"nonCoopVelg500_B");
272 logger->logNumbers(coopVelToolFeasible,
"coopVel");
273 logger->logNumbers(refTool,
"idealTool");
276 return coopVelToolFeasible;
280 Eigen::Matrix<double, VEHICLE_DOF, 1> calculateRefTool(Eigen::Matrix4d wTgoaltool_eigen,
281 Eigen::Matrix4d wTtool_eigen){
283 CMAT::TransfMatrix wTgoaltool_cmat = CONV::matrix_eigen2cmat(wTgoaltool_eigen);
284 CMAT::TransfMatrix wTtool_cmat = CONV::matrix_eigen2cmat(wTtool_eigen);
287 CMAT::Vect6 errorSwapped = CMAT::CartError(wTgoaltool_cmat, wTtool_cmat);
291 Eigen::Matrix<double, VEHICLE_DOF, 1> reference;
292 double gainLin = 0.05;
293 double gainAng = 0.08;
294 reference(0) = gainLin*errorSwapped(4);
295 reference(1) = gainLin*errorSwapped(5);
296 reference(2) = gainLin*errorSwapped(6);
297 reference(3) = gainAng*errorSwapped(1);
298 reference(4) = gainAng*errorSwapped(2);
299 reference(5) = gainAng*errorSwapped(3);
302 reference.topRows(3) = FRM::saturateVectorEigen(reference.topRows(3), 0.1);
303 reference.bottomRows(3) = FRM::saturateVectorEigen(reference.bottomRows(3), 0.1);
310 Eigen::Matrix4d changeGoalLin(
double gain, Eigen::Vector3d forcePegTip, Eigen::Matrix4d wTgoalTool_eigen){
312 if (forcePegTip(1) != 0 || forcePegTip(2) != 0) {
313 Eigen::Vector3d tchange;
314 Eigen::Vector3d wChange;
316 tchange << gain*forcePegTip;
318 wChange = wTgoalTool_eigen.topLeftCorner<3,3>() * tchange;
319 wChange = FRM::saturateVectorEigen(wChange, 0.0007);
321 wTgoalTool_eigen.topRightCorner<3,1>() += wChange;
324 return wTgoalTool_eigen;
333 Eigen::Matrix4d changeGoalAng(
double gain, Eigen::Vector3d torquePegTip,
334 Eigen::Matrix3d wRt_eigen, Eigen::Matrix4d wTgoalTool_eigen){
337 if (torquePegTip(1) != 0 || torquePegTip(2) != 0) {
340 Eigen::Vector3d w_angVel;
346 w_angVel = gain * (wRt_eigen * torquePegTip);
347 w_angVel = FRM::saturateVectorEigen(w_angVel, 0.0001);
349 CMAT::RotMatrix wRg_cmat = CONV::matrix_eigen2cmat(wTgoalTool_eigen.topLeftCorner<3,3>());
351 CMAT::RotMatrix wRnewg = wRg_cmat.StrapDown(CONV::matrix_eigen2cmat(w_angVel), dt);
361 wTgoalTool_eigen.topLeftCorner<3,3>() = CONV::matrix_cmat2eigen(wRnewg);
364 return wTgoalTool_eigen;
The VisionInterfaceCoord class. It is the coordinator interface which take the hole estimated pose fr...
The CoordInterfaceCoord class.
The WorldInterface class to get position from world to anything else.