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'])
),
])
如何从bringup.launch.py
to传递参数child.launch.py
?
解决方案
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
推荐阅读
- jquery - 从 href 锚点中删除最后一个正斜杠
- bash - 如何在 bash 脚本中每天只调用一次特定代码?
- mysql - 如何使用 LIKE 运算符编写 SQL 查询
- ms-access - 他们是我可以有1个输入并将它们放在两个不同表中的字段中的一种方式
- microservices - 在发现服务中注册多个 IP
- java - 如何将 MultipartFile 转换为所需的类型 byte[]
- c# - 如何在gridview asp.net c#中搜索两个日期之间的记录?
- vba - 如何解决“错误 70 权限被拒绝”?
- javascript - 如何在具有暗/亮模式的页面之间进行平滑过渡
- python-3.x - 为什么熊猫分析在 ipython 中没有显示任何输出?