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

Subscribing to multiple topics of the same type.

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

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
2

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

superawesomepandacat gravatar image

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

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

Comments

1
2

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

updated 2013-03-03 23:50:03 -0600

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

Comments

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

superawesomepandacat gravatar image superawesomepandacat  ( 2013-02-25 00:13:37 -0600 )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 image ayush_dewan  ( 2013-02-25 03:02:31 -0600 )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 image superawesomepandacat  ( 2013-02-25 11:17:17 -0600 )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 image ayush_dewan  ( 2013-02-25 22:30:48 -0600 )edit

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

superawesomepandacat gravatar image superawesomepandacat  ( 2013-02-25 23:09:51 -0600 )edit

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

tfoote gravatar image tfoote  ( 2013-03-02 21:10:24 -0600 )edit
0

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

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

Question Tools

Stats

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

Seen: 2,935 times

Last updated: Mar 03 '13