AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
coordInterfaceCoord.cpp
1 #include "header/coordInterfaceCoord.h"
2 
3 CoordInterfaceCoord::CoordInterfaceCoord(ros::NodeHandle nh, std::string robotNameA,
4  std::string robotNameB){
5 
6  this->robotNameA = robotNameA;
7  this->robotNameB = robotNameB;
8 
9  std::cout << "[COORDINATOR][COORD_INTERFACE] Start" << std::endl;
10 
11  readyRobA = false;
12  readyRobB = false;
13  std::string topicReadyA = "/uwsim/" + robotNameA+"_MissionManager" + "/ready";
14  readyRobASub = nh.subscribe(topicReadyA, 1, &CoordInterfaceCoord::readyRobASubCallback, this);
15  std::string topicReadyB = "/uwsim/" + robotNameB+"_MissionManager" + "/ready";
16  readyRobBSub = nh.subscribe(topicReadyB, 1, &CoordInterfaceCoord::readyRobBSubCallback, this);
17 
18 
19  std::string topicCoordFromMMA = "/uwsim/" + robotNameA +"_MissionManager/toCoord";
20  subCoordFromMMA = nh.subscribe(topicCoordFromMMA, 1, &CoordInterfaceCoord::subCoordFromMMACallBack, this);
21  std::string topicCoordFromMMB = "/uwsim/" + robotNameB +"_MissionManager/toCoord";
22  subCoordFromMMB = nh.subscribe(topicCoordFromMMB, 1, &CoordInterfaceCoord::subCoordFromMMBCallBack, this);
23 
24  readxDotA = false;
25  readxDotB = false;
26  readJJsharpA = false;
27  readJJsharpB = false;
28 
29  std::string topicStart = "/uwsim/Coord/StartStopBoth";
30  startBothPub = nh.advertise<std_msgs::Bool>(topicStart, 1);
31 
32  std::string topicCoopVel = "/uwsim/Coord/CoopVel";
33  coopVelPub = nh.advertise<geometry_msgs::TwistStamped>(topicCoopVel, 1);
34 
35  //change goal things
36  if (CHANGE_GOAL){
37  subForceTorque = nh.subscribe("/uwsim/g500_A/forceSensorPeg", 1, &CoordInterfaceCoord::subForceTorqueCallback, this);
38  vectorForce.resize(3, 0.0); //init as dimension 3 and values all 0.0
39  vectorTorque.resize(3, 0.0);
40  std::string updateGoalTopic = "/uwsim/Coord/updatedGoal";
41  //pubUpdateGoal = nh.advertise<geometry_msgs::Vector3Stamped>(updateGoalTopic, 1);
42  pubUpdateGoal = nh.advertise<peg_msgs::transformMat>(updateGoalTopic, 1);
43  }
44 }
45 
46 bool CoordInterfaceCoord::getReadyBothRob(){
47  return (CoordInterfaceCoord::getReadyRobA()
48  && CoordInterfaceCoord::getReadyRobB());
49 }
50 
51 bool CoordInterfaceCoord::getReadyRobA(){
52  return this->readyRobA;
53 }
54 bool CoordInterfaceCoord::getReadyRobB(){
55  return this->readyRobB;
56 }
57 
74  Eigen::Matrix<double, VEHICLE_DOF, 1> *nonCoopCartVelA,
75  Eigen::Matrix<double, VEHICLE_DOF, 1> *nonCoopCartVelB,
76  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> *admisVelToolA,
77  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> *admisVelToolB){
78 
79  int return1 = 0;
80  int return2 = 0;
81  int return3 = 0;
82  int return4 = 0;
83 
84  if (!readxDotA){
85  return1 = CoordInterfaceCoord::getNonCoopCartVel(nonCoopCartVelA, robotNameA);
86  }
87 
88  if (!readxDotB){
89  return2 = CoordInterfaceCoord::getNonCoopCartVel(nonCoopCartVelB, robotNameB);
90 
91  }
92  if (!readJJsharpA){
93  return3 = CoordInterfaceCoord::getJJsharp(admisVelToolA, robotNameA);
94  }
95  if (!readJJsharpB){
96  return4 = CoordInterfaceCoord::getJJsharp(admisVelToolB, robotNameB);
97  }
98 
99  if (return1 == -1 || return2 ==-1 || return3 ==-1 || return4 ==-1){
100  return -1;
101  }
102  if (return1 == -2 || return2 ==-2 || return3 ==-2 || return4 ==-2){
103  return -2;
104  }
105 
106  int counter = return1+return2+return3+return4;
107 
108  //if so, all get returned 0 (message arrived) or someone arent called because called
109  //previously and have the flag read to true.
110  if (counter == 0){ //reset read flags
111  readxDotA = false;
112  readxDotB = false;
113  readJJsharpA = false;
114  readJJsharpB = false;
115  }
116 
117  return counter;
118 
119 }
120 
121 void CoordInterfaceCoord::publishStartBoth(bool start){
122 
123  std_msgs::Bool start_msg;
124  start_msg.data = start;
125  startBothPub.publish(start_msg);
126 
127 }
128 
129 void CoordInterfaceCoord::publishCoopVel(Eigen::Matrix<double, VEHICLE_DOF, 1> coopVel){
130 
131  geometry_msgs::TwistStamped msg;
132  msg.twist.linear.x = coopVel(0);
133  msg.twist.linear.y = coopVel(1);
134  msg.twist.linear.z = coopVel(2);
135  msg.twist.angular.x = coopVel(3);
136  msg.twist.angular.y = coopVel(4);
137  msg.twist.angular.z = coopVel(5);
138 
139  coopVelPub.publish(msg);
140 
141 }
142 
143 
153 int CoordInterfaceCoord::getNonCoopCartVel(
154  Eigen::Matrix<double, VEHICLE_DOF, 1> *nonCoopCartVel_eigen,
155  std::string robotName){
156 
157  int vectSize;
158  std::vector<double> *tempXdot;
159  bool* read;
160  if (robotName.compare(robotNameA) == 0){
161  vectSize = tempXdotA.size();
162  tempXdot = &tempXdotA;
163  read = &readxDotA;
164 
165  } else if (robotName.compare(robotNameB) == 0){
166  vectSize = tempXdotB.size();
167  tempXdot = &tempXdotB;
168  read = &readxDotB;
169  } else {
170  return -2;
171  }
172 
173  if (vectSize == 0){
174 // std::cout << "[COORDINATOR][COORD_INTERFACE] WARNING:"
175 // << "No xDot_tool to read now for "
176 // << robotName << std::endl;
177  return 1;
178  }
179 
180  if (vectSize != VEHICLE_DOF){
181  std::cout << "[COORDINATOR][COORD_INTERFACE] ERROR:"
182  << "something wrong with dimension of xDot_tool received from "
183  << robotName << std::endl;
184  return -1;
185  }
186 
187  for (int i=0; i<vectSize; i++){
188  (*nonCoopCartVel_eigen)(i,0) = tempXdot->at(i);
189  }
190 
191  tempXdot->clear();
192  *read = true;
193 
194  return 0;
195 }
196 
206 int CoordInterfaceCoord::getJJsharp(
207  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> *admisVelTool_eigen,
208  std::string robotName){
209 
210 
211  int nSize;
212  std::vector<double> *tempJJsharp;
213  bool* read;
214  if (robotName.compare(robotNameA) == 0){
215  nSize = tempJJsharpA.size();
216  tempJJsharp = &tempJJsharpA;
217  read = &readJJsharpA;
218  } else if (robotName.compare(robotNameB) == 0){
219  nSize = tempJJsharpB.size();
220  tempJJsharp = &tempJJsharpB;
221  read = &readJJsharpB;
222  } else {
223  return -2;
224  }
225 
226 
227  if (nSize == 0){
228 // std::cout << "[COORDINATOR][COORD_INTERFACE] WARNING:"
229 // << "No JJ# to read now "
230 // << std::endl;
231  return 1;
232  }
233 
234  if (nSize != VEHICLE_DOF*VEHICLE_DOF){
235 // std::cout << "[COORDINATOR][COORD_INTERFACE] ERROR:"
236 // << "something wrong with dimension of JJ# received from "
237 // << robotName << std::endl;
238  return -1;
239  }
240 
241  //Rembering that eigen is column major and the msg is store row major
242  int posRow = 0;
243  for (int i=0; i<VEHICLE_DOF; i++){
244  for (int j=0; j<VEHICLE_DOF; j++){
245  posRow = i*VEHICLE_DOF;
246  (*admisVelTool_eigen)(i,j) = tempJJsharp->at(posRow + j);
247  }
248  }
249 
250  tempJJsharp->clear();
251  *read = true;
252  return 0;
253 }
254 
255 
256 void CoordInterfaceCoord::readyRobASubCallback(const std_msgs::Bool::ConstPtr& start){
257  readyRobA = start->data;
258 }
259 void CoordInterfaceCoord::readyRobBSubCallback(const std_msgs::Bool::ConstPtr& start){
260  readyRobB = start->data;
261 }
262 
263 
272 void CoordInterfaceCoord::subCoordFromMMACallBack(const peg_msgs::toCoord::ConstPtr& msg){
273 
274  tempXdotA.resize(6); //message is a twist so 6 elements
275 
276  tempXdotA.at(0) = msg->xDot.twist.linear.x;
277  tempXdotA.at(1) = msg->xDot.twist.linear.y;
278  tempXdotA.at(2) = msg->xDot.twist.linear.z;
279  tempXdotA.at(3) = msg->xDot.twist.angular.x;
280  tempXdotA.at(4) = msg->xDot.twist.angular.y;
281  tempXdotA.at(5) = msg->xDot.twist.angular.z;
282 
283 
284  //dim[0].stride is the total number of element (row*col)
285  tempJJsharpA.resize(msg->JJsharp.layout.dim[0].stride);
286  tempJJsharpA = msg->JJsharp.data;
287 
288 }
289 
298 void CoordInterfaceCoord::subCoordFromMMBCallBack(const peg_msgs::toCoord::ConstPtr& msg){
299 
300  tempXdotB.resize(6); //message is a twist so 6 elements
301 
302  tempXdotB.at(0) = msg->xDot.twist.linear.x;
303  tempXdotB.at(1) = msg->xDot.twist.linear.y;
304  tempXdotB.at(2) = msg->xDot.twist.linear.z;
305  tempXdotB.at(3) = msg->xDot.twist.angular.x;
306  tempXdotB.at(4) = msg->xDot.twist.angular.y;
307  tempXdotB.at(5) = msg->xDot.twist.angular.z;
308 
309 
310  //dim[0].stride is the total number of element (row*col)
311  tempJJsharpB.resize(msg->JJsharp.layout.dim[0].stride);
312  tempJJsharpB = msg->JJsharp.data;
313 
314 }
315 
316 int CoordInterfaceCoord::getForceTorque(Eigen::Vector3d *force, Eigen::Vector3d *torque){
317 
318  *force = CONV::vector_std2Eigen(vectorForce);
319  *torque = CONV::vector_std2Eigen(vectorTorque);
320 
321  return 0;
322 }
323 
324 void CoordInterfaceCoord::subForceTorqueCallback(const geometry_msgs::WrenchStamped& msg){
325 
326  vectorForce.at(0) = msg.wrench.force.x;
327  vectorForce.at(1) = msg.wrench.force.y;
328  vectorForce.at(2) = msg.wrench.force.z;
329 
330  vectorTorque.at(0) = msg.wrench.torque.x;
331  vectorTorque.at(1) = msg.wrench.torque.y;
332  vectorTorque.at(2) = msg.wrench.torque.z;
333 
334 }
335 
336 void CoordInterfaceCoord::publishUpdatedGoal(Eigen::Matrix4d wTgoal){
337 
338 // geometry_msgs::Vector3Stamped linPosition;
339 // linPosition.vector.x = wTgoal(0,3);
340 // linPosition.vector.y = wTgoal(1,3);
341 // linPosition.vector.z = wTgoal(2,3);
342 
343 // pubUpdateGoal.publish(linPosition);
344 
345  peg_msgs::transformMat transfMsg;
346  transfMsg.linPosition.vector.x = wTgoal(0,3);
347  transfMsg.linPosition.vector.y = wTgoal(1,3);
348  transfMsg.linPosition.vector.z = wTgoal(2,3);
349 
350  //COLUMN MAJOR (column 1 .... column 3)
351  transfMsg.rotation.resize(9);
352  transfMsg.rotation.at(0).data = wTgoal(0,0);
353  transfMsg.rotation.at(1).data = wTgoal(1,0);
354  transfMsg.rotation.at(2).data = wTgoal(2,0);
355  transfMsg.rotation.at(3).data = wTgoal(0,1);
356  transfMsg.rotation.at(4).data = wTgoal(1,1);
357  transfMsg.rotation.at(5).data = wTgoal(2,1);
358  transfMsg.rotation.at(6).data = wTgoal(0,2);
359  transfMsg.rotation.at(7).data = wTgoal(1,2);
360  transfMsg.rotation.at(8).data = wTgoal(2,2);
361 
362  pubUpdateGoal.publish(transfMsg);
363 
364 }
365 
366 
int getMatricesFromRobs(Eigen::Matrix< double, VEHICLE_DOF, 1 > *nonCoopCartVelA, Eigen::Matrix< double, VEHICLE_DOF, 1 > *nonCoopCartVelB, Eigen::Matrix< double, VEHICLE_DOF, VEHICLE_DOF > *admisVelToolA, Eigen::Matrix< double, VEHICLE_DOF, VEHICLE_DOF > *admisVelToolB)
CoordInterfaceCoord::getMatricesFromRobs get all matrices needed to algorithm The function call each ...