赞
踩
动作是ros2通信类型的一种,适用于长时间运行任务.由三部分组成:目标,反馈和结果。
动作由话题和服务构成,它的功能和服务类似,但动作可在执行过程中取消。并且,动作(不仅有响应)也会有一个稳定的反馈,但是服务只是返回一个响应。
动作使用了客户端-服务器模型,类似发布者-订阅者模型(在话题课程有描述)。一个客户端动作节点发送一个目标(服务)给动作服务器节点,服务器节点会获取目标并返回一个反馈流(持续的话题)和一个结果(服务)。
思考及疑问: 图中的通信流程是否具有普遍性,goal service req -> goal service resp -> result req -> feedback topic -> result resp
略
启动节点, /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
话题)
思考及疑问: 此处第一个指令,应该是指‘use’ 及之后的命令,用于控制小乌龟转向和平移,对应 cmd_vel
中的 linear
angular
备注: 大小写字母对应的指令不一样,此处需要切换到大写
可以发现G|B|V|C|D|E|R|T
字母键围着键盘字母F
围成一个盒子。每个键围绕F的位置相当于小乌龟的旋转方向。例如,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
由于受到打断,服务器端中断目标继续执行;因为有了新目标,所以服务器选择放弃第一个目标。它也可以选择其他方式,比如拒绝新目标或在第一个目标完成后执行第二个目标。不要认为每个行动服务器在收到新目标时都会选择放弃当前目标。
要查看 /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 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:
可发现, /turtlesim
节点的 /turtle1/rotate_absolute
动作在 Action Servers
层级下。表示 /turtlesim
会对 /turtle1/rotate_absolute
动作做出响应和反馈。
相应的, /teleop_turtle
节点的 /turtle1/rotate_absolute
动作在 Action Clients
层级下,查看:
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
要识别ROS图中的所有动作,请运行以下命令:
ros2 action list
返回:
/turtle1/rotate_absolute
这是目前ROS网络中唯一的动作。它控制着海龟的旋转。通过之前ros2 node info <node_name>
命令查看, 可发现该动作由动作客户端 ( /teleop_turtle
部分 ) 和动作服务器 ( /turtlesim
部分 ) 组成 。
动作也有类型,与主题和服务类似。要查找 /turtle1/rotate_absolute
的类型,请运行命令:
ros2 action list -t
返回:
/turtle1/rotate_absolute [turtlesim/action/RotateAbsolute]
在每个动作名称(本例中只有 /turtle1/rotate_absolute
)右侧的括号中是动作类型,即 turtlesim/action/RotateAbsolute
。当想通过命令行或代码执行动作时,就需要用到它。
可以使用指令进一步了解动作/turtle1/rotate_absolute
:
ros2 action info /turtle1/rotate_absolute
返回:
Action: /turtle1/rotate_absolute
Action clients: 1
/teleop_turtle
Action servers: 1
/turtlesim
这告诉了我们之前在每个节点上运行 ros2 node info
时了解到的信息:对于动作/turtle1/rotate_absolute
,节点/teleop_turtle
有一个动作客户端,节点/turtlesim
有一个服务器客户端
在发送或执行动作目标之前,需要了解很多动作类型信息。
获取动作/turtle1/rotate_absolute
的类型时,使用到指令ros2 action list -t
在终端输入如下指令,并包含动作的类型:
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
第一部分信息,在---
上方,是目标请求的数据结构(数据类型和名字)。第二部分信息是结果的数据结构。最后一部分是反馈的数据结构。
参考下面语法,从命令行发送一个动作目标:
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}"
可以看到小乌龟旋转,终端返回:
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
正如反馈信息所示,每个目标都有一个独特的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
可持续接受反馈,发送剩下弧度,直到目标完成
动作就像是一种服务,可以让你执行长期运行的任务,提供定期反馈,并且可以取消。
机器人系统可能会使用动作来导航。一个动作目标可以告诉机器人前往某个位置。当机器人导航到该位置时,它可以沿途发送更新(即反馈),然后在到达目的地后发送最终结果信息。
Turtlesim 有一个动作服务器,动作客户端可以向服务器发送目标,以实现乌龟的旋转。在本教程中,可查看该动作/turtle1/rotate_absolute
的具体信息,以便更好地了解动作是什么以及它们是如何工作的。
现在,你已经掌握了 ROS 2 的所有核心概念。本组教程的最后几篇将向您介绍一些工具和技术,从使用 rqt_console 查看日志开始,让您更轻松地使用 ROS 2。
可使用plotjuggler 对topic
数据进行可视化查看,实时曲线; action
中 feedback topic
在动作触发与完成之间会周期性发送
使用键盘字母更改旋转角度时,pose
中theta
会根据目标值变化,cmd_vel
angular
保持不变。

使用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] #发送动作指令
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。