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

How to make a callback function to subscribe to a non string topic?

asked 2011-11-20 20:49:57 -0500

marcobecerrap gravatar image

updated 2011-11-22 16:25:27 -0500

kwc gravatar image

Hi, I've followed succesfully all basic tutorials. Then I moved to stage to work on a robot simulation. I also installed the joystick_drivers stack to use a joystick to move my virtual robot in stage. Everything is working properly (I can see the messages, topics, node graph, etc).

Right now, I tried to modify the simple subscriber 'listener' of the basic tutorials to read the information of my joystick. The problem is that the example is made for a string message.

The callback function in the tutorial is something like this: void chatterCallback(const std_msgs::String::ConstPtr& msg){ ROS_INFO("I heard: [%s]", msg->data.c_str()); }

But in the joy topic I am reading Joy messages: float32[] axes int32[] buttons

How do I manage this 'complex' messages in a correct callback function?

Thnx alot for your help!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2011-11-20 21:53:02 -0500

updated 2011-11-20 22:01:49 -0500

Add the sensor_msgs package to your dependencies in your manifest.xml with <depend package="sensor_msgs"/>, then include the appropriate C++ message header file with #include <sensor_msgs/Joy.h>, and change the callback to

void myCallback (const sensor_msgs::Joy::ConstPtr& msg)
  for (unsigned i = 0; i < msg->axes.size(); ++i) {
    ROS_INFO("Axis %d is now at position %f", i, msg->axes[i]);
edit flag offensive delete link more


Hi Röhling, thank you very much for your help!!! Your answer was the explanation that I needed. Now everything is working just perfect!!! Thanks alot!
marcobecerrap gravatar image marcobecerrap  ( 2011-11-20 23:53:17 -0500 )edit

Question Tools

1 follower


Asked: 2011-11-20 20:49:57 -0500

Seen: 705 times

Last updated: Nov 20 '11