00001
00002 #ifndef INCLUDED_ExploreMachine_h_
00003 #define INCLUDED_ExploreMachine_h_
00004
00005 #include "Behaviors/StateNode.h"
00006 #include "Behaviors/Nodes/WalkNode.h"
00007 #include "Behaviors/Transitions/SmoothCompareTrans.h"
00008 #include <stdlib.h>
00009
00010
00011 class ExploreMachine : public StateNode {
00012 public:
00013
00014 ExploreMachine(StateNode* p=NULL)
00015 : StateNode("ExploreMachine",p), start(NULL), turn(NULL), walkid(MotionManager::invalid_MC_ID)
00016 {
00017
00018 }
00019
00020 virtual void setup() {
00021 SharedObject<WalkMC> walk;
00022 walkid=motman->addMotion(walk);
00023 WalkNode * move=NULL;
00024 start=addNode(turn=new WalkNode(0,0,0.5f,this));
00025 turn->setName(getName()+"::turn");
00026 addNode(move=new WalkNode(150,0,0,this));
00027 move->setName(getName()+"::move");
00028 turn->addTransition(new TimeOutTrans(turn,move,2000));
00029 move->addTransition(new SmoothCompareTrans<float>(move,turn,&state->sensors[IRDistOffset],CompareTrans<float>::LT,350,EventBase(EventBase::sensorEGID,SensorSourceID::UpdatedSID,EventBase::statusETID),.7));
00030 turn->setWalkID(walkid);
00031 move->setWalkID(walkid);
00032 StateNode::setup();
00033 }
00034
00035 virtual void DoStart() {
00036 StateNode::DoStart();
00037 start->DoStart();
00038
00039 erouter->addListener(this,EventBase::stateMachineEGID,(unsigned int)turn);
00040 }
00041
00042 virtual void DoStop() {
00043 erouter->forgetListener(this);
00044 StateNode::DoStop();
00045 }
00046
00047 virtual void teardown() {
00048 motman->removeMotion(walkid);
00049 StateNode::teardown();
00050 }
00051
00052 virtual void processEvent(const EventBase& ) {
00053 sout->printf("IR: %g\n",state->sensors[IRDistOffset]);
00054 float vel=rand()/(float)RAND_MAX*2.0f-1;
00055 if(vel<0)
00056 vel-=.25;
00057 if(vel>0)
00058 vel+=.25;
00059 turn->setAVelocity(vel);
00060 }
00061
00062 protected:
00063 StateNode * start;
00064 WalkNode * turn;
00065 MotionManager::MC_ID walkid;
00066
00067 private:
00068 ExploreMachine(const ExploreMachine&);
00069 ExploreMachine operator=(const ExploreMachine&);
00070 };
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 #endif