简体   繁体   中英

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

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM