RosExample.cpp
Go to the documentation of this file.
00001 
00002 //#include <iostream>
00003 //
00004 //
00005 //#include <boost/thread.hpp>
00006 //#include <deque>
00007 //#include <vector>
00008 //#include <boost/thread/mutex.hpp>
00009 //#include <boost/thread/condition.hpp>
00010 //#include <boost/shared_ptr.hpp>
00011 
00012 
00013 using namespace std;
00014 
00015 #include "SynchCout.h"
00016 
00017 #include "BT.h"
00018 #include "FSM.h"
00019 #include "ROSTask.h"
00020 #include "DecisionMaking.h"
00021 
00022 using namespace decision_making;
00023 
00024 EventQueue* mainEventQueue;
00025 struct RosEventQueueAuto{
00026         RosEventQueueAuto(){mainEventQueue=new RosEventQueue();}
00027         ~RosEventQueueAuto(){delete mainEventQueue;}
00028 };
00029 
00030 //#define X(x) x
00031 //#define PAUSE boost::this_thread::sleep(boost::posix_time::seconds(1.3));
00032 //#define TIMES(N) for(size_t times=0;times<N;times++)
00033 
00034 
00035 //
00036 //FSM(RosTest)
00037 //{
00038 //      enum STATES{
00039 //              run_task,
00040 //              stop
00041 //      }
00042 //      FSM_START(run_task);
00043 //      FSM_BGN
00044 //      {
00045 //              FSM_STATE(run_task)
00046 //              {
00047 //                      CONSTRAINTS(rt,
00048 //                              $ interval 1;
00049 //                              $ fail warn;
00050 //                              core1 = {/scan/ranges[0:1]};
00051 //                              core2 = {/scan/ranges[1:2]};
00052 //                              core3 = {/scan/ranges[2:3]};
00053 //                              core4 = {/scan/ranges[3:4]};
00054 //                              cpu_th = {/scan/ranges[4:5]};
00055 //                              core_th = {/scan/ranges[5:6]};
00056 //                              average = SomeFunction(core1, core2, core3, core4);
00057 //                              average < cpu_th;
00058 //                              core1 < core_th;
00059 //                              core2 < core_th;
00060 //                              core3 < core_th;
00061 //                              core4 < core_th;
00062 //                      );
00063 //                      FSM_CALL_TASK(MYTASK);
00064 //                      FSM_TRANSITIONS
00065 //                      {
00066 //                              FSM_PRINT_EVENT;
00067 //                              FSM_ON_EVENT(/GO, FSM_RISE(GoToStop));
00068 //                              FSM_ON_EVENT(/GO, FSM_NEXT(stop));
00069 //                      }
00070 //              }
00071 //              FSM_STATE(stop)
00072 //              {
00073 //                      FSM_TRANSITIONS
00074 //                      {
00075 //                              FSM_PRINT_EVENT;
00076 //                              FSM_ON_EVENT(/GO, FSM_RISE(GoToRun));
00077 //                              FSM_ON_EVENT(GoToRun, FSM_NEXT(run_task));
00078 //                      }
00079 //              }
00080 //      }
00081 //      FSM_END
00082 //}
00083 
00084 
00085 FSM(RosTestA)
00086 {
00087         FSM_STATES{
00088                 A,
00089                 B,
00090                 C,
00091                 D,
00092                 ERROR
00093         }
00094         FSM_START(A);
00095         FSM_BGN
00096         {
00097                 FSM_STATE(A)
00098                 {
00099                         FSM_CALL_TASK(MYTASK);
00100                         FSM_TRANSITIONS
00101                         {
00102                                 FSM_PRINT_EVENT;
00103                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(ERROR));
00104                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(B));
00105                         }
00106                 }
00107 
00108                 FSM_STATE(B)
00109                 {
00110                         FSM_CALL_TASK(MYTASK);
00111                         FSM_TRANSITIONS
00112                         {
00113                                 FSM_PRINT_EVENT;
00114                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(ERROR));
00115                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(C));
00116                         }
00117                 }
00118 
00119                 FSM_STATE(C)
00120                 {
00121                         FSM_CALL_TASK(MYTASK);
00122                         FSM_TRANSITIONS
00123                         {
00124                                 FSM_PRINT_EVENT;
00125                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(ERROR));
00126                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(D));
00127                         }
00128                 }
00129 
00130                 FSM_STATE(D)
00131                 {
00132                         FSM_CALL_TASK(MYTASK);
00133                         FSM_TRANSITIONS
00134                         {
00135                                 FSM_PRINT_EVENT;
00136                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(ERROR));
00137                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(A));
00138                         }
00139                 }
00140 
00141                 FSM_STATE(ERROR)
00142                 {
00143                         FSM_CALL_TASK(MYTASK);
00144                         FSM_TRANSITIONS
00145                         {
00146                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(A));
00147                         }
00148                 }
00149         }
00150         FSM_END
00151 }
00152 
00153 void run_fsm1(){
00154         FsmRosTestA(0, mainEventQueue);
00155 }
00156 
00157 
00158 
00159 FSM(RosTestB)
00160 {
00161         FSM_STATES{
00162                 SPRINT_START,
00163                 ARI,
00164                 DAN,
00165                 IGOR,
00166                 TOMMY,
00167                 VLAD,
00168                 TALK,
00169                 SPRINT_END
00170         }
00171         FSM_START(SPRINT_START);
00172         FSM_BGN
00173         {
00174                 FSM_STATE(SPRINT_START)
00175                 {
00176                         FSM_CALL_TASK(MYTASK);
00177                         FSM_CALL_TASK(MYTASK1);
00178                         FSM_TRANSITIONS
00179                         {
00180                                 FSM_PRINT_EVENT;
00181                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(TALK));
00182                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(ARI));
00183                         }
00184 
00185                 }
00186 
00187                 FSM_STATE(ARI)
00188                 {
00189                         FSM_TRANSITIONS
00190                         {
00191                                 FSM_PRINT_EVENT;
00192                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(TALK));
00193                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(DAN));
00194                                 FSM_ON_EVENT("/GO", FSM_NEXT(TOMMY));
00195                         }
00196                 }
00197 
00198                 FSM_STATE(DAN)
00199                 {
00200                         FSM_CALL_TASK(MYTASK);
00201                         FSM_TRANSITIONS
00202                         {
00203                                 FSM_PRINT_EVENT;
00204                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(TALK));
00205                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(IGOR));
00206                                 FSM_ON_EVENT("/GO", FSM_NEXT(TOMMY));
00207                         }
00208                 }
00209 
00210                 FSM_STATE(IGOR)
00211                 {
00212                         FSM_CALL_TASK(MYTASK);
00213                         FSM_TRANSITIONS
00214                         {
00215                                 FSM_PRINT_EVENT;
00216                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(TALK));
00217                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(TOMMY));
00218                         }
00219                 }
00220 
00221                 FSM_STATE(TOMMY)
00222                 {
00223                         FSM_CALL_TASK(MYTASK);
00224                         FSM_TRANSITIONS
00225                         {
00226                                 FSM_PRINT_EVENT;
00227                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(TALK));
00228                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(VLAD));
00229                         }
00230                 }
00231 
00232                 FSM_STATE(VLAD)
00233                 {
00234                         FSM_CALL_TASK(MYTASK);
00235                         FSM_TRANSITIONS
00236                         {
00237                                 FSM_PRINT_EVENT;
00238                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(TALK));
00239                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(SPRINT_END));
00240                         }
00241                 }
00242 
00243                 FSM_STATE(TALK)
00244                 {
00245                         FSM_CALL_TASK(MYTASK);
00246                         FSM_TRANSITIONS
00247                         {
00248                                 FSM_PRINT_EVENT;
00249                                 FSM_ON_EVENT("/ERROR", FSM_NEXT(TALK));
00250                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(ARI));
00251                         }
00252                 }
00253 
00254                 FSM_STATE(SPRINT_END)
00255                 {
00256                         FSM_TRANSITIONS
00257                         {
00258                                 FSM_ON_EVENT("/SUCCESS", FSM_NEXT(SPRINT_START));
00259                         }
00260                 }
00261         }
00262         FSM_END
00263 }
00264 
00265 void run_fsm2(){
00266         FsmRosTestB(0, mainEventQueue);
00267 }
00268 
00269 
00270 void run_bt(){
00271 //      BT_ROOT_BGN(RosTest, *mainEventQueue)
00272 //      {
00273 //              BT_CALL_TASK(MYTASK);
00274 //      }
00275 //      BT_END(RosTest) bt;
00276 //      bt.run();
00277 //
00278 //      cout<<"[exit from thread]";
00279 }
00280 
00281 
00282 
00283 
00284 void EVENTS_GENERATOR(){
00285         Event spec[]={"SUCCESS","SUCCESS","SUCCESS","GO","SUCCESS","SUCCESS","SUCCESS","GO", "NOTHING", "SUCCESS", "ERROR", "SUCCESS"};
00286         int i=0;
00287         boost::this_thread::sleep(boost::posix_time::seconds(1));
00288         while(true and ros::ok()){
00289                 Event t = spec[i];
00290                 if(t == "NOTHING"){ i=1; t=spec[0]; }else i++;
00291                 cout << endl << t<<" -> ";
00292                 mainEventQueue->riseEvent(t);
00293                 boost::this_thread::sleep(boost::posix_time::seconds(1));
00294         }
00295         mainEventQueue->close();
00296 }
00297 
00298 TaskResult tst_mytask(std::string task_address, const CallContext& call_ctx, EventQueue& queue){
00299         cout<<" this this my task ";
00300         queue.riseEvent(Event("success", call_ctx));
00301         return TaskResult::SUCCESS();
00302 }
00303 
00304 #include <boost/filesystem.hpp>
00305 
00306 int main(int a, char** aa){
00307 
00308         ros::init(a, aa, "RosExample");
00309         RosEventQueueAuto rosEventQueue;
00310         ros_decision_making_init(a, aa);
00311 
00312         cout<<"Path: "<<boost::filesystem::path(aa[0])<<endl;
00313         cout<<"Name: "<<ros::this_node::getName()<<endl;
00314         cout<<"Namespace: "<<ros::this_node::getNamespace()<<endl;
00315 
00316 
00317         boost::thread_group threads;
00318 
00319         MapResultEvent::map("MYTASK", 0, "success");
00320         LocalTasks::registrate("MYTASK", tst_mytask);
00321         LocalTasks::registrate("MYTASK1", tst_mytask);
00322 
00323         //threads.add_thread(new boost::thread(boost::bind(&run_fsm1)));
00324         threads.add_thread(new boost::thread(boost::bind(&run_fsm2)));
00325         threads.add_thread(new boost::thread(boost::bind(&EVENTS_GENERATOR)));
00326 
00327         ros::spin();
00328         threads.join_all();
00329 
00330 
00331         return 0;
00332 }
00333 
00334 


decision_making
Author(s):
autogenerated on Wed Aug 26 2015 11:16:53