ROS2学习之旅(8)——理解ROS2 Graph中的动作(Action)

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

动作建立在话题和服务之上,它的功能类似于服务,但动作是抢占式的(可以在执行时取消它们)。它还提供稳定的反馈,而不是只返回一个响应的服务。

动作使用客户机-服务器模型,类似于发布者-订阅者模型。“动作客户端”节点向“动作服务器”节点发送一个目标,该节点确认目标并返回一个反馈流和一个结果。

1.准备

运行/turtlesim/teleop_turtle两个节点,分别在两个终端运行:

ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key

此时,出现窗口:

2.使用动作

当启动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.
'Q' to quit.

第二行对应一个动作。

注意:字母键G|B|V|C|D|E|R|T在键盘上的F键周围形成一个“方框”,F周围的每个键的位置都对应于turtlesim中海龟方向。例如,E会将海龟的方向旋转到左上角。

请注意运行/turtlesim节点的终端,每当按下其中一个键时,就向/turtlesim节点的的动作服务器发送了一个目标,目标是将海龟旋转到一个特定的方向。一旦海龟完成旋转,就会显示传递目标结果的消息:

[INFO] [turtlesim]: Rotation goal completed successfully

F键将在执行过程中取消目标,演示了动作的可抢占特性。

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

[INFO] [turtlesim]: Rotation goal canceled

不仅客户端(在teleop中的输入)可以抢占目标,服务器端(/turtlesim节点)也可以抢占目标。当服务器端抢占一个动作时,它“中止”目标。

试着按下D键,然后在旋转完成之前按下G键。在/turtlesim节点运行的终端中,将看到以下消息:

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

服务器端中止了第一个目标,因为它被中断了。

3.ros2 node info

查找/turtlesim节点中的动作,可以运行:

ros2 node info /turtlesim

终端将返回/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
  Services:
    /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/list_parameters: rcl_interfaces/srv/ListParameters
    /turtlesim/set_parameters: rcl_interfaces/srv/SetParameters
    /turtlesim/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
  Action Servers:
    /turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
  Action Clients:

注意:/turtle1/rotate_absolute动作在Action Servers下,这表示/turtlesim响应/turtle1/rotate_absolute动作并提供反馈。

查看teleop_turtle节点的信息:

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/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

Action Clients下,有名为/turtle1/rotate_absolute的节点,这意味着它为该动作名称发送目标。

4.ros2 action lsit

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

ros2 action list

终端返回:

/turtle1/rotate_absolute

这是ROS Graph中唯一的动作,如前所述,它控制海龟的旋转。

4.1ros2 action list -t

查看所有动作的类型,可以运行:

ros2 action list -t

此时,返回:

/turtle1/rotate_absolute [turtlesim/action/RotateAbsolute]

在每个动作名称(在本例中仅为/turtle1/rotate_absolute)右侧的方括号中是动作类型turturlesim /action/RotateAbsolute。当希望从命令行或代码执行操作时,将需要它。

5.ros2 action info

可以通过info进一步得到有关动作的信息,运行:

ros2 action info /turtle1/rotate_absolute

此时,返回:

Action: /turtle1/rotate_absolute
Action clients: 1
    /teleop_turtle
Action servers: 1
    /turtlesim

可以得到:/teleop_turtle节点有一个动作客户端,/turtlesim节点有一个动作服务器,用于/turtle1/rotate_absolute动作。

6.ros2 interface show

在发送或执行操作目标之前,还需要了解动作类型的结构。在运行命令ros2 action list -t时,确定了/turtle1/rotate_absolute的类型。在终端中输入以下的命令:

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

该消息的第一部分(位于--之上)是目标请求的结构(数据类型和名称),中间一节是结果的结构,最后一部分是反馈的结构。

7.ros2 action send_goal

从命令行发送一个动作目标:

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

<values> 必须是YAML形式。

在终端运行:

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: 85bd38f0238745e6af4bf2a64a941133

Result:
    delta: 3.1200063228607178

Goal finished with status: SUCCEEDED

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

要查看此目标的反馈,命令行最后添加--feedback

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

此时,终端输出:

Sending goal:
   theta: -1.57

Goal accepted with ID: e6092c831f994afda92f0086f220da27

Feedback:
  remaining: -3.1268222332000732

Feedback:
  remaining: -3.1108222007751465

…

Result:
  delta: 3.1200008392333984

Goal finished with status: SUCCEEDED

将持续收到反馈,直到目标完成。

8.总结

动作类似于服务,允许执行长时间运行的任务,提供定期反馈,并可取消。

机器人系统可以使用动作来导航。动作目标可以告诉机器人移动到某个位置,当机器人导航到该位置时,它可以沿途发送更新信息(即反馈),然后在到达目的地后发送最终结果信息。

如果给您带来帮助,希望能给点个关注,以后还会陆续更新有关机器人的内容,点个关注不迷路~欢迎大家一起交流学习。
都看到这了,点个推荐再走吧~
未经允许,禁止转载。

posted @ 2021-07-13 22:05  Love&Robot  阅读(1003)  评论(0编辑  收藏  举报