Subscribe and publish to topic
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/cmdvel" and another node B subscribe to the same topic knowing that in A it gives as topic name "turtle1/cmdvel" and in B just "cmd/vel"?
Asked by pg.lopezamaya on 2015-10-16 11:23:11 UTC
Answers
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 inroslaunch
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 toturtle1/cmd_vel
. This could be done using thens
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.
Asked by jarvisschultz on 2015-10-16 17:01:21 UTC
Comments