Homepage Demos Overview Downloads Tutorials Reference
Credits

ExploreMachine.h

Go to the documentation of this file.
00001 //-*-c++-*-
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 //! A state machine for exploring an environment (or searching for an object)
00011 class ExploreMachine : public StateNode {
00012 public:
00013   //!constructor
00014   ExploreMachine(StateNode* p=NULL)
00015     : StateNode("ExploreMachine",p), start(NULL), turn(NULL), walkid(MotionManager::invalid_MC_ID)
00016   {
00017     //setRetain(false);
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     //erouter->addListener(this,EventBase::sensorEGID,SensorSourceID::UpdatedSID);
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& /*e*/) {
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; //!< the node to begin within on DoStart() (turn)
00064   WalkNode * turn; //!< walk node to use when turning
00065   MotionManager::MC_ID walkid; //!< we want to share a walk between turning and walking nodes
00066 
00067 private:
00068   ExploreMachine(const ExploreMachine&); //!< don't use
00069   ExploreMachine operator=(const ExploreMachine&); //!< don't use
00070 };
00071 
00072 /*! @file
00073  * @brief Describes ExploreMachine, a state machine for exploring an environment (or searching for an object)
00074  * @author ejt (Creator)
00075  *
00076  * $Author: ejt $
00077  * $Name: tekkotsu-1_5 $
00078  * $Revision: 1.11 $
00079  * $State: Rel $
00080  * $Date: 2003/10/10 17:46:01 $
00081  */
00082 
00083 #endif

Tekkotsu v1.5
Generated Fri Oct 10 15:51:58 2003 by Doxygen 1.3.4