As per request, here is the code to my arduino hexapod. Please note that I am using an external servo controller, so this code simply sends the servo positions via a serial connection.
More Info (+ youtube video)
I'll try to comment it more fully (and refactor) when finals are over.
PS - it seems blogger is cutting off the right side of the code... :( You should copy/paste into an editor to see the ends of those arrays
/* Reference:
Horizontal movement:
Right side - INCREASING pulse width moves leg toward BACK
Left side - DECREASING pulse width moves leg toward BACK
Vertical movement:
Right side - DECREASING pulse moves leg DOWN
Left side - INCREASING pulse moves leg DOWN
*/
// -------------------------------------------
// Horizontal Max & Min
// -------------------------------------------
#define HorzMid 1515
// Front Right:
#define frHorzMax 1815
#define frHorzMin 1400
// Middle Right
#define mrHorzMax 1715
#define mrHorzMin 1415
// Back Right
#define brHorzMax 1835
#define brHorzMin 1300
// Front Left
#define flHorzMin 1375
#define flHorzMax 1715
// Middle Left
#define mlHorzMin 1344
#define mlHorzMax 1555
// Back Left
#define blHorzMin 1135
#define blHorzMax 1675
// -------------------------------------------
// Vertical Max & Min
// -------------------------------------------
// Right side
#define rVertMax 2300
#define rVertMid 1776
#define rVertMin 900
// Left Side
#define lVertMax 2300
#define lVertMid 1494
#define lVertMin 900
// -------------------------------------------
// Servo Controller Channels:
// -------------------------------------------
// Horizontal Movement:
#define frHorz 0 // Front Right
#define flHorz 1 // Front Left
#define mrHorz 2 // Middle Right
#define mlHorz 3 // Middle Left
#define brHorz 4 // Back Right
#define blHorz 5 // Back Left
// Vertical Movement:
#define frVert 6 // Front Right
#define flVert 7 // Front Left
#define mrVert 8 // Middle Right
#define mlVert 9 // Middle Left
#define brVert 10 // Back Right
#define blVert 11 // Back Left
// Load Servo data into arrays
int servoNumber[12] = {brHorz, flHorz, mrHorz, blHorz, frHorz, mlHorz, brHorz, flHorz, mrHorz, blHorz, frHorz, mlHorz};
int servoHorzDirection[12] = {-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1};
int servoVertDirection[12] = {-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1};
int servoVertMid[12] = {rVertMid, lVertMid, rVertMid, lVertMid, rVertMid, lVertMid, rVertMid, lVertMid, rVertMid, lVertMid, rVertMid, lVertMid};
// -------------------------------------------
// Misc Variables
// -------------------------------------------
int pulse = 0; // Amount to pulse the servo
int potPos = 0;
// -------------------------------------------
// Run-once Setup function
// -------------------------------------------
void setup() {
Serial.begin(115200);
resetAllServos();
delay(5000);
}
// -------------------------------------------
// Loop-forever Function
// -------------------------------------------
void loop() {
// walk forward faster and faster
walk(100);
walk(200);
walk(300);
walk(400);
// relax
delay(1000);
// walk backward faster and faster
walk(-100);
walk(-200);
walk(-300);
// relax
delay(1000);
}
// -------------------------------------------
// Reset All Servos
// -------------------------------------------
void resetAllServos() {
moveServo(frVert, rVertMid);
moveServo(mrVert, rVertMid);
moveServo(brVert, rVertMid);
moveServo(flVert, lVertMid);
moveServo(mlVert, lVertMid);
moveServo(blVert, lVertMid);
moveServo(frHorz, HorzMid);
moveServo(mrHorz, HorzMid);
moveServo(brHorz, HorzMid);
moveServo(flHorz, HorzMid);
moveServo(mlHorz, HorzMid);
moveServo(blHorz, HorzMid);
Serial.println("");
delay(1000);
}
// -------------------------------------------
// Move a Servo
// -------------------------------------------
void moveServo(int ServoPin, int PulseWidth) {
Serial.print(" #");
Serial.print(ServoPin); //which servo to move
Serial.print(" P ");
Serial.print(PulseWidth); // the pulse width to send
}
// -------------------------------------------
// Set Amount of Time Servos Take to Move
// -------------------------------------------
void setMoveTime(int moveTime) {
Serial.print(" T "); //temp command (time = 1 second)
Serial.print(moveTime);
}
// -------------------------------------------
// Walk
// -------------------------------------------
void walk(int walkSpeedAndDirection) {
int intermission = 150; // delay between each part (in ms)
int horzDistance = walkSpeedAndDirection; // Distance to have each leg forward (actual distance of one
// step is 2*horzDistance because the leg moves horzDistance forward and backward)
int vertHeight = 200; // Total height of each step
int i;
for (i=5; i!=-1;i--) {
//Vertical Servo1 Up:
moveServo((servoNumber[i] + 6), (servoVertMid[i] + (servoVertDirection[i] * vertHeight)));
//Horzizontal Servo1 to 2/4
moveServo(servoNumber[i], HorzMid);
//Vertical Servo2 Down:
moveServo((servoNumber[i+1] + 6), servoVertMid[i+1]);
//Horizontal Servo2 to 4/4 (front)
moveServo(servoNumber[i+1], HorzMid + (servoHorzDirection[i+1] * horzDistance));
//Horizontal Servo3 to 3/4
moveServo(servoNumber[i+2], HorzMid + (servoHorzDirection[i+2] * (horzDistance/2)));
//Horizontal Servo4 to 2/4 (midpoint)
moveServo(servoNumber[i+3], HorzMid);
//Horizontal Servo5 to 1/4
moveServo(servoNumber[i+4], HorzMid - (servoHorzDirection[i+4] * (horzDistance/2)));
//Vertical Servo6 Down:
moveServo((servoNumber[i+5] + 6), servoVertMid[i+5]);
//Horizontal Servo6 to 0/4 (back)
moveServo(servoNumber[i+5], HorzMid - (servoHorzDirection[i+5] * horzDistance));
//tell the servo controller to execute the commands and wait for the servos to finish
Serial.println();
delay(intermission);
}
}
/*
Horizontal movement:
Right side - INCREASING pulse width moves leg toward BACK
Left side - DECREASING pulse width moves leg toward BACK
Vertical movement:
Right side - DECREASING pulse moves leg DOWN
Left side - INCREASING pulse moves leg DOWN
*/