ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

In order to include a python launch file, you need to use the following import statement:

from launch.launch_description_sources import PythonLaunchDescriptionSource

Similarly, in order to include an XML launch file, you should import the following statement:

from launch_xml.launch_description_sources import XMLLaunchDescriptionSource

Please see a working example below:

$ cat xml_from_py.launch.py 
"""Invoke XML launch file from Python Launch file."""

import os

from ament_index_python import get_package_share_directory

from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch_xml.launch_description_sources import XMLLaunchDescriptionSource


def generate_launch_description():
    # include xml launch file
    launch_include = IncludeLaunchDescription(
        XMLLaunchDescriptionSource(
            os.path.join(
                get_package_share_directory("demo_nodes_cpp"),
                "launch/topics/talker_listener.launch.xml",
            )
        )
    )

    return LaunchDescription([launch_include])

You can launch it simply by the following command:

$ ros2 launch demo_nodes_cpp xml_from_py.launch.py 
[INFO] [launch]: All log files can be found below /home/ravi/.ros/log/2022-09-04-13-11-49-021399-dell-21519
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [talker-1]: process started with pid [21521]
[INFO] [listener-2]: process started with pid [21523]
[talker-1] [INFO] [1662264710.244856916] [talker]: Publishing: 'Hello World: 1'
[listener-2] [INFO] [1662264710.245174169] [listener]: I heard: [Hello World: 1]
[talker-1] [INFO] [1662264711.244494212] [talker]: Publishing: 'Hello World: 2'
[listener-2] [INFO] [1662264711.245017003] [listener]: I heard: [Hello World: 2]
^C[WARNING] [launch]: user interrupted with ctrl-c (SIGINT)
[listener-2] [INFO] [1662264711.556384949] [rclcpp]: signal_handler(signal_value=2)
[talker-1] [INFO] [1662264711.556404309] [rclcpp]: signal_handler(signal_value=2)
[INFO] [listener-2]: process has finished cleanly [pid 21523]
[INFO] [talker-1]: process has finished cleanly [pid 21521]

The package demo_nodes_cpp is available on GitHub.