Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _iri_action_server_h_
00026 #define _iri_action_server_h_
00027
00028
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
00342
00343
00344
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
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
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
00414 start_action_callback_(goal);
00415
00416
00417
00418
00419
00420 while(this->as_.isActive() )
00421 {
00422 ROS_DEBUG("IriActionServer::executeCallback::Active!");
00423
00424
00425
00426 if( as_.isPreemptRequested() || !ros::ok() )
00427 {
00428 ROS_DEBUG("IriActionServer::executeCallback::PREEMPTED!");
00429
00430
00431
00432 if(!as_.isNewGoalAvailable())
00433 stop_action_callback_();
00434 as_.setPreempted();
00435 }
00436
00437 else if( is_finished_callback_() )
00438 {
00439 ROS_DEBUG("IriActionServer::executeCallback::FINISH!");
00440
00441
00442
00443 ResultPtr result(new Result);
00444 get_result_callback_(result);
00445
00446
00447 if( has_succeed_callback_() )
00448 {
00449 ROS_DEBUG("IriActionServer::executeCallback::SUCCEED!");
00450
00451 as_.setSucceeded(*result);
00452 }
00453
00454 else
00455 {
00456 ROS_DEBUG("IriActionServer::executeCallback::ABORTED!");
00457
00458 as_.setAborted(*result);
00459 }
00460 }
00461
00462 else
00463 {
00464 ROS_DEBUG("IriActionServer::executeCallback::feedback");
00465
00466 FeedbackPtr feedback(new Feedback);
00467 get_feedback_callback_(feedback);
00468 as_.publishFeedback(*feedback);
00469 }
00470
00471
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