Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

RecordMotionNode.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_RecordMotionNode_h_
00003 #define INCLUDED_RecordMotionNode_h_
00004 
00005 #include "Behaviors/StateNode.h"
00006 #include "Events/EventRouter.h"
00007 #include "IPC/SharedObject.h"
00008 #include "Motion/DynamicMotionSequence.h"
00009 #include "Motion/PIDMC.h"
00010 #include "Shared/MarkScope.h"
00011 #include <valarray>
00012 
00013 //! DESCRIPTION
00014 class RecordMotionNode : public StateNode {
00015 
00016   // **************************** //
00017   // ******* CONSTRUCTORS ******* //
00018   // **************************** //
00019 public:
00020   //! constructor, take an instance name, filename to save to, joint position resolution (max error in radians), temporal resolution (0 for every sensor update)
00021   RecordMotionNode(const std::string& nm="", float angularRes=0.1f, unsigned int temporalRes=0)
00022   : StateNode(nm), angRes(angularRes), timeRes(temporalRes), recStartTime(), relaxID(MotionManager::invalid_MC_ID), rec()
00023   {}
00024   
00025   
00026   // **************************** //
00027   // ********* METHODS ********** //
00028   // **************************** //
00029 public:
00030   virtual void preStart() {
00031     StateNode::preStart();
00032     if(timeRes<FrameTime*NumFrames)
00033       erouter->addListener(this,EventBase::sensorEGID);
00034     else
00035       erouter->addTimer(this,0,timeRes);
00036     recStartTime = state->lastSensorUpdateTime;
00037     for(unsigned int i=0; i<NumPIDJoints; ++i) {
00038       lastTime[i] = startTime[i] = recStartTime;
00039       lastPos[i] = startPos[i] = state->outputs[PIDJointOffset+i];
00040       motion[i]=error[i]=0;
00041       time[i]=1;
00042     }
00043     relaxID = motman->addPersistentMotion(SharedObject<PIDMC>(0));
00044   }
00045 
00046   virtual void doEvent() {
00047     for(unsigned int i=0; i<NumPIDJoints; ++i) {
00048       float t = state->lastSensorUpdateTime - startTime[i];
00049       float curPos = state->outputs[PIDJointOffset+i];
00050       float predPos = motion[i]/time[i] * t + startPos[i];
00051       float curErr = predPos-curPos;
00052       const float GAMMA = 0.8f;
00053       error[i] = error[i]*GAMMA + curErr*(1-GAMMA);
00054       /*if(std::abs(motion[i]/time[i])>1e-5)
00055         std::cout << i << " " << t << " @ " << motion[i]/time[i]*1000 << " current " << (curPos-lastPos[i])/(state->lastSensorUpdateTime - lastTime[i])*1000 << " " << error[i] << " " << curErr << (std::abs(curErr)<angRes ? " *" : "") << std::endl;*/
00056       if( std::abs(error[i]) > angRes ) {
00057         //std::cout << "New frame " << i << std::endl;
00058         rec->setTime(lastTime[i]-recStartTime);
00059         rec->setOutputCmd(i,lastPos[i]);
00060         startTime[i] = lastTime[i];
00061         startPos[i] = lastPos[i];
00062         motion[i]=time[i]=error[i]=0;
00063       }
00064       float off = curPos - startPos[i];
00065       motion[i] += off*t;
00066       time[i] += t*t;
00067       if( std::abs(curErr) < angRes ) {
00068         lastPos[i] = curPos;
00069         lastTime[i] = state->lastSensorUpdateTime;
00070       }
00071     }
00072   }
00073 
00074   virtual void stop() {
00075     motman->removeMotion(relaxID);
00076     rec->saveFile((getName()+".mot").c_str());
00077     StateNode::stop(); // do this last (required)
00078   }
00079 
00080   static std::string getClassDescription() { return "Record all joint motion during activation"; }
00081   virtual std::string getDescription() const { return getClassDescription(); }
00082 
00083 
00084   // **************************** //
00085   // ********* MEMBERS ********** //
00086   // **************************** //
00087 protected:
00088   float angRes;
00089   unsigned int timeRes;
00090   unsigned int recStartTime;
00091   
00092   unsigned int startTime[NumPIDJoints];
00093   float startPos[NumPIDJoints];
00094   float lastPos[NumPIDJoints];
00095   unsigned int lastTime[NumPIDJoints];
00096   float error[NumPIDJoints];
00097   
00098   float motion[NumPIDJoints];
00099   float time[NumPIDJoints];
00100   
00101   MotionManager::MC_ID relaxID;
00102   SharedObject<DynamicMotionSequence> rec;
00103   
00104 
00105   // **************************** //
00106   // ********** OTHER *********** //
00107   // **************************** //
00108 private:
00109   // Providing declarations for these functions will avoid a compiler warning if
00110   // you have any class members which are pointers.  However, as it is, an error
00111   // will result if you inadvertantly cause a call to either (which is probably
00112   // a good thing, unless you really intended to copy/assign a behavior, in
00113   // which case simply provide implementations for the functions)
00114   RecordMotionNode(const RecordMotionNode&); //!< don't call (copy constructor)
00115   RecordMotionNode& operator=(const RecordMotionNode&); //!< don't call (assignment operator)
00116 };
00117 
00118 /*! @file
00119  * @brief Defines RecordMotionNode, which DESCRIPTION
00120  * @author Ethan Tira-Thompson (ejt) (Creator)
00121  */
00122 
00123 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:49 2016 by Doxygen 1.6.3