AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
coordInterfaceMissMan.cpp
1 #include "header/coordInterfaceMissMan.h"
2 
3 CoordInterfaceMissMan::CoordInterfaceMissMan(ros::NodeHandle nh, std::string robotName){
4 
5  this->robotName = robotName;
6  this->start = false;
7  std::string topicRoot = "/uwsim/" + robotName + "_MissionManager/";
8 
9  std::cout << "[" << robotName <<"][COORD_INTERFACE] Start"<<std::endl;
10 
11  pubToCoord = nh.advertise<peg_msgs::toCoord>((topicRoot + "toCoord"), 1);
12 
13  //pub to let coord that is ready (e.g. to see if both missManager A & B started)
14  // ASK towrite in doc _missionManager" topic are for mission manager topic...
15  //withotu missman (only robname) are topic of robotInterface
16  readyPub = nh.advertise<std_msgs::Bool>(topicRoot + "ready", 1);
17 
18  //sub to know if other robot is ready
19  //even if we can read directly the topic of the other robot,
20  // for consistency ALL infos received from extern (ie subscribed topic)
21  // are given by coordinator.
22  // for make each miss manager comunicate with its own robot, there is
23  // the robot interface that is another thing
24  std::string topicStart = "/uwsim/Coord/StartStopBoth";
25  startSub = nh.subscribe(topicStart, 1, &CoordInterfaceMissMan::startSubCallback, this);
26 
27  //sub to receive coop vel from coordinator
28  std::string topicCoopVel = "/uwsim/Coord/CoopVel";
29  coopVelSub = nh.subscribe(topicCoopVel, 1, &CoordInterfaceMissMan::subMMFromCoordCallBack, this);
30 
31 
34  //two dimension (it is a matrix)
35  toCoordMsg.JJsharp.layout.dim.push_back(std_msgs::MultiArrayDimension());
36  toCoordMsg.JJsharp.layout.dim.push_back(std_msgs::MultiArrayDimension());
37  toCoordMsg.JJsharp.layout.dim[0].label = "row";
38  toCoordMsg.JJsharp.layout.dim[1].label = "column";
39  toCoordMsg.JJsharp.layout.dim[0].size = VEHICLE_DOF;
40  toCoordMsg.JJsharp.layout.dim[1].size = VEHICLE_DOF;
41  toCoordMsg.JJsharp.layout.dim[0].stride = VEHICLE_DOF*VEHICLE_DOF;
42  toCoordMsg.JJsharp.layout.dim[1].stride = VEHICLE_DOF;
43  toCoordMsg.JJsharp.layout.data_offset = 0;
44 
46  if (CHANGE_GOAL){
47  updateGoalArrived = false;
48  subUpdatedGoal = nh.subscribe("/uwsim/Coord/updatedGoal", 1, &CoordInterfaceMissMan::subUpdatedGoalCallback, this);
49  }
50 }
51 
52 void CoordInterfaceMissMan::pubIamReadyOrNot(bool ready){
53  std_msgs::Bool ready_msg;
54  ready_msg.data = ready;
55  readyPub.publish(ready_msg);
56 }
57 
58 void CoordInterfaceMissMan::startSubCallback(const std_msgs::Bool::ConstPtr& msg){
59  start = msg->data;
60 }
61 
62 bool CoordInterfaceMissMan::getStartFromCoord(){
63  return this->start;
64 }
65 
76 int CoordInterfaceMissMan::publishToCoord(Eigen::Matrix<double, VEHICLE_DOF, 1> nonCoopCartVel_eigen,
77  Eigen::Matrix<double, VEHICLE_DOF, VEHICLE_DOF> admisVelTool_eigen){
78 
79 
80  toCoordMsg.xDot.twist.linear.x = nonCoopCartVel_eigen(0);
81  toCoordMsg.xDot.twist.linear.y = nonCoopCartVel_eigen(1);
82  toCoordMsg.xDot.twist.linear.z = nonCoopCartVel_eigen(2);
83  toCoordMsg.xDot.twist.angular.x = nonCoopCartVel_eigen(3);
84  toCoordMsg.xDot.twist.angular.y = nonCoopCartVel_eigen(4);
85  toCoordMsg.xDot.twist.angular.z = nonCoopCartVel_eigen(5);
86 
87 
88  toCoordMsg.JJsharp.data.clear();
89  //eigen is column major, multiarray is row major, so we cant simply msg.data = eigen.data
90  //and having clear above, we cant directly acces with toCoordMsg.JJsharp.data.at(posRow + j)
91  //ASK in this way is it safer? or we can directly overwrite value of previous message (ie without clear?) it would be also faster?
92  //faster because every time we dont have to delete memory with clear and reassign with the toCoordMsg.JJsharp.data = temp;
93  std::vector<double> temp(VEHICLE_DOF*VEHICLE_DOF, 0);
94  int posRow = 0;
95  for (int i=0; i<VEHICLE_DOF; i++){
96  for (int j=0; j<VEHICLE_DOF; j++){
97  posRow = i*VEHICLE_DOF;
98  temp[posRow + j] = admisVelTool_eigen(i,j);
99  }
100  }
101  toCoordMsg.JJsharp.data = temp;
102  pubToCoord.publish(toCoordMsg);
103 
104  return 0;
105 }
106 
107 
108 int CoordInterfaceMissMan::getCoopCartVel(
109  Eigen::Matrix<double, VEHICLE_DOF, 1> *coopCartVel_eigen){
110 
111  int vectSize = tempCoopVel.size();
112 
113  if (vectSize == 0){
114 // std::cout << "[" << robotName << "][COORD_INTERFACE] WARNING:"
115 // << "No xDot_coopVel to read now "
116 // << std::endl;
117  return 1;
118  }
119 
120  if (vectSize != VEHICLE_DOF){
121  std::cout << "[" << robotName << "][COORD_INTERFACE] ERROR:"
122  << "something wrong with dimension of xDot_coopVel received from "
123  << "Coordinator" << std::endl;
124  return -1;
125  }
126 
127  for (int i=0; i<vectSize; i++){
128  (*coopCartVel_eigen)(i,0) = tempCoopVel.at(i);
129  }
130 
131  tempCoopVel.clear();
132 
133  return 0;
134 }
135 
136 
142 void CoordInterfaceMissMan::subMMFromCoordCallBack(const geometry_msgs::TwistStamped& msg){
143 
144  tempCoopVel.resize(6); //message is a twist so 6 elements
145 
146  tempCoopVel.at(0) = msg.twist.linear.x;
147  tempCoopVel.at(1) = msg.twist.linear.y;
148  tempCoopVel.at(2) = msg.twist.linear.z;
149  tempCoopVel.at(3) = msg.twist.angular.x;
150  tempCoopVel.at(4) = msg.twist.angular.y;
151  tempCoopVel.at(5) = msg.twist.angular.z;
152 
153 }
154 
155 
157 int CoordInterfaceMissMan::getUpdatedGoal(Eigen::Matrix4d *wTgoalTool_eigen){
158 
159  if (updateGoalArrived){
160  (*wTgoalTool_eigen)(0,3) = updatedPosLin.x;
161  (*wTgoalTool_eigen)(1,3) = updatedPosLin.y;
162  (*wTgoalTool_eigen)(2,3) = updatedPosLin.z;
163 
164  // ang is in a array with COLUMN MAJOR
165  (*wTgoalTool_eigen)(0,0) = updatedPosAng.at(0);
166  (*wTgoalTool_eigen)(1,0) = updatedPosAng.at(1);
167  (*wTgoalTool_eigen)(2,0) = updatedPosAng.at(2);
168  (*wTgoalTool_eigen)(0,1) = updatedPosAng.at(3);
169  (*wTgoalTool_eigen)(1,1) = updatedPosAng.at(4);
170  (*wTgoalTool_eigen)(2,1) = updatedPosAng.at(5);
171  (*wTgoalTool_eigen)(0,2) = updatedPosAng.at(6);
172  (*wTgoalTool_eigen)(1,2) = updatedPosAng.at(7);
173  (*wTgoalTool_eigen)(2,2) = updatedPosAng.at(8);
174 
175 
176  updateGoalArrived = false;
177  return 0;
178  }
179 
180  return 1;
181 
182 }
183 
184 void CoordInterfaceMissMan::subUpdatedGoalCallback(const peg_msgs::transformMat::ConstPtr &msg){
185  updateGoalArrived = true;
186  updatedPosLin = msg->linPosition.vector;
187  updatedPosAng.resize(9);
188  for (int i = 0; i < 9; i++){
189  updatedPosAng.at(i) = msg->rotation[i].data;
190  }
191 }
192 
193 
194 
196 //int CoordInterfaceMissMan::getUpdatedGoal(Eigen::Matrix4d *wTgoalTool_eigen){
197 
198 // if (updateGoalArrived){
199 // (*wTgoalTool_eigen)(0,3) = updatedPosLin.x;
200 // (*wTgoalTool_eigen)(1,3) = updatedPosLin.y;
201 // (*wTgoalTool_eigen)(2,3) = updatedPosLin.z;
202 // updateGoalArrived = false;
203 // return 0;
204 // }
205 
206 // return 1;
207 
208 //}
209 
210 //void CoordInterfaceMissMan::subUpdatedGoalCallback(const geometry_msgs::Vector3Stamped &msg){
211 // updateGoalArrived = true;
212 // updatedPosLin = msg.vector;
213 //}
int getUpdatedGoal(Eigen::Matrix4d *wTgoalTool_eigen)
change goal thingss
int publishToCoord(Eigen::Matrix< double, VEHICLE_DOF, 1 > nonCoopCartVel_eigen, Eigen::Matrix< double, VEHICLE_DOF, VEHICLE_DOF > admisVelTool_eigen)
CoordInterface::publishToCoord publish the necessary thing to the coordinator node.
CoordInterfaceMissMan(ros::NodeHandle nh, std::string robotName)