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

Flo*_*ris 2 python ros2

我有一个主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'])
        ),
    ])
Run Code Online (Sandbox Code Playgroud)

我如何从bringup.launch.pyto传递参数child.launch.py?

Flo*_*ris 5

在bringup.launch.py您必须声明发射参数,并将其添加到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()
        ),
    ])
Run Code Online (Sandbox Code Playgroud)

在child.launch.py你读入传递的参数是这样的:

from launch.substitutions import LaunchConfiguration

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

    ...
Run Code Online (Sandbox Code Playgroud)

注意:这适用于 ROS2 版本Dashing