00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
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
00272
00273
00274
00275
00276
00277
00278
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
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