当前位置:网站首页>ROS learning (23) action communication mechanism
ROS learning (23) action communication mechanism
2022-07-07 01:47:00 【Ice cream with code】
List of articles
Preface
ROS The commonly used communication mechanism in is topic and service , But in many scenarios , These two communication mechanisms often cannot meet all requirements .
action Communication mechanism , It is an upper layer communication mechanism with continuous feedback , Bottom based ROS Topic communication .
One 、 What is? action
ROS Medium actionlib Function pack , Used to implement action Communication mechanism .action Similar to the service communication mechanism , The difference is action With continuous feedback , You can constantly feedback the progress of the task , You can also abort the operation during the task .
Two 、action How it works
action Adopt client / The working mode of the server , as follows :
Action The client and Action Between servers actionlib Defined action protocol communicate , This agreement is based on ROS Message mechanism implementation of .
The client releases the task target to the server and cancels the task when necessary , The server will release the current status to the client 、 Real time feedback and results of task execution . Pictured :
3、 ... and 、action The definition of
action adopt .action File defines , Placed in the Feature Pack action Under the folder , The format is as follows :
# Define target information
uint32 dishwasher_id
# Define the result information
uint32 total_dishes_cleaned
# Define periodic feedback messages
float32 percent_complete
so , One action The definition of requires three parts :goal、result、feedback.
Four 、 Code implementation
The main implementation action Client and server nodes , New name is action_tutorials Function pack .
1、 Create client
stay action The definition of , Describes a task of washing dishes . The client node is responsible for sending action request ,DoDishes_client.cpp The contents of the document are as follows :
#include <actionlib/client/simple_action_client.h>
#include "action_tutorials/DoDishesAction.h"
typedef actionlib::SimpleActionClient<action_tutorials::DoDishesAction> Client;
// When action After completion, the callback function will be called once
void doneCb(const actionlib::SimpleClientGoalState& state,
const action_tutorials::DoDishesResultConstPtr& result)
{
ROS_INFO("Yay! The dishes are now clean");
ros::shutdown();
}
// When action After activation, the callback function will be called once
void activeCb()
{
ROS_INFO("Goal just went active");
}
// received feedback Then call the callback function
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");
// Define a client
Client client("do_dishes", true);
// Wait for the server side
ROS_INFO("Waiting for action server to start.");
client.waitForServer();
ROS_INFO("Action server started, sending goal.");
// Create a action Of goal
action_tutorials::DoDishesGoal goal;
goal.dishwasher_id = 1;
// send out action Of goal To the server , And set the callback function
client.sendGoal(goal, &doneCb, &activeCb, &feedbackCb);
ros::spin();
return 0;
}
2、 Create server
The server node is responsible for the task of washing dishes , And feed back the real-time progress of washing dishes ,DoDishes_server.cpp The contents of the document are as follows :
#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include "action_tutorials/DoDishesAction.h"
typedef actionlib::SimpleActionServer<action_tutorials::DoDishesAction> Server;
// received action Of goal Then call the callback function
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);
// Suppose the progress of washing dishes , And in accordance with the 1hz Frequency of release progress feedback
for(int i=1; i<=10; i++)
{
feedback.percent_complete = i * 10;
as->publishFeedback(feedback);
r.sleep();
}
// When action After completion , Return results to client
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;
// Define a server
Server server(n, "do_dishes", boost::bind(&execute, _1, &server), false);
// The server is running
server.start();
ros::spin();
return 0;
}
3、 To configure
stay CMakeLists.txt Add the following rule :
find_package(catkin REQUIRED genmsg actionlib_msgs actionlib)
add_action_files(DIRECTORY action FILES DoDishes.action)
generate_messages(DEPENDENCIES actionlib_msgs)
The function of the package is package.xml Add the following configuration to the file :
<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>
Then compile the Feature Pack , as follows :
From these files generated after compilation ,action It's really a message based 、 Higher level communication mechanism .
4、 function
Start... First master node , The order is as follows :
roscore
Start the server node , The order is as follows :
rosrun action_tutorials DoDishes_server
Then start the client node , The order is as follows :
rosrun action_tutorials DoDishes_client
The effect is as follows :
边栏推荐
- Domestic images of various languages, software and systems. It is enough to collect this warehouse: Thanks mirror
- Mysqlbackup restores specific tables
- uva 1401 dp+Trie
- 拖拽改变顺序
- AI automatically generates annotation documents from code
- AcWing 904. 虫洞 题解(spfa求负环)
- Can't you understand the code of linked list in C language? An article allows you to grasp the secondary pointer and deeply understand the various forms of parameter passing in the function parameter
- 字符串的相关编程题
- Box stretch and pull (left-right mode)
- AcWing 1140. 最短网络 (最小生成树)
猜你喜欢
Scenario practice: quickly build wordpress blog system based on function calculation
Blue Bridge Cup 2022 13th provincial competition real topic - block painting
AcWing 345. 牛站 题解(floyd的性质、倍增)
Can't you understand the code of linked list in C language? An article allows you to grasp the secondary pointer and deeply understand the various forms of parameter passing in the function parameter
设置Wordpress伪静态连接(无宝塔)
Comparison of picture beds of free white whoring
Appium自动化测试基础 — uiautomatorviewer定位工具
AcWing 345. Cattle station solution (nature and multiplication of Floyd)
一起看看matlab工具箱内部是如何实现BP神经网络的
对C语言数组的再认识
随机推荐
ROS学习(26)动态参数配置
Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
图片打水印 缩放 和一个输入流的转换
golang 基础 —— 数据类型
hdu 4661 Message Passing(木DP&amp;组合数学)
What does security capability mean? What are the protection capabilities of different levels of ISO?
First experience of JSON learning - the third-party jar package realizes bean, list and map to create JSON format
Right mouse button customization
域分析工具BloodHound的使用说明
Appium foundation - appium inspector positioning tool (I)
JS ES5也可以创建常量?
Shell script quickly counts the number of lines of project code
ROS学习(25)rviz plugin插件
Appium自动化测试基础 — uiautomatorviewer定位工具
一文带你走进【内存泄漏】
grep查找进程时,忽略grep进程本身
LeetCode:1175. 质数排列
C language instance_ two
Match VIM from zero (0) -- Introduction to vimscript
Yunna | work order management measures, how to carry out work order management