当前位置:网站首页>ROS學習(23)action通信機制
ROS學習(23)action通信機制
2022-07-07 01:47:00 【敲代碼的雪糕】
前言
ROS中常用的通信機制是話題和服務,但是很多場景下,這兩種通信機制往往滿足不了所有需求。
action通信機制,是一種帶有連續反饋的上層通信機制,底層基於ROS話題通信。
一、什麼是action
ROS中的actionlib功能包,用於實現action的通信機制。action類似於服務通信機制,不同之處在於action帶有連續反饋,可以不斷的反饋任務進度,也可以在任務過程中中止運行。
二、action的工作機制
action采用客戶端/服務器的工作模式,如下:
Action客戶端與Action服務端之間通過actionlib定義的action protocol進行通信,這種協議基於ROS的消息機制實現。
客戶端向服務端發布任務目標以及在必要的時候取消任務,服務端會向客戶端發布當前狀態、實時反饋和任務執行的結果。如圖:
三、action的定義
action通過.action文件定義,放置在功能包中的action文件夾下,格式如下:
#定義目標信息
uint32 dishwasher_id
#定義結果信息
uint32 total_dishes_cleaned
#定義周期反饋的消息
float32 percent_complete
可見,一個action的定義需要三部分:goal、result、feedback。
四、代碼實現
主要實現action的客戶端和服務端節點,新建名為action_tutorials功能包。
1、創建客戶端
在action的定義中,描述了一個洗盤子的任務。客戶端節點負責發出action請求,DoDishes_client.cpp文件內容如下:
#include <actionlib/client/simple_action_client.h>
#include "action_tutorials/DoDishesAction.h"
typedef actionlib::SimpleActionClient<action_tutorials::DoDishesAction> Client;
// 當action完成後會調用該回調函數一次
void doneCb(const actionlib::SimpleClientGoalState& state,
const action_tutorials::DoDishesResultConstPtr& result)
{
ROS_INFO("Yay! The dishes are now clean");
ros::shutdown();
}
// 當action激活後會調用該回調函數一次
void activeCb()
{
ROS_INFO("Goal just went active");
}
// 收到feedback後調用該回調函數
void feedbackCb(const action_tutorials::DoDishesFeedbackConstPtr& feedback)
{
ROS_INFO(" percent_complete : %f ", feedback->percent_complete);
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "do_dishes_client");
// 定義一個客戶端
Client client("do_dishes", true);
// 等待服務器端
ROS_INFO("Waiting for action server to start.");
client.waitForServer();
ROS_INFO("Action server started, sending goal.");
// 創建一個action的goal
action_tutorials::DoDishesGoal goal;
goal.dishwasher_id = 1;
// 發送action的goal給服務器端,並且設置回調函數
client.sendGoal(goal, &doneCb, &activeCb, &feedbackCb);
ros::spin();
return 0;
}
2、創建服務端
服務器節點負責完成洗盤子的任務,並且反饋洗盤子的實時進度,DoDishes_server.cpp文件內容如下:
#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include "action_tutorials/DoDishesAction.h"
typedef actionlib::SimpleActionServer<action_tutorials::DoDishesAction> Server;
// 收到action的goal後調用該回調函數
void execute(const action_tutorials::DoDishesGoalConstPtr& goal, Server* as)
{
ros::Rate r(1);
action_tutorials::DoDishesFeedback feedback;
ROS_INFO("Dishwasher %d is working.", goal->dishwasher_id);
// 假設洗盤子的進度,並且按照1hz的頻率發布進度feedback
for(int i=1; i<=10; i++)
{
feedback.percent_complete = i * 10;
as->publishFeedback(feedback);
r.sleep();
}
// 當action完成後,向客戶端返回結果
ROS_INFO("Dishwasher %d finish working.", goal->dishwasher_id);
as->setSucceeded();
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "do_dishes_server");
ros::NodeHandle n;
// 定義一個服務器
Server server(n, "do_dishes", boost::bind(&execute, _1, &server), false);
// 服務器開始運行
server.start();
ros::spin();
return 0;
}
3、配置
在CMakeLists.txt中添加如下規則:
find_package(catkin REQUIRED genmsg actionlib_msgs actionlib)
add_action_files(DIRECTORY action FILES DoDishes.action)
generate_messages(DEPENDENCIES actionlib_msgs)
在功能包的package.xml文件中添加如下配置:
<build_depend>actionlib</build_depend>
<build_depend>actionlib_msgs</build_depend>
<build_depend>roscpp</build_depend>
<run_depend>actionlib</run_depend>
<run_depend>actionlib_msgs</run_depend>
<run_depend>roscpp</run_depend>
然後編譯功能包,如下:
從編譯後生成的這些文件看,action確實是一種基於消息的、更加高層的通信機制。
4、運行
先啟動master節點,命令如下:
roscore
啟動服務端節點,命令如下:
rosrun action_tutorials DoDishes_server
再啟動客戶端節點,命令如下:
rosrun action_tutorials DoDishes_client
效果如下:
边栏推荐
- AcWing 904. Wormhole solution (SPFA for negative rings)
- Mysqlbackup restores specific tables
- Public key \ private SSH avoid password login
- AI automatically generates annotation documents from code
- AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
- 一文带你走进【内存泄漏】
- WCF基金会
- ROS学习(24)plugin插件
- uva 1401 dp+Trie
- AcWing 904. 虫洞 题解(spfa求负环)
猜你喜欢
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
场景实践:基于函数计算快速搭建Wordpress博客系统
Gin introduction practice
1123. The nearest common ancestor of the deepest leaf node
今日问题-2022/7/4 lambda体中修改String引用类型变量
对C语言数组的再认识
2022/0524/bookstrap
JS how to quickly create an array with length n
Appium foundation - appium inspector positioning tool (I)
Yiwen takes you into [memory leak]
随机推荐
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
JS ES5也可以創建常量?
WCF Foundation
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 2)
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
一起看看matlab工具箱内部是如何实现BP神经网络的
uva 1401 dp+Trie
制作带照明的DIY焊接排烟器
C language instance_ three
增加 pdf 标题浮窗
tansig和logsig的差异,为什么BP喜欢用tansig
JS es5 peut également créer des constantes?
Today's question -2022/7/4 modify string reference type variables in lambda body
Appium基础 — Appium Inspector定位工具(一)
What does security capability mean? What are the protection capabilities of different levels of ISO?
C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
Modify the system time of Px4 flight control
Instructions for using the domain analysis tool bloodhound