AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
rosInterface_old2.cpp
1 #include "header/robotInterface.h"
2 
9 robotInterface::robotInterface(std::string topicRoot, std::string robotName,
10  std::string otherRobotName, std::string toolName,
11  int argc, char **argv)
12 {
13 
14  std::cout << "[" << robotName <<"][ROS_INTERFACE] Start"<<std::endl;
15  ros::init(argc, argv, "robotInterface_" + robotName);
16  ros::NodeHandle nh;
17 
18  this->robotName = robotName;
19  this->otherRobotName = otherRobotName;
20  this->topicRoot = topicRoot;
21  this->toolName = toolName;
22 
23  pubTwist = nh.advertise<geometry_msgs::TwistStamped>((topicRoot + "twist_command_A"),1);
24  pubJoint = nh.advertise<sensor_msgs::JointState>((topicRoot + "joint_command_A"),1);
25 
26  subJointState = nh.subscribe(topicRoot+"joint_state_A", 1, &robotInterface::subJointStateCallback, this);
27 }
28 
29 
30 int robotInterface::init(){
31 
32  if(!ros::ok()){
33  return -1;
34  }
35 
36  //Wait to transform wTv to be ready (or fail if wait more than 3 sec)
37  std::string topic = "/" + robotName;
38  tfListener.waitForTransform("world", topic, ros::Time(0), ros::Duration(3.0));
39 
40  //wait to transform wTtool to be ready
41  std::string topic2 = "/" + toolName;
42  tfListener.waitForTransform("world", topic2, ros::Time(0), ros::Duration(1.0));
43 
44  //wait to transform wTv of the other robot to be ready
45  std::string topic3 = "/" + otherRobotName;
46  tfListener.waitForTransform("world", topic3, ros::Time(0), ros::Duration(3.0));
47 
48  //Wait to joint state to be ready (ie : the callback is called at least once)
49  ros::Rate rate(100);
50  while (jState_priv.size()==0){
51  ros::spinOnce();
52  rate.sleep();
53  }
54 
55 
56  std::cout << "[" << robotName <<"][ROS_INTERFACE] Init done" << std::endl;
57 
58  return 0;
59 }
60 
61 int robotInterface::getwTv(Eigen::Matrix4d* wTv_eigen){
62 
63  if(!ros::ok()){
64  return -1;
65  }
66 
67  tf::StampedTransform wTv_tf;
68 
69  std::string topic = "/" + robotName;
70  try {
71  tfListener.lookupTransform("world", topic, ros::Time(0), wTv_tf);
72 
73  } catch (tf::TransformException &ex) {
74  ROS_ERROR("%s",ex.what());
75  ros::Duration(1.0).sleep();
76  }
77 
78  *wTv_eigen = CONV::transfMatrix_tf2eigen(wTv_tf);
79 
80  return 0;
81 }
82 
83 int robotInterface::getwTt(Eigen::Matrix4d* wTt_eigen){
84 
85  if(!ros::ok()){
86  return -1;
87  }
88 
89  tf::StampedTransform wTt_tf;
90 
91  std::string topic = "/" + toolName;
92  try {
93  tfListener.lookupTransform("world", topic, ros::Time(0), wTt_tf);
94 
95  } catch (tf::TransformException &ex) {
96  ROS_ERROR("%s",ex.what());
97  ros::Duration(1.0).sleep();
98  }
99 
100  *wTt_eigen = CONV::transfMatrix_tf2eigen(wTt_tf);
101 
102  return 0;
103 }
104 
105 //TODO: maybe get position of other with another method
106 int robotInterface::getOtherRobPos(Eigen::Vector3d* pos){
107 
108  if(!ros::ok()){
109  return -1;
110  }
111 
112  tf::StampedTransform wTvother_tf;
113 
114  std::string topic = "/" + otherRobotName;
115  try {
116  tfListener.lookupTransform("world", topic, ros::Time(0), wTvother_tf);
117 
118  } catch (tf::TransformException &ex) {
119  ROS_ERROR("%s",ex.what());
120  ros::Duration(1.0).sleep();
121  }
122 
123  Eigen::Matrix4d wTvother_eigen = CONV::transfMatrix_tf2eigen(wTvother_tf);
124  *pos = wTvother_eigen.topRightCorner<3,1>();
125 
126  return 0;
127 }
128 
129 
130 void robotInterface::subJointStateCallback(const sensor_msgs::JointState& js)
131 {
132  jState_priv = js.position;
133 }
134 
141 int robotInterface::getJointState(std::vector<double> *jState){
142 
143  if(!ros::ok()){
144  return -1;
145  }
146 
147  *jState = jState_priv;
148  return 0;
149 }
150 
151 
152 int robotInterface::sendyDot(std::vector<double> yDot){
153 
154  if(!ros::ok()){
155  return -1;
156  }
157  sensor_msgs::JointState js;
158  js.name.push_back(std::string("Slew"));
159  js.velocity.push_back(yDot.at(0));
160  js.name.push_back(std::string("Shoulder"));
161  js.velocity.push_back(yDot.at(1));
162  js.name.push_back(std::string("Elbow"));
163  js.velocity.push_back(yDot.at(2));
164  js.name.push_back(std::string("JawRotate"));
165  js.velocity.push_back(yDot.at(3));
166  geometry_msgs::TwistStamped twist;
167  twist.twist.linear.x=yDot.at(4);
168  twist.twist.linear.y=yDot.at(5);
169  twist.twist.linear.z=yDot.at(6);
170  twist.twist.angular.x=yDot.at(7);
171  twist.twist.angular.y=yDot.at(8);
172  twist.twist.angular.z=yDot.at(9);
173 
174  pubJoint.publish(js);
175  pubTwist.publish(twist);
176  return 0;
177 
178 }
179 
180 void robotInterface::spinOnce(){
181  ros::spinOnce();
182 }