当前位置:网站首页>Ros Learning (23) Action Communication Mechanism
Ros Learning (23) Action Communication Mechanism
2022-07-07 01:47:00 【Crème glacée avec Code】
Catalogue des articles
Préface
ROSLes mécanismes de communication couramment utilisés sont les sujets et les services,Mais beaucoup de scénarios,Ces deux mécanismes de communication ne répondent souvent pas à tous les besoins.
actionMécanismes de communication,Est un mécanisme de communication de haut niveau avec rétroaction continue,Le niveau inférieur est basé surROSCommunication thématique.
Un.、Qu'est - ce queaction
ROSDansactionlibPack de fonctions,Pour réaliseractionMécanisme de communication.actionSimilaire au mécanisme de communication de service,La différence est queactionAvec rétroaction continue,Possibilité de rétroaction continue sur l'avancement des tâches,Vous pouvez également interrompre l'exécution pendant la tâche.
2.、actionMécanismes de travail
actionUtiliser le client/Mode de fonctionnement du serveur,Comme suit:
ActionClients etActionPasser entre les serveursactionlibDéfiniaction protocolPour communiquer,Cet accord est basé surROSMise en œuvre du mécanisme de message pour.
Le client publie la cible de la tâche au serveur et annule la tâche si nécessaire , Le serveur affiche l'état actuel au client 、 Rétroaction en temps réel et résultats de l'exécution des tâches .Comme le montre la figure:
Trois、actionDéfinitions
actionAdoption.actionDéfinition du fichier, Placé dans le pack de fonctions actionSous le dossier,Le format est le suivant::
# Définir l'information cible
uint32 dishwasher_id
# Définir l'information sur les résultats
uint32 total_dishes_cleaned
# Message définissant la rétroaction périodique
float32 percent_complete
Visible,Unaction La définition de :goal、result、feedback.
Quatre、Mise en œuvre du Code
Principales réalisationsaction Noeud client et serveur pour ,Nouveau nomaction_tutorialsPack de fonctions.
1、Créer un client
InactionDans la définition de, Décrit une tâche de lavage de vaisselle . Le noeud client est responsable de l'émission actionDemande,DoDishes_client.cppLe contenu du document est le suivant::
#include <actionlib/client/simple_action_client.h>
#include "action_tutorials/DoDishesAction.h"
typedef actionlib::SimpleActionClient<action_tutorials::DoDishesAction> Client;
// Quandaction Cette fonction de rappel est appelée une fois terminée
void doneCb(const actionlib::SimpleClientGoalState& state,
const action_tutorials::DoDishesResultConstPtr& result)
{
ROS_INFO("Yay! The dishes are now clean");
ros::shutdown();
}
// Quandaction Cette fonction de rappel est appelée une fois activée
void activeCb()
{
ROS_INFO("Goal just went active");
}
// Bien reçu.feedback Appelez la fonction de rappel après
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");
// Définir un client
Client client("do_dishes", true);
// Attente côté serveur
ROS_INFO("Waiting for action server to start.");
client.waitForServer();
ROS_INFO("Action server started, sending goal.");
// Créer unactionDegoal
action_tutorials::DoDishesGoal goal;
goal.dishwasher_id = 1;
// EnvoyeractionDegoalCôté serveur, Et définir la fonction de rappel
client.sendGoal(goal, &doneCb, &activeCb, &feedbackCb);
ros::spin();
return 0;
}
2、Créer un serveur
Le noeud serveur est chargé de faire la vaisselle , Et la rétroaction sur les progrès en temps réel de la vaisselle ,DoDishes_server.cppLe contenu du document est le suivant::
#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include "action_tutorials/DoDishesAction.h"
typedef actionlib::SimpleActionServer<action_tutorials::DoDishesAction> Server;
// Bien reçu.actionDegoal Appelez la fonction de rappel après
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);
// Supposons que la vaisselle avance ,Et selon1hz Fréquence de publication Progrès feedback
for(int i=1; i<=10; i++)
{
feedback.percent_complete = i * 10;
as->publishFeedback(feedback);
r.sleep();
}
// QuandactionUne fois terminé, Retourner les résultats au 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;
// Définir un serveur
Server server(n, "do_dishes", boost::bind(&execute, _1, &server), false);
// Le serveur commence à fonctionner
server.start();
ros::spin();
return 0;
}
3、Configuration
InCMakeLists.txt Ajouter la règle suivante :
find_package(catkin REQUIRED genmsg actionlib_msgs actionlib)
add_action_files(DIRECTORY action FILES DoDishes.action)
generate_messages(DEPENDENCIES actionlib_msgs)
Dans le pack de fonctionspackage.xmlAjouter la configuration suivante au fichier:
<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>
Puis compilez le paquet de fonctions ,Comme suit:

À partir de ces fichiers générés après la compilation ,action C'est vraiment une sorte de message 、 Un mécanisme de communication plus élevé .
4、Exécution
Commencez parmasterNoeud,Les ordres sont les suivants::
roscore
Démarrer le noeud côté service ,Les ordres sont les suivants::
rosrun action_tutorials DoDishes_server
Redémarrer le noeud client ,Les ordres sont les suivants::
rosrun action_tutorials DoDishes_client
Les effets sont les suivants:
边栏推荐
- LeetCode. 剑指offer 62. 圆圈中最后剩下的数
- AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
- Yunna - work order management system and process, work order management specification
- Image watermarking, scaling and conversion of an input stream
- 永久的摇篮
- The difference between Tansig and logsig. Why does BP like to use Tansig
- Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
- AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
- 字符串转成日期对象
- C language instance_ three
猜你喜欢

Yunna | work order management software, work order management software app

Make DIY welding smoke extractor with lighting

鼠标右键 自定义

爬虫实战(六):爬笔趣阁小说

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

JVM 内存模型

Gin introduction practice

Reptile practice (VI): novel of climbing pen interesting Pavilion

New job insights ~ leave the old and welcome the new~

Let's see how to realize BP neural network in Matlab toolbox
随机推荐
Yunna | work order management software, work order management software app
刨析《C语言》【进阶】付费知识【一】
刨析《C语言》【进阶】付费知识【二】
Box stretch and pull (left-right mode)
2022/0524/bookstrap
Ds-5/rvds4.0 variable initialization error
鼠标右键 自定义
图片打水印 缩放 和一个输入流的转换
Make Jar, Not War
AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
C language [23] classic interview questions [Part 2]
[chip scheme design] pulse oximeter
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
Vocabulary in Data Book
JS ES5也可以创建常量?
hdu 4661 Message Passing(木DP&amp;组合数学)
一起看看matlab工具箱内部是如何实现BP神经网络的
Right mouse button customization
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
C language instance_ four