当前位置:网站首页>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 :
边栏推荐
- AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
- Modify the system time of Px4 flight control
- IDEA常用的快捷键
- 新工作感悟~辞旧迎新~
- Gin 入门实战
- New job insights ~ leave the old and welcome the new~
- Long press the button to execute the function
- LeetCode. 剑指offer 62. 圆圈中最后剩下的数
- 百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (下)
- 编译命令行终端 swift
猜你喜欢
js如何快速创建一个长度为 n 的数组
ROS学习(23)action通信机制
LeetCode:1175. 质数排列
Gin introduction practice
今日问题-2022/7/4 lambda体中修改String引用类型变量
JVM memory model
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
454 Baidu Mianjing 1
LeetCode. Sword finger offer 62 The last remaining number in the circle
AcWing 361. 观光奶牛 题解(spfa求正环)
随机推荐
Public key \ private SSH avoid password login
AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
[advanced C language] 8 written questions of pointer
ROS学习(26)动态参数配置
Blue Bridge Cup 2022 13th provincial competition real topic - block painting
Instructions for using the domain analysis tool bloodhound
域分析工具BloodHound的使用说明
Appium automation test foundation uiautomatorviewer positioning tool
Use nodejs to determine which projects are packaged + released
shell脚本快速统计项目代码行数
一文带你走进【内存泄漏】
Appium foundation - appium inspector positioning tool (I)
Long press the button to execute the function
增加 pdf 标题浮窗
Right mouse button customization
IDEA常用的快捷键
Transplant DAC chip mcp4725 to nuc980
How to prevent overfitting in cross validation
AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
JS Es5 can also create constants?