ros笔记06--从零体验ros2中launch系统

介绍

ROS2系统通常由许多节点组成,这些节点运行在许多不同的进程(甚至不同的机器)上。虽然可以通过 ros2 run 单独运行这些节点,但当节点数量很多的时候就会显得很麻烦。

ROS2中的launch系统旨在使用单个命令自动运行多个节点。它帮助用户描述其系统的配置,然后按照描述执行配置。系统的配置包括运行什么程序,在哪里运行它们,传递什么参数给它们,以及特定于ros的约定,这些约定通过给每个组件提供不同的配置,使得在整个系统中重用组件变得容易。它还负责监视已启动的进程的状态,并报告和/或对这些过程状态的变化作出反应。

上述提到的所有内容都可以在在启动文件中指定,该文件可以用Python,XML或YAML编写。然后可以使用ros2启动命令运行此启动文件,进一步运行指定的所有节点。本文基于python启动配置案例,来体验单节点、多节点等常见的ros2 启动系统的使用方法。

创建步骤

最基础的 launch 案例

  1. 创建py_launch_example包

    继续前几篇ros2博文,当前工作目录依旧为dev_ws
    $ cd dev_ws/src
    $ ros2 pkg create --build-type ament_python --license Apache-2.0 py_launch_example
    
  2. 在 launch目录下新增 my_script_launch.py 文件
    vim launch/my_script_launch.py

    import launch
    import launch_ros.actions
    
    def generate_launch_description():
        return launch.LaunchDescription([
            launch_ros.actions.Node(
                package='demo_nodes_cpp',
                executable='talker',
                name='talker'),
      ])
    
  3. 在setup.py中新增launch相关配置

    import os
    from glob import glob
    # Other imports ...
    
    package_name = 'py_launch_example'
    
    setup(
        # Other parameters ...
        data_files=[
            # ... Other data files
            # Include all launch files.
            (os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*launch.[pxy][yma]*')))
        ]
    )
    
  4. 构建测试

    $ colcon build --packages-select py_launch_example
    $ source install/setup.bash
    $ ros2 launch py_launch_example my_script_launch.py
    

    如下,执行launch后talker正常输出Hello World 在这里插入图片描述

多节点 launch 案例

  1. launch目录下新增 turtlesim_mimic_launch.py 文件
    vim launch/turtlesim_mimic_launch.py
    from launch import LaunchDescription
    from launch_ros.actions import Node
    
    def generate_launch_description():
        return LaunchDescription([
            Node(
                package='turtlesim',
                namespace='turtlesim1',
                executable='turtlesim_node',
                name='sim'
            ),
            Node(
                package='turtlesim',
                namespace='turtlesim2',
                executable='turtlesim_node',
                name='sim',
                parameters=[
                    {'background_b': 1}
                ]
            ),
            Node(
                package='turtlesim',
                executable='mimic',
                name='mimic',
                remappings=[
                    ('/input/pose', '/turtlesim1/turtle1/pose'),
                    ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
                ]
            )
        ])
    
    默认是蓝色背景,当我们在启动配置 Node.parameters 中设置 background_b:1 后,将会更改默认背景色。
  2. 运行测试
    启动节点
    $ ros2 launch py_launch_example turtlesim_mimic_launch.py
    
    通过rqt_graph可以观察到节点和topic结构如下:
    在这里插入图片描述
    当我们执行ros2 topic pub让第一个小海龟运动后,第二个小海龟也跟着运动请添加图片描述

注意事项

  1. launch 默认识别哪些启动文件?
    官方文档支持python xml yaml 这3种启动文件,因此我们一般使用这3种格式的启动配置文件即可;
    另外我们一般会在setup.py中添加如下内容:
    setup(
        data_files=[
            (os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*launch.[pxy][yma]*')))
        ]
    )
    
    通过 glob(os.path.join('launch', '*launch.[pxy][yma]*'))) 可以知道其匹配 launch目录中以*launch.[pxy][yma]*结尾的文件,包括我们的 launch/my_script_launch.py

说明

软件版本
ubuntu24.04 Desktop
ros2 jazzy
python 3.12.4(conda)
参考文档
ros官方文档 Tutorials/Intermediate/Launch/Launch-Main
ros官方文档 Tutorials/Intermediate/Launch/Creating-Launch-Files
ROS 2 Launch System -

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昕光xg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值