ROS actionlib学习(一)

  actionlib是ROS中一个很重要的功能包集合,尽管在ROS中已经提供了srevice机制来满足请求—响应式的使用场景,但是假如某个请求执行时间很长,在此期间用户想查看执行的进度或者取消这个请求的话,service机制就不能满足了,但是actionlib可满足用户这种需求。例如,控制机器人运动到地图中某一目标位置,这个过程可能复杂而漫长,执行过程中还可能强制中断或反馈信息,这时actionlib就能大展伸手了。

    actionlib使用client-server工作模式,ActionClient 和ActionServer通过"ROS Action Protocol"进行通信,"ROS Action Protocol"以ROS消息方式进行传输。此外ActionClient 和ActionServer给用户提供了一些简单的接口,用户使用这些接口可以完成goal请求(client-side)和goal执行(server-side)。

  ActionClient 和ActionServer之间使用action protocol通信,action protocol就是预定义的一组ROS message,这些message被放到ROS topic上在 ActionClient 和ActionServer之间进行传实现二者的沟通。

  ROS Messages:

  • goal - Used to send new goals to servers. 代表一个任务,可以被ActionClient发送到ActionServer。比如在MoveBase中,它的类型是PoseStamped,包含了机器人运动目标位置的信息。

  • cancel - Used to send cancel requests to servers

  • status - Used to notify clients on the current state of every goal in the system

  • feedback - Used to send clients periodic auxiliary information for a goal. 服务端定期告知Client当前Goal执行过程中的情况。在Move Base案例中,它表示机器人当前姿态。

  • result - Used to send clients one-time auxiliary information upon completion of a goal

 

  ROS系统在action文件(文件名后缀为.action)中定义了上述goal、result、feedback等消息。The .action file has the goal definition, followed by the result definition, followed by the feedback definition, with each section separated by 3 hyphens (---). 

  下面是一个示意的例子,在./action/DoDishes.action文件中对洗碗这一任务进行定义:goal为使用某一洗碗机洗碗,result为总共洗好的碗数目,feedback为洗碗进度。

复制代码

# Define the goal
uint32 dishwasher_id  # Specify which dishwasher we want to use
---
# Define the result
uint32 total_dishes_cleaned
---
# Define a feedback message
float32 percent_complete

复制代码

   下面在catkin_ws/src目录下创建一个测试package:

catkin_create_pkg actionlib_test roscpp std_msgs actionlib actionlib_msgs message_generation rospy

  在package的CMakeLists.txt文件中加入下面这几行:

#find_package(catkin REQUIRED genmsg actionlib_msgs actionlib)
add_action_files(DIRECTORY action FILES DoDishes.action)
generate_messages(DEPENDENCIES actionlib_msgs)

  注意如果使用catkin_create_pkg创建包时没有添加actionlib相关的依赖项,要将上面CMakeLists中第一行的注释去掉,另外还要在package.xml文件中加入下面几行。因为我们在创建包时就添加好了相关依赖,所以这一步骤可以省略。

<build_depend>actionlib</build_depend>
<build_depend>actionlib_msgs</build_depend>
<run_depend>actionlib</run_depend>
<run_depend>actionlib_msgs</run_depend>

  使用catkin_make编译即可查看生成的消息文件,这些消息之后将会用于ActionClient 和 ActionServer间的通信。

  另外可以看到,在devel/include/actionlib_test/中生成了相关的头文件:

 

 C++ SimpleActionClient 

  client示例代码client.cpp如下,它会等待Server连接,发送Goal,获取状态。SimpleActionClient完整的API可以参考C++ SimpleActionClient

复制代码

#include <actionlib_test/DoDishesAction.h> 
#include <actionlib/client/simple_action_client.h>typedef actionlib::SimpleActionClient<actionlib_test::DoDishesAction> Client;int main(int argc, char** argv)
{ros::init(argc, argv, "do_dishes_client");Client client("do_dishes", true); // true -> don't need ros::spin()client.waitForServer(); // Waits for the ActionServer to connect to this clientactionlib_test::DoDishesGoal goal;// Fill in goal hereclient.sendGoal(goal); // Sends a goal to the ActionServerclient.waitForResult(ros::Duration(5.0)); // Blocks until this goal finishesif (client.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)printf("Yay! The dishes are now clean\n");printf("Current State: %s\n", client.getState().toString().c_str());return 0;
}

复制代码

 C++ SimpleActionServer 

  服务端代码server.cpp如下,SimpleActionServert完整的API可以参考 C++ SimpleActionServer

复制代码

