Rosanswers logo

On the terminal, I'm entering in

$ rostopic echo /robot/limb/left/endpoint_state

to get the position of the endpoints. The output comes out like this:

position: x: 0.0881700859423
y: 0.666633918353
z: -0.25671871773  

Now, in my program, I want to subscribe to this topic and retrieve just the above coordinates: the pose x, y and z. My subscriber looks like this:

ros::Subscriber sub = n.subscribe("/robot/limb/left/endpoint_state", 1, leftEndpointCallback)

In my callback, I'm trying to get just these specific coordinates as floats or doubles or whatever data type they are, and be able to use them later in my code (i.e. use the x coordinate in an if statement, for example). How would I do this?

Originally posted by grisam on ROS Answers with karma: 1 on 2014-07-01

Post score: 0


1 Answer 1


Rosanswers logo

The first thing to do is determine what is the message used in that topic, e.g:

$ rostopic info /robot/limb/left/endpoint_state
Type: baxter_core_msgs/EndpointState   

Therefore, your function leftEndpointCallback should look like this:

void leftEndpointCallback(const baxter_core_msgs::EndpointStateConstPtr& _msg) 
  // The pose is within the _msg object
  ROS_INFO_STREAM("pose: " << _msg->pose);
  ROS_INFO_STREAM("pose.position.x: " << _msg->pose.position.x);
  ROS_INFO_STREAM("pose.position.y: " << _msg->pose.position.y);
  ROS_INFO_STREAM("pose.position.z: " << _msg->pose.position.z);

How to use these values somewhere else is scope of C/C++ callbacks. If you really need to process this data outside the callback you may consider implementing a Class. It seems that most people hate global variables (count me as well)

Originally posted by Francisco with karma: 31 on 2014-07-15

This answer was ACCEPTED on the original site

Post score: 0

Original comments

Comment by grisam on 2014-07-23:
Is there a way I could do this and get only ONE specific point? I keep getting a stream of points but I only want the coordinates at one time instance. Also, what do you mean it's within the scope? If I save _msg->pose.position.x as a variable, how would I access that variable outside the callback?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.