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

How to make sure all messages are published before calling ros::Publisher::shutdown?

asked 2013-01-15 21:34:11 -0500

fredrik.heintz gravatar image

In my C++ program I publish a message to a ros::Publisher with the publish method and then immediately shuts down the publisher using the shutdown method (because the program is finished). However, the result is that the message is not published. If I add a sleep of a few milliseconds before the shutdown, then it works. My question is, how do I make sure that all messages waiting in the publisher's queue are sent before calling shutdown?

edit retag flag offensive close merge delete


I tried explicitly calling the processPublishQueue on the publication associated with the topic after publish and before shutdown: ros::PublicationPtr pub = ros::TopicManager::instance()->lookupPublication(topic_name); if (pub) { pub->processPublishQueue(); } But it didn't send the message.

fredrik.heintz gravatar image fredrik.heintz  ( 2013-01-15 22:17:06 -0500 )edit

Adding a cout statement or sleep(1000) however works fine, but it seems very risky to rely on this to make sure the message is actually sent.

fredrik.heintz gravatar image fredrik.heintz  ( 2013-01-15 22:17:20 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-01-23 08:38:19 -0500

tfoote gravatar image

The standard solution is to wait before shutting down. If you want a communication channel with known delivery you need to use a service call.

edit flag offensive delete link more

Question Tools


Asked: 2013-01-15 21:34:11 -0500

Seen: 497 times

Last updated: Jan 23 '14