ROS2官網教程學習筆記理解ROS2 actions動作

學習目標: 瞭解ROS2 actions動作

背景

actions動作是ROS2用於耗時比較長的一種通訊方式。主要由三部分組成: 目標(goal)、結果(result)、反饋(feedback)
actions動作基於話題和服務,它的功能和服務類似,除了它是可以被搶佔的(可以在執行時取消它)。而且還提供穩定過程的反饋,而不是向服務一樣只進行單一響應反饋。
actions動作使用客戶端-服務器模型,類似於發佈-訂閱模型。客戶端節點(action client)向服務器節點(action server)發送一個目標,服務端節點確認目標並返回一個反饋流和一個結果。

準備條件

需要按照turtlesim篇的軟件,瞭解節點話題和服務的概念

學習內容

1. 啓動節點

和之前一樣先啓動兩個節點

ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key

2. 使用 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.

第二行所說的功能就指的是actions動作。
到了這裏我得吐槽一下turtle_teleop_key這個節點,不知道是電腦問題還是上面其他原因這個節點在我電腦上CPU佔用非常高,而且記得在使用actions動作相關功能的時候鍵盤要用小寫鍵盤不要用大寫,否則沒效果。
這裏面簡單進行一下說明G|B|V|C|D|E|R|T這幾個鍵在F鍵周圍一圈,代表烏龜的8個絕對方向,依次變換45°
當我們在鍵盤控制的終端(/teleop_turtle)按下一個鍵的時候就往actions動作服務器烏龜(/turtlesim)發送一個目標,當烏龜轉到設定的角度的時候就會烏龜的終端就會顯示下面的消息

[INFO] [turtlesim]: Rotation goal completed successfully

我們通過F鍵可以看到actions動作可以被打斷的特性,隨便按下一個鍵讓烏龜轉起來,在沒有到達目標角度之前按下F,我們會在烏龜的終端看到下面的消息

[INFO] [turtlesim]: Rotation goal canceled

此外actions動作還有一個特性就是隻執行最新的目標,當我們連續按下目標角度的時候,新的目標值會覆蓋之前的目標,例如按下D馬上又按下G,我們在烏龜的終端會看到下面的輸出。

[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:

其中的Action Servers: 展示了/turtlesim節點有關Action動作的信息
通過這些信息我們可以得到/turtlesim節點相應並進行/turtle1/rotate_absolute 的Action,這是一個服務器端
我接下來再觀察/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
  Services:
    /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
  Action Servers:

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

同樣我們也看到了名爲/turtle1/rotate_absolute的Action,但是這是作爲客戶端

4. ros2 action list

通過下面命令我們可以獲得當前ros中的所有action

ros2 action list

我們執行過後輸出如下

/turtle1/rotate_absolute

4.1 ros2 action list -t

和之前結束話題和服務的命令類似加上-t選項還可以顯示動作的類型

ros2 action list -t

命令輸出如下

/turtle1/rotate_absolute [turtlesim/action/RotateAbsolute]

5. ros2 action info

我們還可以針對一個action動作,獲得它的信息,例如

ros2 action info /turtle1/rotate_absolute

終端顯示如下

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

結合之前的操作我們知道/turtle1/rotate_absolute 這個Action目前/turtlesim爲服務器端 /teleop_turtle爲客戶端

6. ros2 interface show

我們之前通過ros2 action list -t知道了Action的消息類型,我們可以用下面命令進而獲得消息類型的結構

ros2 interface show turtlesim/action/RotateAbsolute.action

返回結果如下

# 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

我們可以通過命令行的方式向一個action發送目標

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: f8db8f44410849eaa93d3feb747dd444

Result:
  delta: -1.568000316619873

Goal finished with status: SUCCEEDED

所有的Goal都會有一個唯一的ID值,同樣終端顯示了這個ID。終端同時返回了結果delta,表示烏龜轉動的位移。
前面我們說過action動作有過程反饋,我們可以加上--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

總結

Actions通常用於執行時間比較長的過程,例如可以用於機器人導航上,發送一個導航點,然後我們實時監控導航運動過程,在這個過程當中隨時可以取消目標點和發送新的目標點

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章