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>

Servo servoPingSensor;
int ultraSoundSignal = 3; // Ultrasound signal pin

#define DEBUG_RESET 0
 
const float updateAnimSPF = 1.0f/60.0f;
  
Animation* upperLegServoAnimFR;
Animation* upperLegServoAnimFL;
Animation* upperLegServoAnimBR;
Animation* upperLegServoAnimBL;
Animation* lowerLegServoAnimFR;
Animation* lowerLegServoAnimFL;
Animation* lowerLegServoAnimBR;
Animation* lowerLegServoAnimBL;
Animation* pingAnim;

const float overallMovementR = 1.0f;
const float overallMovementL = 1.0f;

const float animSpeed = 1.5f;

const float backLegOffset = 0.8f;

const float rightSideOffset = 3.5f*0.45f;

//Ping
const Animation::AnimMode pingAnimMode = Animation::AnimMode_PingPong;
const int pingAnimNumValues = 2;
Animation::KeyFrame pingAnimValues[pingAnimNumValues] = {{0.0f,-90},{2.0f,90}};

const Animation::AnimMode upperLegMode = Animation::AnimMode_Wrap;
const Animation::AnimMode lowerLegMode = Animation::AnimMode_Wrap;

//Top

//Front
const int numUpperLegAnimValuesF = 4;
Animation::KeyFrame upperLegAnimValuesF[numUpperLegAnimValuesF] = {{0.0f,20},{1.0f,-60},{2.1f,-20},{3.5f,20}};

//Back
const int numUpperLegAnimValuesB = 4;
Animation::KeyFrame upperLegAnimValuesB[numUpperLegAnimValuesB] = {{0.0f,30},{1.0f,-50},{2.1f,10},{3.5f,30}};


//Bottom

//Front
const int numLowerLegAnimValuesF = 5;
Animation::KeyFrame lowerLegAnimValuesF[numLowerLegAnimValuesF] = {{0.0f,-5},{0.9f,90},{1.25f,-20},{2.0f,0},{3.5f,-5}};


//Back
const int numLowerLegAnimValuesB = 5;
Animation::KeyFrame lowerLegAnimValuesB[numLowerLegAnimValuesB] = {{0.0f,15},{0.6f,90},{1.7f,-20},{2.0f,0},{3.5f,15}};


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

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

enum PingState
{
  PingState_Idle,
  PingState_Waiting1,
  PingState_Waiting2,
  PingState_Ready,
};

PingState pingState = PingState_Idle;
unsigned long lastPingStateMicros;

unsigned long echo = 0;

void updatePing(float timeDelta)
{
  switch(pingState)
  {
    case PingState_Idle:
    {
      pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
      digitalWrite(ultraSoundSignal, LOW); // Send low pulse
      
      lastPingStateMicros = micros();
      
      pingState = PingState_Waiting1;
      
      break;
    }
    case PingState_Waiting1:
    {
      unsigned long currMicros = micros();
      
      if(currMicros-lastPingStateMicros >= 2)
      {
        digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
        
        lastPingStateMicros = micros();
        pingState = PingState_Waiting2;
      }
      
      break;
    }
    case PingState_Waiting2:
    {
      unsigned long currMicros = micros();
      
      if(currMicros-lastPingStateMicros >= 5)
      {
        digitalWrite(ultraSoundSignal, LOW); // Holdoff
        pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
        digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
        // please note that pulseIn has a 1sec timeout, which may
        // not be desirable. Depending on your sensor specs, you
        // can likely bound the time like this -- marcmerlin
        // echo = pulseIn(ultraSoundSignal, HIGH, 38000)
        echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
        //ultrasoundValue = (echo / 58.138f) * .39f; //convert to CM then to inches
        
        pingState = PingState_Ready;
      }
      
      break;
    }
    case PingState_Ready:
    {
      //Serial.println(echo);
      
      pingState = PingState_Idle;
      
      break;
    }
  }
}
 
float lastTimeMillis = 0.0f;
void setup() 
{ 
  Serial.begin(9600);
  
  //Create animations
  pingAnim = new Animation(pingAnimValues,pingAnimNumValues,pingAnimMode);
  
  //Left legs
  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);
  lowerLegServoAnimFR = new Animation(lowerLegAnimValuesF,numLowerLegAnimValuesF,lowerLegMode);
  upperLegServoAnimBR = new Animation(upperLegAnimValuesB,numUpperLegAnimValuesB,upperLegMode);
  lowerLegServoAnimBR = new Animation(lowerLegAnimValuesB,numLowerLegAnimValuesB,lowerLegMode);

  //Right legs
  upperLegServoAnimFR->currT += rightSideOffset;
  lowerLegServoAnimFR->currT += rightSideOffset;
  upperLegServoAnimBR->currT += rightSideOffset;
  lowerLegServoAnimBR->currT += rightSideOffset;
  
  //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;
  
  //Left legs
  upperLegServoAnimBL->currT += backLegOffset;
  lowerLegServoAnimBL->currT += backLegOffset;

  //Right legs
  upperLegServoAnimBR->currT += backLegOffset;
  lowerLegServoAnimBR->currT += backLegOffset;
  
  servoUpperFR.attach(4);
  servoUpperFL.attach(5);
  servoUpperBR.attach(6);
  servoUpperBL.attach(7);
  
  servoLowerFR.attach(8);
  servoLowerFL.attach(9);
  servoLowerBR.attach(10);
  servoLowerBL.attach(11);
  
  servoPingSensor.attach(12);
  
  pinMode(ultraSoundSignal,OUTPUT);

  //Init time
  lastTimeMillis = millis();
} 

float pingUpdateTimer = 0.0f;
float animUpdateTimer = 0.0f;

void loop() 
{ 
  //Get current time in seconds
  float currTimeMillis = millis();
  float timeDelta = (currTimeMillis-lastTimeMillis)/1000.0f;
  lastTimeMillis = currTimeMillis;
  
  pingUpdateTimer -= timeDelta;
  if(pingUpdateTimer < 0.0f)
  {
    pingUpdateTimer += 1.0f;
    
    updatePing(timeDelta);
  }
  
  animUpdateTimer -= timeDelta;
  if(animUpdateTimer < 0.0f)
  {
    animUpdateTimer += updateAnimSPF;
    
    UpdateAnims(updateAnimSPF);
  }
} 

void UpdateAnims(float timeDelta)
{
  //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);  
  
  pingAnim->Update(timeDelta);
  float pingAnimVal = pingAnim->CalcAnimVal();
  pingAnimVal += 90.0f;
  
  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 *= overallMovementR;
  bottomFrontRightAnimVal *= overallMovementR;
  topBackRightAnimVal *= overallMovementR;
  bottomBackRightAnimVal *= overallMovementR;
  
  //Left legs
  topFrontLeftAnimVal *= overallMovementL;
  bottomFrontLeftAnimVal *= overallMovementL;
  topBackLeftAnimVal *= overallMovementL;
  bottomBackLeftAnimVal *= overallMovementL;
  
  //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;  
  
 #if !DEBUG_RESET
  servoPingSensor.write(pingAnimVal); 
   //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);
 #endif
}
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.