0
$\begingroup$

Rosanswers logo

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.


Originally posted by superawesomepandacat on ROS Answers with karma: 172 on 2013-02-24

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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.seekp(0);
        ss << "/sicklms" << i << "/scan";
        sicklms_subs[i] = nh.subscribe(ss.str(), 100, &mapper_direct::laserCallback, this);
    }

Originally posted by ayush_dewan with karma: 1610 on 2013-02-24

This answer was NOT ACCEPTED on the original site

Post score: 2


Original comments

Comment by superawesomepandacat on 2013-02-25:
That works. But is it possible to accept multiple different topics of the same type in a single callback function?

Comment by ayush_dewan on 2013-02-25:
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..

Comment by superawesomepandacat on 2013-02-25:
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.

Comment by ayush_dewan on 2013-02-25:
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.

Comment by superawesomepandacat on 2013-02-26:
Yeah I'm trying to figure that out. Thanks anyways :)

Comment by tfoote on 2013-03-02:
Please edit your question to reflect what you want to know.

$\endgroup$
0
$\begingroup$

Rosanswers logo

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) {
}

Originally posted by superawesomepandacat with karma: 172 on 2013-03-01

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by SL Remy on 2013-03-01:
http://www.ros.org/wiki/message_filters#Time_Synchronizer

$\endgroup$

Your Answer

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