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

Subscribing to Multiple Nodes Publishing the Same Topic

asked 2014-07-21 16:03:51 -0500

jc2016 gravatar image

updated 2014-07-22 18:43:34 -0500

demmeln gravatar image

Hi all,

I am having an issue that I'm not sure has been addressed. I am writing a node where I am attempting to subscribe to another two nodes that publish to the same topic (i.e. two lasers that are publishing to a topic with the same name). Going back and changing the topic names would be a little bit tricky, so I was wondering if there was a way to differentiate these two (i.e. write a subscriber that takes into account topic name and node-of-origin name when being instantiated.

Thanks

Edit: I'm confused how the syntax works for remapping and was wondering if I could ask for more help:

Originally, I have as a launch file:

<launch>

    <node pkg="leg_detector" type="leg_detector" name="leg_detector_front" args="scan:=scan_front $(find leg_detector)/config/trained_leg_detector.yaml" output="screen"/>

    <node pkg="leg_detector" type="leg_detector" name="leg_detector_rear" args="scan:=scan_rear $(find leg_detector)/config/trained_leg_detector.yaml" output="screen"/>

</launch>

I tried this:

 <launch>

    <node pkg="leg_detector" type="leg_detector" name="leg_detector_front" args="scan:=scan_front $(find leg_detector)/config/trained_leg_detector.yaml" output="screen"/>

    <group>
      <remap from="leg_tracker_measurements" to="leg_tracker_measurements_rear">
      <node pkg="leg_detector" type="leg_detector" name="leg_detector_rear" args="scan:=scan_rear $(find leg_detector)/config/trained_leg_detector.yaml" output="screen"/>
    </group>
  </launch>

But it threw an invalid syntax error

edit retag flag offensive close merge delete

Comments

'Close' your remap tag. <remap from="leg_tracker_measurements" to="leg_tracker_measurements_rear"/> or <remap from="leg_tracker_measurements" to="leg_tracker_measurements_rear"> </remap>

McMurdo gravatar image McMurdo  ( 2014-07-23 00:41:22 -0500 )edit

3 Answers

Sort by » oldest newest most voted
7

answered 2014-07-21 16:10:15 -0500

demmeln gravatar image

updated 2014-07-22 18:57:17 -0500

I do not know of a way to distinguish by which node sent a message in the callback on the subscriber side.

I suggest you make your two publishing nodes publish to two different topics and your subscriber then subscribe to both indivially. Remapping topics is your friend here.

Edit:

In response to your syntax problem: Have you read the actual error message? It probably indicates that you forgot to close the <remap> tag. You can put it into a group, but I believe it is clearer to put it directly inside the node tag. Also inside a rules file I would not use the remapping syntax with command line arguments like you do for scan. This is just confusing. Try this:

<launch>
  <node pkg="leg_detector" type="leg_detector" name="leg_detector_front" args="$(find leg_detector)/config/trained_leg_detector.yaml" output="screen">
    <remap from="scan" to="scan_front" />
    <remap from="leg_tracker_measurements" to="leg_tracker_measurements_front" />
  </node>

  <node pkg="leg_detector" type="leg_detector" name="leg_detector_rear" args="$(find leg_detector)/config/trained_leg_detector.yaml" output="screen">
    <remap from="scan" to="scan_rear" />
    <remap from="leg_tracker_measurements" to="leg_tracker_measurements_rear" />
  </node>
</launch>

Or alternatively and maybe even better in this case with namespaces, like suggested in another answer below:

<launch>
  <node pkg="leg_detector" type="leg_detector" name="leg_detector_front" args="$(find leg_detector)/config/trained_leg_detector.yaml" output="screen" ns="front" />
  <node pkg="leg_detector" type="leg_detector" name="leg_detector_rear" args="$(find leg_detector)/config/trained_leg_detector.yaml" output="screen" ns="rear" />
</launch>
edit flag offensive delete link more

Comments

That looks like an awesome feature, thanks so much for the quick reply. So, the way I have it set up I instantiate two instances of the node in the launch file with different names. It seems like I should be able to use the remapping, but does that sound right. Thanks again.

