Homepage | Demos | Overview | Downloads | Dev. Resources | Reference | Credits |
MotionSequenceEngine.hGo to the documentation of this file.00001 //-*-c++-*- 00002 #ifndef INCLUDED_MotionSequenceEngine_h_ 00003 #define INCLUDED_MotionSequenceEngine_h_ 00004 00005 #include "Shared/LoadSave.h" 00006 #include "IPC/ListMemBuf.h" 00007 #include "PostureEngine.h" 00008 00009 //! A handy little (or not so little) class for switching between a sequence of postures 00010 /*! Outputs are handled independently. It's easy to add keyframes 00011 * which modify all of the outputs, but since each output is tracked 00012 * individually, OutputCmd's with 0 weight can be used to not affect 00013 * other motions. For instance, pan the head left to right while 00014 * moving the right leg up and down several times, you won't have to 00015 * specify the position of the head in its motion at each of the leg 00016 * motion keyframes. 00017 * 00018 * Be aware that the 0 time frame will be replaced on a call to 00019 * play() with the current body posture. However, this only applies 00020 * to outputs which have a non-zero weighted frame defined at some 00021 * point. The weights, of the 0 time frame will remain unchanged. 00022 * These weights are initially set to 0, so that it's 00023 * possible to 'fade in' the first frame of the motion sequence from 00024 * whereever the body happens to be (or already doing) 00025 * 00026 * To fade out at the end, set a frame with 0 weight for everything. 00027 * Otherwise it will simply die suddenly. When a joint reaches its 00028 * last keyframe, it will be set to 0 weight for all future 00029 * updateOutputs() (unless of course the playhead is reset) 00030 * 00031 * Currently, MotionSequenceEngine is intended mainly for building, 00032 * not editing. It's easy to add keyframes, but hard/impossible to 00033 * delete them. 00034 * 00035 * The MotionSequenceEngine base class is an abstract class so that you can 00036 * create memory efficient motion sequences and simply refer to them 00037 * by the common base class instead of having to worry about the 00038 * actual size allocated in the template, MotionSequenceMC. 00039 * 00040 * @see MotionSequenceEngine::SizeSmall, MotionSequenceEngine::SizeMedium, MotionSequenceEngine::SizeLarge, MotionSequenceEngine::SizeXLarge, 00041 * 00042 * The file format used is as follows: ('<' and '>' are not meant literally) 00043 * - First line: '<tt>\#MSq</tt>' 00044 * - Followed by any series of:\n 00045 * - '<tt>advanceTime </tt><i>time-delta</i>' - moves playhead forward, in milliseconds (synonym for <tt>delay</tt>) 00046 * - '<tt>delay </tt><i>time-delta</i>' - moves playhead forward, in milliseconds (synonym for <tt>advanceTime</tt>) 00047 * - '<tt>setTime </tt><i>time</i>' - sets play time to specified value, in ms 00048 * - '<i>outputname</i><tt> </tt><i>value</i><tt> </tt>[<i>weight</i>]' - sets the specified output to the value - assumes 1 for weight; you can view the list of valid joint names in the outputNames array within the RobotInfo extension namespace for your model. (e.g. ERS210Info::outputNames[]) 00049 * - '<tt>load </tt><i>filename</i>' - file is a posture, sets position 00050 * - '<tt>overlay </tt><i>filename</i>' - file can be a posture or another motion sequence 00051 * - '<tt>degrees</tt>' - following <i>value</i>s will be interpreted as degrees [default] 00052 * - '<tt>radians</tt>' - following <i>value</i>s will be interpreted as radians 00053 * - '<tt>#</tt><i>comment</i>' - a comment line 00054 * - Last line: '<tt>\#END</tt>' 00055 * 00056 * Lines beginning with '#' are ignored as comments. Be aware if you 00057 * load the file and then save it again, these comments will be lost. 00058 * 00059 * After loading a motion sequence, the playtime is left at the end. 00060 * This is to make it easy to append/overlay motion sequences. 00061 * However, the playhead will be reset to the beginning on the first 00062 * call to updateOutputs() if isPlaying() returns true. 00063 * 00064 * You can also create a motion sequence dynamically at run time: 00065 * \code 00066 * //This code sample will stand up, point the head forward and up 0.1 radians, 00067 * //and then autoprune 00068 * 00069 * //First declare the MotionSequence itself: 00070 * SharedObject< MotionSequenceMC<MotionSequenceEngine::SizeSmall> > stand; 00071 * 00072 * //Over the course of the first 700 milliseconds, go to standing posture: 00073 * standSit->setTime(700); 00074 * standSit->setPose(PostureEngine("stand.pos")); // can also use LoadFile("stand.pos") 00075 * 00076 * //Then take another 700 milliseconds to straighten out the head: 00077 * standSit->advanceTime(700); 00078 * //We'll set joints individually this time, instead of loading a posture file: 00079 * standSit->setOutputCmd(HeadOffset+PanOffset,0); 00080 * standSit->setOutputCmd(HeadOffset+RollOffset,0); 00081 * standSit->setOutputCmd(HeadOffset+TiltOffset,0.1); //look up .1 radians 00082 * 00083 * //Add to MotionManager: 00084 * motman->addPrunableMotion(standSit); 00085 * //Playback will now begin automatically, and region deallocated when done 00086 * \endcode 00087 * 00088 * By default, #playing is true. Thus, when you add a MotionSequenceMC 00089 * to the MotionManager, it will begin executing automatically. If 00090 * you do \e not want this behavior, simply call pause() before 00091 * adding the sequence. 00092 * 00093 * When the sequence reaches the end, isAlive() will return false. 00094 * If the motion was added with MotionManager::addPrunableMotion, the 00095 * motion sequence will then autoprune itself from the MotionManager. 00096 * However, you can either call MotionManager::addPersistentMotion() 00097 * to add it, or call setAutoPrune(false), if you want to retain the 00098 * same instantiation between executions. 00099 * 00100 * @see PostureEngine for information on the posture files 00101 */ 00102 class MotionSequenceEngine : public LoadSave { 00103 public: 00104 //!constructor, will start playing immediately 00105 MotionSequenceEngine(); 00106 //!destructor 00107 virtual ~MotionSequenceEngine() {} 00108 00109 //! similar to the MotionCommand::updateOutputs, although this isn't a motion command, and doesn't make any calls on MotionManager - merely updates #lasttime, expects subclasses to do the work of sending new commands to the system 00110 virtual int updateOutputs(); 00111 00112 //!@name LoadSave related 00113 virtual unsigned int getBinSize() const; //!< inherited, returns the size used to save the sequence 00114 virtual unsigned int LoadBuffer(const char buf[], unsigned int len); //!< inherited, doesn't clear before loading - call clear yourself if you want to reset, otherwise it will overlay. Leaves playtime at end of load. 00115 virtual unsigned int SaveBuffer(char buf[], unsigned int len) const; //!< inherited, saves the motion sequence - will save a flat file - doesn't remember references to other files which were loaded 00116 virtual unsigned int LoadFile(const char filename[]); //!< inherited, doesn't clear before loading - call clear yourself if you want to reset, otherwise it will overlay. Leaves playtime at end of load. 00117 virtual unsigned int SaveFile(const char filename[]) const; //!< inherited, saves the motion sequence - will save a flat file - doesn't remember references to other files which were loaded 00118 void setSaveDegrees() { loadSaveMode=M_PI/180; } //!< will store angles as degrees on future saves 00119 bool isSaveDegrees() const { return loadSaveMode!=1; } //!< returns true if will store angles as degrees on future saves 00120 void setSaveRadians() { loadSaveMode=1; } //!< will store angles as radians on future saves 00121 bool isSaveRadians() const { return loadSaveMode==1; } //!< returns true if will store angles as degrees on future saves 00122 //@} 00123 00124 //!@name Sequence Construction 00125 virtual void clear()=0; //!< empties out the sequence (constant time operation - faster than a series of pops) 00126 void setTime(unsigned int x); //!< set the time for both playback and editing (in milliseconds) 00127 unsigned int advanceTime(unsigned int x) {setTime(playtime+x); return playtime; } //!< advance the play/edit index by @a x milliseconds, and then returns the new getTime() 00128 void setOutputCmd(unsigned int i, const OutputCmd& cmd); //!< will insert a keyframe for the given output, or change an existing one 00129 const OutputCmd& getOutputCmd(unsigned int i); //!< gets the value of output @a i at the playhead 00130 void setPose(const PostureEngine& pose); //!< calls setOutputCmd on each of the OutputCmds in @a pose 00131 PostureEngine getPose(); //!< returns the set of OutputCmd's at the current playhead as a PostureEngine 00132 void getPose(PostureEngine& pose); //!< stores the set of OutputCmd's at the current playhead into the specified PostureEngine 00133 void overlayPose(const PostureEngine& pose); //!< calls setOutputCmd on non-zero weighted OutputCmds in @a pose 00134 void compress(); //!< compresses the sequence by eliminating sequences of moves which are identical 00135 virtual unsigned int getMaxFrames() const=0; //!< returns the maximum number of key frames (Move's) which can be stored, determined by the instantiating MotionSequenceMC's template parameter 00136 virtual unsigned int getUsedFrames() const=0; //!< returns the number of used key frames (Move's) which have been stored by the instantiation MotionSequenceEngine subclass 00137 void makeSafe(const float vels[NumOutputs], float margin); //!< will insert time into the motion where needed to keep the joint velocities at or below the speeds given in @a vels * @a margin 00138 //@} 00139 00140 //!@name Playback Control 00141 bool isPlaying(); //! returns true if currently playing 00142 void play(); //!< restarts playback from beginning 00143 void pause() { playing=false; } //!< pauses playback until another call to play() or resume() 00144 void resume(); //!< begins playback from the current playtime 00145 unsigned int getTime() const { return playtime; } //!< returns the current position of the playback (in milliseconds), see setTime() 00146 unsigned int getEndTime() const { return endtime; } //!< returns the length of the motion sequence (in milliseconds) 00147 void setSpeed(float x) { playspeed=x; } //!< sets the playback speed (e.g. 1=regular, 0.5=half speed, -1=@b backwards) 00148 float getSpeed() const { return playspeed; } //!< returns the playback speed 00149 00150 virtual void setHold(bool h=true) { hold=h; } //!< Sets #hold - if this is set to false, it will allow a persistent motion to behave the same as a pruned motion, without being pruned 00151 virtual bool getHold() { return hold; } //!< return #hold 00152 00153 //@} 00154 00155 protected: 00156 // TYPES: 00157 typedef unsigned short Move_idx_t; //!< type for indexes to move structures in subclass's storage 00158 static Move_idx_t invalid_move; //!< used to mark the ends of the Move linked lists 00159 00160 //! This struct holds all the information needed about a frame for a particular output 00161 struct Move { 00162 //!constructor 00163 Move() : cmd(), next(), prev(), starttime(0) {} 00164 OutputCmd cmd; //!< the actual command to use 00165 Move_idx_t next; //!< the next frame 00166 Move_idx_t prev; //!< the previous frame 00167 unsigned int starttime; //!< the time (relative to first frame) this frame should be expressed at 00168 }; 00169 00170 // MEMBERS: 00171 Move_idx_t starts[NumOutputs]; //!< the beginning frame for each output animation 00172 Move_idx_t prevs[NumOutputs]; //!< the previous frame (the starttime for this frame will always be less than or equal to playtime) 00173 Move_idx_t nexts[NumOutputs]; //!< the upcoming frame (the starttime for this frame will always be greater than playtime) 00174 OutputCmd curs[NumOutputs]; //!< merely a cache of current values (if computed, see #curstamps) 00175 unsigned int curstamps[NumOutputs]; //!< timestamp of corresponding value in #curs 00176 unsigned int playtime; //!< the current time of playback, 0 is start of sequence 00177 unsigned int lasttime; //!< the time of the last update 00178 unsigned int endtime; //!< max of moves's Move::starttime's 00179 float playspeed; //!< multiplies the difference between current time and starttime, negative will cause play backwards 00180 bool playing; //!< true if playing, false if paused 00181 bool hold; //!< if set to true, the posture will be kept active; otherwise joints will be marked unused after each posture is achieved (as if the posture was pruned); set through setHold() 00182 00183 float loadSaveMode; //!< 1 to use radians, M_PI/180 for degrees during a save 00184 00185 virtual Move& getKeyFrame(Move_idx_t x) =0; //!< returns the Move struct corresponding to @a x in the subclass's actual data structure 00186 virtual const Move& getKeyFrame(Move_idx_t x) const=0; //!< returns the Move struct corresponding to @a x in the subclass's actual data structure 00187 virtual Move_idx_t newKeyFrame()=0; //!< causes subclass to create a new Move structure, returns its index 00188 virtual void eraseKeyFrame(Move_idx_t x)=0; //!< causes subclass to mark the corresponding Move structure as free 00189 00190 //!Does the actual calculation of position information. Perhaps replace with a Bezier or spline or something? 00191 void calcOutput(OutputCmd& ans, unsigned int t, const Move& prev, const Move& next) const { 00192 float prevweight=(float)(next.starttime-t)/(float)(next.starttime-prev.starttime); 00193 ans.set(prev.cmd,next.cmd,prevweight); 00194 } 00195 00196 //!Sets prev and next to the appropriate values for the given time and output index 00197 virtual void setRange(unsigned int t,Move_idx_t& prev, Move_idx_t& next) const=0; 00198 00199 //!sets playtime to next time for which any output has a keyframe, -1 if none exists 00200 unsigned int setNextFrameTime(Move_idx_t p[NumOutputs], Move_idx_t n[NumOutputs]) const; 00201 00202 //!reads a line from a file, parsing it into variables, returns ending position 00203 static unsigned int readWord(const char buf[], const char * const buflen, char word[], const unsigned int wordlen); 00204 00205 //!returns the index for the output named in the string or NumOutputs if not found, begins search through RobotInfo::outputName's at index @a i 00206 static unsigned int getOutputIndex(const char name[], unsigned int i); 00207 }; 00208 00209 /*! @file 00210 * @brief Describes MotionSequenceEngine, abstract code for smoothly transitioning between a sequence of postures 00211 * @author ejt (Creator) 00212 * 00213 * $Author: ejt $ 00214 * $Name: tekkotsu-2_4_1 $ 00215 * $Revision: 1.13 $ 00216 * $State: Exp $ 00217 * $Date: 2005/08/07 04:11:03 $ 00218 */ 00219 00220 #endif |
Tekkotsu v2.4.1 |
Generated Tue Aug 16 16:32:48 2005 by Doxygen 1.4.4 |