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

标签 python ros2

我有一个主要的 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​​

最佳答案

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

关于python - ROS2:如何将参数从一个启动文件传递到子启动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57696569/

相关文章:

ubuntu - 多台机器上的 ROS2 : ros2 multicast working, 谈话者/听众不工作

python - python中的Opencv + blob

python - 结果未打印,为什么 `print` 在 `return` 之后被忽略?

c++ - 具有奇怪类型的成员函数导致回调函数不匹配

c++ - 寻找 ROS2 的 C++ 中的 cv_bridge 示例

c++ - Ament Tool ROS中如何链接共享库

python - 尝试在 TensorFlow 中使用 BasicLSTMCell 和dynamic_rnn 设置网络时出现 ValueError

python - 如何将私有(private) python 包添加到 cloud composer 的要求中?

python - Python list 的 count() 函数的时间复杂度是多少?

c++ - 使用 ROS2 环境变量创建 C++ 项目