Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

TailWagMC.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_TailWagMC_h
00003 #define INCLUDED_TailWagMC_h
00004 
00005 #include "Motion/MotionCommand.h"
00006 #include "Motion/MotionManager.h"
00007 #include "Shared/get_time.h"
00008 #include "math.h"
00009 #include "Shared/ERS7Info.h"
00010 #include "Shared/ERS210Info.h"
00011 #include "Shared/WorldState.h"
00012 
00013 //! A simple motion command for wagging the tail - you can specify period, magnitude, and tilt
00014 class TailWagMC : public MotionCommand {
00015 public:
00016 
00017   //!constructor
00018   TailWagMC() :
00019     MotionCommand(), period(500), magnitude(22*M_PI/180), 
00020     offset(0), active(true), last_pan(0.0), last_sign(0), tilt() { }
00021 
00022   //!constructor
00023   TailWagMC(unsigned int cyc_period, float cyc_magnitude) :
00024     MotionCommand(), period(cyc_period), magnitude(cyc_magnitude), 
00025     offset(0), active(true), last_pan(0.0), last_sign(0), tilt() { }
00026 
00027   //!destructor
00028   virtual ~TailWagMC() {}
00029 
00030   virtual int updateOutputs() {
00031     if(!active)
00032       return 0;
00033 
00034     // only ERS-210 and ERS-7 have moveable tails
00035     unsigned int tail_pan_offset, tail_tilt_offset;
00036     if(state->robotDesign & WorldState::ERS210Mask) {
00037       tail_pan_offset = ERS210Info::TailOffset+PanOffset;
00038       tail_tilt_offset = ERS210Info::TailOffset+TiltOffset;
00039     }
00040     else if(state->robotDesign & WorldState::ERS7Mask) {
00041       tail_pan_offset = ERS7Info::TailOffset+PanOffset;
00042       tail_tilt_offset = ERS7Info::TailOffset+TiltOffset;
00043     }
00044     else return 0;
00045 
00046     for(unsigned int i=0; i<NumFrames; i++) {
00047       float new_pan = sin((2*M_PI*(get_time()+offset+i*FrameTime))/period)*magnitude; //bug fix thanks L.A.Olsson AT herts ac uk
00048       pans[i].set(new_pan);
00049       if ( (new_pan-last_pan >= 0 && last_sign == -1) ||
00050            (new_pan-last_pan < 0 && last_sign == +1) )
00051         postEvent(EventBase(EventBase::motmanEGID,getID(),EventBase::statusETID));
00052       last_sign = new_pan-last_pan >= 0 ? 1 : -1;
00053       last_pan = new_pan;
00054     }
00055     motman->setOutput(this,tail_pan_offset,pans);
00056     motman->setOutput(this,tail_tilt_offset,tilt);
00057     return tilt.weight>0?2:1;
00058   }
00059 
00060   virtual int isDirty() { return active; }
00061 
00062   virtual int isAlive() { return true; }
00063 
00064   //! sets the period of time between swings, in milliseconds
00065   /*! a bit complicated in order to avoid jerking around when the period changes */
00066   void setPeriod(unsigned int p) {
00067     float prevPos=((get_time()+offset)%period)/(float)period;
00068     period=p;
00069     float curPos=(get_time()%period)/(float)period;
00070     offset=static_cast<unsigned int>((prevPos-curPos)*period);
00071   } 
00072   unsigned int getPeriod() { return period; } //!< gets the period of time between swings, in milliseconds
00073   void setMagnitude(double mag) { magnitude=mag; } //!< sets the magnitude of swings, in radians
00074   double getMagnitude() { return magnitude; } //!< gets the magnitude of swings, in radians
00075   void setTilt(double r) { tilt.set(r,1); }  //!< sets the tilt of the tail while wagging, in radians
00076   void unsetTilt() { tilt.unset(); } //!< makes the tilt control unspecified, will let something else control tilt
00077   double getTilt() { return tilt.value; }  //!< sets the tilt of the tail while wagging, in radians
00078 
00079   bool getActive() { return active; } //!< returns true if this is currently trying to wag the tail
00080 
00081   //! turns the tail wagger on or off
00082   void setActive(bool a) {
00083     if ( active != a )
00084       last_sign = 0;
00085     active=a;
00086   }
00087   
00088 protected:
00089   unsigned int period; //!< period of time between swings, in milliseconds
00090   double magnitude; //!< magnitude of swings, in radians
00091   unsigned int offset; //!< offset in the period, only used if period is changed to avoid twitching
00092   bool active; //!< true if this is currently trying to wag the tail
00093   float last_pan; //!< last tail position
00094   int last_sign; //!< sign of tail movement direction
00095   OutputCmd tilt; //!< holds current setting for the tilt joint
00096   OutputCmd pans[NumFrames]; //!< holds commands for planning ahead the wagging
00097 };
00098 
00099 /*! @file
00100  * @brief Defines TailWagMC, which will wag the tail on a ERS-210 robot.
00101  * @author ejt (Creator)
00102  *
00103  * $Author: ejt $
00104  * $Name: tekkotsu-3_0 $
00105  * $Revision: 1.11 $
00106  * $State: Exp $
00107  * $Date: 2006/02/15 19:14:13 $
00108  */
00109 
00110 #endif
00111 

Tekkotsu v3.0
Generated Wed Oct 4 00:03:46 2006 by Doxygen 1.4.7