Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

WorldStateVelDaemon.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_WorldStateVelDaemon_h_
00003 #define INCLUDED_WorldStateVelDaemon_h_
00004 
00005 #include "Behaviors/BehaviorBase.h"
00006 #include "Events/EventRouter.h"
00007 #include "Events/LocomotionEvent.h"
00008 #include "Shared/WorldState.h"
00009 #include "Events/EventTrapper.h"
00010 
00011 //! Listens for LocomotionEvents and updates the velocity fields of WorldState
00012 /*! If we get multiple ways of locomoting, this would be a good place
00013  *  to manage them to determine the actual final velocity.
00014  *
00015  *  Right now it'll correctly handle one (or more i suppose) e-stops
00016  *  with a single other locomotor.  But if there's two active
00017  *  locomotors, I dunno how to handle that.
00018  */
00019 class WorldStateVelDaemon : public BehaviorBase, public EventTrapper {
00020 public:
00021   //! constructor
00022   WorldStateVelDaemon() : BehaviorBase("WorldStateVelDaemon"), estopTime(1), old_x(0), old_y(0), old_a(0) {}
00023 
00024   virtual void DoStart() {
00025     BehaviorBase::DoStart(); // do this first
00026     erouter->addTrapper(this,EventBase::locomotionEGID);
00027     erouter->addListener(this,EventBase::estopEGID);
00028   }
00029 
00030   virtual void DoStop() {
00031     erouter->removeListener(this);
00032     erouter->removeTrapper(this);
00033     BehaviorBase::DoStop(); // do this last
00034   }
00035 
00036   //! traps locomotion events - will filter them out if currently in EStop
00037   virtual bool trapEvent(const EventBase& e) {
00038     const LocomotionEvent& le=dynamic_cast<const LocomotionEvent&>(e);
00039     old_x=le.x;
00040     old_y=le.y;
00041     old_a=le.a;
00042     if(!estopTime) {
00043       state->vel_x=le.x;
00044       state->vel_y=le.y;
00045       state->vel_a=le.a;
00046       state->vel_time=le.getTimeStamp();
00047       return false;
00048     }
00049     return true;
00050   }
00051 
00052   virtual void processEvent(const EventBase& e) {
00053     if(e.getTypeID()==EventBase::deactivateETID) {
00054       if(estopTime) {
00055         estopTime=0;
00056         LocomotionEvent *le = new LocomotionEvent(EventBase::locomotionEGID,e.getSourceID(),EventBase::statusETID,e.getTimeStamp()-state->vel_time);
00057         le->setXYA(old_x,old_y,old_a);
00058         erouter->postEvent(le);
00059       }
00060     } else {
00061       if(!estopTime) {
00062         float older_x=old_x;
00063         float older_y=old_y;
00064         float older_a=old_a;
00065         erouter->postEvent(new LocomotionEvent(EventBase::locomotionEGID,e.getSourceID(),EventBase::statusETID,e.getTimeStamp()-state->vel_time));
00066         estopTime=e.getTimeStamp();
00067         old_x=older_x;
00068         old_y=older_y;
00069         old_a=older_a;
00070       }
00071     }
00072   }
00073 
00074   static std::string getClassDescription() { return "Keeps the WorldState's velocity fields up to date"; }
00075   virtual std::string getDescription() const { return getClassDescription(); }
00076   
00077 protected:
00078   unsigned int estopTime; //!< time estop activation was received
00079   float old_x; //!< current velocity of underlying locomotor
00080   float old_y; //!< current velocity of underlying locomotor
00081   float old_a; //!< current velocity of underlying locomotor
00082 };
00083 
00084 /*! @file
00085  * @brief Defines WorldStateVelDaemon, which listens for LocomotionEvents and updates the velocity fields of WorldState
00086  * @author ejt (Creator)
00087  *
00088  * $Author: ejt $
00089  * $Name: tekkotsu-2_4_1 $
00090  * $Revision: 1.5 $
00091  * $State: Exp $
00092  * $Date: 2005/06/29 22:00:03 $
00093  */
00094 
00095 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:50 2005 by Doxygen 1.4.4