DynamicMotionSequence.h
Go to the documentation of this file.00001
00002 #ifndef INCLUDED_DynamicMotionSequence_h_
00003 #define INCLUDED_DynamicMotionSequence_h_
00004
00005 #include "MotionSequenceEngine.h"
00006 #include "MotionCommand.h"
00007 #include "MotionManager.h"
00008 #include "Events/EventBase.h"
00009 #include <vector>
00010
00011
00012
00013 class DynamicMotionSequence : public MotionCommand, public MotionSequenceEngine {
00014 public:
00015
00016
00017 DynamicMotionSequence() : MotionCommand(), MotionSequenceEngine(), moves(), erased(), lastDirty(false) { clear(); }
00018
00019
00020 explicit DynamicMotionSequence(const char* filename) : MotionCommand(), MotionSequenceEngine(), moves(), erased(), lastDirty(false) {
00021 clear();
00022 loadFile(filename);
00023 setTime(1);
00024 }
00025
00026
00027 virtual ~DynamicMotionSequence() {}
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 virtual int isDirty() { return isPlaying(); }
00044 virtual int isAlive() { return (playspeed>0) ? (playtime<=endtime) : (playtime>0); }
00045
00046 virtual int updateOutputs() {
00047 MotionSequenceEngine::updateOutputs();
00048 if(!isPlaying()) {
00049 if(lastDirty)
00050 postEvent(EventBase(EventBase::motmanEGID,getID(),EventBase::statusETID));
00051 lastDirty=false;
00052 for(unsigned int i=0; i<NumOutputs; i++)
00053 motman->setOutput(this,i,getOutputCmd(i));
00054 } else {
00055 lastDirty=true;
00056 for(unsigned int i=0; i<NumOutputs; i++) {
00057 Move_idx_t prev=prevs[i],next=nexts[i];
00058 OutputCmd frames[NumFrames];
00059 frames[0]=getOutputCmd(i);
00060 for(unsigned int t=playtime+FrameTime,j=1;j<NumFrames;j++,t+=FrameTime) {
00061 setRange(t,prev,next);
00062 if(next!=invalid_move)
00063 calcOutput(frames[j],t,moves[prev],moves[next]);
00064 else
00065 frames[j].unset();
00066 }
00067 motman->setOutput(this,i,frames);
00068 }
00069 }
00070 return NumOutputs;
00071
00072
00073 }
00074
00075 virtual void clear() {
00076 moves.clear();
00077 erased.clear();
00078 for(unsigned int i=0; i<NumOutputs; i++) {
00079 moves.push_back(Move());
00080 moves.back().cmd.unset();
00081 moves.back().next=invalid_move;
00082 moves.back().prev=invalid_move;
00083 prevs[i]=starts[i]=moves.size()-1;
00084 nexts[i]=invalid_move;
00085 }
00086 setTime(1);
00087 }
00088 virtual unsigned int getMaxFrames() const { return -1U; }
00089 virtual unsigned int getUsedFrames() const { return moves.size()-erased.size(); }
00090
00091 protected:
00092
00093 typedef std::vector<Move> list_t;
00094
00095
00096 list_t moves;
00097 std::vector<Move_idx_t> erased;
00098 bool lastDirty;
00099
00100 virtual Move& getKeyFrame(Move_idx_t x) { return moves[x]; }
00101 virtual const Move& getKeyFrame(Move_idx_t x) const { return moves[x]; }
00102 virtual Move_idx_t newKeyFrame() {
00103 if(erased.empty()) {
00104 moves.push_back(Move());
00105 return moves.size()-1;
00106 } else {
00107 Move_idx_t x=erased.back();
00108 erased.pop_back();
00109 return x;
00110 }
00111 }
00112
00113 virtual void eraseKeyFrame(Move_idx_t x) { erased.push_back(x); }
00114
00115 bool setRange(unsigned int t,Move_idx_t& prev, Move_idx_t& next) const {
00116 bool moved=false;
00117 if(next!=invalid_move && moves[next].starttime<=t) {
00118 moved=true;
00119 do {
00120 prev=next;
00121 next=moves[prev].next;
00122 } while(next!=invalid_move && moves[next].starttime<=t);
00123 } else {
00124 while(moves[prev].prev!=invalid_move && t<moves[prev].starttime) {
00125 next=prev;
00126 prev=moves[next].prev;
00127 moved=true;
00128 }
00129 }
00130 return moved;
00131 }
00132 };
00133
00134
00135
00136
00137
00138
00139 #endif