jc2016 gravatar image jc2016  ( 2014-07-21 16:50:28 -0500 )edit

I don't have time to check it out yet, but this seems to work, and I'll make it the answer once I figure out if it does. Thanks

jc2016 gravatar image jc2016  ( 2014-07-21 16:51:03 -0500 )edit

Sounds right. Use the <remap> tag in your launch file for one (or if you like both) of the nodes. Feel free to update your question with more concrete info if you get stuck.

demmeln gravatar image demmeln  ( 2014-07-21 16:56:23 -0500 )edit
2

Although @McMurdo's answer is the precise reply to the question asked, it is important to note that this answer is the right way to do it!

dornhege gravatar image dornhege  ( 2014-07-22 05:54:39 -0500 )edit

Alright that worked out well, thanks so much for all of your help!

jc2016 gravatar image jc2016  ( 2014-07-23 09:50:39 -0500 )edit
2

answered 2014-07-22 01:54:03 -0500

BennyRe gravatar image

Just for completeness: You could also launch your two nodes in different name spaces.

edit flag offensive delete link more

Comments

So I had already renamed both nodes from which the topics were being published from, if that what you man by name spaces. But I believe I still run into the same issue that the nodes both publish to the same topic.

jc2016 gravatar image jc2016  ( 2014-07-22 15:46:56 -0500 )edit

No, the name of the nodes is not what is referred to here. The node tag can have a `ns` attribute (http://wiki.ros.org/roslaunch/XML/node). This causes all topics for this node to have prefix. E.g. if your node publishes to the `data` topic. If you add `ns="foo"` it will then publish to `foo/data`.

demmeln gravatar image demmeln  ( 2014-07-22 18:47:52 -0500 )edit

Would you mind looking at the code provided in the edit to the main question? I'm not sure where I'm going wrong there

jc2016 gravatar image jc2016  ( 2014-07-22 18:51:07 -0500 )edit
4

answered 2014-07-22 04:18:34 -0500

McMurdo gravatar image

updated 2014-07-22 06:12:59 -0500

The real answer is there in the connection header:

http://wiki.ros.org/rospy/Overview/Pu... see the last few lines.

A subscriber can get access to a "connection header", which includes debugging information such as who sent the message, as well information like whether or not a message was latched. This data is stored as the _connection_header field of a received message.

e.g.

print m._connection_header
{'callerid': '/talker_38321_1284999593611',
 'latching': '0',
 'md5sum': '992ce8a1687cec8c8bd883ec73ca41d1',
 'message_definition': 'string data\n\n',
 'topic': '/chatter',
 'type': 'std_msgs/String'}

EDIT: THIS METHOD IS NOT RECOMMENDED. IT IS ONLY FOR DEBUGGING PURPOSES.

edit flag offensive delete link more

Comments

Well, I would not rely on that tbh. The section you link says directly after your quote: "We do not recommend using callerid information beyond debugging purposes, as it can lead to brittle code in an anonymous publish/subscribe architecture." And I don't think this is available with C++?

demmeln gravatar image demmeln  ( 2014-07-22 05:55:55 -0500 )edit
1

Yes. The answer is merely precise, not recommended. see @dornhege's comment below.I should edit the answer to point out that it isn't recommended.

McMurdo gravatar image McMurdo  ( 2014-07-22 06:11:54 -0500 )edit
2

In C++ there is a callback signature that subscribes to MessageEvents that hold the publisher's name, see: http://wiki.ros.org/roscpp/Overview/Publishers%20and%20Subscribers#MessageEvent_.5BROS_1.1.2B-.5D

dornhege gravatar image dornhege  ( 2014-07-22 06:18:58 -0500 )edit

Ah, I wasn't aware of the MessageEvent form for callbacks in roscpp. Thanks for pointing that out.

demmeln gravatar image demmeln  ( 2014-07-22 06:38:26 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-07-21 16:03:51 -0500

Seen: 7,618 times

Last updated: Jul 22 '14