19. ROS2 Launch startup file configuration

 

1. Introduction to Launch

Until now, every time we launched a ROS node, we had to open a new terminal and run a command. With so many nodes in a robotic system, doing this every time is cumbersome. Is there a way to launch all nodes at once? The answer is, of course, a launch file, a script that launches and configures multiple nodes in the ROS system.

In ROS2, launch is used to launch multiple nodes and configure program parameters. ROS2 launch files are available in XML, YAML, and Python formats. This lesson uses a Python launch file as an example. Compared to the other two formats, the Python format is more flexible:

The key to writing ROS2 launch files in Python is to abstract each node, file, script, etc. into an action, launching them using a unified interface.

References:

2. Writing a Single Node Launch Program

2.1. Creating a Launch File

Create a launch folder under the package, then create a file called [single_node_launch.py] within the launch folder. Copy the following content into the file:

image-20250905174633450

 

2.2 Configuring the setup.py File

The launch file is often named "LaunchName_launch.py." LaunchName is customizable, while _launch.py is considered fixed. You need to modify the setup.py file in the package to add the files in the launch path and compile to generate the executable .py file.

image-20231031114303641

 

2.3. Compile the package

image-20250905174942260

2.4. Run the program

image-20250905175036003

2.5. Source Code Analysis

  1. Import related libraries
  1. Define a function called generate_launch_description and return a launch_description.

We define a variable called node as the return value of a node startup. We then call the Node function with two important parameters: package and executable.

Finally, we call the LaunchDescription function, passing in the node parameter, and execute the function.

 

3. Writing a Launch Program for Multiple Nodes

3.1. Creating a Launch File

Create a new file called [multi_node_launch.py] and add the following content:

3.2. Compile the package

image-20250905174942260

3.3. Run the program

image-20250905175250026

If the terminal does not print anything, we can verify that the nodes have started successfully by checking which nodes have started. In the terminal, enter:

image-20231031120049494

3.4 Source Code Analysis

Similar to simple_node_launch.py, except for one more node.

4 Topic Remapping Example

4.1 Creating a New Launch File

Create a new file called [remap_name_launch.py] in the same directory as multi_node_launch.py and add the following content:

4.2. Compile the package

image-20250905174942260

4.3. Run the program

Let's first see what topics the publisher_demo node publishes before remapping topics:

image-20231031121230604

The topic here is [/topic_demo]

image-20250905175613834

As shown above, the topic name has been remapped to [/topic_update]

4.4 Source Code Analysis

The following sections have been added:

Here, the original /topic_demo topic is remapped to /topic_update

5. Example of Launching Another Launch File from a Nested Launch File

5.1. Creating a New Launch File

Create a new file, [include_launch.py], in the same directory as multi_node_launch.py ​​and add the following content:

 

5.2. Compiling the Function Package

image-20250905174942260

5.3. Running the Program

image-20250905180404738

5.4. Source Code Analysis

6. Comprehensive Launch File Example

This example primarily demonstrates how to write a complex launch file; the program's functionality is ignored.

6.1. Creating a New Launch File

Create a new file, [complex_launch.py], in the same directory as [multi_node_launch.py] and add the following content:

6.2. Compile the Workspace

image-20250905174942260

6.3. Run the Program

Two turtles will appear on the host machine's VNC.

image-20250905181104164

image-20250902150215135

6.4. Program Description

The program mainly starts:

  1. The talker_listener node in demo_nodes_cpp
  2. The namespaced talker_listener node
  3. Turtle 1, which has been namespaced as turtlesim1
  4. Turtle 2, which has been namespaced as turtlesim2
  5. Perform remapping so that both turtles can hear the same command topic

7. XML Implementation

7.1. Creating a Launch File

Create a file called [complex_launch.xml] in the same directory as complex_launch.py ​​and add the following content:

7.2. setup.py File Configuration

image-20250905182956976

7.3. Compile the Function Package

image-20250905174942260

7.4. Run the Program

Enter the terminal:

image-20250905181104164

Use keyboard control to start Turtle 1. Turtle 2 will completely mimic Turtle 1's behavior.

8. YAML Implementation

8.1. Create a New Launch File

Create a new file, [complex_launch.yaml], in the same directory as complex_launch.py ​​and add the following content:

8.2. Configuration

image-20250905183832452

8.3. Compile the package

image-20250905174942260

8.4. Run the program

image-20250905181104164

Start Turtle 1 using keyboard control. Turtle 2 will completely mimic Turtle 1's behavior.