Go to the documentation of this file.00001 #include <ros/ros.h>
00002 #include <actionlib/client/simple_action_client.h>
00003 #include <actionlib/client/terminal_state.h>
00004
00005 #include <robot_task/RobotTask.h>
00006 #include <robot_task/RobotTaskAction.h>
00007 using namespace std;
00008 using namespace robot_task;
00009
00010 typedef actionlib::SimpleActionClient<RobotTaskAction> Client;
00011
00012
00013 std::string find(std::vector<string>& s, std::string key){
00014 for(size_t i=0;i<s.size();i++){
00015 if(s[i].find(key)==0) return s[i];
00016 }
00017 return "";
00018 }
00019 std::string value(std::vector<string>& s, std::string key){
00020 for(size_t i=0;i<s.size();i++){
00021 if(s[i].find(key)==0) return s[i].substr(s[i].find('=')+1);
00022 }
00023 return "";
00024 }
00025 template<class A> A cast(std::string name){
00026 std::stringstream n;n<<name;
00027 A a; n>>a;
00028 return a;
00029 }
00030
00031
00032 int main (int argc, char **argv)
00033 {
00034 if(argc<0) return 1;
00035 std::string tname (argv[1]);
00036
00037 if(tname=="-h" || tname=="--help"){
00038 cout<<"Robot task tester"<<endl;
00039 cout<<" calls robot task with special parameters"<<endl;
00040 cout<<"Syntax: task_tester [TASK_NAME] [ARG1=VALUE] [ARG2=VALUE] ..."<<endl;
00041 cout<<"Args:"<<endl;
00042 cout<<" time=INTEGER : time (in sec) for waiting answer from task before abort it. if -1, wait forevere"<<endl;
00043 cout<<" arg=ARGS_LIST : arguments for task call. ARGS_LIST example: x=10,y=12,z=11"<<endl;
00044 return 0;
00045 }
00046
00047 ROS_INFO("Test caller for task [%s] is started.", tname.c_str());
00048 stringstream sname ; sname << "task_tester"<<"_"<<tname<<"_"<<::time(NULL);
00049 ros::init(argc, argv, sname.str().c_str());
00050
00051
00052
00053 Client ac(tname.c_str(), true);
00054
00055 std::vector<string> params;
00056 if(argc>2){
00057 for(int i=2; i<argc; i++){
00058 params.push_back(argv[i]);
00059 }
00060 }
00061
00062
00063 ROS_INFO("Waiting for action server to start task %s.", tname.c_str());
00064
00065
00066
00067 bool serverFound = false;
00068 while( !serverFound && ros::ok() ){
00069 ac.waitForServer(ros::Duration(1.0));
00070 if(ac.isServerConnected()){
00071 ROS_INFO("... Task is found.");
00072 serverFound = true;
00073 }else{
00074 ROS_INFO("... Task not found, retry...");
00075 }
00076 }
00077
00078
00079 RobotTaskGoal goal;
00080 goal.name = tname;
00081 stringstream suid; suid<<"task_id_tested_"<<::time(NULL);
00082 goal.uid = suid.str();
00083 goal.parameters="";
00084 if(find(params,"arg=").size()!=0)
00085 goal.parameters = value(params, "arg=");
00086 else if(find(params,"args=").size()!=0)
00087 goal.parameters = value(params, "args=");
00088
00089 ROS_INFO("Sending goal.");
00090 ROS_INFO(" name=%s", goal.name.c_str());
00091 ROS_INFO(" uid=%s", goal.uid.c_str());
00092 ROS_INFO(" parameters=%s", goal.parameters.c_str());
00093
00094 ac.sendGoal(goal);
00095 ROS_INFO("... Goal sent.");
00096
00097
00098 ROS_INFO("Wait for result.");
00099
00100 int time = -1;
00101 if( find(params, "time=").size()>0 ){
00102 time = cast<int>(value(params, "time="));
00103 }
00104 bool finished = false;
00105 while(time!=0 && ros::ok()){
00106 time -- ;
00107
00108 ROS_INFO("... Wait for 1 sec");
00109
00110 bool finished_before_timeout = ac.waitForResult(ros::Duration(1.0));
00111
00112 if (finished_before_timeout)
00113 {
00114 actionlib::SimpleClientGoalState state = ac.getState();
00115 ROS_INFO("... Task finished: %s",state.toString().c_str());
00116 finished = true;
00117 if(
00118 state==actionlib::SimpleClientGoalState::SUCCEEDED ||
00119 state==actionlib::SimpleClientGoalState::PREEMPTED ||
00120 state==actionlib::SimpleClientGoalState::ABORTED
00121 ){
00122 ROS_INFO("... ... Task result is : ");
00123 ROS_INFO("... ... ... result=%i", ac.getResult()->success);
00124 ROS_INFO("... ... ... desc=%s", ac.getResult()->description.c_str());
00125 ROS_INFO("... ... ... plan=%s", ac.getResult()->plan.c_str());
00126 }
00127 break;
00128 }
00129 else
00130 ROS_INFO("... ... Task did not finish before the time out. retry...");
00131 }
00132
00133 if(!finished && ros::ok()){
00134 ROS_INFO("Abort task.");
00135 ac.cancelGoal();
00136 }
00137
00138 ROS_INFO("Exit from task tester.");
00139 return 0;
00140 }