8.5. Add launch files to your package#

In the Section 6.2, we saw how to write a standalone XML launch file. This section will show how to add such a launch file to an ROS package in your workspace and some of the typical directory and file naming conventions.

Important

The steps in this section will assume that you have already created either the py_pubsub Python package from Section 8.3, or the cpp_pubsub C++ package from Section 8.4.

8.5.1. Creating the structure to hold launch files#

By convention, all launch files for a package are stored in a launch/ directory inside of the package. So, make sure to create a launch directory at the top-level of the package you created above.

If you have not done so yet, change to the src/py_pubsub package directory in your workspace and create a launch directory.

$ cd ~/ros2_ws/src/py_pubsub/
$ mkdir launch

For Python packages, the directory containing your package should look like this:

src/
  py_pubsub/
    launch/
    package.xml
    py_pubsub/
    resource/
    setup.cfg
    setup.py
    test/

To enable colcon to locate and utilize our launch files, we need to inform Python’s setup tools of their presence. To achieve this, open the setup.py file, add the necessary import statements at the top, and include the launch files into the data_files parameter of setup:

import os
from glob import glob
# Other imports ...

package_name = 'py_pubsub'

setup(
    # Other parameters ...
    data_files=[
        # ... Other data files
        # Include all launch files.
        (os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*launch.[pxy][yma]*')))
    ]
)

If you have not done so yet, change directory to the src/cpp_pubsub package directory in your workspace, and create a launch directory.

$ cd ~/ros2_ws/src/cpp_pubsub/
$ mkdir launch

For C++ packages, we will only be adjusting the CMakeLists.txt file by adding to the end of the file (but before ament_package()):

# Install launch files.
install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)

Tip

It is also a good idea to add an exec_depend dependency on the ros2launch package in your package’s package.xml:

<exec_depend>ros2launch</exec_depend>

That was, using rosdep to install missing dependencies will ensure that the ros2 launch command is available after building your package. It also ensures that all launch file formats are recognized.

8.5.2. Writing the launch file#

Write a launch file to start the talker node you created. Inside your launch directory (cd launch), create a new XML launch file called my_script.launch.xml and add the following to it:

<launch>
  <node pkg="py_pubsub" exec="talker" name="talker"/>
</launch>
<launch>
  <node pkg="cpp_pubsub" exec="talker" name="talker"/>
</launch>

8.5.3. Building the package and running the launch file#

Go to the root of the workspace, and (re)build it:

$ colcon build

After colcon build completed successfully (make sure to check the output) and you’ve (re)sourced the workspace, you should be able to start the launch file as follows:

$ ros2 launch py_pubsub my_script.launch.xml
$ ros2 launch cpp_pubsub my_script.launch.xml