#include <actionlib_test/DoDishesAction.h> 
#include <actionlib/server/simple_action_server.h>typedef actionlib::SimpleActionServer<actionlib_test::DoDishesAction> Server;void execute(const actionlib_test::DoDishesGoalConstPtr& goal, Server* as) 
{// Do lots of awesome groundbreaking robot stuff hereas->setSucceeded();
}int main(int argc, char** argv)
{ros::init(argc, argv, "do_dishes_server");ros::NodeHandle n;Server server(n, "do_dishes", boost::bind(&execute, _1, &server), false);server.start();ros::spin();return 0;
}

复制代码

  在CMakeLists文件中加入下面这几行:

add_executable(client   src/client.cpp)
add_executable(server   src/server.cpp)
target_link_libraries( client ${catkin_LIBRARIES})
target_link_libraries( server ${catkin_LIBRARIES})

  使用catkin_make进行编译完成后输入指令rosrun actionlib_test server 运行server,通过rostopic list查看系统中的话题如下:

  使用rqt_graph命令可以查看节点和消息的关系,可以看出server端会接收goal和cancel消息,并发出status、result、feedback消息:

  接着输入指令rosrun actionlib_test client 运行client,结果如下图所示:

 

 Python SimpleActionClient 

  除了C++也可以使用Python实现同样的功能,client.py如下(API可以参考Python SimpleActionClient):

复制代码

#! /usr/bin/env pythonimport roslib
roslib.load_manifest('actionlib_test')
import rospy
import actionlibfrom actionlib_test.msg import DoDishesAction, DoDishesGoalif __name__ == '__main__':rospy.init_node('do_dishes_client')client = actionlib.SimpleActionClient('do_dishes', DoDishesAction)client.wait_for_server()goal = DoDishesGoal()# Fill in the goal hereclient.send_goal(goal)client.wait_for_result(rospy.Duration.from_sec(5.0))

复制代码

 Python SimpleActionServer 

  server.py程序如下(API可参考Python SimpleActionServer):

复制代码

#! /usr/bin/env pythonimport roslib
roslib.load_manifest('actionlib_test')
import rospy
import actionlibfrom actionlib_test.msg import DoDishesActionclass DoDishesServer:def __init__(self):self.server = actionlib.SimpleActionServer('do_dishes', DoDishesAction, self.execute, False)self.server.start()def execute(self, goal):# Do lots of awesome groundbreaking robot stuff hereself.server.set_succeeded()if __name__ == '__main__':rospy.init_node('do_dishes_server')server = DoDishesServer()rospy.spin()

复制代码

  注意在运行程序前先用chmod +x命令给Python文件添加可执行权限:

   运行client.py和server.py,注意client.py运行就会返回:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440069.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

机器学习笔记(十五):推荐系统

目录 1&#xff09;Problem formulation 2&#xff09;Content-based recommendations 3&#xff09;Collaborative filtering 4&#xff09;Collaborative filtering algorithm 5&#xff09;Vectorization: Low rank matrix factorization 6&#xff09;Implementation…

*【CodeForces - 280C】Game on Tree(期望模型,期望的线性性)

题干&#xff1a; Momiji has got a rooted tree, consisting of n nodes. The tree nodes are numbered by integers from 1 to n. The root has number 1. Momiji decided to play a game on this tree. The game consists of several steps. On each step, Momiji chooses…

武侠风云(基础版)

基本任务&#xff1a; 1 建立角色类&#xff0c;角色拥有生命值的属性和攻击的方法&#xff0c;攻击值是随机的。 2 建立职业子类&#xff0c;刀客&#xff0c;&#xff08;伤害少&#xff0c;血量多&#xff09;剑客&#xff08;伤害正常&#xff0c;血量正常&#xff0c;有几…

机器学习笔记(十六):大规模机器学习

目录 1&#xff09;Learning with large datasets 2&#xff09;Stochastic gradient descent 3&#xff09;Mini-batch gradient descent 4&#xff09;Stochastic gradient descent convergence 1&#xff09;Learning with large datasets 回顾一下我们之前提到的这句…

【ZOJ - 3329】One Person Game(带循环的概率dp,数学期望,高斯消元,数学)

题干&#xff1a; There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the dice are fair dice, so the probability of rolling each value, 1 to K1, K…

智能聊天机器人系统

# 智能聊天机器人系统 # 1.系统简介 # 随着社会的各个公司以及大学对人工智能技术的深入研究和快速发展&#xff0c;人工智能技术将逐步应用到 # 方方面面。智能聊天机器人系统是基于各类传感器收集人类语音数据&#xff08;智能电视、智能空调&#xff0c; # 智能冰箱、智能音…

