ROS2: How to pass arguments from one launch file to a child launch file
Asked Answered
C

2

6

I have a main bringup.launch.py launch file of which the launch descriptor includes child.launch.py as a child launch file like this:

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource

def generate_launch_description():
    package_prefix = get_package_share_directory('child_package')
    argument_for_child = "lala"

    return LaunchDescription([
        # include the child launch file
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource([package_prefix, '/launch/child.launch.py'])
        ),
    ])

How do I pass an argument from bringup.launch.py to child.launch.py?

Criminate answered 28/8, 2019 at 16:22 Comment(0)
C
13

In bringup.launch.py you have to declare the launch argument, and add it to the launch_arguments map like this:

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.actions import DeclareLaunchArgument

def generate_launch_description():
    package_prefix = get_package_share_directory('child_package')
    argument_for_child = "lala"

    return LaunchDescription([
        # Declare the launc parameter
        DeclareLaunchArgument(
            'argument_for_child',
            default_value = argument_for_child,
            description = 'Argument for child launch file'),

        # include the child launch file
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource([package_prefix, '/launch/child.launch.py'])
            launch_arguments = {'argument_for_child': argument_for_child}.items()
        ),
    ])

In child.launch.py you read in the passed argument like this:

from launch.substitutions import LaunchConfiguration

def generate_launch_description():
    value= LaunchConfiguration('argument_for_child', default='-')

    ...

Note: this for ROS2 version Dashing

Criminate answered 28/8, 2019 at 16:22 Comment(0)
O
2

Years passed, now there's an official tutorial as well that basically covers what Floris explains.

Rolling version (docs.ros.org)

def generate_launch_description():
    colors = {
        'background_r': '200'
    }

    return LaunchDescription([
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource([
                PathJoinSubstitution([
                    FindPackageShare('launch_tutorial'),
                    'example_substitutions_launch.py'
                ])
            ]),
            launch_arguments={
                'turtlesim_ns': 'turtlesim2',
                'use_provided_red': 'True',
                'new_background_r': TextSubstitution(text=str(colors['background_r']))
            }.items()
        )
    ])

Nice thing of the official tutorial is it covers boolean arg too (which took me awhile to figure out).

Orford answered 18/4, 2023 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.