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

Subscribe and publish to topic

asked 2015-10-16 11:23:11 -0500

pg.lopezamaya gravatar image

HI I just read the explanation about the topics. Looking at the codes in c++ I manage to understand how to subscribe to a topic and how to publish into a topic. My question is, how can a node A publish into a topic "/tutle1/cmd_vel" and another node B subscribe to the same topic knowing that in A it gives as topic name "turtle1/cmd_vel" and in B just "cmd/vel"?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-10-16 17:01:21 -0500

Let's say node A has created it's publisher using something like

ros::Publisher pub = node.advertise<std_msgs::Twist>("turtle1/cmd_vel", 100);

and node B has subscribed using something like

ros::Subscriber sub = node.subscribe("cmd_vel", 100, datacb);

I believe that is the situation you are describing. Then the question is how to get the subscriber in B to successfully subscribe to the topic published by A. This is fundamentally a question of how ROS Names are resolved. Below are a few possible solutions. The correct solution depends on your specific application:

  • Remapping You could use "ROS Remapping" to either remap the publisher or the subscriber to be the correct topic. This can be done with rosrun via Remapping Arguments, or in roslaunch via the remap tag.
  • Namespaces You could modify the namespace that node B is in so that it runs in the turtle1/ namespace. Then its subscriber would automatically resolve to turtle1/cmd_vel. This could be done using the ns attribute of either the node tag or the group tag. You could also modify the namespace of a node at the command line with the ROS_NAMESPACE environment variable.
edit flag offensive delete link more

Question Tools


Asked: 2015-10-16 11:23:11 -0500

Seen: 2,068 times

Last updated: Oct 16 '15