ROS2 Tutorials #6: How to create a ROS2 launch file [NEW]

How to create a ROS2 launch file

Written by Alberto Ezquerro

04/09/2019

About

In this post, you will learn how to create a ROS2 launch file.

The problem: we have a ROS2 C++ package but it has no launch file. Yes, we can run the code of this package using ros2 run..., but we also want to be able to run it using ros2 launch...

Let’s get to work right away and solve this problem!

Step 1: Get the package without a launch file

Remember we have a package…get a copy of the ROSject containing this package using this link: http://www.rosject.io/l/bce4ffd/.

If you would like to learn how we created this ROSject and package, please see this post: https://www.theconstruct.ai/ros2-tutorials-create-a-ros2-package-cpp/.

Step 2: Recompile the package

Open the ROSject. This might take a few moments, please be patient.

Once the ROSject opens, fire up a Shell from the Tools menu and run the following commands to recompile the package.

user:~$ source /opt/ros/crystal/setup.bash
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~$ cd ros2_ws
user:~/ros2_ws$ colcon build --symlink-install
Starting >>> ros2_cpp_pkg
 
Finished <<< ros2_cpp_pkg [21.9s]
 
Summary: 1 package finished [22.0s]
user:~/ros2_ws$ source install/setup.bash # source the workspace
ROS_DISTRO was set to 'crystal' before. Please make sure that the environment does not mix paths from different distributions. 
ROS_DISTRO was set to 'melodic' before. Please make sure that the environment does not mix paths from different distributions.
user:~/ros2_ws$

Step 3: Create a ROS2 launch file for this package

Create a launch directory within the package and create a launch file within the directory.

On the Shell, run:

user:~/ros2_ws$ cd src/ros2_cpp_pkg/
user:~/ros2_ws/src/ros2_cpp_pkg$ mkdir launch
user:~/ros2_ws/src/ros2_cpp_pkg$ cd launch && touch ros2_cpp_code.launch.py
user:~/ros2_ws/src/ros2_cpp_pkg$

Fire up an IDE from the Tools menu, find the launch file just created and paste the following code into it:

"""Launch the cpp_code executable in this package"""

from launch import LaunchDescription
import launch_ros.actions


def generate_launch_description():
    return LaunchDescription([
        launch_ros.actions.Node(
            # the name of the executable is set in CMakeLists.txt, towards the end of
            # the file, in add_executable(...) and the directives following it
            package='ros2_cpp_pkg', node_executable='cpp_code', output='screen'),
    ])

Now, we need to tell ROS to recognize the launch file. Open up  CMakeLists.txt in the IDE and add the following lines at the bottom of it.

# install the launch directory
install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)

Don’t forget to save all the changes!!

Step 4: Compile and source the workspace again.

Again? Yes, again! We need to.

user:~/ros2_ws/src/ros2_cpp_pkg/launch$ cd /home/user/ros2_ws
user:~/ros2_ws$ colcon build --symlink-install
Starting >>> ros2_cpp_pkg
Finished <<< ros2_cpp_pkg [3.35s]

Summary: 1 package finished [3.48s]
user:~/ros2_ws$ source install/setup.bash # source the workspace
user:~/ros2_ws$

Step 5: Launch the package using the new launch file and be happy!

You’ve been working very hard, time to eat the fruit of your labor!

The format for the command is ros2 launch <package_name> <launch_file_name>.

We are launching the C++ code in src/ros2_cpp_code.cpp, and the output should be something like this:

user:~/ros2_ws$ ros2 launch ros2_cpp_pkg ros2_cpp_code.launch.py
[INFO] [launch]: process[cpp_code-1]: started with pid [13071]
[INFO] [ObiWan]: Help me Obi-Wan Kenobi, you're my only hope
[INFO] [launch]: process[cpp_code-1]: process has finished cleanly
user:~/ros2_ws$

Sweet! Done!!

Extra 1: ROSject link

Get the ROSject containing all code used in the post in the following link: http://www.rosject.io/l/bcf8985/.

Extra 2: ROS2 Full Course for Beginners

Extra 3: Video

Prefer to watch a video demonstrating the steps above? We have one for you below!

Related Resources

ROS2 Basics

Feedback

Did you like this post? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know in the comments area and we will do a video/post about it 🙂

Topics: ROS Q&A
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This