Several subscriptions pointing to one callback
My problem is simple: I have several topics of the same message type. I want to subscribe to all of them and have them all point to the same callback method. Since I want to have access to the topic name in the callback, I am using boost::bind
:
topicSubs.push_back(nh_.subscribe<nav_msgs::Odometry>(odomTopic, 1, boost::bind(&MyClass::odometryCallback, this, _1, odomTopicName)));
I loop through the list of topic names in my launch file and make the above call for each. topicSubs
is a member variable vector, so the subscriber object isn't going out of scope. If I subscribe to only one topic, the callback is successfully called for that topic. If I subscribe to two, I only get the second. In other words, it only actually subscribes to the last topic I call subscribe
on. Does ROS internally do some kind of one-to-one mapping between topic types and callbacks, or is there clearly some kind of error in how I'm handling the subscription?
I already verified that the topic names are correct for each subscription. I looked for similar issues, but didn't find anything that answered this. I apologize in advance if this was answered before.
EDIT: Responding to a comment. My launch file looks like this:
<param name="odom0" value="odomTopicA"/>
<param name="odom1" value="odomTopicB"/>
The user can specify an arbitrary number of topics in the form of "odomX". The code to load them just creates string streams, appends the value (0, 1, etc.) and then grabs that parameter from the launch file. I've verified that the correct names are being loaded, and clearly subscribing works, but only for the last subscriber that gets pushed back.
Nope, I use one cb for multiple subscribers in quite a few of my nodes, so it's definitely possible. Could you explain what you mean by "loop through the list of topic names in my launch file"?
See edit. If it works for you, then it must be something else I'm doing. Do you know if the use of boost::bind is a problem, or are you also using it?