乐于分享
好东西不私藏

机器人系统——ROS2文档(进阶)—使用事件处理器

机器人系统——ROS2文档(进阶)—使用事件处理器

目标:了解 ROS 2 启动文件中的事件处理器。教程级别:中级时间: 15 分钟

目录

  • 背景
  • 前置条件
  • 使用事件处理器
    • 事件处理器示例启动文件
  • 构建包
  • 启动示例
  • 文档
  • 总结

背景

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

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

前置条件

本教程使用turtlesim包。本教程还假定您已经创建了一个名为launch_tutorial、构建类型为ament_python的新包。本教程扩展了启动文件中使用替换教程中所示的代码。

使用事件处理器

1事件处理器示例启动文件

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

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
)
from launch_ros.actions import Node
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_msgs/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')]
)]
)
),
])

在启动描述中定义了OnProcessStartOnProcessIOOnExecutionCompleteOnProcessExitOnShutdown事件的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操作向标准输出写入时执行。它记录 spawn 请求的结果。

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_rchange_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 节点退出时执行。它在 turtlesim 节点退出时向控制台记录一条消息并执行EmitEvent操作以发出Shutdown事件。这意味着当 turtlesim 窗口关闭时,启动进程将关闭。

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

最后,OnShutdown事件处理器用于注册一个回调函数,该函数在启动文件被要求关闭时执行。它会向控制台记录启动文件被要求关闭的原因。它会记录关闭原因的消息,如 turtlesim 窗口关闭或用户发出的ctrl-c信号。

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

构建包

转到工作空间的根目录,并构建包:

$ colcon build

构建后记得设置工作空间。

启动示例

现在您可以使用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参数为200use_provided_red参数为True,则在两秒后将颜色更改为粉色
  5.  turtlesim 窗口关闭时关闭启动文件

此外,它会在以下情况向控制台记录消息:

  1. turtlesim 节点启动时
  2. spawn 操作执行时
  3. change_background_r操作执行时
  4. change_background_r_conditioned操作执行时
  5. turtlesim 节点退出时
  6. 启动进程被要求关闭时

文档

启动文档提供了有关可用事件处理器的详细信息。

总结

在本教程中,您学习了在启动文件中使用事件处理器的知识。您了解了它们的语法和使用示例,以定义一组复杂的规则来动态修改启动文件。

本文档完整翻译自 ROS 2 Lyrical 官方文档,仅用于学习交流。