【ROS2】初级:CLI工具-理解动作

 理解动作

目标:在 ROS 2 中内省Action。

 教程级别:初学者

 时间:15 分钟

 目录

  •  背景

  •  先决条件

  •  任务

    •  设置

    •  使用操作

    • 3 ros2 节点信息

    • 4 ros2 动作列表

    • 5 ros2 动作类型

    • 6 ros2 动作信息

    • 7 ros2 界面显示

    • 8 ros2 操作 发送目标

  •  摘要

  •  下一步

  •  相关内容

 背景

动作是 ROS 2 中的一种通信类型,旨在用于长时间运行的任务。它们由三个部分组成:一个目标、反馈和一个结果

动作是基于主题和服务构建的。它们的功能与服务类似,不同之处在于动作可以被取消。它们还提供稳定的反馈,与只返回单一响应的服务不同。

动作使用客户端-服务器模型,类似于发布者-订阅者模型(在主题教程中有描述)。一个“动作客户端”节点向一个“动作服务器”节点发送一个目标,该节点确认目标并返回一系列反馈和结果。

da0db83444ce7a7cf9095d58b8aab6ce.png

 先决条件

本教程基于先前教程中涉及的概念,如节点和主题。

本教程使用 turtlesim 包。

始终不要忘记在您打开的每个新终端中获取 ROS 2 的源。

 任务

 1. 设置

启动两个 turtlesim 节点, /turtlesim 和 /teleop_turtle 。

打开一个新的终端并运行:

ros2 run turtlesim turtlesim_node

打开另一个终端并运行:

ros2 run turtlesim turtle_teleop_key

使用操作

当您启动 /teleop_turtle 节点时,您将在终端中看到以下消息:

Use arrow keys to move the turtle.
Use G|B|V|C|D|E|R|T keys to rotate to absolute orientations. 'F' to cancel a rotation.

让我们关注第二行,它对应于一个动作。(第一条指令对应于“cmd_vel”主题,在之前的主题教程中已经讨论过。)

请注意,字母键 G|B|V|C|D|E|R|T 在美式 QWERTY 键盘上围绕 F 键形成一个“盒子”(如果您没有使用 QWERTY 键盘,请查看此链接以跟进)。每个键的位置都与 turtlesim 中的该方向相对应。例如, E 将使乌龟的方向旋转到左上角。

请注意运行 /turtlesim 节点的终端。每次按下这些键中的一个,您都会向 /turtlesim 节点的一部分即动作服务器发送一个目标。该目标是旋转乌龟面向特定方向。一旦乌龟完成旋转,应显示传达目标结果的消息:

[INFO] [turtlesim]: Rotation goal completed successfully

按 F 键将在执行过程中取消一个目标。

尝试按下 C 键,然后在乌龟完成旋转之前按下 F 键。在运行 /turtlesim 节点的终端中,您将看到消息:

[INFO] [turtlesim]: Rotation goal canceled

不仅客户端(您在遥控操作中的输入)可以阻止一个目标,服务器端( /turtlesim 节点)也可以。当服务器端选择停止处理一个目标时,它被称为“中止”目标。

尝试先后按下 D 键和 G 键,在第一次旋转完成之前。在运行 /turtlesim 节点的终端中,你会看到这条消息:

[WARN] [turtlesim]: Rotation goal received before a previous goal finished. Aborting previous goal

这个动作服务器选择放弃第一个目标,因为它得到了一个新的目标。它本可以选择其他做法,比如拒绝新目标或在第一个目标完成后执行第二个目标。不要假设每个动作服务器在得到新目标时都会选择放弃当前目标。

3 ros2 节点信息

要查看节点提供的操作列表,在这种情况下, /turtlesim ,打开一个新的终端并运行命令:

ros2 node info /turtlesim

将返回 /turtlesim 的订阅者、发布者、服务、动作服务器和动作客户端列表:

