Commits

Jody McAdams  committed 18d4383

horse code update and animation library in progress

  • Participants
  • Parent commits 71be7cc

Comments (0)

Files changed (5)

File ArduinoSerialXcode/ArduinoSerial/ArduinoSerial.xcodeproj/project.xcworkspace/xcuserdata/jodymcadams.xcuserdatad/UserInterfaceState.xcuserstate

Binary file modified.

File Experimental/MicroHorse/MicroHorse.ino

 
 
 #include <Servo.h> 
- 
-const int firstServoPin = 4;
-const int numServos = 8;
-Servo servo[numServos]; 
- 
-int pos = 0;    // variable to store the servo position 
- 
+#include <MathTypes.h>
+#include <MathUtil.h>
+#include <Animation.h>
+
+Animation* upperLegServoR;
+Animation* lowerLegServoR;
+Animation* upperLegServoL;
+Animation* lowerLegServoL;
+
+const Animation::AnimMode upperLegMode = Animation::AnimMode_PingPong;
+const int numUpperLegAnimValues = 2;
+float upperLegAnimValues[numUpperLegAnimValues] = {30,135};
+
+const Animation::AnimMode lowerLegMode = Animation::AnimMode_Wrap;
+const int numLowerLegAnimValues = 2;
+float lowerLegAnimValues[numLowerLegAnimValues] = {30,180};
+
+Servo servoUpperR;
+Servo servoLowerR;
+
+Servo servoUpperL;
+Servo servoLowerL;
+
+float lastTimeMillis = 0.0f;
 void setup() 
 { 
-  int pinNumber = firstServoPin;
-  for(int i=0; i<numServos; ++i)
-  {
-    servo[i].attach(pinNumber);  // attaches the servo on pin
-    ++pinNumber;
-  }
+  //Create animations
+  const float overallSpeed = 0.8f;
+  
+  upperLegServoR = new Animation(upperLegAnimValues,numUpperLegAnimValues,upperLegMode);
+  upperLegServoR->animSpeed = 2.0f * overallSpeed;
+  
+  lowerLegServoR = new Animation(lowerLegAnimValues,numLowerLegAnimValues,lowerLegMode);
+  lowerLegServoR->animSpeed = 1.0f * overallSpeed;
+  
+  const float offset = 0.6f;
+  
+  upperLegServoL = new Animation(upperLegAnimValues,numUpperLegAnimValues,upperLegMode);
+  upperLegServoL->animSpeed = 2.0f * overallSpeed;
+  upperLegServoL->currT = offset;
+  
+  lowerLegServoL = new Animation(lowerLegAnimValues,numLowerLegAnimValues,lowerLegMode);
+  lowerLegServoL->animSpeed = 1.0f * overallSpeed;
+  lowerLegServoL->currT = offset;
+  
+  servoUpperR.attach(5);
+  servoLowerR.attach(4);
+  
+  servoUpperL.attach(6);
+  servoLowerL.attach(7);
+  
+  //Init time
+  lastTimeMillis = millis();
 } 
- 
-int servoDelay = 5;
- 
+
 void loop() 
 { 
-  for(int i=0; i<numServos; ++i)
-  { 
-    for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
-    {                                  // in steps of 1 degree 
-      servo[i].write(pos);              // tell servo to go to position in variable 'pos' 
-      delay(servoDelay);                       // waits 15ms for the servo to reach the position 
-    } 
-    for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
-    {                                
-      servo[i].write(pos);              // tell servo to go to position in variable 'pos' 
-      delay(servoDelay);                       // waits 15ms for the servo to reach the position 
-    } 
-  }
+  //Get current time in seconds
+  float currTimeMillis = millis();
+  float timeDelta = (currTimeMillis-lastTimeMillis)/1000.0f;
+  lastTimeMillis = currTimeMillis;
+  
+  //Update animations
+  upperLegServoR->Update(timeDelta);
+  lowerLegServoR->Update(timeDelta);
+  
+  upperLegServoL->Update(timeDelta);
+  lowerLegServoL->Update(timeDelta);
+  
+  //Set servo values from animations
+  servoUpperR.write(upperLegServoR->CalcAnimVal());
+  servoLowerR.write(lowerLegServoR->CalcAnimVal());
+  
+  servoUpperL.write(180.0f - upperLegServoL->CalcAnimVal());
+  servoLowerL.write(180.0f - lowerLegServoL->CalcAnimVal());
 } 

File Libraries/Animation/Animation.cpp

+#include "Animation.h"
+#include "MathTypes.h"
+#include "MathUtil.h"
+
+  Animation::Animation(float* valueArray, int numValues, AnimMode mode)
+  {
+    animDir = 1.0f;
+    currT = 0.0f;
+    animSpeed = 0.2f;
+    numAnimValues = MAX_NUM_ANIM_VALUES;
+    animMode = mode;
+    
+    SetValues(valueArray,numValues);
+  }
+
+  float Animation::CalcAnimVal()
+  {
+	const float animVal = CatmullRom_EvaluateCurve_F32(animValues,numAnimValues,currT);
+	
+	return animVal;
+  }
+
+  void Animation::SetValues(float* valueArray, int numValues)
+  {
+    //Safety check
+    if(numValues > MAX_NUM_ANIM_VALUES)
+    {
+      return;
+    }
+    
+    for(int i=0; i<numValues; ++i)
+    {
+      animValues[i] = valueArray[i];
+    }
+    
+    numAnimValues = numValues;
+  }
+  
+  void Animation::Update(float timeDelta)
+  {
+    switch(animMode)
+    {
+      case AnimMode_Wrap:
+      {
+        currT += animSpeed*timeDelta;
+    
+        if(currT >= 1.0f)
+        {
+          currT -= 1.0f;
+        }
+        
+        break;
+      }
+      case AnimMode_PingPong:
+      {
+        currT += animSpeed*animDir*timeDelta;
+        
+        if(currT >= 1.0f)
+        {
+          currT = 2.0f - currT;
+          animDir = -1.0f;
+        }
+        else if(currT <= 0.0f)
+        {
+          currT = -currT;
+          animDir = 1.0f;
+        }
+        
+        break;
+      }
+    }
+  }

File Libraries/Animation/Animation.h

+#define MAX_NUM_ANIM_VALUES 8
+
+//My animated servo
+class Animation
+{
+public:
+  enum AnimMode
+  {
+    AnimMode_Wrap,
+    AnimMode_PingPong,
+  };
+
+	Animation(float* valueArray, int numValues, AnimMode mode);
+  
+	void SetValues(float* valueArray, int numValues);
+	float CalcAnimVal();
+	
+	void Update(float timeDelta);
+	
+	float currT;
+	float animSpeed;
+	float animDir;
+
+	float animValues[MAX_NUM_ANIM_VALUES];
+	int numAnimValues;
+  
+  AnimMode animMode;
+	
+private:
+
+};

File Libraries/Animation/keywords.txt

+###################################
+# Syntax Coloring Map For NewPing
+###################################
+
+###################################
+# Datatypes (KEYWORD1)
+###################################
+
+
+###################################
+# Methods and Functions (KEYWORD2)
+###################################
+
+
+###################################
+# Constants (LITERAL1)
+###################################