【ROS2】中级:Launch- 使用事件处理程序

目标:学习 ROS 2 启动文件中的事件处理程序

教程级别:中级

 时间:15 分钟

 目录

  •  背景

  •  先决条件

  •  使用事件处理程序

    • 1 事件处理程序示例启动文件

  •  构建包

  •  启动示例

  •  文档

  •  摘要

 背景

ROS 2 中的 Launch  是一个执行和管理用户定义的进程的系统。它负责监控其启动的进程的状态,以及报告和响应这些进程状态的变化。这些变化被称为事件,可以通过在启动系统中注册一个事件处理器来处理。事件处理器可以为特定事件注册,并可用于监控进程状态。此外,它们还可以用来定义一组复杂的规则,这些规则可以用来动态修改启动文件。

本教程展示了 ROS 2 启动文件中事件处理程序的使用示例。

 先决条件

本教程使用 turtlesim 包。本教程还假设您已经创建了一个名为 launch_tutorial 的新 ament_python 类型构建包。

这个教程扩展了在使用启动文件中的替换教程https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Launch/Using-Substitutions.html 中显示的代码。

使用事件处理程序

1 .事件处理程序示例启动文件 

在 launch_tutorial 包的 launch 文件夹中创建一个名为 example_event_handlers_launch.py 的新文件。

5f13c00db29bff2287f2e1eee98e4d51.png

# 导入Node类,用于创建节点
from launch_ros.actions import Node


# 导入相关的类和函数
from launch import LaunchDescription
from launch.actions import (DeclareLaunchArgument, EmitEvent, ExecuteProcess,
                            LogInfo, RegisterEventHandler, TimerAction)
from launch.conditions import IfCondition
from launch.event_handlers import (OnExecutionComplete, OnProcessExit,
                                OnProcessIO, OnProcessStart, OnShutdown)
from launch.events import Shutdown
from launch.substitutions import (EnvironmentVariable, FindExecutable,
                                LaunchConfiguration, LocalSubstitution,
                                PythonExpression)


# 定义启动描述生成函数
def generate_launch_description():
    # 创建启动配置对象
    turtlesim_ns = LaunchConfiguration('turtlesim_ns')  # 创建命名空间配置
    use_provided_red = LaunchConfiguration('use_provided_red')  # 创建使用提供的红色配置
    new_background_r = LaunchConfiguration('new_background_r')  # 创建新的背景红色配置


    # 声明启动参数
    turtlesim_ns_launch_arg = DeclareLaunchArgument(
        'turtlesim_ns',  # 参数名
        default_value='turtlesim1'  # 默认值
    )
    use_provided_red_launch_arg = DeclareLaunchArgument(
        'use_provided_red',
        default_value='False'
    )
    new_background_r_launch_arg = DeclareLaunchArgument(
        'new_background_r',
        default_value='200'
    )


    # 创建节点
    turtlesim_node = Node(
        package='turtlesim',  #
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值