Source

ArduinoProjects / Experimental / MicroHorse / MicroHorse.ino

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#include <Servo.h> 
#include <MathTypes.h>
#include <MathUtil.h>
#include <Animation.h>

Animation* upperLegServoAnimFR;
Animation* upperLegServoAnimFL;
Animation* upperLegServoAnimBR;
Animation* upperLegServoAnimBL;
Animation* lowerLegServoAnimFR;
Animation* lowerLegServoAnimFL;
Animation* lowerLegServoAnimBR;
Animation* lowerLegServoAnimBL;

const float animSpeed = 3.0f;

const float rightSideOffset = 1.8f;

const float frontOffset = 0.0f;
const float backOffset = 0.0f;

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;

//Front
const int numUpperLegAnimValuesF = 4;
float upperLegAnimValuesF[numUpperLegAnimValuesF] = {-90,-10,20,20};

const int numLowerLegAnimValuesF = 5;
float lowerLegAnimValuesF[numLowerLegAnimValuesF] = {0,0,80,-5,0};

//Back
const int numUpperLegAnimValuesB = 4;
float upperLegAnimValuesB[numUpperLegAnimValuesB] = {-50,-20,20,10};

const int numLowerLegAnimValuesB = 5;
float lowerLegAnimValuesB[numLowerLegAnimValuesB] = {0,0,80,-5,0};

Servo servoUpperFR;
Servo servoUpperFL;
Servo servoUpperBR;
Servo servoUpperBL;

Servo servoLowerFR;
Servo servoLowerFL;
Servo servoLowerBR;
Servo servoLowerBL;

float lastTimeMillis = 0.0f;
void setup() 
{ 
  //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);
  
  //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;

  //Right legs
  upperLegServoAnimFR->currT += frontOffset + rightSideOffset;
  lowerLegServoAnimFR->currT += frontOffset + rightSideOffset + frontOffsetLower;
  upperLegServoAnimBR->currT += backOffset + rightSideOffset;
  lowerLegServoAnimBR->currT += backOffset + rightSideOffset + backOffsetLower;
  
  //Left legs
  upperLegServoAnimFL->animSpeed = animSpeed;
  lowerLegServoAnimFL->animSpeed = animSpeed;
  upperLegServoAnimBL->animSpeed = animSpeed;
  lowerLegServoAnimBL->animSpeed = animSpeed;

  //Right legs
  upperLegServoAnimFR->animSpeed = animSpeed;
  lowerLegServoAnimFR->animSpeed = animSpeed;
  upperLegServoAnimBR->animSpeed = animSpeed;
  lowerLegServoAnimBR->animSpeed = animSpeed;
  
  servoUpperFR.attach(4);
  servoUpperFL.attach(5);
  servoUpperBR.attach(6);
  servoUpperBL.attach(7);
  
  servoLowerFR.attach(8);
  servoLowerFL.attach(9);
  servoLowerBR.attach(10);
  servoLowerBL.attach(11);
  
  //Init time
  lastTimeMillis = millis();
} 

void loop() 
{ 
  //Get current time in seconds
  float currTimeMillis = millis();
  float timeDelta = (currTimeMillis-lastTimeMillis)/1000.0f;
  lastTimeMillis = currTimeMillis;
  
  //Update animations
  //Right legs
  upperLegServoAnimFR->Update(timeDelta); 
  lowerLegServoAnimFR->Update(timeDelta);
  upperLegServoAnimBR->Update(timeDelta); 
  lowerLegServoAnimBR->Update(timeDelta);
  
  //Left legs
  upperLegServoAnimFL->Update(timeDelta);
  lowerLegServoAnimFL->Update(timeDelta);
  upperLegServoAnimBL->Update(timeDelta);
  lowerLegServoAnimBL->Update(timeDelta);  
  
  float topFrontRightAnimVal =  upperLegServoAnimFR->CalcAnimVal();
  float topFrontLeftAnimVal = -upperLegServoAnimFL->CalcAnimVal();
  float topBackRightAnimVal =  upperLegServoAnimBR->CalcAnimVal();
  float topBackLeftAnimVal = -upperLegServoAnimBL->CalcAnimVal();
  
  float bottomFrontRightAnimVal =  lowerLegServoAnimFR->CalcAnimVal();
  float bottomFrontLeftAnimVal = -lowerLegServoAnimFL->CalcAnimVal();
  float bottomBackRightAnimVal =  lowerLegServoAnimBR->CalcAnimVal();
  float bottomBackLeftAnimVal = -lowerLegServoAnimBL->CalcAnimVal();
  
  //Right legs
  topFrontRightAnimVal += 90.0f;
  bottomFrontRightAnimVal += 90.0f;
  topBackRightAnimVal += 90.0f;
  bottomBackRightAnimVal += 90.0f;
  
  //Left legs
  topFrontLeftAnimVal += 90.0f;
  bottomFrontLeftAnimVal += 90.0f;
  topBackLeftAnimVal += 90.0f;
  bottomBackLeftAnimVal += 90.0f;  
  
  //Set servo values from animations
  servoUpperFR.write(topFrontRightAnimVal);
  servoLowerFR.write(bottomFrontRightAnimVal);
  servoUpperBR.write(topBackRightAnimVal);
  servoLowerBR.write(bottomBackRightAnimVal);
  
  servoUpperFL.write(topFrontLeftAnimVal);
  servoLowerFL.write(bottomFrontLeftAnimVal);
  servoUpperBL.write(topBackLeftAnimVal);
  servoLowerBL.write(bottomBackLeftAnimVal);
} 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.