iri_action_server.h
Go to the documentation of this file.
00001 // Copyright (C) 2010-2011 Institut de Robotica i Informatica Industrial, CSIC-UPC.
00002 // Author Joan Perez & Sergi Hernandez
00003 // All rights reserved.
00004 //
00005 // This file is part of iri-ros-pkg
00006 // iri-ros-pkg is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public License
00017 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 // 
00019 // IMPORTANT NOTE: This code has been generated through a script from the 
00020 // iri_ros_scripts. Please do NOT delete any comments to guarantee the correctness
00021 // of the scripts. ROS topics can be easly add by using those scripts. Please
00022 // refer to the IRI wiki page for more information:
00023 // http://wikiri.upc.es/index.php/Robotics_Lab
00024 
00025 #ifndef _iri_action_server_h_
00026 #define _iri_action_server_h_
00027 
00028 // ros action server
00029 #include <actionlib/server/simple_action_server.h>
00030 #include <actionlib/action_definition.h>
00031 
00047 template <class ActionSpec>
00048 class IriActionServer
00049 {
00050   public:
00057     ACTION_DEFINITION(ActionSpec);
00058 
00064     typedef boost::shared_ptr<Result> ResultPtr;
00065 
00071     typedef boost::shared_ptr<Feedback> FeedbackPtr;
00072     
00073   private:
00074   
00084     boost::function<void (const GoalConstPtr&)> start_action_callback_;
00085 
00095     boost::function<void ()> stop_action_callback_;
00096 
00108     boost::function<bool ()> is_finished_callback_;
00109 
00121     boost::function<bool ()> has_succeed_callback_;
00122     
00133     boost::function<void (ResultPtr&)> get_result_callback_;
00134     
00145     boost::function<void (FeedbackPtr&)> get_feedback_callback_;
00146     
00160     void executeCallback(const GoalConstPtr& goal);
00161 
00167     std::string action_name_;
00168     
00176     actionlib::SimpleActionServer<ActionSpec> as_;
00177 
00184     ros::Rate loop_rate_;
00185 
00192     bool is_started;
00193 
00194   public:
00204     IriActionServer(ros::NodeHandle & nh, const std::string & action_name);
00205 
00212     ~IriActionServer(void);
00213     
00220     void start(void);
00221 
00227     bool isStarted(void) const;
00228    
00234     void shutdown(void);
00235 
00244     void setAborted(const Result& result = Result(), const std::string& text = std::string(""));
00245 
00254     void setPreempted(const Result& result = Result(), const std::string& text = std::string(""));
00255 
00262     bool isActive(void);
00263 
00270     void setLoopRate(ros::Rate r);
00271 
00279     void registerStartCallback(boost::function<void (const GoalConstPtr&)> cb);
00280     
00288     void registerStopCallback(boost::function<void ()> cb);
00289     
00297     void registerIsFinishedCallback( boost::function<bool ()> cb);
00298     
00306     void registerHasSucceedCallback( boost::function<bool ()> cb);
00307     
00315     void registerGetResultCallback(boost::function<void (ResultPtr&)> cb);
00316     
00324     void registerGetFeedbackCallback(boost::function<void (FeedbackPtr&)> cb);
00325     
00326 };
00327 
00328 template <class ActionSpec>
00329 IriActionServer<ActionSpec>::IriActionServer(ros::NodeHandle & nh, const std::string & aname) :
00330   action_name_(aname),
00331   as_(nh, action_name_, boost::bind(&IriActionServer<ActionSpec>::executeCallback, this, _1), false),
00332   loop_rate_(10),
00333   is_started(false)
00334 {
00335   ROS_DEBUG("IriActionServer::Constructor");
00336 }
00337 
00338 template <class ActionSpec>
00339 IriActionServer<ActionSpec>::~IriActionServer(void)
00340 {
00341 //   std::cout << "IriActionServer::Destructor:: active=" << as_.isActive() << std::endl;
00342 //   if( as_.isActive() )
00343 //     as_.setAborted();
00344 //   as_.shutdown();
00345   ROS_DEBUG("IriActionServer::Destructor");
00346 }
00347 
00348 template <class ActionSpec>
00349 void IriActionServer<ActionSpec>::setLoopRate(ros::Rate r)
00350 {
00351   loop_rate_ = r;
00352 }
00353 
00354 template <class ActionSpec>
00355 void IriActionServer<ActionSpec>::start(void)
00356 {
00357   ROS_DEBUG("IriActionServer::Start");
00358 
00359   // check if all callbacks have been registered
00360   if( start_action_callback_ && 
00361       stop_action_callback_  &&
00362       is_finished_callback_  && 
00363       has_succeed_callback_  && 
00364       get_result_callback_   && 
00365       get_feedback_callback_ )
00366   {
00367     // launch action server
00368     as_.start();
00369     is_started = true;
00370   }
00371   else
00372     ROS_FATAL("Some Callbacks have not been registered yet!");
00373 }
00374 
00375 template <class ActionSpec>
00376 bool IriActionServer<ActionSpec>::isStarted(void) const
00377 {
00378   return is_started;
00379 }
00380 
00381 template <class ActionSpec>
00382 void IriActionServer<ActionSpec>::shutdown(void)
00383 {
00384   as_.shutdown();
00385   is_started = false;
00386 }
00387 
00388 template <class ActionSpec>
00389 void IriActionServer<ActionSpec>::setAborted(const Result& result, const std::string& text)
00390 {
00391   as_.setAborted(result, text);
00392 }
00393 
00394 template <class ActionSpec>
00395 void IriActionServer<ActionSpec>::setPreempted(const Result& result, const std::string& text)
00396 {
00397   as_.setPreempted(result, text);
00398 }
00399 
00400 template <class ActionSpec>
00401 bool IriActionServer<ActionSpec>::isActive(void)
00402 {
00403   return as_.isActive();
00404 }
00405 
00406 template <class ActionSpec>
00407 void IriActionServer<ActionSpec>::executeCallback(const GoalConstPtr& goal)
00408 {
00409   ROS_DEBUG("IriActionServer::executeCallback::start");
00410   
00411   try
00412   {
00413     // init action
00414     start_action_callback_(goal);
00415 
00416     // boolean to control action status
00417     //bool is_active = true;
00418 
00419     // while action is active
00420     while(this->as_.isActive() )
00421     {
00422       ROS_DEBUG("IriActionServer::executeCallback::Active!");
00423       
00424       // check if preempt has been requested
00425       // and if ROS connection is OK
00426       if( as_.isPreemptRequested() || !ros::ok() )
00427       {
00428         ROS_DEBUG("IriActionServer::executeCallback::PREEMPTED!");
00429         //is_active = false;
00430 
00431         // stop action
00432         if(!as_.isNewGoalAvailable())
00433           stop_action_callback_();
00434         as_.setPreempted();
00435       }
00436       // check if action has finished
00437       else if( is_finished_callback_() )
00438       {
00439         ROS_DEBUG("IriActionServer::executeCallback::FINISH!");
00440         //is_active = false;
00441 
00442         // get action result
00443         ResultPtr result(new Result);
00444         get_result_callback_(result);
00445 
00446         // action has been successfully accomplished
00447         if( has_succeed_callback_() )
00448         {
00449           ROS_DEBUG("IriActionServer::executeCallback::SUCCEED!");
00450           // set action as succeeded
00451           as_.setSucceeded(*result); 
00452         }
00453         // action needs to be aborted
00454         else
00455         {
00456           ROS_DEBUG("IriActionServer::executeCallback::ABORTED!");
00457           // set action as aborted
00458           as_.setAborted(*result); 
00459         }
00460       }
00461       // action has not finished yet
00462       else
00463       {
00464         ROS_DEBUG("IriActionServer::executeCallback::feedback");
00465         // get action feedback and publish it
00466         FeedbackPtr feedback(new Feedback);
00467         get_feedback_callback_(feedback);
00468         as_.publishFeedback(*feedback); 
00469       }
00470       
00471       // force loop rate's frequency if convenient 
00472       loop_rate_.sleep();
00473     }
00474   
00475   }
00476   catch(std::exception &e)
00477   {
00478     std::cout << e.what() << std::endl;
00479     as_.setAborted(Result(), "something went wrong!");
00480   }
00481 }
00482 
00483 template <class ActionSpec>
00484 void IriActionServer<ActionSpec>::registerStartCallback(boost::function<void (const GoalConstPtr&)> cb)
00485 {
00486   start_action_callback_ = cb;
00487 }
00488 
00489 template <class ActionSpec>
00490 void IriActionServer<ActionSpec>::registerStopCallback(boost::function<void ()> cb)
00491 {
00492   stop_action_callback_ = cb;
00493 }
00494 
00495 template <class ActionSpec>
00496 void IriActionServer<ActionSpec>::registerIsFinishedCallback( boost::function<bool ()> cb)
00497 {
00498   is_finished_callback_ = cb;
00499 }
00500 
00501 template <class ActionSpec>
00502 void IriActionServer<ActionSpec>::registerHasSucceedCallback( boost::function<bool ()> cb)
00503 {
00504   has_succeed_callback_ = cb;
00505 }
00506 
00507 template <class ActionSpec>
00508 void IriActionServer<ActionSpec>::registerGetResultCallback(boost::function<void (ResultPtr&)> cb)
00509 {
00510   get_result_callback_ = cb;
00511 }
00512 
00513 template <class ActionSpec>
00514 void IriActionServer<ActionSpec>::registerGetFeedbackCallback(boost::function<void (FeedbackPtr&)> cb)
00515 {
00516   get_feedback_callback_ = cb;
00517 }
00518 
00519 #endif


iri_action_server
Author(s): Joan Perez, jnperez at iri.upc.edu
autogenerated on Fri Dec 6 2013 19:58:20