Close

Leg V2 super simple gait programmed

A project log for OpenK9

Open source, low-cost robotic quadraped for reinforcement learning

Ben NortierBen Nortier 03/28/2018 at 15:130 Comments

I programmed a basic 4-phase gait:

#include <Arduino.h>
#include <Servo.h>

Servo upper;
Servo lower;

int upperfrom = 25;
int upperto = 125;
int lowerfrom = 55;
int lowerto = 125;
void setup() {
  upper.attach(9);
  lower.attach(10);
  upper.write(90);
  lower.write(90);
  delay(1000);
  upper.write(upperfrom);
  lower.write(lowerfrom);
  delay(1000);
}

int phase = 0;
int tick_counter = 0;
int ticks_per_phase[] = {100, 50, 50, 50}; // Fast
// int ticks_per_phase[] = {50, 20, 20, 20}; // Slow
int upper_from_per_phase[] = {25, 75, 125, 75};
int lower_from_per_phase[] = {80, 55, 125, 125};
void loop() {
  int upper_from = upper_from_per_phase[phase];
  int upper_to = upper_from_per_phase[(phase + 1) % 4];
  int upper_pos = (upper_to - upper_from) * tick_counter / ticks_per_phase[phase] + upper_from;

  int lower_from = lower_from_per_phase[phase];
  int lower_to = lower_from_per_phase[(phase + 1) % 4];
  int lower_pos = (lower_to - lower_from) * tick_counter / ticks_per_phase[phase] + lower_from;

  upper.write(upper_pos);
  lower.write(lower_pos);

  tick_counter += 1;
  if (tick_counter == ticks_per_phase[phase]) {
    phase = (phase + 1) % 4;
    tick_counter = 0;
  }
  delay(10);
}

You'll have to turn your head upside-down...

 

Discussions