Tech Arcanist
Get in touch

23Chapter 23 of 74

ROS 2 Launch Files: Visualizing the Robot with One Command

From Multiple Terminals to One Command

Before starting this chapter, go through Chapter 21, where we visualized our robot in RViz2.

In Chapter 21, we successfully visualized the robot, but we had to open three terminals and start different ROS 2 components manually:

Terminal 1
    ↓
robot_state_publisher

Terminal 2
    ↓
joint_state_publisher_gui

Terminal 3
    ↓
RViz2

This works, but imagine having to start 5, 10, or even more nodes for a larger robot.

Wouldn’t it be easier if we could start everything with one command?

That’s exactly what a ROS 2 launch file allows us to do.


What We Are Going to Build

In this chapter, we will create a launch file that starts everything we used in Chapter 21:

         display.launch.py
                 │
┌────────────────┼─────────────┐
↓                ↓             ↓
robot_state_  joint_state_    RViz2
publisher      publisher       GUI

Open VS Code

Open the bumperbot_description package in VS Code.

Inside this package, create a new folder called:

launch

Create the Launch File

Inside the launch folder, create:

display.launch.py

Write the Launch File

import os
from ament_index_python.packages import get_package_share_directory

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import Command, LaunchConfiguration

from launch_ros.actions import Node
from launch_ros.parameter_descriptions import ParameterValue


def generate_launch_description():
    bumperbot_description_dir = get_package_share_directory("bumperbot_description")

    model_arg = DeclareLaunchArgument(
        name="model",
        default_value=os.path.join(bumperbot_description_dir, "urdf", "bumperbot.urdf.xacro"),
        description="Absolute path to robot urdf file")

    robot_description = ParameterValue(Command(["xacro ", LaunchConfiguration("model")]),value_type=str)

    robot_state_publisher_node = Node(
        package="robot_state_publisher",
        executable="robot_state_publisher",
        parameters=[{"robot_description": robot_description}]
    )

    joint_state_publisher_gui_node = Node(
        package="joint_state_publisher_gui",
        executable="joint_state_publisher_gui"
    )

    rviz_node = Node(
        package="rviz2",
        executable="rviz2",
        name="rviz2",
        output="screen",
        arguments=["-d", os.path.join(bumperbot_description_dir, "rviz", "display.rviz")],
    )

    return LaunchDescription([
        model_arg,
        joint_state_publisher_gui_node,
        robot_state_publisher_node,
        rviz_node
    ])

Explanation

Adding launch and rviz to CMakeLists.txt

Add:

install(
  DIRECTORY meshes urdf launch rviz
  DESTINATION share/${PROJECT_NAME}
)

CMakeLists.txt

is basically telling the build system:

"When you build my package, what should you do with my files?"

In this particular case it means:

Take these folders:

meshes
urdf
launch
rviz

        ↓

put them inside the installed package's
share/<package-name> directory
Source package
      │
      ├── meshes
      ├── urdf
      ├── launch
      └── rviz
             │
             │ colcon build
             ▼
      Installed package
             │
             └── share/bumperbot_description/

CMakeLists.txt tells the build system what to build or install and where to put it.

Adding Dependencies to package.xml

Now we need to tell ROS 2 something different.

CMakeLists.txt tells the build system what to do with our package files.

package.xml tells ROS 2:

"What other ROS 2 packages does my package depend on?"

This is the easiest distinction to remember:

CMakeLists.txt
      ↓
How should my package be built/installed?

package.xml
      ↓
What packages does my package need?

Add

  <exec_depend>robot_state_publisher</exec_depend>
  <exec_depend>joint_state_publisher_gui</exec_depend>
  <exec_depend>rviz2</exec_depend>
  <exec_depend>ros2launch</exec_depend>

Why do we need these?

Because our launch file is going to start:

robot_state_publisher
joint_state_publisher_gui
rviz2

So our package needs those ROS 2 packages to be available.

Relating package.xml to Our Launch File

Look at the node in our launch file:

Node(
    package="robot_state_publisher",
    executable="robot_state_publisher"
)

Here:

package
   ↓
robot_state_publisher
   ↓
The ROS 2 package we are using

and:

executable
   ↓
robot_state_publisher
   ↓
The program we want to run from that package

Because our launch file uses the robot_state_publisher package, we need to declare that package as a dependency in package.xml:

<exec_depend>robot_state_publisher</exec_depend>

The same applies to the others

Build the Workspace

After modifying the package, go back to the workspace and build

cd bumperbot_ws
colcon build

Run the Launch File

Open a new terminal.

Go to the workspace if necessary and source it:

. install/setup.bash
ros2 launch bumperbot_description display.launch.py

What Happens Now?

Instead of manually starting:

robot_state_publisher

then:

joint_state_publisher_gui

then:

rviz2

the launch system starts all of them.