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

Multiple suscribers (subscribing different topics) to publish same topic

asked 2013-04-27 07:52:50 -0500

Hemu gravatar image

I have two subscribers which should continuously subscribe their respective topics and both the subscribers are supposed to publish the same topic (velocity of robot) depending on priority i.e. one subscriber should publish the topic under certain conditions and then the other subscriber takes up the task of publishing the topic when the priority changes while both continue to subscribe data even if they are not publishing. How can I implement this logic?
Thanks Hemu

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-04-28 18:36:50 -0500

updated 2013-04-29 11:26:46 -0500

ROS allows multiple publishers on the same topic. So, you could have two subscribers (in the same or different nodes) to different topics that each publish a velocity command on the same topic. Your "priority logic" would need to be smart enough to prevent publishing conflicting messages at the same time, to avoid confusing other nodes.

To accomplish the "message priority" functionality, you could also have a higher-level "gateway" node that listens to both nodes and decides which messages to publish on to the rest of ROS. I'm thinking something like the joint state publisher source_list functionality.

See a similar question here where @Ben_S warns against the difficulties with synchronization in the above approach. His suggestion is to, instead, have the higher-priority task subscribe to messages from the lower-priority task and decide whether to re-publish the low-priority messages or replace them with internally-generated high-priority messages.

ROS provides several methods to control how Subscriber Callbacks are processed, as described here. By default, all callbacks are processed by a single thread in the same order as the incoming messages are received. It is possible to use multiple threads (with different priority levels) to process the incoming messages, by assigning them to different Callback Queues as described in the above link.

This may be more complex than is required for your application, though. It may be easiest to use the callback functions to store the latest result from each subscriber in globally-accessible variables for your node, then use a separate thread to examine both messages and decide what to publish.

edit flag offensive delete link more


Hi Jeremy, Thanks a lot for the answer. I still have a doubt as to how are the multiple subscriber callbacks (in the same node) executed. Is there a certain order in which the callbacks are called during execution of the program?

Hemu gravatar image Hemu  ( 2013-04-29 11:06:01 -0500 )edit

hello I want to subscribe to rgb and depth image simultaneously and publish it to multiple topics using one node in cvbrige, plz plz can u provide me ur code, I m in trouble

ros gravatar image ros  ( 2013-06-12 08:34:24 -0500 )edit

Question Tools


Asked: 2013-04-27 07:52:50 -0500

Seen: 4,289 times

Last updated: Apr 29 '13