00001
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
00014 class TailWagMC : public MotionCommand {
00015 public:
00016
00017
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
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
00028 virtual ~TailWagMC() {}
00029
00030 virtual int updateOutputs() {
00031 if(!active)
00032 return 0;
00033
00034
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;
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
00065
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; }
00073 void setMagnitude(double mag) { magnitude=mag; }
00074 double getMagnitude() { return magnitude; }
00075 void setTilt(double r) { tilt.set(r,1); }
00076 void unsetTilt() { tilt.unset(); }
00077 double getTilt() { return tilt.value; }
00078
00079 bool getActive() { return active; }
00080
00081
00082 void setActive(bool a) {
00083 if ( active != a )
00084 last_sign = 0;
00085 active=a;
00086 }
00087
00088 protected:
00089 unsigned int period;
00090 double magnitude;
00091 unsigned int offset;
00092 bool active;
00093 float last_pan;
00094 int last_sign;
00095 OutputCmd tilt;
00096 OutputCmd pans[NumFrames];
00097 };
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 #endif
00111