我有一个主要的bringup.launch.py
启动文件,启动描述符包括child.launch.py
作为 child 启动文件,如下所示:
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.py
传递到child.launch.py
?
答案 0 :(得分:2)
在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()
),
])
在child.launch.py
中,您像这样读取了传递的参数:
from launch.substitutions import LaunchConfiguration
def generate_launch_description():
value= LaunchConfiguration('argument_for_child', default='-')
...
注意:对于ROS2版本Dashing