0
$\begingroup$

Rosanswers logo

Is there a way to get a node's current namespace?

Say I launch a node like this

<launch>
<group ns="robot_1">
<node pkg="myrobot" type="robot" name="robot"/>
</group>
</launch>

then this node is really called /robot_1/robot. Now what if it should publish something on a topic whose name depends on the namespace?

So in this case the topic may be /robot_1/control.

Is there a way I can get my code to dynamically find this namespace?


Originally posted by Claudio on ROS Answers with karma: 859 on 2013-05-13

Post score: 6


Original comments

Comment by SL Remy on 2013-05-13:
Do you mean that you'd prefer to select that the node publishes in /robot_1/robot instead of /robot?

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

If something needs to subscribe to the control topic, if it is in the same namespace as the robot node then subscribing to control will automatically find the /robot_1/control topic. Subscribing to /control will not automatically find the one you want. More can be read here.

Also note that if for some reason you need to get the namespace of a particular node from within that node you can do this in both roscpp and rospy using

std::string ns = ros::this_node::getNamespace();

and

ns = rospy.get_namespace()

Originally posted by jarvisschultz with karma: 9031 on 2013-05-13

This answer was ACCEPTED on the original site

Post score: 20


Original comments

Comment by Claudio on 2013-05-13:
Thanks Jarvis, that was exactly what I needed.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.