Ask Your Question

Subscribing to multiple topics of the same type.

asked 2013-02-24 14:16:55 -0500

superawesomepandacat gravatar image

Is it possible to subscribe to multiple topics of the same type using a single callback function? If it's possible, I can't figure out how.

Please and thank you.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2013-03-01 06:32:44 -0500

superawesomepandacat gravatar image

updated 2013-03-03 23:46:20 -0500

felix k gravatar image

Answer (should work with regular messages as well):

SubscriberFilter leftsub = leftsub(it, "/multisense_sl/camera/left/image_raw", 1);
SubscriberFilter rightsub = rightsub(it, "/multisense_sl/camera/right/image_raw", 1);
TimeSynchronizer<Image, Image> sync(leftsub, rightsub, 10);
sync.registerCallback(boost::bind(&imagesub::callback, this, _1, _2));
void imagesub::callback(const ImageConstPtr& left, const ImageConstPtr& right) {
edit flag offensive delete link more



answered 2013-02-24 17:54:57 -0500

updated 2013-03-03 23:50:03 -0500

felix k gravatar image

You can easily do that by creating multiple subscribers or a vector of subscribers and make all of them point at same callback function.

  sicklms_subs = new ros::Subscriber[no_of_ugv];
    for(int i = 0; i < no_of_ugv; i++)
        ss << "/sicklms" << i << "/scan";
        sicklms_subs[i] = nh.subscribe(ss.str(), 100, &mapper_direct::laserCallback, this);
edit flag offensive delete link more


That works. But is it possible to accept multiple different topics of the same type in a single callback function?

superawesomepandacat gravatar imagesuperawesomepandacat ( 2013-02-25 00:13:37 -0500 )edit

yeah it is possible to that. Every element of vector can be subscribed to a different message name and still can have same callback function. I hope this is what ur asking..

ayush_dewan gravatar imageayush_dewan ( 2013-02-25 03:02:31 -0500 )edit

It seems like I phrased my original question incorrectly, apologies for that. What I meant was, say I have to two subscribed topics, A and B. I want to process both A and B in a single callback function at the same time, for example A+B. Just wondering if that's possible.

superawesomepandacat gravatar imagesuperawesomepandacat ( 2013-02-25 11:17:17 -0500 )edit

I think its not possible to do that...ROS does not provide a direct functionality for it so u have to write ur callback function in a way that provides you this functionality.

ayush_dewan gravatar imageayush_dewan ( 2013-02-25 22:30:48 -0500 )edit

Yeah I'm trying to figure that out. Thanks anyways :)

superawesomepandacat gravatar imagesuperawesomepandacat ( 2013-02-25 23:09:51 -0500 )edit

Please edit your question to reflect what you want to know.

tfoote gravatar imagetfoote ( 2013-03-02 21:10:24 -0500 )edit

answered 2013-03-02 21:12:55 -0500

tfoote gravatar image

Based on your comments you're looking for getting paired messages on two topics. The recommended way to do this is using message_filters Not knowing your use case exactly I would venture a guess that the ApproximateTime Syncronizer is what you want.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-02-24 14:16:55 -0500

Seen: 2,129 times

Last updated: Mar 03 '13