1 #include "header/robotInterface.h" 12 this->robotName = robotName;
13 this->topicRoot =
"/uwsim/" + robotName +
"/";
15 std::cout <<
"[" << robotName <<
"][ROBOT_INTERFACE] Start"<<std::endl;
17 pubTwist = nh.advertise<geometry_msgs::TwistStamped>((topicRoot +
"twist_command"),1);
18 pubJoint = nh.advertise<sensor_msgs::JointState>((topicRoot +
"joint_command"),1);
20 subJointState = nh.subscribe(topicRoot+
"joint_state", 1, &RobotInterface::subJointStateCallback,
this);
24 vectorForceQueue.resize(3);
25 vectorTorqueQueue.resize(3);
26 for (
int i =0; i<3; i++){
27 vectorForceQueue.at(i) = boost::circular_buffer<double>(NELEMENTQUEUEFOR);
28 vectorTorqueQueue.at(i) = boost::circular_buffer<double>(NELEMENTQUEUETOR);
34 subForceTorque = nh.subscribe(
"/uwsim/g500_A/forceSensorPeg", 1, &RobotInterface::subForceTorqueCallback,
this);
38 int RobotInterface::init(){
46 std::string topic =
"/" + robotName;
47 tfListener.waitForTransform(
"world", topic, ros::Time(0), ros::Duration(3.0));
51 while (jState_priv.size()==0){
52 std::cout <<
"[" << robotName <<
"][ROBOT_INTERFACE] Waiting for Jstate..." 58 std::cout <<
"[" << robotName <<
"][ROBOT_INTERFACE] Init done" << std::endl;
62 int RobotInterface::getwTv(Eigen::Matrix4d* wTv_eigen){
68 tf::StampedTransform wTv_tf;
70 std::string topic =
"/" + robotName;
72 tfListener.lookupTransform(
"world", topic, ros::Time(0), wTv_tf);
74 }
catch (tf::TransformException &ex) {
75 ROS_ERROR(
"%s",ex.what());
76 ros::Duration(1.0).sleep();
88 *wTv_eigen = CONV::transfMatrix_tf2eigen(wTv_tf);
94 void RobotInterface::subJointStateCallback(
const sensor_msgs::JointState& js)
96 jState_priv = js.position;
111 *jState = jState_priv;
119 void RobotInterface::subForceTorqueCallback(
const geometry_msgs::WrenchStamped& msg){
125 vectorForceQueue.at(0).push_back(msg.wrench.force.x);
126 vectorForceQueue.at(1).push_back(msg.wrench.force.y);
127 vectorForceQueue.at(2).push_back(msg.wrench.force.z);
134 vectorTorqueQueue.at(0).push_back(msg.wrench.torque.x);
135 vectorTorqueQueue.at(1).push_back(msg.wrench.torque.y);
136 vectorTorqueQueue.at(2).push_back(msg.wrench.torque.z);
140 int RobotInterface::getForceTorque(Eigen::Vector3d *force, Eigen::Vector3d *torque){
142 if (vectorForceQueue.at(0).size() == 0){
148 std::vector<double> sumForces (3);
149 std::vector<double> sumTorques (3);
151 for (
int i=0; i<3; i++){
152 sumForces.at(i) = std::accumulate(vectorForceQueue.at(i).begin(), vectorForceQueue.at(i).end(), 0.0);
153 sumTorques.at(i) = std::accumulate(vectorTorqueQueue.at(i).begin(), vectorTorqueQueue.at(i).end(), 0.0);
155 sumForces.at(i) /= vectorForceQueue.at(i).size();
156 sumTorques.at(i) /= vectorTorqueQueue.at(i).size();
160 *force = CONV::vector_std2Eigen(sumForces);
161 *torque = CONV::vector_std2Eigen(sumTorques);
167 int RobotInterface::sendyDot(std::vector<double> yDot){
172 sensor_msgs::JointState js;
173 js.name.push_back(std::string(
"Slew"));
174 js.velocity.push_back(yDot.at(0));
175 js.name.push_back(std::string(
"Shoulder"));
176 js.velocity.push_back(yDot.at(1));
177 js.name.push_back(std::string(
"Elbow"));
178 js.velocity.push_back(yDot.at(2));
179 js.name.push_back(std::string(
"JawRotate"));
180 js.velocity.push_back(yDot.at(3));
181 geometry_msgs::TwistStamped twist;
182 twist.twist.linear.x=yDot.at(4);
183 twist.twist.linear.y=yDot.at(5);
184 twist.twist.linear.z=yDot.at(6);
185 twist.twist.angular.x=yDot.at(7);
186 twist.twist.angular.y=yDot.at(8);
187 twist.twist.angular.z=yDot.at(9);
189 pubJoint.publish(js);
190 pubTwist.publish(twist);
int getJointState(std::vector< double > *jState)
robotInterface::getJointState
RobotInterface(ros::NodeHandle nh, std::string robotName)
robotInterface::robotInterface Constructor