Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

AutoGetupBehavior.cc

Go to the documentation of this file.
00001 #if defined(TGT_HAS_ACCELEROMETERS) && defined(TGT_HAS_LEGS)
00002 
00003 #include "Behaviors/BehaviorBase.h"
00004 #include "Shared/WorldState.h"
00005 #include "Events/EventRouter.h"
00006 #include "IPC/SharedObject.h"
00007 #include "Motion/MotionManager.h"
00008 #include "Motion/MotionSequenceMC.h"
00009 #include "Shared/Config.h"
00010 #include "Sound/SoundManager.h"
00011 
00012 //! a little background behavior to keep the robot on its feet
00013 class AutoGetupBehavior : public BehaviorBase {
00014 public:
00015   //! constructor
00016   AutoGetupBehavior() : BehaviorBase("AutoGetupBehavior"), back(0), side(0), gamma(.9f), sensitivity(.85f*.85f), waiting(false) {}
00017   //! destructor
00018   virtual ~AutoGetupBehavior() {}
00019 
00020   //! Listens for the SensorSrcID::UpdatedSID
00021   virtual void doStart() {
00022     BehaviorBase::doStart();
00023     erouter->addListener(this,EventBase::sensorEGID,SensorSrcID::UpdatedSID);
00024   }
00025   //! Stops listening for events
00026   virtual void doStop() {
00027     erouter->removeListener(this);
00028     BehaviorBase::doStop();
00029   }
00030   //! Run appropriate motion script if the robot falls over
00031   virtual void doEvent() {
00032     if(event->getGeneratorID()==EventBase::motmanEGID) {
00033       //previous attempt at getting up has completed
00034       std::cout << "Getup complete" << std::endl;
00035       erouter->removeListener(this,EventBase::motmanEGID);
00036       waiting=false;
00037       return;
00038     }
00039     back=back*gamma+(1-gamma)*state->sensors[BAccelOffset];
00040     side=side*gamma+(1-gamma)*state->sensors[LAccelOffset];
00041     if(!waiting && back*back+side*side>sensitivity*WorldState::g*WorldState::g) {
00042       //fallen down
00043       std::cout << "I've fallen!" << std::endl;
00044       sndman->playFile("yipper.wav");
00045       std::string gu;
00046       //config->motion.makePath will return a path relative to config->motion.root (from config file read at boot)
00047       if(fabs(back)<fabs(side))
00048         gu=config->motion.makePath("gu_side.mot");
00049       else if(back<0)
00050         gu=config->motion.makePath("gu_back.mot");
00051       else
00052         gu=config->motion.makePath("gu_front.mot");
00053       SharedObject<MediumMotionSequenceMC> getup(gu.c_str());
00054       MotionManager::MC_ID id=motman->addPrunableMotion(getup,MotionManager::kHighPriority);
00055       erouter->addListener(this,EventBase::motmanEGID,id,EventBase::deactivateETID);
00056       waiting=true;
00057     }
00058   }
00059   static std::string getClassDescription() { return "Monitors gravity's influence on the accelerometers - if it seems the robot has fallen over, it runs appropriate getup script"; }
00060   virtual std::string getDescription() const { return getClassDescription(); }
00061 
00062 protected:
00063   float back;          //!< exponential average of backwards accel
00064   float side;          //!< exponential average of sideways accel
00065   float gamma;         //!< default 0.9, gamma parameter for exponential average of above
00066   float sensitivity;   //!< default 0.85*0.85, squared threshold to consider having fallen over, use values 0-1
00067   bool  waiting;       //!< true while we're waiting to hear from completion of MotionSequence, won't try again until this is cleared
00068 };
00069 
00070 REGISTER_BEHAVIOR_MENU_OPT(AutoGetupBehavior,"Background Behaviors/System Daemons",BEH_NONEXCLUSIVE);
00071 
00072 #endif
00073 
00074 /*! @file
00075  * @brief Defines AutoGetupBehavior, a little background behavior to keep the robot on its feet
00076  * @author ejt (Creator)
00077  */

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