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

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

<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?

edit retag flag offensive close merge delete

Comments

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
20

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

updated 2021-06-11 08:19:06 -0600

130s gravatar image

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()
edit flag offensive delete link more

Comments

Thanks Jarvis, that was exactly what I needed.

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

Question Tools

1 follower

Stats

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

Seen: 11,460 times

Last updated: Jun 11 '21