actions类似于允许执行长时间运行的任务、提供定期反馈并且可以取消的服务。
机器人系统可能会使用动作进行导航。一个动作目标可以告诉机器人移动到一个位置。当机器人导航到该位置时,它可以沿途发送更新(即反馈),然后在到达目的地后发送最终结果消息。
Turtlesim有一个动作服务器,动作客户端可以向其发送旋转乌龟的目标。
背景
-
Actions是ROS 2中的通信类型之一,用于长时间运行的任务。它们由三部分组成:目标、反馈和结果。
-
Actions建立在主题和服务之上。它们的功能类似于服务,只是可以取消操作。它们还提供稳定的反馈,而不是返回单一响应的服务。
-
Actions使用客户端-服务器模型,类似于发布者-订阅者模型 ——具体看什么是ROS2中的——topics。“Actions客户端”节点将目标发送到“Actions服务器”节点,该节点确认目标并返回反馈流和结果。
ROS2中的Actions
1、启动
启动两个turtlesim节点/turtlesim和/termop_turtle。
打开一个新的终端并运行:
ros2 run turtlesim turtlesim_node
打开另一个终端并运行:
ros2 run turtlesim turtle_teleop_key
2、使用actions
当启动/teolep_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.
第二行中,对应于一个action
请注意/turtlesim节点正在运行的终端。每次按下其中一个键,都会向/turtlesim节点中的动作服务器发送一个目标。目标是旋转乌龟,使其面向特定的方向。乌龟完成旋转后,应显示一条传达进球结果的信息:
[INFO] [1685673023.138085322] [turtlesim]: Rotation goal completed successfully
F键将在执行过程中取消一个动作。 试着按下C键,然后按下F键,乌龟才能完成旋转。在/turtlesim节点正在运行的终端中,将看到消息:
[INFO] [1685673144.100667377] [turtlesim]: Rotation goal canceled
客户端(您在teleop中的输入)不仅可以停止目标,服务器端(/turtlesim节点)也可以。当服务器端选择停止处理目标时,称为“中止”该目标。
试着在第一次旋转完成之前先按D键,然后按G键。在/turtlesim节点正在运行的终端中,将看到消息:
[WARN] [1685673679.685480196] [turtlesim]: Rotation goal received before a previous goal finished. Aborting previous goal
此操作服务器选择中止第一个目标,因为它获得了一个新目标。它可以选择其他的东西,比如拒绝新的目标,或者在第一个目标完成后执行第二个目标。不要以为每个操作服务器在获得新目标时都会选择中止当前目标。
3、ros2 node info
要查看节点提供的操作列表,在本例中为/turtlesim,请打开一个新终端并运行命令:
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/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 actions位于actions服务器下。这意味着/turtlesim响应/turtle1/rotate_absolute 动作并为其提供反馈。 /teolep_turtle节点在Action Clients下的名称为/turtle1/rotate_absolute,这意味着它发送该动作名称的目标。要查看,请运行:
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
4、ros2 action list
要识别ROS Graph中的所有action,请运行以下命令:
ros2 action list
返回:
/turtle1/rotate_absolute
这是目前ROS图中唯一的动作。它控制着乌龟的旋转,正如之前看到的那样。已经知道,通过使用ros2 node-info<node_name>命令,有一个操作客户端(/toreop_turtle的一部分)和一个操作服务器(/turtlesim的一部分。
5、ros2 action info
您可以使用以下命令进一步内省/turtle1/rotate_absolute操作:
ros2 action info /turtle1/rotate_absolute
返回
Action: /turtle1/rotate_absolute
Action clients: 1
/teleop_turtle
Action servers: 1
/turtlesim
这告诉了我们之前从在每个节点上运行ros2节点信息中学到的东西:/teleop_turtle节点有一个操作客户端,而/turlsim节点有用于/turl1/rotate_ansabsolute操作的操作服务器。
6、ros2 接口
在自己发送或执行行动目标之前,还需要一条信息,那就是行动类型的结构。
在运行命令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
此消息中第一个---之上的部分是目标请求的结构(数据类型和名称)。下一节是结果的结构。最后一节是反馈的结构。
ros2 action send_goal
使用以下语法从命令行发送一个动作目标:
ros2 action send_goal <action_name> <action_type> <values>
例如:
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: 450a208db628417782badf7e4676ec87
Result:
delta: -1.5520000457763672
Goal finished with status: SUCCEEDED
所有目标都有一个唯一的ID,显示在返回消息中。还可以看到结果,一个名为delta的字段,它是到起始位置的位移。
要查看此目标的反馈,请在ros2 action send_goal命令中添加--feedback:
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: 57a081393b4f4751b13ed3c0e02f13e2
Feedback:
remaining: -3.122000217437744
...
...
Feedback:
remaining: -0.018000006675720215
Result:
delta: 3.1040000915527344
Goal finished with status: SUCCEEDED
将继续收到反馈,剩余的弧度,直到目标完成。