Search code examples
ros

ROS : Subscribe to topic in different namespace


I just started to use ROS tonight.

What I'm trying to achieve is this:

enter image description here

This is what I have already done :

  • publish and subscribe to topic_b.

  • publish to ns1/topic_a (I checked with rostopic echo /ns1/topic_a)

  • publish to ns2/topic_a (I checked with rostopic echo /ns2/topic_a)

  • subscribe to ns2/topic_a (ros::Subscriber sub = n.subscribe("topic_a", 1000, callback);)

What I don't know how to do :

  • subscribe to ns1/topic_a from node_b.

After reading the tutorial, I tried this :

1/ In node_b.cpp, I subscribed to /topic_a_temp.

2/ In the launch file, inside the node_b tags, I added <remap from="topic_a_temp" to="ns1/topic_a">

But it doesn't work.

It may be very basic since I'm a beginner but I don't know how to read the ns1 topic from the ns2 node.

Thank you for your help,


Solution

  • Add a slash to your topic remap instruction: <remap from="topic_a_temp" to="/ns1/topic_a">

    ROS will then look up the name globally as it's written, not relative to the namespace the node is launching in. For more, see how names are resolved in ROS.

    rosnode info or other introspection tools to check which topics nodes are actually subscribing can be helpful for resolving such problems