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

Multiple Subscribers in one Cpp file.

asked 2013-04-21 08:49:44 -0600

Devasena Inupakutika gravatar image

updated 2013-04-21 08:51:01 -0600

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

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
8

answered 2013-04-21 09:57:35 -0600

updated 2013-04-21 09:59:34 -0600

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.

edit flag offensive delete link more

Comments

Thank You. This will be of great help to me.

Devasena Inupakutika gravatar image Devasena Inupakutika  ( 2013-04-22 00:10:44 -0600 )edit

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?

ZiyangLI gravatar image ZiyangLI  ( 2015-01-24 07:16:18 -0600 )edit
2

answered 2013-04-21 09:38:31 -0600

updated 2013-04-21 09:38:46 -0600

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

edit flag offensive delete link more

Comments

Thank You.

Devasena Inupakutika gravatar image Devasena Inupakutika  ( 2013-04-22 00:09:44 -0600 )edit
0

answered 2017-02-06 18:21:45 -0600

Morid_92 gravatar image

hi

i write a multi subscriber and one publisher in on cpp, but my publish speed is verry slow... how can i fix it ???

edit flag offensive delete link more

Comments

1

you can increase the publishing rate by reducing the number ros::Publisher pub = nh.advertise<std_msgs::string>("topic_name", 5);

sadeksadek gravatar image sadeksadek  ( 2019-10-11 06:18:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-04-21 08:49:44 -0600

Seen: 18,755 times

Last updated: Apr 21 '13