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
00013 class AutoGetupBehavior : public BehaviorBase {
00014 public:
00015
00016 AutoGetupBehavior() : BehaviorBase("AutoGetupBehavior"), back(0), side(0), gamma(.9f), sensitivity(.85f*.85f), waiting(false) {}
00017
00018 virtual ~AutoGetupBehavior() {}
00019
00020
00021 virtual void doStart() {
00022 BehaviorBase::doStart();
00023 erouter->addListener(this,EventBase::sensorEGID,SensorSrcID::UpdatedSID);
00024 }
00025
00026 virtual void doStop() {
00027 erouter->removeListener(this);
00028 BehaviorBase::doStop();
00029 }
00030
00031 virtual void doEvent() {
00032 if(event->getGeneratorID()==EventBase::motmanEGID) {
00033
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
00043 std::cout << "I've fallen!" << std::endl;
00044 sndman->playFile("yipper.wav");
00045 std::string gu;
00046
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;
00064 float side;
00065 float gamma;
00066 float sensitivity;
00067 bool waiting;
00068 };
00069
00070 REGISTER_BEHAVIOR_MENU_OPT(AutoGetupBehavior,"Background Behaviors/System Daemons",BEH_NONEXCLUSIVE);
00071
00072 #endif
00073
00074
00075
00076
00077