Launch file
Creating a launch file
The launch system in ROS 2 is responsible for helping the user describe the configuration of their system and then execute it as described. The configuration of the system includes what programs to run, where to run them, what arguments to pass them, and ROS-specific conventions which make it easy to reuse components throughout the system by giving them each a different configuration. It is also responsible for monitoring the state of the processes launched, and reporting and/or reacting to changes in the state of those processes.
Launch files written in Python, XML, or YAML can start and stop different nodes as well as trigger and act on various events. See Using Python, XML, and YAML for ROS 2 Launch Files for a description of the different formats. The package providing this framework is launch_ros
, which uses the non-ROS-specific launch
framework underneath.
Setup
Create a new directory to store your launch files:
Write the launch file
Examine the launch file
All of the launch files above are launching a system of three nodes, all from the turtlesim
package. The goal of the system is to launch two turtlesim windows, and have one turtle mimic the movements of the other.
When launching the two turtlesim nodes, the only difference between them is their namespace values. Unique namespaces allow the system to start two nodes without node name or topic name conflicts. Both turtles in this system receive commands over the same topic and publish their pose over the same topic. With unique namespaces, messages meant for different turtles can be distinguished.
The final node is also from the turtlesim
package, but a different executable: mimic
. This node has added configuration details in the form of remappings. mimic
’s /input/pose
topic is remapped to /turtlesim1/turtle1/pose
and it’s /output/cmd_vel
topic to /turtlesim2/turtle1/cmd_vel
. This means mimic
will subscribe to /turtlesim1/sim
’s pose topic and republish it for /turtlesim2/sim
’s velocity command topic to subscribe to. In other words, turtlesim2
will mimic turtlesim1
’s movements.
ros2 launch
To run the launch file created above, enter into the directory you created earlier and run the following command:
Integrating launch files into ROS 2 packages
Create a package
Creating the structure to hold launch files
By convention, all launch files for a package are stored in the launch
directory inside of the package. Make sure to create a launch
directory at the top-level of the package you created above.
For Python packages, the directory containing your package should look like this:
To enable colcon to locate and utilize our launch files, we need to inform Python’s setup tools of their presence. To achieve this, open the setup.py
file, add the necessary import
statements at the top, and include the launch files into the data_files
parameter of setup
Writing the launch file
Inside your launch
directory, create a new launch file called my_script_launch.py
. _launch.py
is recommended, but not required, as the file suffix for Python launch files. However, the launch file name needs to end with launch.py
to be recognized and autocompleted by ros2 launch
.
Your launch file should define the generate_launch_description()
function which returns a launch.LaunchDescription()
to be used by the ros2 launch
verb.
Building and running the launch file
After the colcon build
has been successful and you’ve sourced the workspace, you should be able to run the launch file as follows:
Using substitutions
Create and setup the package
Create a new package of build_type ament_python
called launch_tutorial
:
Inside of that package, create a directory called launch
:
Finally, make sure to add in changes to the setup.py
of the package so that the launch files will be installed:
Parent launch file
Let’s create a launch file that will call and pass arguments to another launch file. To do this, create an example_main.launch.py
file in the launch
folder of the launch_tutorial
package.
In the example_main.launch.py
file, the FindPackageShare
substitution is used to find the path to the launch_tutorial
package. The PathJoinSubstitution
substitution is then used to join the path to that package path with the example_substitutions.launch.py
file name.
The launch_arguments
dictionary with turtlesim_ns
and use_provided_red
arguments is passed to the IncludeLaunchDescription
action. The TextSubstitution
substitution is used to define the new_background_r
argument with the value of the background_r
key in the colors
dictionary.
Substitutions example launch file
Now create an example_substitutions.launch.py
file in the same folder.
n the example_substitutions.launch.py
file, turtlesim_ns
, use_provided_red
, and new_background_r
launch configurations are defined. They are used to store values of launch arguments in the above variables and to pass them to required actions. These LaunchConfiguration
substitutions allow us to acquire the value of the launch argument in any part of the launch description.
DeclareLaunchArgument
is used to define the launch argument that can be passed from the above launch file or from the console.
The turtlesim_node
node with the namespace
set to turtlesim_ns
LaunchConfiguration
substitution is defined.
Afterwards, the ExecuteProcess
action called spawn_turtle
is defined with the corresponding cmd
argument. This command makes a call to the spawn service of the turtlesim node.
Additionally, the LaunchConfiguration
substitution is used to get the value of the turtlesim_ns
launch argument to construct a command string.
The same approach is used for the change_background_r
and change_background_r_conditioned
actions that change the turtlesim background’s red color parameter. The difference is that the change_background_r_conditioned
action is only executed if the provided new_background_r
argument equals 200
and the use_provided_red
launch argument is set to True
. The evaluation inside the IfCondition
is done using the PythonExpression
substitution.
Build the package
Launching example
Now you can launch the example_main.launch.py
file using the ros2 launch
command.
This will do the following:
Start a turtlesim node with a blue background
Spawn the second turtle
Change the color to purple
Change the color to pink after two seconds if the provided
background_r
argument is200
anduse_provided_red
argument isTrue
Modifying launch argument
Modifying launch argument
If you want to change the provided launch arguments, you can either update them in launch_arguments
dictionary in the example_main.launch.py
or launch the example_substitutions.launch.py
with preferred arguments. To see arguments that may be given to the launch file, run the following command:
This will show the arguments that may be given to the launch file and their default values.
Now you can pass the desired arguments to the launch file as follows:
Last updated