機器人編程趣味實踐10-做個任務(行動)
這是ROS2中比服務更為復雜的基礎模塊。
白話一下:一個行動,可以時刻匯報進展,無法完成時可以變更或者取消,完成后會告知
Mission completed!
具體應用案例,如導航行為樹
這個后續細說,現在先從簡單的入手吧。
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
本節目標通過基礎案例掌握ROS2內部行動的基本使用。
預備知識
行動是 ROS 2 中的又一種通信類型,用于長時間運行的任務,由三部分組成:目標、反饋和結果。行動建立在主題和服務上。 它們的功能類似于服務,但可以取消,還提供穩定的反饋,而不同于返回單一響應的服務。行動同樣使用客戶端-服務器模型,類似于發布者-訂閱者模型(在主題教程中進行了描述)。 “行動客戶端”節點向“動作服務器”節點發送目標,該節點確認目標并返回反饋過程流和結果。
功能包要點
ros2基礎包
turtlesim包
source
實踐
1 配置
運行如下兩個節點 /turtlesim 和 /teleop_turtle :
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
2 基礎使用
可以看到右側啟動/teleop_turtle節點后,終端顯示如下:
使用方向鍵移動機器人.
使用 G|B|V|C|D|E|R|T 鍵使機器人轉相應角度. 'F' 鍵取消旋轉.
讓我們專注于第二行,它對應于一個行動。 (第一條指令對應于“cmd_vel”主題,之前在主題教程中討論過。)請注意,字母鍵 G|B|V|C|D|E|R|T 在美國 QWERTY 鍵盤上的 F 鍵周圍形成一個“框”。 F 周圍的每個鍵的位置對應于turtlesim 中的方向。 例如,E 會將海龜的方向旋轉到左上角。
注意 /turtlesim 節點運行的終端。 每次按下其中一個鍵時,都會向作為 /turtlesim 節點一部分的動作服務器發送一個目標。 目標是旋轉烏龜以使其朝向特定方向。 烏龜完成旋轉后,應顯示一條中繼目標結果的消息:
[INFO] [1622462396.650765434] [turtlesim]: 開啟二維機器人仿真節點 /turtlesim
[INFO] [1622462396.735890281] [turtlesim]: Spawning turtle [turtle1] at x=[9.855556], y=[7.388889], theta=[0.000000]
[INFO] [1622462694.049672436] [turtlesim]: Rotation goal completed successfully
如上對應:
if (fabs(normalizeAngle(static_cast
{
RCLCPP_INFO(nh_->get_logger(), "Rotation goal completed successfully");
rotate_absolute_goal_handle_->succeed(rotate_absolute_result_);
rotate_absolute_goal_handle_ = nullptr;
lin_vel_x_ = 0.0;
lin_vel_y_ = 0.0;
ang_vel_ = 0.0;
}
F 鍵將在執行過程中取消目標。
嘗試按 B 鍵,然后在海龜完成旋轉之前按 F 鍵。 在運行 /turtlesim 節點的終端中,將看到以下消息:
[INFO] [1622462850.641914700] [turtlesim]: Rotation goal canceled
如上對應:
if (rotate_absolute_goal_handle_->is_canceling())
{
RCLCPP_INFO(nh_->get_logger(), "Rotation goal canceled");
rotate_absolute_goal_handle_->canceled(rotate_absolute_result_);
rotate_absolute_goal_handle_ = nullptr;
lin_vel_x_ = 0.0;
lin_vel_y_ = 0.0;
ang_vel_ = 0.0;
}
客戶端(在Teleop中的輸入)不僅可以停止目標,而且服務器端(/turtlesim節點)也可以停止目標。 當服務器端選擇停止處理一個目標時,稱為“中止”該目標。 嘗試按 D 鍵,然后在旋轉完成之前按 G 鍵。 在運行 /turtlesim 節點的終端中,將看到以下消息:
[WARN] [1622462989.905265647] [turtlesim]: Rotation goal received before a previous goal finished. Aborting previous goal
如上對應:
void Turtle::rotateAbsoluteAcceptCallback(const std::shared_ptr
{
// Abort any existing goal
if (rotate_absolute_goal_handle_)
{
RCLCPP_WARN(nh_->get_logger(), "Rotation goal received before a previous goal finished. Aborting previous goal");
rotate_absolute_goal_handle_->abort(rotate_absolute_result_);
}
rotate_absolute_goal_handle_ = goal_handle;
rotate_absolute_feedback_.reset(new turtlesim::action::RotateAbsolute::Feedback);
rotate_absolute_result_.reset(new turtlesim::action::RotateAbsolute::Result);
rotate_absolute_start_orient_ = orient_;
}
該行動服務器選擇中止第一個目標,因為有了新目標。 可以選擇其他目標,例如拒絕新目標或在第一個目標完成后執行第二個目標。 不要假設每個行動服務器在獲得新目標時都會選擇中止當前目標。
3 節點信息
/turtlesim
Action Servers:
/turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
zhangrelay@ros2fun:~/RobSoft/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:
/teleop_turtle
Action Clients:
/turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
zhangrelay@ros2fun:~/RobSoft/turtlesim$ 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
顯示如下:
/turtle1/rotate_absolute
ros2 action list -t
顯示如下:
/turtle1/rotate_absolute [turtlesim/action/RotateAbsolute]
5 行動信息
ros2 action info /turtle1/rotate_absolute
顯示結果如下:
Action: /turtle1/rotate_absolute
Action clients: 1
/teleop_turtle
Action servers: 1
/turtlesim
6 行動接口消息
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 /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: 9e134c1c6d3d41bbac1aa20038487d07
Result:
delta: 1.5520031452178955
Goal finished with status: SUCCEEDED
注意,誤差大約0.02。可以修改機器人PID轉向提高精度留作思考題。
當然控制周期也可以好好研究一下哦^_^
如果需要看過程:
ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 0.0}" --feedback
Waiting for an action server to become available...
Sending goal:
theta: 0.0
Feedback:
remaining: -0.10079997777938843
Goal accepted with ID: e06e9a8bc25345e18ace2a0fdaa55b80
Feedback:
remaining: -0.09919998049736023
....
Feedback:
remaining: -0.0016000281320884824
Result:
delta: 0.09919995069503784
Goal finished with status: SUCCEEDED
總結
行動類似服務,允許執行長時間運行的任務、提供定時反饋并且可以取消。
機器人系統可能會使用行動進行導航。 動作目標可以告訴機器人行進到一個位置。 機器人導航到該位置時,可以沿途發送更新(即反饋),一旦到達目的地,它就會發送最終結果消息。
turtlesim 有一個行動服務器,行動客戶端可以將目標發送到旋轉機器人。 在本教程中,學習行動 /turtle1/rotate_absolute,更好了解什么是行動以及它們如何工作。
機器人
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。