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 #include "Shared/ERS210Info.h"
00013 #include "Shared/ERS7Info.h"
00014 
00015 //! just a little demo behavior which lifts a leg higher as more pressure is put on a head button
00016 /*! Based on an idea from Alan Chun-ho Ho for a basic demo program */
00017 class AlanBehavior : public BehaviorBase {
00018 public:
00019   AlanBehavior()
00020     : BehaviorBase(), pose_id(MotionManager::invalid_MC_ID)
00021   {}
00022   
00023   virtual void DoStart() {
00024     //call superclass first for housekeeping:
00025     BehaviorBase::DoStart();
00026 
00027     //now do your code:
00028     
00029     // creates a PostureMC class to move the joint(s) and adds it to global MotionManager
00030     pose_id=motman->addPersistentMotion(SharedObject<PostureMC>());
00031     // subscribe to sensor updated events through the global EventRouter
00032     erouter->addListener(this,EventBase::sensorEGID,SensorSourceID::UpdatedSID);
00033   }
00034   
00035   virtual void DoStop() {
00036     //do your code first:
00037     motman->removeMotion(pose_id);  // removes your posture controller
00038     erouter->removeListener(this); // stops getting events (and timers, if we had any)
00039 
00040     //but don't forget to call superclass at the end:
00041     BehaviorBase::DoStop();
00042   }
00043   
00044   virtual void processEvent(const EventBase& event) {
00045     // to be more general, let's check that it's the right event:
00046     if(event.getGeneratorID()==EventBase::sensorEGID) {
00047       //we'll need to specify the ERS210Info namespace below when
00048       //referencing the button offsets so that this will compile for
00049       //the ERS-7 as well (which lacks front and back head buttons),
00050       //but for your own code, you could leave it off if you weren't
00051       //worried about compiling for other models.
00052 
00053       // "checks out" the posture motion command from MotionManager
00054       // (this is the PostureMC we created in DoStart())
00055       MMAccessor<PostureMC> pose_mc(pose_id);
00056       
00057       //Joint offsets are defined in ERS210Info.h, ERS220Info.h, ERS2xxInfo.h, and ERS7Info.h
00058       unsigned int joint=LFrLegOffset+RotatorOffset;
00059       
00060       //state is a global instantiation of WorldState, kept up to date by framework;
00061       //pressure is in range 0 to 1 - we use the pressure on the front head button here
00062       float pressure=0;
00063       if(state->robotDesign&WorldState::ERS210Mask) {
00064         pressure=state->buttons[ERS210Info::HeadFrButOffset];
00065         std::cout << "HeadFrBut Pressure: " << pressure << std::endl;
00066       } else if(state->robotDesign&WorldState::ERS7Mask) {
00067         pressure=state->buttons[ERS7Info::HeadButOffset];
00068         std::cout << "HeadBut Pressure: " << pressure << std::endl;
00069       } else {
00070         //only really works on the ERS-210 or ERS-7 models - the others don't have a proper pressure sensor
00071         //(the 220's antenna-thing is close, but doesn't give a continuous range)
00072         std::cout << "Unknown model" << std::endl;
00073         erouter->removeListener(this); // stops getting events (and timers, if we had any)
00074         return;
00075       }
00076       
00077       //outputRanges is a constant table, also defined in ERS210Info.h or ERS220Info.h
00078       float angle=outputRanges[joint][MaxRange]*pressure;
00079 
00080       // now send the joint angle to the posture motion command
00081       pose_mc->setOutputCmd(joint,angle);
00082 
00083       //let's do the whole thing again with the other head button for the other leg:
00084       // (cutting out a some of the intermediary steps this time)
00085       joint=RFrLegOffset+RotatorOffset;
00086       if(state->robotDesign&WorldState::ERS210Mask)
00087         pose_mc->setOutputCmd(joint,outputRanges[joint][MaxRange]*state->buttons[ERS210Info::HeadBkButOffset]);
00088       else if(state->robotDesign&WorldState::ERS7Mask) //ERS7 doesn't have another head button, we'll use one of its back buttons
00089         pose_mc->setOutputCmd(joint,outputRanges[joint][MaxRange]*state->buttons[ERS7Info::FrontBackButOffset]);
00090 
00091       // notice that there's no "check in" for pose_mc
00092       // MMAccessor's destructor does this automatically
00093 
00094     } else {
00095       //should never happen
00096       cout << "Unhandled Event:" << event.getName() << endl;
00097     }
00098   }
00099   
00100   virtual std::string getName() const {
00101     // Name is used for menus, or debugging.
00102     return "AlanBehavior";
00103   }
00104   
00105   static std::string getClassDescription() {
00106     // This string will be shown by the HelpControl or by the tooltips of the Controller GUI
00107     return "Lifts the left/right front legs higher as more pressure is applied to the front/back head buttons";
00108   }
00109   
00110 protected:
00111   MotionManager::MC_ID pose_id; //!< ID of PostureMC, set up in DoStart() and used in processEvent()
00112 };
00113 
00114 /*! @file
00115  * @brief Defines AlanBehavior, a little demo behavior which lifts a leg higher as more pressure is put on the back head button
00116  * @author ejt (Creator)
00117  *
00118  * $Author: ejt $
00119  * $Name: tekkotsu-2_2 $
00120  * $Revision: 1.11 $
00121  * $State: Exp $
00122  * $Date: 2004/10/17 01:16:10 $
00123  */
00124 
00125 #endif

Tekkotsu v2.2
Generated Tue Oct 19 14:19:13 2004 by Doxygen 1.3.9.1