Commits

Jody McAdams  committed 99d10bb

new animation library update, awesome micro horse update

  • Participants
  • Parent commits ef91d2d

Comments (0)

Files changed (3)

File Experimental/MicroHorse/MicroHorse.ino

 Animation* lowerLegServoAnimBR;
 Animation* lowerLegServoAnimBL;
 
-const float animSpeed = 3.0f;
+const float overallMovement = 0.6f;
 
-const float rightSideOffset = 1.8f;
+const float animSpeed = 3.5f;
 
-const float frontOffset = 0.0f;
-const float backOffset = 0.0f;
+const float rightSideOffset = 3.5f*0.50f;
 
-const float backOffsetLower = 2.0f;
-const float frontOffsetLower = 1.4f;
-  
-const float animTimeShared = 3.0f;
-
-//Front
-const float pauseTimeF = 0.0f;
-//Back
-const float pauseTimeB = 0.9f;
-
-const float animTimeSharedF = animTimeShared + pauseTimeB;
-const float animTimeSharedB = animTimeShared + pauseTimeF;
-  
 const Animation::AnimMode upperLegMode = Animation::AnimMode_Wrap;
 const Animation::AnimMode lowerLegMode = Animation::AnimMode_Wrap;
 
+//Top
+
 //Front
-const int numUpperLegAnimValuesF = 4;
-float upperLegAnimValuesF[numUpperLegAnimValuesF] = {-90,-10,20,20};
+const int numUpperLegAnimValuesF = 5;
+Animation::KeyFrame upperLegAnimValuesF[numUpperLegAnimValuesF] = {{0.0f,-55},{1.0f,40},{2.2f,40},{3.1f,-55},{3.5f,-55}};
+//Back
+const int numUpperLegAnimValuesB = 4;
+Animation::KeyFrame upperLegAnimValuesB[numUpperLegAnimValuesB] = {{0.0f,-50},{1.0f,35},{2.0f,-70},{3.5f,-50}};
 
+//Bottom
+
+//Front
 const int numLowerLegAnimValuesF = 5;
-float lowerLegAnimValuesF[numLowerLegAnimValuesF] = {0,0,80,-5,0};
+Animation::KeyFrame lowerLegAnimValuesF[numLowerLegAnimValuesF] = {{0.0f,-15},{0.2f,-15},{2.0f,0},{2.8,90},{3.5f,-15}};
 
 //Back
-const int numUpperLegAnimValuesB = 4;
-float upperLegAnimValuesB[numUpperLegAnimValuesB] = {-50,-20,20,10};
-
-const int numLowerLegAnimValuesB = 5;
-float lowerLegAnimValuesB[numLowerLegAnimValuesB] = {0,0,80,-5,0};
+const int numLowerLegAnimValuesB = 4;
+Animation::KeyFrame lowerLegAnimValuesB[numLowerLegAnimValuesB] = {{0.0f,-15},{1.0f,0},{1.5f,90},{3.5f,-15}};
 
 Servo servoUpperFR;
 Servo servoUpperFL;
   //Create animations
   
   //Left legs
-  upperLegServoAnimFL = new Animation(upperLegAnimValuesF,numUpperLegAnimValuesF,upperLegMode,animTimeSharedF);
-  lowerLegServoAnimFL = new Animation(lowerLegAnimValuesF,numLowerLegAnimValuesF,lowerLegMode,animTimeSharedF);
-  upperLegServoAnimBL = new Animation(upperLegAnimValuesB,numUpperLegAnimValuesB,upperLegMode,animTimeSharedB);
-  lowerLegServoAnimBL = new Animation(lowerLegAnimValuesB,numLowerLegAnimValuesB,lowerLegMode,animTimeSharedB);
+  upperLegServoAnimFL = new Animation(upperLegAnimValuesF,numUpperLegAnimValuesF,upperLegMode);
+  lowerLegServoAnimFL = new Animation(lowerLegAnimValuesF,numLowerLegAnimValuesF,lowerLegMode);
+  upperLegServoAnimBL = new Animation(upperLegAnimValuesB,numUpperLegAnimValuesB,upperLegMode);
+  lowerLegServoAnimBL = new Animation(lowerLegAnimValuesB,numLowerLegAnimValuesB,lowerLegMode);
   
   //Right legs
-  upperLegServoAnimFR = new Animation(upperLegAnimValuesF,numUpperLegAnimValuesF,upperLegMode,animTimeSharedF);
-  lowerLegServoAnimFR = new Animation(lowerLegAnimValuesF,numLowerLegAnimValuesF,lowerLegMode,animTimeSharedF);
-  upperLegServoAnimBR = new Animation(upperLegAnimValuesB,numUpperLegAnimValuesB,upperLegMode,animTimeSharedB);
-  lowerLegServoAnimBR = new Animation(lowerLegAnimValuesB,numLowerLegAnimValuesB,lowerLegMode,animTimeSharedB);
-  
-  upperLegServoAnimFL->endPauseTime = pauseTimeF;
-  lowerLegServoAnimFL->endPauseTime = pauseTimeF;
-  upperLegServoAnimFR->endPauseTime = pauseTimeF;
-  lowerLegServoAnimFR->endPauseTime = pauseTimeF;
-  
-  upperLegServoAnimBL->endPauseTime = pauseTimeB;
-  lowerLegServoAnimBL->endPauseTime = pauseTimeB;
-  upperLegServoAnimBR->endPauseTime = pauseTimeB;
-  lowerLegServoAnimBR->endPauseTime = pauseTimeB;
-
-  //Left legs
-  upperLegServoAnimFL->currT += frontOffset;
-  lowerLegServoAnimFL->currT += frontOffset + frontOffsetLower;
-  upperLegServoAnimBL->currT += backOffset;
-  lowerLegServoAnimBL->currT += backOffset + backOffsetLower;
+  upperLegServoAnimFR = new Animation(upperLegAnimValuesF,numUpperLegAnimValuesF,upperLegMode);
+  lowerLegServoAnimFR = new Animation(lowerLegAnimValuesF,numLowerLegAnimValuesF,lowerLegMode);
+  upperLegServoAnimBR = new Animation(upperLegAnimValuesB,numUpperLegAnimValuesB,upperLegMode);
+  lowerLegServoAnimBR = new Animation(lowerLegAnimValuesB,numLowerLegAnimValuesB,lowerLegMode);
 
   //Right legs