cxy@ubuntu2404-cxy:~$ ros2 node info /turtlesim
/turtlesim
  Subscribers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /turtle1/cmd_vel: geometry_msgs/msg/Twist
  Publishers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /rosout: rcl_interfaces/msg/Log
    /turtle1/color_sensor: turtlesim/msg/Color
    /turtle1/pose: turtlesim/msg/Pose
  Service Servers:
    /clear: std_srvs/srv/Empty
    /kill: turtlesim/srv/Kill
    /reset: std_srvs/srv/Empty
    /spawn: turtlesim/srv/Spawn
    /turtle1/set_pen: turtlesim/srv/SetPen
    /turtle1/teleport_absolute: turtlesim/srv/TeleportAbsolute
    /turtle1/teleport_relative: turtlesim/srv/TeleportRelative
    /turtlesim/describe_parameters: rcl_interfaces/srv/DescribeParameters
    /turtlesim/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
    /turtlesim/get_parameters: rcl_interfaces/srv/GetParameters
    /turtlesim/get_type_description: type_description_interfaces/srv/GetTypeDescription
    /turtlesim/list_parameters: rcl_interfaces/srv/ListParameters
    /turtlesim/set_parameters: rcl_interfaces/srv/SetParameters
    /turtlesim/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
  Service Clients:


  Action Servers:
    /turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
  Action Clients:

请注意, /turtlesim 的 /turtle1/rotate_absolute 动作在 Action Servers 之下。这意味着 /turtlesim 会对 /turtle1/rotate_absolute 动作做出响应并提供反馈。

/teleop_turtle 节点在 Action Clients 下的名称为 /turtle1/rotate_absolute ,意味着它会发送该动作名称的目标。要查看该目标,请运行:

ros2 node info /teleop_turtle

将返回:

cxy@ubuntu2404-cxy:~$ ros2 node info /teleop_turtle
/teleop_turtle
  Subscribers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
  Publishers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /rosout: rcl_interfaces/msg/Log
    /turtle1/cmd_vel: geometry_msgs/msg/Twist
  Service Servers:
    /teleop_turtle/describe_parameters: rcl_interfaces/srv/DescribeParameters
    /teleop_turtle/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
    /teleop_turtle/get_parameters: rcl_interfaces/srv/GetParameters
    /teleop_turtle/get_type_description: type_description_interfaces/srv/GetTypeDescription
    /teleop_turtle/list_parameters: rcl_interfaces/srv/ListParameters
    /teleop_turtle/set_parameters: rcl_interfaces/srv/SetParameters
    /teleop_turtle/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
  Service Clients:


  Action Servers:


  Action Clients:
    /turtle1/rotate_absolute: turtlesim/action/RotateAbsolute

4 ros2 动作列表 

要识别 ROS 图中的所有动作,请运行命令:

ros2 action list

将返回:

cxy@ubuntu2404-cxy:~$ ros2 action list
/turtle1/rotate_absolute

这是 ROS 图中当前唯一的动作。它控制着乌龟的旋转,正如你之前看到的。你也已经知道,使用 ros2 node info <node_name> 命令,这个动作有一个动作客户端( /teleop_turtle 的一部分)和一个动作服务器( /turtlesim 的一部分)。

4.1 ros2 动作列表 -t 

动作有类型,类似于主题和服务。要找到 /turtle1/rotate_absolute 的类型,请运行命令:

ros2 action list -t

将返回:

/turtle1/rotate_absolute [turtlesim/action/RotateAbsolute]

在每个动作名称的右侧括号中(在本例中仅为 /turtle1/rotate_absolute )是动作类型, turtlesim/action/RotateAbsolute 。当你想要从命令行或代码中执行一个动作时,你会需要这个。

5 ros2 动作类型

如果您想要检查操作的操作类型,请运行以下命令:

ros2 action type /turtle1/rotate_absolute

 将返回:

turtlesim/action/RotateAbsolute

6 ros2 动作信息 

您可以使用以下命令进一步内省 /turtle1/rotate_absolute 操作:

