ROS 2 Lyrical Luth · Lesson 12 of 86
Launch files (Python launch system)
Starting many nodes with parameters, remappings, arguments and includes.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain launch files (python launch system) in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
A launch file starts a whole system with one command and records how it was configured: nodes, parameters, remappings, environment variables and other launch files. ROS 2 launch files are Python functions that return a LaunchDescription (XML and YAML are also supported).
Use DeclareLaunchArgument and LaunchConfiguration for values that change between runs (such as use_sim_time). Install launch folders from CMake (install(DIRECTORY launch DESTINATION share/robot_nodes)) or via data_files for Python packages, then run ros2 launch package file.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the launch files (python launch system) example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 4.
Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.
Worked example
Read it before you run it
from launch import LaunchDescription
# The container for everything to start.
from launch.actions import DeclareLaunchArgument
# Lets the user pass arguments on the command line.
from launch.substitutions import LaunchConfiguration
# Reads an argument's value.
from launch_ros.actions import Node
# Starts one ROS 2 node.
def generate_launch_description():
# The function ros2 launch looks for.
sim = DeclareLaunchArgument('use_sim_time', default_value='false')
# Declare an argument with a default.
talker = Node(package='demo_nodes_cpp', executable='talker', parameters=[{'use_sim_time': LaunchConfiguration('use_sim_time')}])
# Start the talker and pass the argument as a parameter.
return LaunchDescription([sim, talker])
# Hand ROS the list of things to start.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
Watch it run · Messages between nodes
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Launch files (Python launch system). Expected result: Both nodes start and the listener prints messages from robot/chatter
Practice code
Now type it yourself
Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.
# launch/bringup.launch.py
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
# TODO: start demo_nodes_cpp talker and listener, both remapping chatter to robot/chatter
return LaunchDescription([talker, listener])Expected output
Both nodes start and the listener prints messages from robot/chatterStuck? Show the full solution (try it yourself first)
# launch/bringup.launch.py
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
remap = [('chatter', 'robot/chatter')]
talker = Node(package='demo_nodes_cpp', executable='talker', remappings=remap)
listener = Node(package='demo_nodes_cpp', executable='listener', remappings=remap)
return LaunchDescription([talker, listener])Expected result
How you know it worked
- ROS_DISTRO is lyrical in every terminal used for the exercise.
- The complete package from the exercise builds and the expected graph entity appears in a second terminal.
Practical exercise
Apply the lesson
Write bringup.launch.py that starts a talker and a listener, remaps the topic for both and exposes a launch argument. Install it, run it with ros2 launch, and override the argument on the command line.
Before you continue
- ROS_DISTRO reports lyrical in every terminal you used
- The example builds and runs without errors
- You checked the result with a ros2 command, RViz2 or a test, not just by reading the code
If something goes wrong
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.