RecordMotionNode.h
Go to the documentation of this file.00001
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
00014 class RecordMotionNode : public StateNode {
00015
00016
00017
00018
00019 public:
00020
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
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
00055
00056 if( std::abs(error[i]) > angRes ) {
00057
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();
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
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
00107
00108 private:
00109
00110
00111
00112
00113
00114 RecordMotionNode(const RecordMotionNode&);
00115 RecordMotionNode& operator=(const RecordMotionNode&);
00116 };
00117
00118
00119
00120
00121
00122
00123 #endif