Homepage Demos Overview Downloads Tutorials Reference
Credits

DynamicMotionSequence.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_DynamicMotionSequenceMC_h_
00003 #define INCLUDED_DynamicMotionSequenceMC_h_
00004 
00005 #include "MotionSequenceEngine.h"
00006 #include "MotionCommand.h"
00007 #include <vector>
00008 
00009 //! Uses STL's vector for dynamic memory allocation - don't use this as a motion command, pointers in shared memory regions can be invalid in other processes
00010 /*! See MotionSequenceEngine for documentation on its members */
00011 class DynamicMotionSequence : public MotionCommand, public MotionSequenceEngine {
00012 public:
00013   //!constructor
00014   DynamicMotionSequence() : MotionCommand(), MotionSequenceEngine(), moves(), erased() {clear();}
00015   //!constructor, loads from a file and then resets the playtime to beginning and begins to play
00016   explicit DynamicMotionSequence(const char* filename) : MotionCommand(), MotionSequenceEngine(), moves(), erased() {clear();LoadFile(filename);setPlayTime(1);}
00017   //!destructor
00018   virtual ~DynamicMotionSequence() {}
00019 
00020   // I put this here because i want direct access to moves so it'll be faster
00021 /*  void planJointCmds(unsigned int i, JointCmd frames[NumFrames]) {
00022     Move_idx_t prev=prevs[i],next=nexts[i];
00023     frames[0]=getJointCmd(i);
00024     for(unsigned int t=playtime+FrameTime,j=1;j<NumFrames;j++,t+=FrameTime) {
00025       setRange(t,prev,next);
00026       if(next!=invalid_move)
00027         calcJoint(frames[j],t,moves[prev],moves[next]);
00028       else
00029         frames[j]=unusedJoint;
00030     }
00031   }*/
00032   
00033 
00034   virtual int isDirty() { return isPlaying(); }
00035   virtual int isAlive() { return (playspeed>0) ? (playtime<=endtime) : (playtime>0); }
00036   
00037   virtual int updateOutputs() {
00038     MotionSequenceEngine::updateOutputs();
00039     if(!isPlaying()) {
00040       for(unsigned int i=0; i<NumOutputs; i++) //just copies getOutputCmd(i) across frames
00041         motman->setOutput(this,i,getOutputCmd(i));
00042     } else {
00043       for(unsigned int i=0; i<NumOutputs; i++) { //fill out the buffer of commands for smoother movement
00044         Move_idx_t prev=prevs[i],next=nexts[i];
00045         OutputCmd frames[NumFrames];
00046         frames[0]=getOutputCmd(i);
00047         for(unsigned int t=playtime+FrameTime,j=1;j<NumFrames;j++,t+=FrameTime) {
00048           setRange(t,prev,next);
00049           if(next!=invalid_move)
00050             calcOutput(frames[j],t,moves[prev],moves[next]);
00051           else
00052             frames[j].unset();
00053         }
00054         motman->setOutput(this,i,frames);
00055       }
00056     }
00057     return NumOutputs;
00058     //    if(i<NumLegJointss)
00059     //      log.push_back(logent(get_time(),playtime,i,frames));
00060   }
00061   
00062   virtual void clear() {
00063     moves.clear();
00064     erased.clear();
00065     for(unsigned int i=0; i<NumOutputs; i++) {
00066       moves.push_back(Move());
00067       moves.back().cmd.unset();
00068       moves.back().next=invalid_move;
00069       moves.back().prev=invalid_move;
00070       prevs[i]=starts[i]=moves.size()-1;
00071       nexts[i]=invalid_move;
00072     }
00073     setPlayTime(1);
00074   }
00075   virtual unsigned int getMaxFrames() const { return -1U; }
00076   virtual unsigned int getUsedFrames() const { return moves.size()-erased.size(); }
00077 
00078 protected:
00079   // TYPES:
00080   typedef std::vector<Move> list_t; //!< shorthand for the ListMemBuf that stores all of the movement frames
00081 
00082   // MEMBERS:
00083   list_t moves;                     //!< stores all of the movement keyframes
00084   std::vector<Move_idx_t> erased;   //!< recycles erased keyframes, can't just shift elements in #moves, it would throw of index numbers in Move structures
00085 
00086   virtual Move& getKeyFrame(Move_idx_t x) { return moves[x]; }
00087   virtual const Move& getKeyFrame(Move_idx_t x) const { return moves[x]; }
00088   virtual Move_idx_t newKeyFrame() {
00089     if(erased.empty()) {
00090       moves.push_back(Move());
00091       return moves.size()-1;
00092     } else { //recycle from used list
00093       Move_idx_t x=erased.back();
00094       erased.pop_back();
00095       return x;
00096     }
00097   }
00098   virtual void eraseKeyFrame(Move_idx_t x) { erased.push_back(x); }
00099   void setRange(unsigned int t,Move_idx_t& prev, Move_idx_t& next) const {
00100     if(next!=invalid_move && moves[next].starttime<=t) {
00101       do {
00102         prev=next;
00103         next=moves[prev].next;
00104       } while(next!=invalid_move && moves[next].starttime<=t);
00105     } else if(t<moves[prev].starttime) {
00106       do {
00107         next=prev;
00108         prev=moves[next].prev;
00109       } while(t<moves[prev].starttime);
00110     }
00111   }
00112 };
00113 
00114 /*! @file
00115  * @brief Uses STL's vector for dynamic memory allocation - don't use this as a motion command, pointers in shared memory regions can be invalid in other processes
00116  * @author ejt (Creator)
00117  *
00118  * $Author: ejt $
00119  * $Name: tekkotsu-2_2_2 $
00120  * $Revision: 1.5 $
00121  * $State: Exp $
00122  * $Date: 2004/12/21 22:39:01 $
00123  */
00124 
00125 #endif

Tekkotsu v2.2.2
Generated Tue Jan 4 15:43:13 2005 by Doxygen 1.4.0