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