ROS2 Launch File
In ROS1, we write a launch file in XML. In ROS2, there are three ways to write a launch file:
- Using Python
- Using XML
- Using YAML
Since the API of ROS2 launch is written in Python, you have a lower level access to the launch features if you write your launch file in Python. This may let you get the most of ROS2 launch capabilities. However, a Python launch file may look a little bit more complex than the XML and YAML ones. But it is actually not complex at all. It is very simple. Writing a launch file in Python also gives you more flexibility since Python is a scripting language so that you can do more by scripting.
An example of ROS2 launch file in Python can be seen here: https://docs.ros.org/en/foxy/Tutorials/Launch-Files/Creating-Launch-Files.html. This launch file is to run two turtlesim robots; the motion of one turtlesim is to mimic the motion of another turtlesim. This behavior is governed by a node called “mimic”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
from launch import LaunchDescription from launch_ros.actions import Node def generate_launch_description(): return LaunchDescription([ Node( package='turtlesim', namespace='turtlesim1', executable='turtlesim_node', name='sim' ), Node( package='turtlesim', namespace='turtlesim2', executable='turtlesim_node', name='sim' ), Node( package='turtlesim', executable='mimic', name='mimic', remappings=[ ('/input/pose', '/turtlesim1/turtle1/pose'), ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'), ] ) ]) |
There are three nodes executed in the launch file above. The first and second nodes are actually the same node but using different namespaces. The third node is the “mimic” node.
A comparison between launch files written in Python, XML, and YAML to do the same thing can be found here: https://docs.ros.org/en/foxy/Guides/Launch-file-different-formats.html
Launching a launch file
A launch file can be run in two ways:
1) running the launch file directly by specifying the path to the launch file:
1 |
ros2 launch <path_to_launch_file> |
2) wrapping the launch file in a package and running it using the same command like in ROS1:
1 |
ros2 launch [PACKAGE_NAME] [LAUNCH_FILE] |
If the launch file is written in Python, make sure that the Python file is executable. If not, you can make executable by using “chmod +x” command.