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 client
actionlib_test::DoDishesGoal goal;
// Fill in goal here
client.sendGoal(goal); // Sends a goal to the ActionServer
client.waitForResult(ros::Duration(5.0)); // Blocks until this goal finishes
if (client.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
printf("Yay! The dishes are now clean\n");
printf("Current State: %s\n", client.getState().toString().c_str());
return ;
}
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 here
as->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 ;
}
在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 python import roslib
roslib.load_manifest('actionlib_test')
import rospy
import actionlib from actionlib_test.msg import DoDishesAction, DoDishesGoal if __name__ == '__main__':
rospy.init_node('do_dishes_client')
client = actionlib.SimpleActionClient('do_dishes', DoDishesAction)
client.wait_for_server() goal = DoDishesGoal()
# Fill in the goal here
client.send_goal(goal)
client.wait_for_result(rospy.Duration.from_sec(5.0))
Python SimpleActionServer
server.py程序如下(API可参考Python SimpleActionServer):
#! /usr/bin/env python import roslib
roslib.load_manifest('actionlib_test')
import rospy
import actionlib from actionlib_test.msg import DoDishesAction class 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 here
self.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运行就会返回:

参考:
actionlib-Detailed Description
ROS actionlib学习(一)的更多相关文章
- ROS actionlib学习(三)
下面这个例子将展示用actionlib来计算随机变量的均值和标准差.首先在action文件中定义goal.result和feedback的数据类型,其中goal为样本容量,result为均值和标准差, ...
- ROS actionlib学习(二)
在ROS actionlib学习(一)中的例子展示了actionlib最基本的用法,下面我们看一个稍微实际一点的例子,用actionlib计算斐波那契数列,并发布反馈(feedback)和结果(res ...
- ROS入门学习
ROS学习笔记 ROS入门网站; ROS入门书籍 ROS主要包含包括功能包.节点.话题.消息类型和服务; ROS功能包/软件包(Packages) ROS软件包是一组用于实现特定功能的相关文件的集合, ...
- ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - ROSMapModify - ROS地图修改
ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - 2 - MapModify地图修改 We can use gmapping model to genera ...
- ROS wiki 学习(1)创建程序包时遇到的rosdep update出错
1. 使用turtlebot官网的ubuntu14.04走ROS维基时,在创建程序包后出现错误. 按照提示执行之后,出现以下错误. 搜寻度娘,几经波折后,终于解决.解决过程如下: 首先删除默认文件20 ...
- ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM
(写在前面: 这里参考rbx书中第八章和ROS社区教程进行学习,先看社区教程) === Doing the Turtlebot Navigation === ref ros wiki: http ...
- ROS进阶学习手记 7.2 -- RViz仿真实例2:Create SmartCar
上一节玩了 exbot 在RViz里的仿真控制,这里我们用urdf文件写个自己的小车模型,ref: http://blog.csdn.net/hcx25909/article/details/8904 ...
- ROS基础学习总结
最近一周因为工程需要,把ROS的一些基础学习了一下,现在做一下总结. 学习资源: #创客智造整理的wiki上的ROS入门教程(中文)https://www.ncnynl.com/category/ro ...
- actionlib学习
ROS中的服务service是一问一答的形式,你来查询了,我就返给你要的信息. action也有服务的概念,但是它不一样的地方是:不是一问一答,而多了一个反馈,它会不断反馈项目进度. 如navigat ...
随机推荐
- TOP排行新闻列表。
效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- 【C++ Primer | 15】虚继承
虚基类 一.虚基类介绍 多继承时很容易产生命名冲突,即使我们很小心地将所有类中的成员变量和成员函数都命名为不同的名字,命名冲突依然有可能发生,比如非常经典的菱形继承层次.如下图所示: 类A派生出类B和 ...
- jsp+servlet实现最基本的注册登陆功能
源码和数据库下载地址:http://download.csdn.net/detail/biexiansheng/9759722 1:首先需要设计好数据库和数据表,这里简单截图说明我创建的字段和类型. ...
- python学习之for循环
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 实例: #!/usr/bin/env python for letter in 'Python': # 第一个实例 prin ...
- springmvc返回中文乱码问题
关于springmvc的返回中文乱码的问题,网上可谓是清一色的一样,无外乎就两种,要么在局部类或这方法上解决,类似如下的代码: @GetMapping(value="/error/query ...
- Codeforces 138C Mushroom Gnomes - 2 线段树
Mushroom Gnomes - 2 感觉没啥东西, 用线段树算算每个被覆盖的概率, 坑点是有很多个在同一个点. #include<bits/stdc++.h> #define LL l ...
- 096实战 在windows下新建maven项目
1.拷贝settings到.m2文件下 2.修改文件 3.新建Project项目 4.转换为maven项目 config下转换 5.拷贝pom文件 <project xmlns="ht ...
- AngularJS表格神器“ui-grid”的应用
HTML: (代码仅用于解释得更清楚,并未完全展示) <!doctype html> <html ng-app="app"> <head> & ...
- P2326 AKN’s PPAP
P2326 AKN’s PPAP比较裸的贪心从高位向下枚举,如果当前位为1的个数大于1,ans+=(1<<i),然后从这些数中再向下枚举. #include<iostream> ...
- 图片裁切展示css
.wen_photo{ overflow:hidden;}.wen_photo img{ margin:0; padding:0; width:100%; height: 100%; object-f ...