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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <sstream>
00037
00038 #include <ros/ros.h>
00039 #include <actionlib/server/simple_action_server.h>
00040 #include <actionlib_tutorials/FibonacciAction.h>
00041
00042 class FibonacciAction
00043 {
00044 public:
00045
00046 FibonacciAction(std::string name) :
00047 as_(nh_, name, boost::bind(&FibonacciAction::executeCB, this, _1), false),
00048 action_name_(name)
00049 {
00050
00051 std::stringstream ss;
00052
00053 if(!nh_.getParam(action_name_ + "/seed0", seed0_))
00054 {
00055
00056 ss << action_name_.c_str() << ": Aborted, seed0 param was not set.";
00057 throw ss.str();
00058 }
00059
00060 if(!nh_.getParam(action_name_ + "/seed1", seed1_))
00061 {
00062 ss << action_name_.c_str() << ": Aborted, seed1 param was not set.";
00063 throw ss.str();
00064 }
00065
00066
00067 as_.start();
00068 }
00069
00070 ~FibonacciAction(void)
00071 {
00072 }
00073
00074 void executeCB(const actionlib_tutorials::FibonacciGoalConstPtr &goal)
00075 {
00076
00077
00078 ros::Rate r(1);
00079 std::vector<int> sequence;
00080 int temp;
00081 bool success = true;
00082
00083
00084 feedback_.sequence.clear();
00085 feedback_.sequence.push_back(seed0_);
00086 feedback_.sequence.push_back(seed1_);
00087
00088
00089 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]);
00090
00091
00092 for(int i=1; i<=goal->order; i++)
00093 {
00094
00095 if (as_.isPreemptRequested() || !ros::ok())
00096 {
00097 ROS_INFO("%s: Preempted", action_name_.c_str());
00098
00099 as_.setPreempted();
00100 success = false;
00101 break;
00102 }
00103 feedback_.sequence.push_back(feedback_.sequence[i] + feedback_.sequence[i-1]);
00104
00105 as_.publishFeedback(feedback_);
00106
00107 r.sleep();
00108 }
00109
00110 if(success)
00111 {
00112 result_.sequence = feedback_.sequence;
00113 ROS_INFO("%s: Succeeded", action_name_.c_str());
00114
00115 as_.setSucceeded(result_);
00116 }
00117 }
00118
00119 protected:
00120
00121 ros::NodeHandle nh_;
00122 actionlib::SimpleActionServer<actionlib_tutorials::FibonacciAction> as_;
00123 std::string action_name_;
00124
00125 actionlib_tutorials::FibonacciFeedback feedback_;
00126 actionlib_tutorials::FibonacciResult result_;
00127 int seed0_, seed1_;
00128 };
00129
00130
00131 int main(int argc, char** argv)
00132 {
00133 ros::init(argc, argv, "fibonacci");
00134
00135 try
00136 {
00137 FibonacciAction fibonacci(ros::this_node::getName());
00138 ros::spin();
00139 }
00140 catch(std::string str)
00141 {
00142 ROS_ERROR(str.c_str());
00143 }
00144 return 0;
00145 }