Homepage Demos Overview Downloads Tutorials Reference
Credits

AlanBehavior.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_AlanBehavior_h_
00003 #define INCLUDED_AlanBehavior_h_
00004 
00005 #include "Behaviors/BehaviorBase.h"
00006 #include "Motion/MotionManager.h"
00007 #include "Motion/MMAccessor.h"
00008 #include "Shared/SharedObject.h"
00009 #include "Shared/WorldState.h"
00010 #include "Events/EventRouter.h"
00011 #include "Motion/PostureMC.h"
00012 
00013 //! just a little demo behavior which lifts a leg higher as more pressure is put on a head button
00014 /*! Based on an idea from Alan Chun-ho Ho for a basic demo program */
00015 class AlanBehavior : public BehaviorBase {
00016 public:
00017   AlanBehavior()
00018     : BehaviorBase(), pose_id(MotionManager::invalid_MC_ID)
00019   {}
00020   
00021   virtual void DoStart() {
00022     //call superclass first for housekeeping:
00023     BehaviorBase::DoStart();
00024 
00025     //now do your code:
00026     
00027     // creates a PostureMC class to move the joint(s) and adds it to global MotionManager
00028     pose_id=motman->addMotion(SharedObject<PostureMC>());
00029     // subscribe to sensor updated events through the global EventRouter
00030     erouter->addListener(this,EventBase::sensorEGID,SensorSourceID::UpdatedSID);
00031   }
00032   
00033   virtual void DoStop() {
00034     //do your code first:
00035     motman->removeMotion(pose_id);  // removes your posture controller
00036     erouter->forgetListener(this); // stops getting events (and timers, if we had any)
00037 
00038     //but don't forget to call superclass at the end:
00039     BehaviorBase::DoStop();
00040   }
00041   
00042   virtual void processEvent(const EventBase& event) {
00043     // to be more general, let's check that it's the right event:
00044     if(event.getGeneratorID()==EventBase::sensorEGID) {
00045 
00046       // "checks out" the posture motion command from MotionManager
00047       // (this is the PostureMC we created in DoStart())
00048       MMAccessor<PostureMC> pose_mc(pose_id);
00049       
00050       //Joint offsets are defined in ERS210Info.h, ERS220Info.h and ERS2xxInfo.h
00051       unsigned int joint=LFrLegOffset+RotatorOffset;
00052       
00053       //state is a global instantiation of WorldState, kept up to date by framework;
00054       //pressure is in range 0 to 1 - we use the pressure on the front head button here
00055       float pressure=state->buttons[HeadFrButOffset];
00056       std::cout << "HeadFrBut Pressure: " << pressure << std::endl;
00057       
00058       //outputRanges is a constant table, also defined in ERS210Info.h or ERS220Info.h
00059       float angle=outputRanges[joint][MaxRange]*pressure;
00060 
00061       // now send the joint angle to the posture motion command
00062       pose_mc->setOutputCmd(joint,angle);
00063 
00064       //let's do the whole thing again with the other head button for the other leg:
00065       // (cutting out a some of the intermediary steps this time)
00066       joint=RFrLegOffset+RotatorOffset;
00067       pose_mc->setOutputCmd(joint,outputRanges[joint][MaxRange]*state->buttons[HeadBkButOffset]);
00068 
00069       // notice that there's no "check in" for pose_mc
00070       // MMAccessor's destructor does this automatically
00071 
00072     } else {
00073       //should never happen
00074       cout << "Unhandled Event:" << event.getName() << endl;
00075     }
00076   }
00077   
00078   virtual std::string getName() const {
00079     // Name is used for menus, or debugging.
00080     return "AlanBehavior";
00081   }
00082   
00083   static std::string getClassDescription() {
00084     // This string will be shown by the HelpControl or by the tooltips of the Controller GUI
00085     return "Lifts the left/right front legs higher as more pressure is applied to the front/back head buttons";
00086   }
00087   
00088 protected:
00089   MotionManager::MC_ID pose_id; //!< ID of PostureMC, set up in DoStart() and used in processEvent()
00090 };
00091 
00092 /*! @file
00093  * @brief Defines AlanBehavior, a little demo behavior which lifts a leg higher as more pressure is put on the back head button
00094  * @author ejt (Creator)
00095  *
00096  * $Author: ejt $
00097  * $Name: tekkotsu-1_5 $
00098  * $Revision: 1.4 $
00099  * $State: Rel $
00100  * $Date: 2003/09/26 03:09:04 $
00101  */
00102 
00103 #endif

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