|
My pet 18 servo hexapod <3 |
In terms of programming, I’m following the tripod gait (3 legs on the ground at any time, legs in the air move forward while legs on the ground move backward). I’m using the Arduino mini as the microcontroller (brains) (with the 2.007 carrier board to make interfacing with the servos easy). I’m controlling 12 channels (which is the max for the ServoWrite function in the Arduino software library), so twelve of the servos are in pairs (using six Y splitter cables). Thus, if I named each leg:
front
A D
B E
C F
back
The servos on AC and DF are linked together, while the servos on B and E are controlled independently. Then I only need 12 channels instead of 18: AC (coxa, femur, tibia). DF (coxa, femur, tibia). B (coxa, femur, tibia). E (coxa, femur, tibia).
Anyway, in my code, I took 4 “snapshots” (sets of servo positions) of the tripod gait and I’m looping through them. In other words, I’m following http://www.pololu.com/docs/0J42/4. I’m pretending this is a 12 servo hexapod (since this was my KISS code) in that the tibia segments are kept at one position the whole time.
- AC, E legs forward. DF, B legs backward.
- AC, E legs down. DF, B legs up.
- AC, E legs backward. DF, B legs forward.
- AC, E legs up. DF, B legs down.
And the actual code I’m using to get it to walk forward:
(also here: https://docs.google.com/document/pub?id=1N9dLEcLXiCvFRI4JRVfJUyylEhZFk4azP8_gsickQv4)
#include <servo.h>
// Declare constants in degrees for the servos
#define TIBIA 45
#define DELAY 200
// +J, BWD = forward, backward (coxa)
// UP, DOWN = up, down (femur)
#define AC_FWD 105
#define AC_BWD 70
#define AC_UP 92
#define AC_DOWN 125
#define B_FWD 95
#define B_BWD 90
#define DF_FWD 70
#define DF_BWD 105
#define E_FWD 90
#define E_BWD 95
int UP = AC_UP;
int DOWN = AC_DOWN;
Servo E_coxa;
Servo E_femur;
Servo E_tibia;
Servo B_coxa;
Servo B_femur;
Servo B_tibia;
Servo AC_coxa;
Servo AC_femur;
Servo AC_tibia;
Servo DF_coxa;
Servo DF_femur;
Servo DF_tibia;
int pos = 0; // variable to store the servo position
void setup()
{
//Set all pins to output, disable the pullup resistors or something, etc.
digitalWrite(2, OUTPUT);
digitalWrite(3, OUTPUT);
digitalWrite(4, OUTPUT);
digitalWrite(5, OUTPUT);
digitalWrite(6, OUTPUT);
digitalWrite(7, OUTPUT);
digitalWrite(8, OUTPUT);
digitalWrite(9, OUTPUT);
digitalWrite(10, OUTPUT);
digitalWrite(11, OUTPUT);
digitalWrite(12, OUTPUT);
digitalWrite(13, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
E_coxa.attach(2);
E_femur.attach(3);
E_tibia.attach(4);
B_coxa.attach(5);
B_femur.attach(6);
B_tibia.attach(7);
AC_coxa.attach(11);
AC_femur.attach(12);
AC_tibia.attach(13);
DF_coxa.attach(8);
DF_femur.attach(9);
DF_tibia.attach(10);
}
void loop()
{
tibia();
tri1();
tri2();
tri3();
tri4();
}
void tibia() {
AC_tibia.write(TIBIA);
B_tibia.write(TIBIA);
DF_tibia.write(TIBIA);
E_tibia.write(TIBIA);
}
void tri1() {
// changed: 1,3,5 fwd, other back [COXA]
// [FEMUR] unchanged
AC_coxa.write(AC_FWD);
AC_femur.write(AC_UP);
E_coxa.write(E_FWD);
E_femur.write(UP);
DF_coxa.write(DF_BWD);
DF_femur.write(DOWN);
B_coxa.write(B_BWD);
B_femur.write(DOWN);
delay(DELAY);
};
void tri2() {
// [COXA] unchanged
// changed: 1,3,5 down, other up [FEMUR]
AC_coxa.write(AC_FWD);
AC_femur.write(AC_DOWN);
E_coxa.write(E_FWD);
E_femur.write(DOWN);
DF_coxa.write(DF_BWD);
DF_femur.write(UP);
B_coxa.write(B_BWD);
B_femur.write(UP);
delay(DELAY);
};
void tri3() {
// changed: 1,3,5 down, other up [COXA]
// [FEMUR] 1,3,5 bwd, other fwd [COXA]
AC_coxa.write(AC_BWD);
AC_femur.write(AC_DOWN);
E_coxa.write(E_BWD);
E_femur.write(DOWN);
DF_coxa.write(DF_FWD);
DF_femur.write(UP);
B_coxa.write(B_FWD);
B_femur.write(UP);
delay(DELAY);
};
void tri4() {
// [COXA] unchanged
// changed: 1,3,5 up, other down [FEMUR]
AC_coxa.write(AC_BWD);
AC_femur.write(AC_UP);
E_coxa.write(E_BWD);
E_femur.write(UP);
DF_coxa.write(DF_FWD);
DF_femur.write(DOWN);
B_coxa.write(B_FWD);
B_femur.write(DOWN);
delay(DELAY);
};