0
$\begingroup$

Rosanswers logo

Can I subscribe to multiple topics / multiple callbacks (declaring callbacks as member functions inwith one publisher in ROS C++ code


Originally posted by Devasena Inupakutika on ROS Answers with karma: 320 on 2013-04-21

Post score: 2

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

It's perfectly acceptable to subscribe to multiple topics and/or publish to multiple topics from within the same ROS node. Each topic publisher or subscriber will have its own ros::Publisher or ros::Subscriber object, and each subscriber will probably have its own callback function (though this is not strictly required).

See here for a mroe detailed description of publishers/subscribers in C++.

Note that subscriber callbacks are generally called by background ROS threads. If you use the default node setup shown in most examples (using ros::spin), all callbacks are called from a single thread. See here for a more detailed explanation of multi-threaded usage of roscpp, if that's important for your application.

Edit:
Whoops! Sorry for the double-answer. I'll leave this here, in case any of this info is helpful.


Originally posted by Jeremy Zoss with karma: 4976 on 2013-04-21

This answer was ACCEPTED on the original site

Post score: 8


Original comments

Comment by Devasena Inupakutika on 2013-04-22:
Thank You. This will be of great help to me.

Comment by ZiyangLI on 2015-01-24:
Hi @Jeremy, when subscribing to multiple topics and the queue size is just 1, what will happen in spinOnce? Will I randomly lose some messages?

$\endgroup$
0
$\begingroup$

Rosanswers logo

Each ros::Subscriber subscribes to one topic, and has one callback function.

You can create as many subscribers as you need. The callback functions can be regular functions or class methods.

For more details, see the ROS Subscriber API


Originally posted by Ivan Dryanovski with karma: 4954 on 2013-04-21

This answer was NOT ACCEPTED on the original site

Post score: 2


Original comments

Comment by Devasena Inupakutika on 2013-04-22:
Thank You.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.