ros2 action info /turtle1/rotate_absolute

将返回:

cxy@ubuntu2404-cxy:~$ ros2 action info /turtle1/rotate_absolute
Action: /turtle1/rotate_absolute
Action clients: 1
    /teleop_turtle
Action servers: 1
    /turtlesim

这告诉我们我们之前从在每个节点上运行 ros2 node info 所学到的: /teleop_turtle 节点有一个动作客户端, /turtlesim 节点为 /turtle1/rotate_absolute 动作有一个动作服务器。

7 ros2 接口显示

在您自己发送或执行动作目标之前,您需要了解的另一项信息是动作类型的结构。

回想一下,当您运行命令 ros2 action list -t 时,您已经确定了 /turtle1/rotate_absolute 的类型。在您的终端输入以下命令,并加上动作类型:

ros2 interface show turtlesim/action/RotateAbsolute

将返回:

cxy@ubuntu2404-cxy:~$ ros2 interface show turtlesim/action/RotateAbsolute
# The desired heading in radians
float32 theta
---
# The angular displacement in radians to the starting position
float32 delta
---
# The remaining rotation in radians
float32 remaining

本消息中第一个 --- 以上的部分是目标请求的结构(数据类型和名称)。下一部分是结果的结构。最后一部分是反馈的结构。

8 ros2 动作发送目标 

现在让我们使用以下语法从命令行发送一个动作目标:

ros2 action send_goal <action_name> <action_type> <values>

<values> 需要以 YAML 格式。

请持续关注 turtlesim 窗口,并在您的终端输入以下命令:

ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 1.57}"

您应该会看到乌龟在旋转,以及终端中出现以下消息:

cxy@ubuntu2404-cxy:~$ ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 1.57}"
Waiting for an action server to become available...
Sending goal:
     theta: 1.57


Goal accepted with ID: 75405e2c9b644c1bab0160149ba03a5b


Result:
    delta: 1.568000078201294


Goal finished with status: SUCCEEDED

所有目标都有一个唯一的 ID,在返回消息中显示。您还可以看到结果,一个名为 delta 的字段,它是相对于起始位置的位移。

要查看此目标的反馈,请将 --feedback 添加到 ros2 action send_goal 命令中:

cxy@ubuntu2404-cxy:~$ ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: -1.57}" --feedback
Waiting for an action server to become available...
Sending goal:
     theta: -1.57


Goal accepted with ID: c9ba7a727f024f8193f22d5e5ece5336


Feedback:
    remaining: 3.134000062942505


Feedback:
    remaining: 3.118000030517578


Feedback:
    remaining: 3.1019999980926514
……


Feedback:
    remaining: 0.029999971389770508


Feedback:
    remaining: 0.01399993896484375


Result:
    delta: -3.119999885559082


Goal finished with status: SUCCEEDED

您将继续收到反馈,剩余的弧度,直到目标完成。

 摘要

动作就像服务,允许您执行长时间运行的任务,提供定期反馈,并且可以取消。

机器人系统可能会使用动作进行导航。动作目标可以指示机器人前往一个位置。当机器人导航到该位置时,它可以沿途发送更新(即反馈),然后在到达目的地后发送最终结果消息。

Turtlesim 拥有一个动作服务器,动作客户端可以向其发送目标以旋转乌龟。在本教程中,您对该动作进行了内省, /turtle1/rotate_absolute ,以更好地了解动作是什么以及它们是如何工作的。

 下一步

现在您已经了解了所有核心的 ROS 2 概念。本套教程的最后几节将向您介绍一些工具和技巧,这些工具和技巧将使使用 ROS 2 变得更加容易,首先从使用 rqt_console 查看日志开始。https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Using-Rqt-Console/Using-Rqt-Console.html

 相关内容 

您可以在此处了解更多关于 ROS 2 中操作背后的设计决策。https://design.ros2.org/articles/actions.html

ab4536853db6e81cbd27db26063eaef9.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值