-  upperLegServoAnimFR->currT += frontOffset + rightSideOffset;
-  lowerLegServoAnimFR->currT += frontOffset + rightSideOffset + frontOffsetLower;
-  upperLegServoAnimBR->currT += backOffset + rightSideOffset;
-  lowerLegServoAnimBR->currT += backOffset + rightSideOffset + backOffsetLower;
+  upperLegServoAnimFR->currT += rightSideOffset;
+  lowerLegServoAnimFR->currT += rightSideOffset;
+  upperLegServoAnimBR->currT += rightSideOffset;
+  lowerLegServoAnimBR->currT += rightSideOffset;
   
   //Left legs
   upperLegServoAnimFL->animSpeed = animSpeed;
   float bottomBackRightAnimVal =  lowerLegServoAnimBR->CalcAnimVal();
   float bottomBackLeftAnimVal = -lowerLegServoAnimBL->CalcAnimVal();
   
+   //Right legs
+  topFrontRightAnimVal *= overallMovement;
+  bottomFrontRightAnimVal *= overallMovement;
+  topBackRightAnimVal *= overallMovement;
+  bottomBackRightAnimVal *= overallMovement;
+  
+  //Left legs
+  topFrontLeftAnimVal *= overallMovement;
+  bottomFrontLeftAnimVal *= overallMovement;
+  topBackLeftAnimVal *= overallMovement;
+  bottomBackLeftAnimVal *= overallMovement;
+  
   //Right legs
   topFrontRightAnimVal += 90.0f;
   bottomFrontRightAnimVal += 90.0f;

File Libraries/Animation/Animation.cpp

 #include "MathTypes.h"
 #include "MathUtil.h"
 
-  Animation::Animation(float* valueArray, int numValues, AnimMode mode, float animTime)
+  Animation::Animation(KeyFrame* valueArray, int numValues, AnimMode mode)
   {
     animDir = 1.0f;
-	totalAnimTime = animTime;
+	totalAnimTime = valueArray[numValues-1].time;
     currT = 0.0f;
     animSpeed = 1.0f;
     numAnimValues = MAX_NUM_ANIM_VALUES;
 
   float Animation::CalcAnimVal()
   {
-	const float lerpT = currT/totalAnimTime;
-	const float animVal = CatmullRom_EvaluateCurve_F32(animValues,numAnimValues,lerpT);
+	if(currT >= totalAnimTime)
+	{
+		return animValues[numAnimValues-1].value;
+	}
+	  
+	KeyFrame* pStartFrame = 0;
+	KeyFrame* pEndFrame = 0;
+	  
+	for(int i=0; i<numAnimValues-1; ++i)
+	{
+		KeyFrame* pCurrFrame = &animValues[i];
+		if(pCurrFrame->time < currT)
+		{
+			pStartFrame = pCurrFrame;
+			pEndFrame = &animValues[i+1];
+		}
+	}
+	  
+	if(pStartFrame == 0
+	   || pEndFrame == 0)
+	{
+		return 0.0f;
+	}
+
+	const float startTime = pStartFrame->time;
+	const float endTime = pEndFrame->time;
+	const float lerpT = (currT - startTime)/(endTime-startTime);
 	
+	const float animVal = Lerp(pStartFrame->value,pEndFrame->value,lerpT);
+	  
 	return animVal;
   }
 
-  void Animation::SetValues(float* valueArray, int numValues)
+  void Animation::SetValues(KeyFrame* valueArray, int numValues)
   {
     //Safety check
     if(numValues > MAX_NUM_ANIM_VALUES)

File Libraries/Animation/Animation.h

 class Animation
 {
 public:
-  enum AnimMode
-  {
-    AnimMode_Wrap,
-    AnimMode_PingPong,
-  };
+	enum AnimMode
+	{
+		AnimMode_Wrap,
+		AnimMode_PingPong,
+	};
+	
+	struct KeyFrame
+	{
+		float time;
+		float value;
+	};
 
-	Animation(float* valueArray, int numValues, AnimMode mode, float animTime);
-  
-	void SetValues(float* valueArray, int numValues);
+	Animation(KeyFrame* valueArray, int numValues, AnimMode mode);
+
+	void SetValues(KeyFrame* valueArray, int numValues);
 	float CalcAnimVal();
-	
+
 	void Update(float timeDelta);
-	
+
 	float currT;
 	float totalAnimTime;
 	float animSpeed;
 	float animDir;
 
-	float animValues[MAX_NUM_ANIM_VALUES];
+	KeyFrame animValues[MAX_NUM_ANIM_VALUES];
 	int numAnimValues;
-	
+
 	float endPauseTime;
 	float pauseTimer;
-  
+
 	AnimMode animMode;
 	
 private: