4-使用事件处理程序

背景

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

使用事件处理程序

1 事件处理程序launch文件示例

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

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',
        namespace=turtlesim_ns,
        executable='turtlesim_node',
        name='sim'
    )
    spawn_turtle = ExecuteProcess(
        cmd=[[
            FindExecutable(name='ros2'),
            ' service call ',
            turtlesim_ns,
            '/spawn ',
            'turtlesim/srv/Spawn ',
            '"{x: 2, y: 2, theta: 0.2}"'
        ]],
        shell=True
    )
    change_background_r = ExecuteProcess(
        cmd=[[
            FindExecutable(name='ros2'),
            ' param set ',
            turtlesim_ns,
            '/sim background_r ',
            '120'
        ]],
        shell=True
    )
    change_background_r_conditioned = ExecuteProcess(
        condition=IfCondition(
            PythonExpression([
                new_background_r,
                ' == 200',
                ' and ',
                use_provided_red
            ])
        ),
        cmd=[[
            FindExecutable(name='ros2'),
            ' param set ',
            turtlesim_ns,
            '/sim background_r ',
            new_background_r
        ]],
        shell=True
    )

    return LaunchDescription([
        turtlesim_ns_launch_arg,
        use_provided_red_launch_arg,
        new_background_r_launch_arg,
        turtlesim_node,
        RegisterEventHandler(
            OnProcessStart(
                target_action=turtlesim_node,
                on_start=[
                    LogInfo(msg='Turtlesim started, spawning turtle'),
                    spawn_turtle
                ]
            )
        ),
        RegisterEventHandler(
            OnProcessIO(
                target_action=spawn_turtle,
                on_stdout=lambda event: LogInfo(
                    msg='Spawn request says "{}"'.format(
                        event.text.decode().strip())
                )
            )
        ),
        RegisterEventHandler(
            OnExecutionComplete(
                target_action=spawn_turtle,
                on_completion=[
                    LogInfo(msg='Spawn finished'),
                    change_background_r,
                    TimerAction(
                        period=2.0,
                        actions=[change_background_r_conditioned],
                    )
                ]
            )
        ),
        RegisterEventHandler(
            OnProcessExit(
                target_action=turtlesim_node,
                on_exit=[
                    LogInfo(msg=(EnvironmentVariable(name='USER'),
                            ' closed the turtlesim window')),
                    EmitEvent(event=Shutdown(
                        reason='Window closed'))
                ]
            )
        ),
        RegisterEventHandler(
            OnShutdown(
                on_shutdown=[LogInfo(
                    msg=['Launch was asked to shutdown: ',
                        LocalSubstitution('event.reason')]
                )]
            )
        ),
    ])

    在launch文件中定义了OnProcessStart, OnProcessIO, OnExecutionComplete, onprocesssexit和OnShutdown事件的RegisterEventHandler动作。

    OnProcessStart事件处理程序用于注册一个回调函数,该函数将在turtlesim节点启动时执行。它将一条消息记录到控制台,并在turtlesim节点启动时执行spawn_turtle动作。

RegisterEventHandler(
    OnProcessStart(
        target_action=turtlesim_node,
        on_start=[
            LogInfo(msg='Turtlesim started, spawning turtle'),
            spawn_turtle
        ]
    )
),

    OnProcessIO事件处理程序用于注册一个回调函数,该回调函数在spawn_turtle动作写入其标准输出时执行。它记录生成请求的结果。

RegisterEventHandler(
    OnProcessIO(
        target_action=spawn_turtle,
        on_stdout=lambda event: LogInfo(
            msg='Spawn request says "{}"'.format(
                event.text.decode().strip())
        )
    )
),

    OnExecutionComplete事件处理程序用于注册一个回调函数,该函数在spawn_turtle动作完成时执行。它将一条消息记录到控制台,并在spawn操作完成时执行change_background_r和change_background_r_conditioned操作。

RegisterEventHandler(
    OnExecutionComplete(
        target_action=spawn_turtle,
        on_completion=[
            LogInfo(msg='Spawn finished'),
            change_background_r,
            TimerAction(
                period=2.0,
                actions=[change_background_r_conditioned],
            )
        ]
    )
),

    OnProcessExit事件处理程序用于注册一个回调函数,该函数在turtlesim节点退出时执行。它将一条消息记录到控制台,并执行EmitEvent操作,以便在turtlesim节点退出时发出一个Shutdown事件。这意味着当turturlesim窗口关闭时,launch将关闭。

RegisterEventHandler(
    OnProcessExit(
        target_action=turtlesim_node,
        on_exit=[
            LogInfo(msg=(EnvironmentVariable(name='USER'),
                    ' closed the turtlesim window')),
            EmitEvent(event=Shutdown(
                reason='Window closed'))
        ]
    )
),

    最后,OnShutdown事件处理程序用于注册一个回调函数,该函数在launch文件被要求关闭时执行。它向控制台记录一条消息,说明为什么要求关闭launch文件。它记录消息的关闭原因,如关闭乌龟窗口或用户发出的ctrl-c信号。

RegisterEventHandler(
    OnShutdown(
        on_shutdown=[LogInfo(
            msg=['Launch was asked to shutdown: ',
                LocalSubstitution('event.reason')]
        )]
    )
),

编译包

    转到工作区的根目录,编译包:

colcon build

    注意在编译之后对工作空间进行source。

launch示例

    现在可以使用ros2 launch命令启动example_event_handlers.launch.py文件。

ros2 launch launch_tutorial example_event_handlers.launch.py turtlesim_ns:='turtlesim3' use_provided_red:='True' new_background_r:=200

    这将进行以下操作:
    1. 启动一个具有蓝色背景的turtlesim节点
    2.产生第二只乌龟
    3.将窗口背景颜色改为紫色
    4.如果提供的background_r参数为200并且use_provided_red参数为True,则在两秒后将颜色更改为粉红色
    5.当turtlesim窗口关闭时,关闭启动文件
    此外,它将在以下情况下将消息记录到控制台:
    1.启动turturlesim节点
    2.执行spawn动作
    3.执行change_background_r动作
    4.执行change_background_r_conditioned动作
    5.turtle节点退出
    6.launch进程关闭。

参考文献:https://github.com/ros2/launch/blob/humble/launch/doc/source/architecture.rst

  • 13
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值