A Beginner’s Guide to Creating ROS 2 Launch Files Using Python

In ROS 2, launch files allow you to automate the startup of multiple nodes and configurations in a clean, scalable way. Launch files are often written in Python, which makes them more flexible compared to the XML format used in ROS 1. Today, we’ll go through the process of creating a basic ROS 2 launch file in Python using a practical example involving TurtleBot3 and Gazebo.

Here’s the full example code:

from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch_ros.actions import Node
from launch.launch_description_sources import PythonLaunchDescriptionSource
from ament_index_python.packages import get_package_share_directory
import os

def generate_launch_description():
    turtlebot3_model = 'burger'  # Choose between 'burger', 'waffle', 'waffle_pi'

    turtlebot3_description_path = get_package_share_directory('turtlebot3_description')
    gazebo_launch = get_package_share_directory('turtlebot3_gazebo')

    # Include robot_state_publisher with parameter
    robot_state_pub_launch = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(
            os.path.join(turtlebot3_description_path, 'launch', 'robot_state_publisher.launch.py')
        ),
        launch_arguments={'model': turtlebot3_model}.items()
    )

    # Gazebo simulation launch
    gazebo_launch_file = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(
            os.path.join(gazebo_launch, 'launch', 'gazebo.launch.py')
        )
    )

    return LaunchDescription([
        gazebo_launch_file,
        robot_state_pub_launch
    ])

Breaking Down the Launch File

1. Imports and Setup

At the top, several important modules and functions are imported:

  • LaunchDescription: The core class representing the launch description.
  • IncludeLaunchDescription: Allows you to include other launch files.
  • Node: Defines a node that can be launched.
  • PythonLaunchDescriptionSource: Specifies that the included launch files are in Python.
  • get_package_share_directory: Retrieves the path of a package’s shared files, which is necessary for finding other launch files or configurations.

2. Defining the TurtleBot3 Model

turtlebot3_model = 'burger'

Here, the model of the TurtleBot3 robot is specified. You can choose between 'burger', 'waffle', or 'waffle_pi' depending on which version of the robot you want to simulate.

3. Getting Package Paths

turtlebot3_description_path = get_package_share_directory('turtlebot3_description')
gazebo_launch = get_package_share_directory('turtlebot3_gazebo')

These lines use get_package_share_directory() to locate the installation paths of the turtlebot3_description and turtlebot3_gazebo packages. This helps load launch files and resources from these packages.

4. Including the robot_state_publisher Launch

robot_state_pub_launch = IncludeLaunchDescription(
    PythonLaunchDescriptionSource(
        os.path.join(turtlebot3_description_path, 'launch', 'robot_state_publisher.launch.py')
    ),
    launch_arguments={'model': turtlebot3_model}.items()
)

This block includes the robot_state_publisher launch file. The launch file publishes the robot’s state (joints, transforms) based on the URDF model. The launch_arguments parameter is used to pass the robot model (in this case, 'burger') to the launch file.

5. Including the Gazebo Launch File

gazebo_launch_file = IncludeLaunchDescription(
    PythonLaunchDescriptionSource(
        os.path.join(gazebo_launch, 'launch', 'gazebo.launch.py')
    )
)

The second block includes the Gazebo simulation launch file for TurtleBot3. This file will set up a Gazebo world and spawn the robot model.

6. Combining Everything in LaunchDescription

return LaunchDescription([
    gazebo_launch_file,
    robot_state_pub_launch
])

Finally, the launch description returns a list of launch actions. In this case, it includes both the Gazebo launch file and the robot_state_publisher launch file, ensuring that both are started when you run this launch script.

Running the Launch File

To run the launch file in ROS 2, save the above code in a file, such as turtlebot3_gazebo_launch.py. Then, you can launch it using the following command:

ros2 launch <your_package_name> turtlebot3_gazebo_launch.py

This will start both Gazebo and the robot_state_publisher for your TurtleBot3 model.

Key Concepts

  • Modularity: By using IncludeLaunchDescription, you can break up complex setups into smaller, reusable launch files. This way, you can keep your main launch files clean and modular.
  • Parameters: Passing parameters like the robot model via launch_arguments allows for flexibility, making it easy to switch between different configurations.

Practical Exercise

Try modifying this launch file to:

  1. Change the TurtleBot3 model to waffle.
  2. Add a node that controls the robot’s movement by publishing velocity commands.

This will help you get familiar with adding more components to your launch file.

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Posts

Exploring ROSA – Revolutionizing ROS/ROS 2 Debugging and Human-Robot Interaction

A Beginner’s Guide to Creating ROS 2 Launch Files Using Python

Leveraging VS Code Devcontainers for Efficient ROS 2 Development

ROS2WASM: ROS 2 in the Browser

We Are Social

Your
Gateway
to
Future

Related Posts

Exploring ROSA – Revolutionizing ROS/ROS 2 Debugging and Human-Robot Interaction

Leveraging VS Code Devcontainers for Efficient ROS 2 Development

ROS2WASM: ROS 2 in the Browser