ROS2中launch文件里的IncludeLaunchDescription、PythonLaunchDescriptionSource用法

在ROS 2中,launch文件 用于配置和启动多个节点、参数以及其他资源,通常以Python或XML格式编写。在launch文件中,IncludeLaunchDescriptionPythonLaunchDescriptionSource 是用来 嵌套和复用其他launch文件 的功能,下面详细介绍它们的用法和用例。


1. IncludeLaunchDescription 用法

功能

  • IncludeLaunchDescription 允许你在一个launch文件中嵌入或调用另一个launch文件。
  • 它可以将其他launch文件的内容包含进来,实现代码复用和模块化。

基本语法

from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch_ros.actions import Node
import os

def generate_launch_description():
    return LaunchDescription([
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(
                os.path.join(
                    get_package_share_directory('your_package_name'),
                    'launch/another_launch_file.launch.py'
                )
            ),
            launch_arguments={
                'arg_name_1': 'value_1',
                'arg_name_2': 'value_2'
            }.items()
        )
    ])

解释

  1. IncludeLaunchDescription:

    • 主要作用是包含其他的launch文件。
    • 它是一个动作(launch.actions 模块中的类)。
  2. PythonLaunchDescriptionSource:

    • 这是IncludeLaunchDescription的一个来源类,它指定所包含的launch文件是Python格式的。
    • 其他来源类型还包括XML和YAML格式。
  3. 路径拼接:

    • 使用 get_package_share_directory 获取ROS 2包的共享目录路径。
    • 通过 os.path.join 拼接出被包含的launch文件的完整路径。
  4. launch_arguments:

    • 如果被包含的launch文件有定义可选参数(使用DeclareLaunchArgument),你可以通过 launch_arguments 字典传递值。
    • .items() 是将字典转换为列表的键值对,用于满足函数要求。

2. PythonLaunchDescriptionSource 用法

功能

  • PythonLaunchDescriptionSource 是指定包含的launch文件的来源类型(Python格式)。
  • 它配合IncludeLaunchDescription使用,表示要包含的是一个Python写的launch文件。

代码示例

假设有两个launch文件:
  1. parent_launch.py (父launch文件)
  2. child_launch.py (被包含的子launch文件)

child_launch.py(被嵌入的launch文件)

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='demo_nodes_cpp',
            executable='talker',
            name='talker_node'
        )
    ])

parent_launch.py(包含child_launch.py的文件)

from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from ament_index_python.packages import get_package_share_directory
import os

def generate_launch_description():
    # 获取 child_launch.py 的路径
    child_launch_path = os.path.join(
        get_package_share_directory('your_package_name'),  # 包名
        'launch/child_launch.py'# 子 launch 文件名
    )

    return LaunchDescription([
        # 包含 child_launch.py
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(child_launch_path),
            launch_arguments={'arg_name': 'value'}.items()  # 如果有参数
        )
    ])

3. 使用场景

场景 1:模块化启动

  • 在大型项目中,可以将不同功能的节点拆分到多个launch文件中。
  • 使用IncludeLaunchDescription将这些launch文件组合在一起。

场景 2:复用已有launch文件

  • 如果某个launch文件在不同场景中都需要启动,可以将其包含到不同的父launch文件中。

场景 3:传递参数

  • launch文件通过launch_arguments传递参数给子launch文件。

4. 完整的示例:传递参数

child_launch.py(接收参数的子文件):

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch_ros.actions import Node
import os

def generate_launch_description():
    return LaunchDescription([
        DeclareLaunchArgument('robot_name', default_value='robot1'),
        Node(
            package='demo_nodes_cpp',
            executable='listener',
            name='listener_node',
            parameters=[{'robot_name': os.environ.get('robot_name')}]
        )
    ])

parent_launch.py(父文件,传递参数):

from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from ament_index_python.packages import get_package_share_directory
import os

def generate_launch_description():
    child_launch_path = os.path.join(
        get_package_share_directory('your_package_name'),
        'launch/child_launch.py'
    )

    return LaunchDescription([
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(child_launch_path),
            launch_arguments={'robot_name': 'my_robot'}.items()
        )
    ])

运行父launch文件时,会传递参数robot_name的值为my_robot,覆盖子launch文件中的默认值。


5. 总结

  • IncludeLaunchDescription: 用于在一个launch文件中包含另一个launch文件。
  • PythonLaunchDescriptionSource: 明确指定所包含的launch文件为Python格式。
  • 优势: 支持模块化、代码复用、参数传递,适用于复杂项目中的启动管理。

Q&A(想到便会更新)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值