码迷,mamicode.com
首页 > 其他好文 > 详细

Writing a Simple Action Server using the Execute Callback

时间:2014-12-08 12:30:09      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:actionlib

fibonacci_server.cpp

/*
黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21、……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*)
*/
#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>//action lib
#include <learning_actionlib/FibonacciAction.h>//从Fibonacci.action中生成的头文件,FibonacciAction.msg 自动生成的

class FibonacciAction
{
protected:

  ros::NodeHandle nh_;//句柄
  // NodeHandle instance must be created before this line. Otherwise strange error may occur.
  actionlib::SimpleActionServer<learning_actionlib::FibonacciAction> as_; 
  std::string action_name_;
  // create messages that are used to published feedback/result
  learning_actionlib::FibonacciFeedback feedback_;
  learning_actionlib::FibonacciResult result_;

public:

  FibonacciAction(std::string name) :
    as_(nh_, name, boost::bind(&FibonacciAction::executeCB, this, _1), false),
    action_name_(name)
  {
    as_.start();
  }//
 

  ~FibonacciAction(void)
  {
  }

  void executeCB(const learning_actionlib::FibonacciGoalConstPtr &goal)
  {
    // helper variables
    ros::Rate r(1);
    bool success = true;

    // push_back the seeds for the fibonacci sequence
    feedback_.sequence.clear();
    feedback_.sequence.push_back(0);//在容器里添加相应的元素
    feedback_.sequence.push_back(1);

    // publish info to the console for the user,发布信息到控制台的用户
    ROS_INFO("%s: Executing, creating fibonacci sequence of order %i with seeds %i, %i", action_name_.c_str(), goal->order, feedback_.sequence[0], feedback_.sequence[1]);//创建的操作的内部。

    // start executing the action
    for(int i=1; i<=goal->order; i++)
    {
      // check that preempt has not been requested by the client
      if (as_.isPreemptRequested() || !ros::ok())
      {
        ROS_INFO("%s: Preempted", action_name_.c_str());
        // set the action state to preempted
        as_.setPreempted();//setPreempted(),这标志着该行动已经抢占了用户的要求。
        success = false;
        break;
      }//动作服务器的一个重要组成部分是允许的动作的客户端请求将当前的目标执行被取消的能力
      
      
      feedback_.sequence.push_back(feedback_.sequence[i] + feedback_.sequence[i-1]);
      // publish the feedback
      as_.publishFeedback(feedback_);
      // this sleep is not necessary, the sequence is computed at 1 Hz for demonstration purposes
      r.sleep();
    }

    if(success)
    {
      result_.sequence = feedback_.sequence;
      ROS_INFO("%s: Succeeded", action_name_.c_str());
      // set the action state to succeeded
      as_.setSucceeded(result_);
    }
  }


};


int main(int argc, char** argv)
{
  ros::init(argc, argv, "fibonacci");

  FibonacciAction fibonacci(ros::this_node::getName());
  ros::spin();

  return 0;
}


bubuko.com,布布扣

本教程介绍使用simple_action_server库创建一个斐波那契动作服务器本实施例的动作的服务器产生一个Fibonacci序列,目标是该序列的顺序中,反馈是序列,因为它是计算的,其结果是最后的序列。

Writing a Simple Action Server using the Execute Callback

标签:actionlib

原文地址:http://blog.csdn.net/dxuehui/article/details/41800513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!