当前位置:   article > 正文

ROS2 学习笔记7:了解ROS2 动作 action_ros2 action

ros2 action

Background 背景

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

动作由话题和服务构成,它的功能和服务类似,但动作可在执行过程中取消。并且,动作(不仅有响应)也会有一个稳定的反馈,但是服务只是返回一个响应。

动作使用了客户端-服务器模型,类似发布者-订阅者模型(在话题课程有描述)。一个客户端动作节点发送一个目标(服务)给动作服务器节点,服务器节点会获取目标并返回一个反馈流(持续的话题)和一个结果(服务)。

思考及疑问: 图中的通信流程是否具有普遍性,goal service req -> goal service resp -> result req -> feedback topic -> result resp
在这里插入图片描述

Prerequisites 前提

Tasks 任务

1 Setup

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

ros2 run turtlesim turtlesim_node      
  • 1
ros2 run turtlesim turtle_teleop_key      
  • 1

2 Use actions

当你启动/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.     
  • 1
  • 2

第二行的操作对应一个动作。(第一个指令相当于前面话题课程所讨论的cmd_vel话题)

思考及疑问: 此处第一个指令,应该是指‘use’ 及之后的命令,用于控制小乌龟转向和平移,对应 cmd_vel中的 linear angular

备注: 大小写字母对应的指令不一样,此处需要切换到大写

可以发现G|B|V|C|D|E|R|T字母键围着键盘字母F围成一个盒子。每个键围绕F的位置相当于小乌龟的旋转方向。例如,E让小乌龟往左上角旋转。

运行/turtlesim节点的终端输出是在变换的。每按一次键盘,就发送一个目标到动作服务器,该服务器也是节点/turtlesim的一部分。目标是旋转小乌龟朝向指定方向。一旦小乌龟完成旋转,一条表示目标(执行)结果的消息显示出来:

[INFO] [turtlesim]: Rotation goal completed successfully  
  • 1

F键是取消执行中的目标

示例:先按一下 C键,在小乌龟完成转动前键入.运行节点/turtlesim的终端,你会看到这么一条消息:

[INFO] [turtlesim]: Rotation goal canceled 
  • 1

不仅客户端(从键盘输入)可以抢占目标,服务器端(运行节点/turtlesim)也是可以的。当服务器端抢占了动作,会导致目标的中断执行。

示例:先按 D键,然后在旋转运动完成前按着G键.运行节点/turtlesim的终端,你会看见信息:

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

由于受到打断,服务器端中断目标继续执行;因为有了新目标,所以服务器选择放弃第一个目标。它也可以选择其他方式,比如拒绝新目标或在第一个目标完成后执行第二个目标。不要认为每个行动服务器在收到新目标时都会选择放弃当前目标。

3 ros2 node info

要查看 /turtlesim 节点的动作,打开一个新终端并运行以下命令:

ros2 node info /turtlesim
  • 1

它将返回 /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/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:
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

可发现, /turtlesim 节点的 /turtle1/rotate_absolute 动作在 Action Servers 层级下。表示 /turtlesim 会对 /turtle1/rotate_absolute 动作做出响应和反馈。

相应的, /teleop_turtle 节点的 /turtle1/rotate_absolute 动作在 Action Clients 层级下,查看:

ros2 node info /teleop_turtle
  • 1

返回:

/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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

4 ros2 action list

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

ros2 action list
  • 1

返回:

/turtle1/rotate_absolute
  • 1

这是目前ROS网络中唯一的动作。它控制着海龟的旋转。通过之前ros2 node info <node_name> 命令查看, 可发现该动作由动作客户端 ( /teleop_turtle部分 ) 和动作服务器 ( /turtlesim部分 ) 组成 。

4.1 ros2 action list -t

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

ros2 action list -t
  • 1

返回:

/turtle1/rotate_absolute [turtlesim/action/RotateAbsolute]
  • 1

在每个动作名称(本例中只有 /turtle1/rotate_absolute)右侧的括号中是动作类型,即 turtlesim/action/RotateAbsolute。当想通过命令行或代码执行动作时,就需要用到它。

5 ros2 action info

可以使用指令进一步了解动作/turtle1/rotate_absolute

ros2 action info /turtle1/rotate_absolute
  • 1

返回:

Action: /turtle1/rotate_absolute
Action clients: 1
    /teleop_turtle
Action servers: 1
    /turtlesim
  • 1
  • 2
  • 3
  • 4
  • 5

这告诉了我们之前在每个节点上运行 ros2 node info时了解到的信息:对于动作/turtle1/rotate_absolute,节点/teleop_turtle有一个动作客户端,节点/turtlesim有一个服务器客户端

6 ros2 interface show

在发送或执行动作目标之前,需要了解很多动作类型信息。

获取动作/turtle1/rotate_absolute的类型时,使用到指令ros2 action list -t 在终端输入如下指令,并包含动作的类型:

ros2 interface show turtlesim/action/RotateAbsolute
  • 1

返回:

# 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

第一部分信息,在---上方,是目标请求的数据结构(数据类型和名字)。第二部分信息是结果的数据结构。最后一部分是反馈的数据结构。

7 ros2 action send_goal

参考下面语法,从命令行发送一个动作目标:

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

<values>要符合yaml格式

查看turtlesim窗口,输入如下指令:

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

可以看到小乌龟旋转,终端返回:

Waiting for an action server to become available...
Sending goal:
   theta: 1.57

Goal accepted with ID: f8db8f44410849eaa93d3feb747dd444

Result:
  delta: -1.568000316619873

Goal finished with status: SUCCEEDED
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

正如反馈信息所示,每个目标都有一个独特的id。你也可以看到这个结果,带有delta的一段字段,表示从起始位置的角度位移。

添加--feedback到最后运行的指令,查看目标反馈结果。

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

终端返回:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

可持续接受反馈,发送剩下弧度,直到目标完成

Summary 总结

动作就像是一种服务,可以让你执行长期运行的任务,提供定期反馈,并且可以取消。

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

Turtlesim 有一个动作服务器,动作客户端可以向服务器发送目标,以实现乌龟的旋转。在本教程中,可查看该动作/turtle1/rotate_absolute的具体信息,以便更好地了解动作是什么以及它们是如何工作的。

Next steps 下一步

现在,你已经掌握了 ROS 2 的所有核心概念。本组教程的最后几篇将向您介绍一些工具和技术,从使用 rqt_console 查看日志开始,让您更轻松地使用 ROS 2。

Test & debug

可使用plotjuggler 对topic数据进行可视化查看,实时曲线; actionfeedback topic 在动作触发与完成之间会周期性发送

使用键盘字母更改旋转角度时,posetheta 会根据目标值变化,cmd_vel angular保持不变。

在这里插入图片描述
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/67774e85065e21b27435ddd8d41317da.png)

使用rqt查看,节点 teleop_turtle 可通过 话题 /turtle1/cmd_vel 及 动作 /turtle/rotate_absolute 控制小乌龟旋转,二者独立控制;节点 turtlesim 可通过 话题 /turtle1/pose 周期性发送。

在这里插入图片描述

命令合集

ros2 action list #所有动作罗列  

ros2 action list -t #动作罗列并附加动作类型  

ros2 action info /turtle1/rotate_absolute #动作类型信息概述

ros2 interface show turtlesim/action/RotateAbsolute #动作类型信息详细展开

ros2 action send_goal <action_name> <action_type> <values>  [可选--feedback] #发送动作指令
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/982436
推荐阅读
相关标签
  

闽ICP备14008679号