Skip to content
  • Home
  • About the Blog
  • About the Author
  • Sitemap

Abdur Rosyid's Blog

Just a few notes on mechanical engineering and robotics

ROS2 Launch File

July 9, 2021 by Abdur Rosyid

In ROS1, we write a launch file in XML. In ROS2, there are three ways to write a launch file:

  • Using Python
  • Using XML
  • Using YAML

Since the API of ROS2 launch is written in Python, you have a lower level access to the launch features if you write your launch file in Python. This may let you get the most of ROS2 launch capabilities. However, a Python launch file may look a little bit more complex than the XML and YAML ones. But it is actually not complex at all. It is very simple. Writing a launch file in Python also gives you more flexibility since Python is a scripting language so that you can do more by scripting.

An example of ROS2 launch file in Python can be seen here: https://docs.ros.org/en/foxy/Tutorials/Launch-Files/Creating-Launch-Files.html. This launch file is to run two turtlesim robots; the motion of one turtlesim is to mimic the motion of another turtlesim. This behavior is governed by a node called “mimic”.

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='turtlesim',
            namespace='turtlesim1',
            executable='turtlesim_node',
            name='sim'
        ),
        Node(
            package='turtlesim',
            namespace='turtlesim2',
            executable='turtlesim_node',
            name='sim'
        ),
        Node(
            package='turtlesim',
            executable='mimic',
            name='mimic',
            remappings=[
                ('/input/pose', '/turtlesim1/turtle1/pose'),
                ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
            ]
        )
    ])

There are three nodes executed in the launch file above. The first and second nodes are actually the same node but using different namespaces. The third node is the “mimic” node.

A comparison between launch files written in Python, XML, and YAML to do the same thing can be found here: https://docs.ros.org/en/foxy/Guides/Launch-file-different-formats.html

Launching a launch file

A launch file can be run in two ways:

1) running the launch file directly by specifying the path to the launch file:

1
ros2 launch <path_to_launch_file>

2) wrapping the launch file in a package and running it using the same command like in ROS1:

1
ros2 launch  [PACKAGE_NAME] [LAUNCH_FILE]

If the launch file is written in Python, make sure that the Python file is executable. If not, you can make executable by using “chmod +x” command.

Post navigation

Previous Post:

ROS2 & ROS1 CLI Commands

Next Post:

Creating ROS2 Action in C++

Leave a Reply Cancel reply

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

Categories

  • STEM 101
  • Robotics
  • Kinematics
  • Dynamics
  • Control
  • Robot Operating System (ROS)
  • Robot Operating System (ROS2)
  • Software Development
  • Mechanics of Materials
  • Finite Element Analysis
  • Fluid Mechanics
  • Thermodynamics

Recent Posts

  • Pull Request on Github
  • Basics of Git and Github
  • Conda vs Docker
  • A Conda Cheat Sheet
  • Installing NVIDIA GPU Driver on Ubuntu

Archives

  • June 2025
  • July 2021
  • June 2021
  • March 2021
  • September 2020
  • April 2020
  • January 2015
  • April 2014
  • March 2014
  • March 2012
  • February 2012
  • June 2011
  • March 2008
© 2026 Abdur Rosyid's Blog | WordPress Theme by Superbthemes