Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

MotionSequenceMC.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_MotionSequenceMC_h_
00003 #define INCLUDED_MotionSequenceMC_h_
00004 
00005 #include "MotionSequenceEngine.h"
00006 #include "MotionCommand.h"
00007 #include "MotionManager.h"
00008 
00009 //! Instantiates MotionSequenceEngines - when you want to run a motion sequence, make one of these
00010 /*! Allows a compile-time variable amount of data storage through its template parameter.
00011  *  @see MotionSequenceEngine for the majority of the usage documentation
00012  *  @see TinyMotionSequenceMC, SmallMotionSequenceMC, MediumMotionSequenceMC, LargeMotionSequenceMC, XLargeMotionSequenceMC
00013  *  */
00014 template<unsigned int MAXMOVE>
00015 class MotionSequenceMC : public MotionCommand, public MotionSequenceEngine {
00016 public:
00017   static const unsigned int CAPACITY=MAXMOVE; //!< allows recovery of capacity in a general way (MAXMOVE may, and probably will, be obscured by a typedef)
00018 
00019   //!constructor
00020   MotionSequenceMC()
00021     : MotionCommand(), MotionSequenceEngine(), moves()
00022   {
00023     clear();
00024   }
00025   //!constructor, loads from a file and then resets the playtime to beginning and begins to play
00026   explicit MotionSequenceMC(const std::string& filename)
00027     : MotionCommand(), MotionSequenceEngine(), moves()
00028   {
00029     clear();
00030     LoadFile(filename.c_str());
00031     setTime(1);
00032   }
00033   //!destructor
00034   virtual ~MotionSequenceMC() {}
00035 
00036   virtual int isDirty() { return isPlaying(); }
00037   virtual int isAlive() { return (playspeed>0) ? (playtime<=endtime) : (playtime>0); }
00038   
00039   // I put this here because i want direct access to moves so it'll be faster
00040   virtual int updateOutputs() {
00041     MotionSequenceEngine::updateOutputs();
00042     if(!isPlaying()) {
00043       for(unsigned int i=0; i<NumOutputs; i++) //just copies getOutputCmd(i) across frames
00044         motman->setOutput(this,i,getOutputCmd(i));
00045     } else {
00046       //cout << getTime() << ": ";
00047       for(unsigned int i=0; i<NumOutputs; i++) { //fill out the buffer of commands for smoother movement
00048         Move_idx_t prev=prevs[i],next=nexts[i];
00049         OutputCmd frames[NumFrames];
00050         frames[0]=getOutputCmd(i);
00051         for(unsigned int t=playtime+FrameTime,j=1;j<NumFrames;j++,t+=FrameTime) {
00052           setRange(t,prev,next);
00053           if(next!=invalid_move)
00054             calcOutput(frames[j],t,moves[prev],moves[next]);
00055           else if(hold)
00056             frames[j]=moves[prev].cmd;
00057         }
00058         /*if(i==RFrLegOffset+RotatorOffset)
00059           for(unsigned int j=0; j<NumFrames; j++)
00060           cout << '(' << frames[j].value << ',' << frames[j].weight << ") "; */
00061         motman->setOutput(this,i,frames);
00062       }
00063       //cout <<'\n'<< flush;
00064     }
00065     return NumOutputs;
00066     //    if(i<NumLegJointss)
00067     //      log.push_back(logent(get_time(),playtime,i,frames));
00068   }
00069   
00070   virtual void clear() {
00071     moves.clear();
00072     for(unsigned int i=0; i<NumOutputs; i++) {
00073       prevs[i]=starts[i]=moves.new_back();
00074       moves.back().cmd.unset();
00075       moves.back().next=invalid_move;
00076       moves.back().prev=invalid_move;
00077       moves.back().starttime=0;
00078       nexts[i]=invalid_move;
00079     }
00080     setTime(1);
00081   }
00082 
00083   virtual unsigned int getMaxFrames() const { return moves.getMaxCapacity(); }
00084   virtual unsigned int getUsedFrames() const { return moves.size(); }
00085 
00086 protected:
00087   // TYPES:
00088   typedef ListMemBuf<Move,MAXMOVE,Move_idx_t> list_t; //!< shorthand for the ListMemBuf that stores all of the movement frames
00089 
00090   // MEMBERS:
00091   list_t moves; //!< stores all of the movement keyframes
00092 
00093   virtual Move& getKeyFrame(Move_idx_t x) { return moves[x]; } //!< returns #moves[@a x]
00094   virtual const Move& getKeyFrame(Move_idx_t x) const { return moves[x]; } //!< returns #moves[@a x]
00095   virtual Move_idx_t newKeyFrame() {
00096     Move_idx_t i=moves.new_front();
00097     if(i==invalid_move)
00098       serr->printf("ERROR: MotionSequenceMC %d has run out of memory\n",getID());
00099     return i;
00100   }
00101   //! marks keyframe @a x unused
00102   virtual void eraseKeyFrame(Move_idx_t x) { moves.erase(x); }
00103   //! advances (or rewinds) @a prev and @a next so that @a t falls between them
00104   void setRange(unsigned int t,Move_idx_t& prev, Move_idx_t& next) const {
00105     if(next!=invalid_move && moves[next].starttime<=t) {
00106       do {
00107         prev=next;
00108         next=moves[prev].next;
00109       } while(next!=invalid_move && moves[next].starttime<=t);
00110     } else {
00111       while(moves[prev].prev!=invalid_move && t<moves[prev].starttime) {
00112         next=prev;
00113         prev=moves[next].prev;
00114       } 
00115     }
00116   }
00117 };
00118 
00119 typedef MotionSequenceMC<NumOutputs*2> TinyMotionSequenceMC; //!< Tiny, but enough to handle a transition into a full-body pose
00120 typedef MotionSequenceMC<NumOutputs*3> SmallMotionSequenceMC;//!< Small, but still big enough to handle most of the included MS's (2 full-body frames ~ around 1KB)
00121 typedef MotionSequenceMC<NumOutputs*6> MediumMotionSequenceMC;//!< Medium (5 full body frames ~ est 4KB)
00122 typedef MotionSequenceMC<NumOutputs*11> LargeMotionSequenceMC;//!< Large (10 full body frames ~ est 8KB)
00123 typedef MotionSequenceMC<NumOutputs*26> XLargeMotionSequenceMC;//!< eXtra Large (25 full body frames ~ est 16KB)
00124 
00125 /*! @file
00126  * @brief Describes MotionSequenceEngine and defines MotionSequenceMC, handy little (or not so little) classes for switching between a sequence of postures
00127  * @author ejt (Creator)
00128  *
00129  * $Author: ejt $
00130  * $Name: tekkotsu-2_4_1 $
00131  * $Revision: 1.28 $
00132  * $State: Exp $
00133  * $Date: 2005/08/07 04:11:03 $
00134  */
00135 
00136 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:48 2005 by Doxygen 1.4.4