Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Publishers should be persistent; you shouldn't be creating a new publisher on each callback. This is because publisher setup can take a few seconds under poor conditions, and this can cause messages to be dropped before the publisher is completely set up.

Instead, you should create your publisher in main(), and the callback should only be interpreting the data and using the existing publisher to publish a new message.

You should NEVER call ros::spinOnce() from inside a callback; spinOnce is the call which processes message callbacks in the first place. Most ROS nodes call ros::spin() or ros::spinOnce() from main.

If you're trying for consistent performance, calling cv::waitKey from within your callback is also a bad idea; you don't want to do anything within your callback which could create a significant delay.