DummyTask.cpp
Go to the documentation of this file.
00001 #ifndef __DUMMYTASK__HPP
00002 #define __DUMMYTASK__HPP
00003 
00004 #include <actionlib/server/simple_action_server.h>
00005 #include <robot_task/RobotTask.h>
00006 #include <robot_task/RobotTaskAction.h>
00007 using namespace std;
00008 using namespace robot_task;
00009 
00010 ostream& operator<<(ostream& o, std::vector<string>& s){
00011         size_t s_size=s.size();
00012         if(s_size>0){
00013                 for(size_t i=0;i<s_size-1;i++)
00014                         o<<s[i]<<',';
00015                 return o<<s[s.size()-1];
00016         }
00017         return o;
00018 }
00019 
00020 class DummyTaskServer: public RobotTask {
00021 protected:
00022 
00023         std::vector<string> params;
00024         
00025         int time;
00026         int retValue;
00027         string outputstr;
00028         
00029 public:
00030     DummyTaskServer(std::string name, std::vector<string> par):
00031         RobotTask(name), params(par)
00032     {
00033                 std::cout<<"params: "<<params<<std::endl;
00034                 
00035                 time = -1;
00036                 if( find(params, "time=").size()>0 ){
00037                         time = cast<int>(value(params,"time="));
00038                 }
00039                 
00040                 retValue = 0;
00041                 if( find(params, "return=").size()>0 ){
00042                         retValue = cast<int>(value(params,"return="));
00043                 }
00044                 
00045                 outputstr="process...";
00046                 if( find(params, "print=").size()>0 ){
00047                         outputstr = value(params,"print=");
00048                 }
00049     }
00050 
00051     bool exists(Arguments& args, std::string name){
00052                 return args.find(name) != args.end();
00053         }
00054         
00055         std::string find(std::vector<string>& s, std::string key){
00056                 for(size_t i=0;i<s.size();i++){
00057                         if(s[i].find(key)==0) return s[i];
00058                 }
00059                 return "";
00060         }
00061         std::string value(std::vector<string>& s, std::string key){
00062                 for(size_t i=0;i<s.size();i++){
00063                         if(s[i].find(key)==0) return s[i].substr(s[i].find('=')+1);
00064                 }
00065                 return "";
00066         }
00067         template<class A> A cast(std::string name){
00068                 std::stringstream n;n<<name;
00069                 A a; n>>a;
00070                 return a;
00071         }       
00072  
00073         template<class A> A cast(Arguments& args, std::string name){
00074                 std::stringstream n;n<<args[name];
00075                 A a; n>>a;
00076                 return a;
00077         }
00078     
00079     TaskResult task(const string& name, const string& uid, Arguments& args) {
00080                 int time = this->time;
00081                 
00082                 while(time != 0) {
00083                         time -- ;
00084                         
00085                         if(isPreempt()){
00086                                 
00087                                 return TaskResult::PREEMPTED();
00088                         }
00089                         
00090                         if(outputstr!="no_print"){
00091                                 ROS_INFO("%s: %s", _name.c_str(), outputstr.c_str());
00092                         }
00093                         sleep(1);
00094                 }
00095                 
00096         
00097         //return TaskResult::Preempted();
00098         if( retValue > 0 ){
00099                         return TaskResult(retValue, "ERROR");
00100                 }
00101         return TaskResult(SUCCESS, "OK");
00102     }
00103 
00104 };
00105 #endif
00106 
00107 
00108 #include "ros/ros.h"
00109 
00110 int main(int argc, char **argv)
00111 {
00112         if(argc<0) return 1;
00113         std::string tname (argv[1]);
00114         
00115         if(tname=="-h" || tname=="--help"){
00116                 cout<<"Dummy Robot Task for testing"<<endl;
00117                 cout<<"   run empty robot task with any name"<<endl;
00118                 cout<<"Syntax: dummy [TASK_NAME] [ARG1=VALUE] [ARG2=VALUE] ..."<<endl;
00119                 cout<<"Args:"<<endl;
00120                 cout<<"    time=INTEGER    : time (in millisec) for task running. if -1, wait forevere"<<endl;
00121                 cout<<"    return=INTEGER  : return value of task: 0 is OK, -1 is PLAN, 0< is a ERROR CODE "<<endl;
00122                 cout<<"    print=STRING    : print progress text. default is 'process...' and 'no_print' suppress printing."<<endl;
00123                 return 0;
00124         }
00125         
00126         stringstream snodename; snodename<<"DummyTaskNode_"<<time(NULL);
00127         ROS_INFO("Start %s with Task %s", snodename.str().c_str(),argv[1]);
00128         
00129         ros::init(argc, argv, snodename.str().c_str());
00130         ros::NodeHandle node;
00131         
00132         ROS_INFO("craete task");
00133         
00134 
00135         std::vector<string> params;
00136         if(argc>2){
00137                 for(size_t i=2; i<argc; i++){
00138                         params.push_back(argv[i]);
00139                 }
00140         }
00141         DummyTaskServer task(tname, params);
00142 
00143         ros::spin();
00144 
00145         return 0;
00146 }


robot_task
Author(s):
autogenerated on Wed Aug 26 2015 11:16:50