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

Revision history [back]

Have a look at the tutorial Writing a Simple Publisher and Subscriber (C++). That does exactly the thing you want to do (sending an receiving a string). Note that there are no arrays sent or received in ROS, but rather std::strings and C++ vectors.

If you want to handle all three topics at once in the same callback, you would probably want a Time Synchronizer from message_filters.

Have a look at the tutorial Writing a Simple Publisher and Subscriber (C++). That does exactly the thing you want to do (sending an receiving a string). Note that there are no arrays sent or received in ROS, but rather std::strings and C++ vectors.

If you want to handle all three topics at once in the same callback, you would probably want a Time Synchronizer from message_filters.

Updated answer to your code snipplet: You need to move the callback creation out of the loop, else you will constantly create and destroy callbacks. You only create it once before the loop. Also, you need to replace spin() with spinOnce() if you have your own loop:

ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

while (ros::ok())
{
ros::spinOnce();
sleep(0.1);
printf("\nIM Not stuck anymore!\n");
}
return 0;

For details, see Callbacks and Spinning