Skip to content
Jonathan Cox edited this page Mar 9, 2023 · 1 revision

ROS2 Launch Files

In ROS2 applications that have multiple nodes it quickly becomes impractical to start each node, with its own configuration, manually in a different terminal. With ROS launch files you can write all the nodes with a complete configuration (remapping, parameters, etc.) in a single file, that you can launch with only one command line.

Launch files in ROS2 can either be written in Python, XML, or YAML, the most commonly used in ROS2 is python launch files. *note ROS1 launch files only use XML.

A good start is to look at the official ROS2 on how to create and use launch files:

Example the turtlebot object search launch file, object-search-1.launch.py

The launch files for the turtlebot simulator in this module are written in python and follow the standard python style of importing modules and defining functions.

First the relevant ROS2 modules are imported;

import os

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

Define a function that generates the launch description;

def generate_launch_description():

Define arguments / parameters

The next lines are parameters for the Gazebo simulation, the file path to the turtlebot and gazebo packages. Then some parameters for the simulation time and the location (x=0, y=0) to spawn the robot in the gazebo simulation. Last is the file path to the gazebo .world file that defines all the objects (tables, wall, coloured poles) in the simulation.

gazebo_launch_file_dir = os.path.join(get_package_share_directory('turtlebot3_gazebo'), 'launch')
pkg_gazebo_ros = get_package_share_directory('gazebo_ros')

use_sim_time = LaunchConfiguration('use_sim_time', default='true')
x_pose = LaunchConfiguration('x_pose', default='0.0')
y_pose = LaunchConfiguration('y_pose', default='0.0')

world = os.path.join(
    get_package_share_directory('uol_turtlebot_simulator'),
    'worlds',
    'object-search-test.world'

Include Launch files

The next four parameters include launch files from other launch files in other packages (gazebo, robot_state_publisher, spawn_turtlebot). In ROS launch files can launch other launch files, instead of including all of the nodes and parameters in one launch file they can be split in to several launch files and all launched together in one file, using IncludeLaunchDescription in python.

E.g. this code will launch the spawn_turtlebot3.launch.py launch file in the turtlebot3_gazebo package, with some launch arguments/parameters.

spawn_turtlebot_cmd = IncludeLaunchDescription(
    PythonLaunchDescriptionSource(
        os.path.join(gazebo_launch_file_dir, 'spawn_turtlebot3.launch.py')
    ),
    launch_arguments={
        'x_pose': x_pose,
        'y_pose': y_pose
    }.items()
)

Launch Nodes

To launch nodes use the Node function in python, defining the package where the node is stored and the python file to execute and a name.

twist_watchdog = Node(
    package='uol_turtlebot_simulator',
    executable='twist_watchdog.py',
    name='twist_watchdog'
)

Create the python launch description object

Finally create the launch description object and add all the includes, nodes etc. then return the launch description.

ld = LaunchDescription()

# Add the commands to the launch description
ld.add_action(gzserver_cmd)
ld.add_action(gzclient_cmd)
ld.add_action(robot_state_publisher_cmd)
ld.add_action(spawn_turtlebot_cmd)
ld.add_action(twist_watchdog)

return ld