机器学习笔记:总结

下面是我们本课程学到的要点&#xff1a; 1&#xff09;监督学习&#xff1a;线性回归&#xff0c;逻辑回归&#xff0c;神经网络&#xff0c;SVM&#xff1b; 2&#xff09;无监督学习&#xff1a;k均值&#xff0c;PCA&#xff0c;异常检测 3&#xff09;特别应用&#xf…

ROS探索总结(十二)——坐标系统

在机器人的控制中&#xff0c;坐标系统是非常重要的&#xff0c;在ROS使用tf软件库进行坐标转换。 相关链接&#xff1a;http://www.ros.org/wiki/tf/Tutorials#Learning_tf 一、tf简介 我们通过一个小小的实例来介绍tf的作用。 1、安装turtle包 <span>$ rosdep instal…

【BZOJ - 3036】绿豆蛙的归宿(概率DAG图dp,拓扑排序,概率dp,期望的线性性)

题干&#xff1a; 随着新版百度空间的下线&#xff0c;Blog宠物绿豆蛙完成了它的使命&#xff0c;去寻找它新的归宿。 给出一个有向无环的连通图&#xff0c;起点为1终点为N&#xff0c;每条边都有一个长度。绿豆蛙从起点出发&#xff0c;走向终点。 到达每一个顶点时&#x…

【LightOJ - 1079】Just another Robbery(概率dp,概率背包)

题干&#xff1a; As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermi…

强烈推荐的TensorFlow、Pytorch和Keras的样例资源(深度学习初学者必须收藏)

目录 一、TensorFlow 二、Keras 三、Pytorch 总结 本文转自微信公众号&#xff1a;机器学习初学者 原创&#xff1a; 机器学习初学者 机器学习初学者 TensorFlow、Keras和Pytorch是目前深度学习的主要框架&#xff0c;也是入门深度学习必须掌握的三大框架&#xff0c;但…

【LightOJ - 1027】A Dangerous Maze(概率dp,数学期望)

题干&#xff1a; You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors. If you choose the ith door, it can either take you back to the same position wh…

由浅到深理解ROS(2)

ROS文件系统 用户可以直接参看官网&#xff1a; http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem ROS文件系统中的两个最基本的概念&#xff1a;Package和Manifest&#xff0c;即包和清单文件。 &#xff08;1&#xff09;Package是组织ROS代码的最基本单位&…

Django员工管理系统

Django员工管理系统&#xff08;ems&#xff09; 需求分析&#xff1a; 1.实现管理员的注册、登陆页面注册&#xff1a;用户名、真实名字、密码、确认密码、性别、验证码用户名需要判断是否合法、是否存在&#xff0c;loading图片提示密码和确认密码是否相同&#xff0c;load…

【LightOJ - 1104】Birthday Paradox(概率,思维)

题干&#xff1a; Sometimes some mathematical results are hard to believe. One of the common problems is the birthday paradox. Suppose you are in a party where there are 23 people including you. What is the probability that at least two people in the party…

爬虫小记

中国商标网 找到正确的入口 在此页面加入全部data数据获取xhr请求包

【LightOJ - 1038】Race to 1 Again(概率dp,数学期望)

题干&#xff1a; Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be divided by its divisors. So, he is now playing with this property. He selects a number N. And he calls this D. In each turn he randomly choose…

使用matplotlib进行简单的数据展示

import numpy as np import matplotlib.pyplot as plt# 解决中文乱码 plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签 plt.rcParams[axes.unicode_minus] False # 用来正常显示负号# 建立一个坐标系 plt.subplot(1, 1, 1) # 指明x和y值 x np.array([1, 2…

由浅到深理解ROS(6)-坐标转换

转自 ROS 中对于多坐标系的处理是使用树型表示&#xff0c;在机器人自主导航中&#xff0c;ROS会构建这几个很重要的坐标系&#xff1a; base_link: 一般位于tf tree的最根部&#xff0c;物理语义原点一般为表示机器人中心&#xff0c;为相对机器人的本体的坐标系。(base_foot…

【BZOJ - 3450】Tyvj1952 Easy(数学期望,期望的线性性)

题干&#xff1a; 某一天WJMZBMR在打osu~~~但是他太弱逼了&#xff0c;有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有n次点击要做&#xff0c;成功了就是o&#xff0c;失败了就是x&#xff0c;分数是按comb计算的&#xff0c;连续a个comb就有a*a分&#xff0c;comb就…