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

Revision history [back]

You shouldn't call ros::spinOnce() inside the callback. What it does is that it checks the subscriber queue, then calls your callback again, so that now you're calling the callback recursively. Instead, process the single message and exit the callback, which will drop you back into the ros::spin() call in main().

Also, you shouldn't re-create the publisher every time you want to publish a message (inside the callback). Instead, create it once (in main) and use it inside the callback. Otherwise, the subscribers won't be able to read the message before the publisher is destroyed again (when exiting the callback).