Go to the documentation of this file.00001
00002 #include <ros/ros.h>
00003 #include <decision_making/SynchCout.h>
00004 #include <decision_making/BT.h>
00005 #include <decision_making/FSM.h>
00006 #include <decision_making/ROSTask.h>
00007 #include <decision_making/DecisionMaking.h>
00008
00009 using namespace std;
00010 using namespace decision_making;
00011
00012 EventQueue* mainEventQueue;
00013
00014 FSM(Turnstile)
00015 {
00016 FSM_STATES
00017 {
00018 Locked,
00019 Unlocked
00020 }
00021 FSM_START(Locked);
00022 FSM_BGN
00023 {
00024 FSM_STATE(Locked)
00025 {
00026 FSM_CALL_TASK(MYTASK);
00027 FSM_ON_STATE_EXIT_BGN
00028 {
00029 FSM_CALL_TASK(ONEXIT);
00030 }
00031 FSM_ON_STATE_EXIT_END
00032
00033 FSM_TRANSITIONS
00034 {
00035 FSM_PRINT_EVENT
00036 FSM_ON_EVENT(/COIN, FSM_NEXT(Unlocked));
00037 FSM_ON_EVENT(/PUSH, FSM_NEXT(Locked));
00038 }
00039 }
00040 FSM_STATE(Unlocked)
00041 {
00042 FSM_TRANSITIONS
00043 {
00044 FSM_PRINT_EVENT
00045 FSM_ON_EVENT(/COIN, FSM_NEXT(Unlocked));
00046 FSM_ON_EVENT(/PUSH, FSM_NEXT(Locked));
00047 }
00048 }
00049 }
00050 FSM_END
00051 }
00052
00053 class PP: public CallContextParameters{
00054 public:
00055 int x;
00056 virtual std::string str()const{ std::stringstream s; s<<x; return s.str();};
00057 };
00058 void run_fsm(){
00059 CallContext ct;
00060 ct.createParameters<PP>();
00061 ct.parameters<PP>().x =10;
00062 FsmTurnstile(&ct, mainEventQueue);
00063 }
00064
00065
00066 void EVENTS_GENERATOR(){
00067 Event spec[]={"COIN","COIN","PUSH","PUSH", "NOTHING"};
00068 int i=0;
00069 boost::this_thread::sleep(boost::posix_time::seconds(1));
00070 while(true and ros::ok()){
00071 Event t = spec[i];
00072 if(t == "NOTHING"){ i=1; t=spec[0]; }else i++;
00073 cout << endl << t<<" -> ";
00074 mainEventQueue->riseEvent(t);
00075 boost::this_thread::sleep(boost::posix_time::seconds(1));
00076 }
00077 mainEventQueue->close();
00078 }
00079
00080 class MMM{
00081 int aa;
00082 public:
00083 MMM():aa(1){}
00084 TaskResult tst_mytask(std::string task_address, const CallContext& call_ctx, EventQueue& queue, int ww){
00085 cout<<endl<<"[ this my dummy task("<<task_address<<") : x="<<call_ctx.parameters<PP>().x<<", ww="<<ww<<", aa="<<aa<<" ]"<<endl;
00086 call_ctx.parameters<PP>().x = call_ctx.parameters<PP>().x + 1;
00087 aa+=1;
00088
00089 queue.riseEvent(Event("success", call_ctx));
00090 return TaskResult::SUCCESS();
00091 }
00092 TaskResult tst_onexit(std::string task_address, const CallContext& call_ctx, EventQueue& queue, int ww){
00093 cout<<endl<<"[ on exit task("<<task_address<<") : x="<<call_ctx.parameters<PP>().x<<", ww="<<ww<<", aa="<<aa<<" ]"<<endl;
00094 call_ctx.parameters<PP>().x = call_ctx.parameters<PP>().x + 1;
00095 aa+=1;
00096
00097 queue.riseEvent(Event("success", call_ctx));
00098 return TaskResult::SUCCESS();
00099 }
00100
00101 };
00102
00103 #include <boost/filesystem.hpp>
00104
00105 int main(int a, char** aa){
00106
00107 ros::init(a, aa, "RosExample");
00108 ros_decision_making_init(a, aa);
00109 mainEventQueue = new RosEventQueue();
00110
00111 boost::thread_group threads;
00112
00113 MapResultEvent::map("MYTASK", 0, "success");
00114
00115 MMM mmm;
00116 LocalTasks::Function f = boost::bind(&MMM::tst_mytask, &mmm, _1, _2, _3, 15);
00117 LocalTasks::Function f_onexit = boost::bind(&MMM::tst_onexit, &mmm, _1, _2, _3, 15);
00118 LocalTasks::registrate("MYTASK", f);
00119 LocalTasks::registrate("ONEXIT", f_onexit);
00120
00121 threads.add_thread(new boost::thread(boost::bind(&run_fsm)));
00122 threads.add_thread(new boost::thread(boost::bind(&EVENTS_GENERATOR)));
00123
00124 ros::spin();
00125 threads.join_all();
00126
00127 delete mainEventQueue;
00128 return 0;
00129 }
00130
00131