Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

WorldStateVelDaemon.cc

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

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:53 2016 by Doxygen 1.6.3