繁体   English   中英

ROS2:如何将参数从一个启动文件传递到子启动文件

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

我有一个主要的bringup.launch.py启动文件,其中启动描述符包括child.launch.py作为启动文件,如下所示:

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'])
        ),
    ])

如何将参数从bringup.launch.pychild.launch.py

bringup.launch.py您必须声明 launch 参数,并将其添加到 launch_arguments 映射中,如下所示:

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()
        ),
    ])

child.launch.py您读取传递的参数,如下所示:

from launch.substitutions import LaunchConfiguration

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

    ...

注意:这对于 ROS2 版本Dashing

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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