当前位置:网站首页>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 :
边栏推荐
猜你喜欢
454-百度面经1
Instructions for using the domain analysis tool bloodhound
Appium基础 — Appium Inspector定位工具(一)
【唯一】的“万字配图“ | 讲透【链式存储结构】是什么?
刨析《C语言》【进阶】付费知识【一】
ROS学习(22)TF变换
AcWing 345. Cattle station solution (nature and multiplication of Floyd)
Comparison of picture beds of free white whoring
Make Jar, Not War
Transplant DAC chip mcp4725 to nuc980
随机推荐
AcWing 345. 牛站 题解(floyd的性质、倍增)
The cradle of eternity
增加 pdf 标题浮窗
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
Domestic images of various languages, software and systems. It is enough to collect this warehouse: Thanks mirror
Public key \ private SSH avoid password login
curl 命令
How can I code for 8 hours without getting tired.
C语言实例_3
Get to know MySQL for the first time
Gin introduction practice
1123. The nearest common ancestor of the deepest leaf node
百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (下)
【唯一】的“万字配图“ | 讲透【链式存储结构】是什么?
736. LISP syntax parsing: DFS simulation questions
js如何快速创建一个长度为 n 的数组
Reptile practice (VI): novel of climbing pen interesting Pavilion
How to prevent overfitting in cross validation
长按按钮执行函数
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP