ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Problem using subscribed motor position topic

asked 2011-05-23 11:11:24 -0500

kp3509 gravatar image

updated 2011-07-26 13:39:07 -0500

mmwise gravatar image

Hi, I am new to ROS and I am trying to get access to my servo motor position to use in broadcasting a transform. I have done the listener tutorial and my listen program below runs and compiles fine, it even triggers the void function when the motor publishes a position topic. How can I get pull the position variable that I am subscribed to in order to use it in a program? Specifically what do I need to change "sg->data.c_str()" to in order for it to say the motor position integer in the message?


#include "ros/ros.h"

#include "ax12_driver_core/MotorStateList.h"

void motorchatterCallback(const ax12_driver_core::MotorStateList::ConstPtr&)

 // ROS_INFO("I heard: [%s]", msg->data.c_str());


int main(int argc, char **argv)
   ros::init(argc, argv, "listener");

  ros::NodeHandle n;

  ros::Subscriber sub = n.subscribe("motor_states/ttyUSB0", 1000, motorchatterCallback);


  return 0;

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2011-06-13 14:07:09 -0500

dking gravatar image

updated 2011-07-26 13:39:40 -0500

mmwise gravatar image

ax12_driver_core::MotorStateList contains an array of ax12_driver_core::MotorState messages. In C++, ROS arrays are converted to C++ std::vector.

To print the motor position of the first MotorState in the array you might use something like:

ROS_INFO("I heard : %d", msg->motor_states[0].position;);

You might want to also check that motor_states vector has at least 1 element :

if (msg->motor_states.size() > 0)
edit flag offensive delete link more

answered 2012-06-01 19:16:25 -0500

malong gravatar image

hi,it is my first time to post message on line ,i tried to ask my question in the page( but it is shamble,how to set type.could you tell me ? thanks.

edit flag offensive delete link more

answered 2011-06-13 14:47:10 -0500

arebgun gravatar image

I would suggest going through the Creating a joint controller tutorial. Once you create a proper joint controller you will be able to subscribe to a state topic and get all information about the joint position, speed, load, etc. in proper ROS units (e.g. position in radians, speed in radians per second) instead of raw servo encoder ticks. Although you can subscribe to motor_states/ttyUSB0 topic, it really wasn't meant to be used directly. A joint controller will also provide a bunch of services through which you will be able to set joint speed, enable/disable torque, change torque limits, change compliance margin and slope among other things. There will also be a command topic which can be used to control you joint (sending a simple float value will move the motor).

edit flag offensive delete link more

Question Tools


Asked: 2011-05-23 11:11:24 -0500

Seen: 688 times

Last updated: Jun 01 '12