configurable_fibonacci_server.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2008, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  **********************************************************************/
00034 
00035 /* Author: Melonee Wise */
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       // check if the configuration is loaded on the param server
00053       if(!nh_.getParam(action_name_ + "/seed0", seed0_))
00054       {   
00055         // throw an exception if the param is not there and exit the action server
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       //start the action server
00067       as_.start();
00068     }
00069 
00070     ~FibonacciAction(void)
00071     {
00072     }
00073 
00074     void executeCB(const actionlib_tutorials::FibonacciGoalConstPtr &goal)
00075     {
00076       // create the action messages that will use published for feeback and result
00077       actionlib_tutorials::FibonacciFeedback feedback;
00078       actionlib_tutorials::FibonacciResult result;
00079 
00080       // helper variables
00081       ros::Rate r(50); 
00082       std::vector<int> sequence;
00083       int temp;  
00084       bool success = true;
00085 
00086       // push_back the seeds for the fibonacci sequence
00087       sequence.push_back(seed0_);
00088       sequence.push_back(seed1_);
00089 
00090       // publish some info to the console for the user to see when the action starts
00091             ROS_INFO("%s: Executing, creating fibonacci sequence of order %i with seeds %i, %i", action_name_.c_str(), goal->order, sequence[0], sequence[1]);
00092           
00093       // start executing the action
00094       for(int i=1; i<=goal->order; i++)
00095       {        
00096         // check to make sure that preempt has not been requested by the client
00097         if (as_.isPreemptRequested() || !ros::ok())
00098               {
00099           ROS_INFO("%s: Preempted", action_name_.c_str());
00100           // set the action state to preempted
00101           as_.setPreempted();
00102           success = false;
00103           break;
00104         }
00105         temp = sequence[i] + sequence[i-1];
00106         sequence.push_back(temp);
00107         for(std::vector<int>::const_iterator it = sequence.begin();
00108             it != sequence.end();
00109             ++it)
00110           feedback.sequence.push_back(*it);
00111         // publish the feedback 
00112         as_.publishFeedback(feedback);
00113         // this sleep is not necessary, it is just to slow down the action for demonstration purposes
00114         r.sleep(); 
00115       }
00116 
00117       if(success)
00118       {
00119         for(std::vector<int>::const_iterator it = sequence.begin();
00120             it != sequence.end();
00121             ++it)
00122           result.sequence.push_back(*it);
00123         ROS_INFO("%s: Succeeded", action_name_.c_str());
00124         // set the action state to succeeded
00125         as_.setSucceeded(result);
00126       }
00127     }
00128 
00129 protected:
00130     
00131     ros::NodeHandle nh_;
00132     actionlib::SimpleActionServer<actionlib_tutorials::FibonacciAction> as_;
00133     std::string action_name_;
00134     int seed0_, seed1_;
00135 };
00136 
00137 
00138 int main(int argc, char** argv)
00139 {
00140     ros::init(argc, argv, "fibonacci");
00141 
00142     try
00143     {
00144             FibonacciAction fibonacci(ros::this_node::getName());
00145             ros::spin();
00146     }
00147     catch(std::string str) 
00148     {
00149       ROS_ERROR("%s",str.c_str());
00150     }
00151     return 0;
00152 }


actionlib_tutorials
Author(s): Melonee Wise
autogenerated on Thu Jan 2 2014 11:09:44