A better way to launch the ROS Nodes!
This is an article about launch files and different ways of launching multiple instances of a node in ROS. The content of this article is as follows:
- Introduction to launch files
- Method-1: Using XML to launch multiple instances of the same node
- Method-2: Using Python script to launch multiple instances of the same node
If you have worked with ROS on a large project, I am sure that you must have used launch files. ROS1 uses
xml launch files while ROS2 uses Python scripts to launch the nodes.
Generally, launch files can be used to:
- Launch nodes
- Launch another launch files
- Pass arguments to the nodes
- Initialize and assign values to ROS parameters
Sometimes, while working on a project you might need to launch multiple instances of the same node with different names, publishing unique data on a different topic and broadcast a transform from each node. The following image shows 5 instances of a node, each with a different name, broadcasting different transforms.
A better analogy would be to consider a node as a class and the created instances of a node as objects of this class. This can be done using 2 ways —
1. Using XML launch files
2. Using python launch file
In both the approaches, the code for the blueprint node (or the class node) will be different.
Method1: XML Files
The code below is a blueprint of a node running at 10Hz and can
- create a topic and publish data on the topic
- broadcast the transform
In order to create 5 copies of such nodes with different names and different topic names, we can create a launch file using a manual method or the dynamic method
The code below describes the manual method. In this method, a different node name is assigned to each node. The package name and executable type name are same for all the nodes. This will create 5 instances of the node
We can always add or remove the number of nodes based on our requirement. But it is always better to have a dynamic option to create multiple instances of the node. This can be done by the following method
Run the launch file, using the following command
$ roslaunch pylauncher manual.launch
Instead of adding or removing node definitions in the launch file, we can create a launch multiple instances of the same node using recursion. Following is the code for dynamic launch file
In this launch file, a variable named
num is created with a default value of
<arg name="num" default="1"/>
A namespace is created to initialize nodes with sequential name s— turtle1, turtle2, and so on. In order to add the number to the node name, first the
evalfunction will read the value from the command line and then a string value of this number will be added to the node name.
<group ns="$(eval 'node' + str(arg('num')))">
<node name="turtle" pkg="pylauncher" type="blueprint_node_launch.py" output="screen"/>
To create multiple nodes, the same launch file is opened recursively by reducing the value of
num variable by 1.
<include file="$(find pylauncher)/launch/dynamic.launch" if="$(eval arg('num') - 1 > 0)">
<arg name="num" value="$(eval arg('num')-1)"/>
Summary of the logic for 5 instances of the same node
- Read argument value from the command line
num = 5
- Launch node with a name
- Check if num-1 is greater than 0: if yes, num=4 and repeat
The command to launch this launch file
$ roslaunch pylauncher dynamic.launch num:=5
- Even though launching multiple instances dynamically is a great way, it has some limitations
- It’s difficult to create topics with different names
- Reading the argument value from launch file, in a node is not straight forward
- Even after creating a ROS parameter, it is not easy to get this parameter value in an instance of a node.
Along with the above limitations, the sequence of launching instances cannot be controlled. To overcome all these limitations, ROS2 uses Python launch files. ROS1 can also use python scripts as launch files using ROSLaunch API
Method 2: ROSLaunch API
The code below is a blueprint for the node for multiple instances
And here is a code to launch multiple nodes using python script
Command to launch 5 instances of nodes
$ python pylaunch.py 5
The node graph