How to Create a Launch File in ROS 2 Foxy Fitzroy

cover_turtles_mimic

In this post, we will learn how to create a launch file in ROS 2. A launch file enables you to launch multiple ROS 2 nodes at the same time. These files are created in Python and are run using the ros2 launch command (we’ll cover this later in this tutorial).

The official tutorial is located in the ROS 2 Foxy documentation, but we’ll run through the entire process step-by-step below.

You Will Need

In order to complete this tutorial, you will need:

Prerequisites

It is helpful if you’ve completed this tutorial on the Turtlesim package in ROS 2, but it is not required.

Setup

Open a new terminal window, and type the following command to create a new folder:

mkdir launch

If you don’t have gedit installed, install it now.

sudo apt-get install gedit

Open up a new launch file.

gedit launch/turtlesim_mimic_launch.py

Write the following code inside the launch file.

# This is an example of a launch file for ROS 2

# Import launch modules that are Python-compatible
from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
  """
  This file is the launch description. It will launch three nodes:
  turtlesim1: A turtle
  turtlesim2: Another turtle
  mimic: Causes one turtle to mimic the movements of the other turtle
  """
  return LaunchDescription([
  
    # Launches a window with a turtle in it
    Node(
      package='turtlesim',
      namespace='turtlesim1',
      executable='turtlesim_node',
      name='sim'
    ),
    # Launches another window with a turtle in it
    Node(
      package='turtlesim',
      namespace='turtlesim2',
      executable='turtlesim_node',
      name='sim'
    ),
    # The mimic executable contains code that causes turtlesim2 to mimic
    # turtlesim1
    Node(
      package='turtlesim',
      executable='mimic',
      name='mimic',
      remappings=[
        ('/input/pose', '/turtlesim1/turtle1/pose'),
        ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
      ]
    )
  ])

Click Save and close the file to return to the terminal.

Launch the Launch File

To launch the launch file, move to the launch file:

cd launch

Type:

ros2 launch turtlesim_mimic_launch.py
1_terminal_outputJPG

In a new terminal tab, publish a velocity command to force turtlesim1 to move at a forward velocity of 2.0 meters per second and an angular velocity of -1.8 radians per second. All of this stuff below is a single command.

ros2 topic pub -r 1 /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}"

You will see the turtles move around in unison in a circle.

2_turtles_mimicJPG

To see the relationship between nodes, open a new terminal window, and type:

rqt_graph
3-node-relationshipJPG

The hidden node n___ros2cli_4394 represents the command you entered in the command line interface (i.e. the terminal window). This node publishes the velocity command to a topic named /turtlesim1/turtle1/cmd_vel.

The /turtlesim1/sim node subscribes to velocity commands that are published on the /turtlesim1/turtle1/cmd_vel topic.

The /turtlesim1/sim node publishes to /turtlesim1/sim’s pose topic.

/mimic is subscribed to /turtlesim1/sim’s pose topic.

/mimic publishes to /turtlesim2/sim’s velocity command topic, which causes the second turtle to move like the first turtle.

When you’re done, press CTRL+C to shut down everything.