Ask Your Question

Programmatically get node's Namespace

asked 2013-05-13 06:48:25 -0600

Claudio gravatar image

updated 2017-07-11 08:40:00 -0600

lucasw gravatar image

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

Say I launch a node like this

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

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?

edit retag flag offensive close merge delete


Do you mean that you'd prefer to select that the node publishes in /robot_1/robot instead of /robot?

SL Remy gravatar image SL Remy  ( 2013-05-13 06:54:31 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-05-13 10:54:41 -0600

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 rospy and roscpp using

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


ns = rospy.get_namespace()
edit flag offensive delete link more


Thanks Jarvis, that was exactly what I needed.

Claudio gravatar image Claudio  ( 2013-05-13 20:53:43 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-05-13 06:48:25 -0600

Seen: 7,169 times

Last updated: Jul 11 '17