1 #include "header/robotInterface.h" 9 robotInterface::robotInterface(std::string topicRoot, std::string robotName,
10 std::string otherRobotName, std::string toolName,
11 int argc,
char **argv)
14 std::cout <<
"[" << robotName <<
"][ROS_INTERFACE] Start"<<std::endl;
15 ros::init(argc, argv,
"robotInterface_" + robotName);
18 this->robotName = robotName;
19 this->otherRobotName = otherRobotName;
20 this->topicRoot = topicRoot;
21 this->toolName = toolName;
23 pubTwist = nh.advertise<geometry_msgs::TwistStamped>((topicRoot +
"twist_command_A"),1);
24 pubJoint = nh.advertise<sensor_msgs::JointState>((topicRoot +
"joint_command_A"),1);
26 subJointState = nh.subscribe(topicRoot+
"joint_state_A", 1, &robotInterface::subJointStateCallback,
this);
30 int robotInterface::init(){
37 std::string topic =
"/" + robotName;
38 tfListener.waitForTransform(
"world", topic, ros::Time(0), ros::Duration(3.0));
41 std::string topic2 =
"/" + toolName;
42 tfListener.waitForTransform(
"world", topic2, ros::Time(0), ros::Duration(1.0));
45 std::string topic3 =
"/" + otherRobotName;
46 tfListener.waitForTransform(
"world", topic3, ros::Time(0), ros::Duration(3.0));
50 while (jState_priv.size()==0){
56 std::cout <<
"[" << robotName <<
"][ROS_INTERFACE] Init done" << std::endl;
61 int robotInterface::getwTv(Eigen::Matrix4d* wTv_eigen){
67 tf::StampedTransform wTv_tf;
69 std::string topic =
"/" + robotName;
71 tfListener.lookupTransform(
"world", topic, ros::Time(0), wTv_tf);
73 }
catch (tf::TransformException &ex) {
74 ROS_ERROR(
"%s",ex.what());
75 ros::Duration(1.0).sleep();
78 *wTv_eigen = CONV::transfMatrix_tf2eigen(wTv_tf);
83 int robotInterface::getwTt(Eigen::Matrix4d* wTt_eigen){
89 tf::StampedTransform wTt_tf;
91 std::string topic =
"/" + toolName;
93 tfListener.lookupTransform(
"world", topic, ros::Time(0), wTt_tf);
95 }
catch (tf::TransformException &ex) {
96 ROS_ERROR(
"%s",ex.what());
97 ros::Duration(1.0).sleep();
100 *wTt_eigen = CONV::transfMatrix_tf2eigen(wTt_tf);
106 int robotInterface::getOtherRobPos(Eigen::Vector3d* pos){
112 tf::StampedTransform wTvother_tf;
114 std::string topic =
"/" + otherRobotName;
116 tfListener.lookupTransform(
"world", topic, ros::Time(0), wTvother_tf);
118 }
catch (tf::TransformException &ex) {
119 ROS_ERROR(
"%s",ex.what());
120 ros::Duration(1.0).sleep();
123 Eigen::Matrix4d wTvother_eigen = CONV::transfMatrix_tf2eigen(wTvother_tf);
124 *pos = wTvother_eigen.topRightCorner<3,1>();
130 void robotInterface::subJointStateCallback(
const sensor_msgs::JointState& js)
132 jState_priv = js.position;
141 int robotInterface::getJointState(std::vector<double> *jState){
147 *jState = jState_priv;
152 int robotInterface::sendyDot(std::vector<double> yDot){
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);
174 pubJoint.publish(js);
175 pubTwist.publish(twist);
180 void robotInterface::spinOnce(){