CodeTheRobot

C++ Advanced · Lesson 16 of 23

Launch files

Starting several nodes with parameters and remappings from one Python launch file.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain launch files 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: nodes, parameters, remappings and other launch files. ROS 2 launch files are usually written in Python (a function that returns a LaunchDescription), though XML and YAML also work.

Install the launch folder from CMake with install(DIRECTORY launch DESTINATION share/${PROJECT_NAME}), rebuild, and start it with ros2 launch package_name file.launch.py. Use launch arguments for values that change between runs.

Guided lesson

Work through these steps

  1. 1.

    Read the launch files example and identify what each line owns or changes.

  2. 2.

    Create a small source file, compile with -std=c++17 and warnings enabled, and run it.

  3. 3.

    Change one input, predict the result before running, then compare the output.

  4. 4.

    Record one compiler or runtime error and explain the correction in your own words.

Worked example

Read it before you run it

from launch import LaunchDescription
# Import the class that holds the list of things to start.
from launch_ros.actions import Node
# Import the action that starts one ROS 2 node.
def generate_launch_description():
# The function ros2 launch looks for.
    talker = Node(package='robot_nodes', executable='talker', name='talker', parameters=[{'rate_hz': 5.0}])
# Start the talker with one parameter set.
    listener = Node(package='robot_nodes', executable='listener', remappings=[('chatter', 'robot/chatter')])
# Start the listener and rename its topic.
    return LaunchDescription([talker, listener])
# Hand ROS the list of nodes to start.

What the example is doing

  1. 1.Read declarations from right to left: identify each value's type, name, and initial value.
  2. 2.Follow the data through each expression before considering syntax shortcuts.
  3. 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.

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.

/topicpublishersubscribersubscribermessages delivered: 0MESSAGES BETWEEN NODES

Demo for Launch files. Expected result: ros2 launch robot_nodes bringup.launch.py -> talker and listener both start

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 the talker with a rate_hz parameter and the listener with chatter remapped to robot/chatter
    return LaunchDescription([talker, listener])

Expected output

ros2 launch robot_nodes bringup.launch.py  ->  talker and listener both start
Stuck? 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():
    talker = Node(package='robot_nodes', executable='talker', name='talker',
                  parameters=[{'rate_hz': 5.0}], remappings=[('chatter', 'robot/chatter')])
    listener = Node(package='robot_nodes', executable='listener',
                    remappings=[('chatter', 'robot/chatter')])
    return LaunchDescription([talker, listener])

Expected result

How you know it worked

  • After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
  • Changing the stated input produces the result you predicted.

Practical exercise

Apply the lesson

Write bringup.launch.py that starts a talker and a listener, passes a rate parameter to the talker and remaps the topic for both. Install it, launch it, and check the graph with ros2 node list and ros2 topic list.

Before you continue

  • The program builds with -std=c++17 and no warnings
  • You can explain the key rule without reading the example
  • The normal and edge cases both have recorded results

If something goes wrong

The compiler reports that a name or type is unknown.

Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.

The program runs but the value is wrong.

Print the inputs and intermediate values, then check conversions, units, and collection order.