How To Use a ROS Anonymous Node

First, to understand when you should use a ROS anonymous node, let’s start with the problem.

So, you’ve just created a node for your infra-red sensor, it works well, and you’re happy with it.

But now, your robot has 12 infra-red sensors. Knowing that a node should have a unique name, and that you can’t run 2 nodes with the same name, what can you do?

You might think of some alternatives:

Instead of having a node dealing with one sensor, you could have a node dealing with an array of sensors. So, your node would monitor all infra-red sensors, and publish the data for each – separately or in a common array. This could be a viable solution, depending on what you’re trying to achieve.

Another solution is to duplicate your infra-red node for each sensor you have, with each node having a unique name. It can work great if you know exactly how many sensors you’ll have, and this number is small and won’t grow. For example, if you have 2 cameras on your robot, this might be a good idea. But if you’ve ever been tempted of going this way for 12 nodes, please read the following. This will save you a good amount of time and headaches.


You are learning ROS?

Check out ROS For Beginners and learn ROS step by step.


If at this point you want to freshen up your memory about ROS node, check out this guide first, which is a part of the What is ROS? series.

What is a ROS anonymous node?

A ROS node should have a unique name. If you run a node in a terminal, and try to run another node with the same name on another terminal, then the first node will be killed so the second one can start. All in all, you’ll only be able to run one node with the same name at the same time.

Anonymous nodes allow you to get rid of this rule. When you create an anonymous node, a random unique ID will be added to the name of the node. So, now you can run as many nodes as you want. Each of your 12 infra-red sensors can have its own node, and you don’t need to create an ID yourself.

Let’s now see how to actually use those anonymous nodes in your code. Before that, let’s quickly create a Python and Cpp node.

Simple Python and Cpp node with unique name

Standard Python node

Here is the most basic thing you can do to create a Python node:

#!/usr/bin/env python
import rospy

if __name__ == '__main__':
    rospy.init_node("my_infra_red_sensor")
    rospy.spin()

Here we just initialize the node and give it a name “my_infra_red_sensor”. We also use rospy.spin() so we can keep the program alive until we manually kill the node.

Standard Cpp node

#include <ros/ros.h>

int main (int argc, char **argv)
{
    ros::init(argc, argv, "my_infra_red_sensor");
    ros::NodeHandle nh;
    ros::spin();
}

The behavior for the Cpp node is exactly the same as for the Python node just above.

Run and test the node

When starting this node (either the Python or Cpp one) with rosrun, you see a new node when running the rosnode list command line tool:

~$ rosnode list 
/my_infra_red_sensor
/rosout

Nothing new here. If you start this same node in another terminal, the previous one will be killed, and you’ll always have only one “my_infra_red_sensor” node running.

Now, let’s make the node anonymous.

Adapting your code for using ROS anonymous nodes

That’s pretty simple. In fact, there is only one line you need to change for each node. And this line is the line where you initialize the node and give a name to it.

Anonymous node in Python

#!/usr/bin/env python
import rospy

if __name__ == '__main__':
    rospy.init_node("my_infra_red_sensor", anonymous=True)
    rospy.spin()

As simple as that! You just have to add “anonymous=True” as a second parameter in the init_node() function.

Anonymous node in Cpp

#include <ros/ros.h>

int main (int argc, char **argv)
{
    ros::init(argc, argv, "my_infra_red_sensor", 
            ros::init_options::AnonymousName);
    ros::NodeHandle nh;
    ros::spin();
}

Here, we add “ros::init_options::AnonymousName” as a second parameter in the init() function. And… that’s it! Nothing else. You don’t have to modify the name of the file, or the name of the executable in the CMakeLists.txt.

So, what does it do?

Run and test the anonymous node

Let’s start the node again and monitor it with rosnode list:

~$ rosnode list 
/my_infra_red_sensor_6625_1550398083304
/rosout

The name of the node has changed. It still starts with “my_infra_red_sensor”, but now you can see that a unique identifier was added at the end.

Let’s open a few other terminals, and start the exact same node in all those new terminals.

~$ rosnode list 
/my_infra_red_sensor_6625_1550398083304
/my_infra_red_sensor_7343_1550398378088
/my_infra_red_sensor_7698_1550398382283
/my_infra_red_sensor_8052_1550398385927
/rosout

Tadaa! Here I have launched the same node 4 times, and you can see, there are 4 nodes running at the same time. Each has a unique ID. Problem solved!

You can now start as many infra-red nodes as you want, one for each of your sensor.

Start a ROS anonymous node with roslaunch

If you’re starting your application using a launch file, you’ll also have to modify the name of the node inside your “node” tags.

This:

<launch>
    <node name="my_infra_red_sensor" pkg="your_package" type="infra_red_sensor.py" />
</launch>

Becomes:

<launch>
    <node name="$(anon my_infra_red_sensor)" pkg="your_package" type="infra_red_sensor.py" />
</launch>

You have to change the name attribute of the node tag, using $(anon node_name).

Now, you can also easily start anonymous nodes with launch files.

Did you find this tutorial useful?

Do you want to learn how to program with ROS?

If yes, this course is for you:

ROS For Beginners - A Step By Step Course

>> ROS For Beginners - A Step By Step Course <<