1.使用目标回调方法(Goal Callback Method)编写一个简单的行为服务器
开始编写行为前,先创建行为消息文件Averaging.action
#goal definition
int32 samples
---
#result definition
float32 mean
float32 std_dev
---
#feedback
int32 sample
float32 data
float32 mean
float32 std_dev
手动生成消息
rosrun actionlib_msgs genaction.py -o msg/ action/Averaging.action
在编译过程中自动生成消息文件,在CMakeList.txt中添加
find_package(catkin REQUIRED COMPONENTS actionlib std_msgs message_generation)
add_action_files(DIRECTORY action FILES Averaging.action)
generate_messages(DEPENDENCIES std_msgs actionlib_msgs)
2.编写简单的行为服务器
#include <ros/ros.h>
#include <std_msgs/Float32.h>
#include <actionlib/server/simple_action_server.h>
#include <actionlib_tutorials/AveragingAction.h>
class AveragingAction
{
public:
//在行为构造函数中,创建一个行为服务器
//行为服务器加载一个节点句柄(node handle)、行为名称和可选的回调(executeCB)。
//在本示例中创建的行为服务器不需要回调(executeCB)参数。
//在行为服务器构造之后,等价替换成注册的目标(goal)和抢占(preempt)回调用于行为的构造函数。
AveragingAction(std::string name) :
as_(nh_, name, false),
action_name_(name)
{
//注册目标和反馈回调函数
as_.registerGoalCallback(boost::bind(&AveragingAction::goalCB, this)); as_.registerPreemptCallback(boost::bind(&AveragingAction::preemptCB