当前位置:网站首页>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 :
边栏推荐
猜你喜欢

2022/0524/bookstrap

鼠标右键 自定义

dvajs的基础介绍及使用

Appium基础 — Appium Inspector定位工具(一)

从底层结构开始学习FPGA----FIFO IP的定制与测试

Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period

Modify the system time of Px4 flight control

Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)

Shell script quickly counts the number of lines of project code

mongodb查看表是否导入成功
随机推荐
Make DIY welding smoke extractor with lighting
制作带照明的DIY焊接排烟器
AcWing 1142. 繁忙的都市 题解(最小生成树)
JS es5 peut également créer des constantes?
curl 命令
Modify the system time of Px4 flight control
JS Es5 can also create constants?
Set WordPress pseudo static connection (no pagoda)
修改px4飞控的系统时间
Compile command line terminal swift
C语言实例_4
What does security capability mean? What are the protection capabilities of different levels of ISO?
Blue Bridge Cup 2022 13th provincial competition real topic - block painting
Mongodb checks whether the table is imported successfully
ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
增加 pdf 标题浮窗
C语言实例_3
JS ES5也可以創建常量?
永久的摇篮