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

get all the active namespaces rospy | roscpp

asked 2019-12-17 10:34:50 -0500

davidem gravatar image

updated 2019-12-18 07:59:32 -0500

In my project, multiple robots may be spawned both at launch and at runtime, each under a certain namespace. Generally, that is 'robot_ID' where ID == a robot-unique integer, but nothing stops the user from selecting its own namespace.

I need to programmaticaly retrieve a list of namespaces currently active from a node that is in the global namespace. That must be like that.
When I launch my project, on std rosout this gets printed:

  /robot_2/               # <- NAMESPACE
    amcl (amcl/amcl)
    move_base (move_base/move_base)
    robot_state_publisher (robot_state_publisher/robot_state_publisher)
    spawn_urdf (gazebo_ros/spawn_model)
  /robot_1/              # <- NAMESPACE
    amcl (amcl/amcl)
    move_base (move_base/move_base)
    robot_state_publisher (robot_state_publisher/robot_state_publisher)
    spawn_urdf (gazebo_ros/spawn_model)
  /                     # <- STANDARD NAMESPACE
    gazebo (gazebo_ros/gzserver)
    map_loader (map_server/map_server)
    rviz (rviz/rviz)

So this leads me into thinking that there is a way to do so. I'm mostly using Python, but for this purpose only I would also be willing to use a C++ node.

EDIT: I've marked what I would like to retrieve from the above piece of output.

edit retag flag offensive close merge delete


Can you provide more details about what you want to achieve please ? If you just want to list the namespaces without processing the list then a simple bash/python script parsing the ouput of rosnode list would work.

Delb gravatar image Delb  ( 2019-12-18 04:47:52 -0500 )edit

I thought it was clear, I'm sorry. I would like to retrieve a "list" of all the namespaces, being all the "prefixes" being currently applied to nodes. In the rosout, at launch, that "resume" gets printed: I want to know if there is a direct/indirect way to get all the shown namespaces. Parsing the output of rosnode list is an option, but that requires some work, meaning that you have to do string comparison to find recurring pieces of string in all the nodes. Not much of a deal, but I was asking if there was a better way

davidem gravatar image davidem  ( 2019-12-18 07:57:42 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2019-12-20 08:45:58 -0500

davidem gravatar image

updated 2019-12-21 04:58:51 -0500

After an analysis of the various solutions, also thanks to @Delb and @gvdhoorn for some suggestions, here are what I think to be the best suited:

  • use a bash command from inside a script: as I said, I'm mainly using Python so that would be achieved via the subprocess module. The idea is to call rosnode list, parse the output and do some string comparison to find the recurring substrings at the start of each line. It needs an extra node dedicated.
  • using the XML-RPC protocol in ROS, throughout the use of appropriate libraries and APIs. The wiki says it's unnecessary to call the APIs directely, so I've passed this option.
  • setting a parameter in the launch file: this is what I ended up adopting in my project, though it required some workarounds. Every time a robot is launched, it sets its namespace to the ROS Parameter Server, so that every node that needs the list of the active namespaces can get the parameter value via rosparam get /namespaces. The .launch syntax is pretty straight-forward:

<rosparam ns="/namespaces" param="$(arg robot_name)" subst_value="true" > $(arg robot_name) </rosparam>

In my case, name and value were the same, but it is in fact arbitrary.

edit flag offensive delete link more


The wiki says it's unnecessary to call the APIs directely, so I'll passed this option.

For normal use, yes. You should typically try to use APIs supported by the client libraries (such as roscpp and rospy).

But I don't believe there is a regular API for this.

So use of the master and node APIs is justified here, and I believe the only way to really implement what you're asking for.

gvdhoorn gravatar image gvdhoorn  ( 2019-12-20 12:35:16 -0500 )edit

answered 2019-12-18 09:44:41 -0500

Delb gravatar image

I insist on the fact that a little script would be a nice option but I would suggest a workaround :

You could use the rospy API : There is the method get_namespace() that direclty returns the namespace of a node. You could create a simple node that get the namespace and then publish it once to a topic. Adding this simple node in your launch file for each group and then echoing the topic or using a subscriber you could retrieve all the namespaces.

edit flag offensive delete link more


I thought of this solution too, but it more or less resembles the other one you suggested for one thing: it needs an extra node just for this purpose (to be precise, this solution will generate as many extra nodes as robots, while the other just a single one). I sincerely hoped there was an API function that I was missing but apparently I have to work it around. I'll probably stick with the first solution.

davidem gravatar image davidem  ( 2019-12-18 10:03:03 -0500 )edit

If the connection to the topic is latched then you can end your node once it has published its message (I mean you don't need to add ros::spin() to keep the node alive).

Delb gravatar image Delb  ( 2019-12-18 10:10:26 -0500 )edit

I don't believe using additional nodes is needed.

@davidem: look into the Master and Node XML-RPC APIs.

gvdhoorn gravatar image gvdhoorn  ( 2019-12-18 13:46:48 -0500 )edit

Question Tools



Asked: 2019-12-17 10:34:50 -0500

Seen: 970 times

Last updated: Dec 21 '19