Arduino based hexapod robot

This is a project I’m working on: a six-footed robot controlled by an Arduino processor.

The Arduino is a Duemilanove with a BMA180 accelerometer shield attached (this is used for dead-reckoning of the robot’s movement). There are six “legs” using twelve servos (HobbyKing HK15138 rated at 3.8kg @ 5V) arranged radially in pairs around a central platform. The upper servo on each leg is mounted horizontally and controls the leg twist. The lower servo is mounted vertically and has a short dowel foot that can rotate in the vertical plane.

The servos are powered from a separate 5Volt high current supply (from an old Sun desktop workstation) that can deliver several amps. The servos are attached to a servo board made from a small piece of Veroboard which shares a common ground with the Arduino.

The Arduino code uses the servos.h library to position the servos. Here is the code:


// Control code for the hexapod
// JJB 2012

#include

Servo legServo[6];
Servo footServo[6];
// angles for each foot servo so that the foot is vertical
int zeroFoot[6] = {60,90,100,95,95,95};
// angles for each leg servo so the servo arm is at right angles to the platform
int zeroLeg[6] = {120,100,95,90,110,100};
int maxangle = 45;
int NLEGS = 6;

void setup() {
Serial.begin(9600);
Serial.println("Attaching servos:");
for(int i=0;i<NLEGS;i++) {
int ileg = 2*i+2;
int ifoot = ileg+1;
legServo[i].attach(ileg);
footServo[i].attach(ifoot);
Serial.print("Leg ");
Serial.print(i);
Serial.print(" Leg servo pin ");
Serial.print(ileg);
Serial.print(" Foot servo pin ");
Serial.println(ifoot);
}
centreServos();
}

void centreServos() {
Serial.println("Centre servos");
for(int i=0;i<NLEGS;i++) {
legServo[i].write(zeroLeg[i]);
//delay(2000);
footServo[i].write(zeroFoot[i]);
//delay(2000);
}
}

void crouch() {
Serial.println("Crouch");
for(int i=0;i<NLEGS;i++) {
footServo[i].write(zeroFoot[i]-maxangle);
}
}

void stand() {
Serial.println("Stand");
for(int i=0;i<NLEGS;i++) {
footServo[i].write(zeroFoot[i]);
}
}

void liftFoot(int i) {
footServo[i].write(zeroFoot[i]-maxangle);
}

void rotateLeft() {
Serial.println("Rotate Left");
for(int i=0;i<NLEGS;i++) {
legServo[i].write(zeroLeg[i]-maxangle);
}
}

void rotateRight() {
Serial.println("Rotate Right");
for(int i=0;i<NLEGS;i++) {
legServo[i].write(zeroLeg[i]+maxangle);
}
}

void liftFootSet0(int dir) {
// dir = +1 forward, -1 backward
footServo[0].write(zeroFoot[0]-(dir*maxangle));
footServo[2].write(zeroFoot[2]+(dir*maxangle));
footServo[4].write(zeroFoot[4]-(dir*maxangle));
}
void liftFootSet1(int dir) {
// dir = +1 forward, -1 backward
footServo[1].write(zeroFoot[1]+(dir*maxangle));
footServo[3].write(zeroFoot[3]+(dir*maxangle));
footServo[5].write(zeroFoot[5]-(dir*maxangle));
}

void lowerFootSet(int i) {
footServo[i].write(zeroFoot[i]);
footServo[i+2].write(zeroFoot[i+2]);
footServo[i+4].write(zeroFoot[i+4]);
}

void swingLegSet(int i) {
legServo[i].write(zeroLeg[i]-maxangle);
legServo[i+2].write(zeroLeg[i+2]-maxangle);
legServo[i+4].write(zeroLeg[i+4]-maxangle);
}

void rotateLeg(int i, int dir) {
legServo[i].write(zeroLeg[i]+(dir*maxangle));
}

void tripodWalk(int nsteps) {
centreServos();
// align first tripod feet
legServo[0].write(zeroLeg[0]-maxangle);
legServo[2].write(zeroLeg[2]);
legServo[4].write(zeroLeg[4]+maxangle);
// align second tripod feet
legServo[1].write(zeroLeg[1]+maxangle);
legServo[3].write(zeroLeg[3]-maxangle);
legServo[5].write(zeroLeg[5]);
delay(1000);

for(int i=0;i 0) {
int inbyte = Serial.read();
switch (inbyte) {
case 't':
tripodWalk(10);
break;
case 'c': // crouch
crouch();
break;
case 'i': // init - centreServos
centreServos();
break;
case 's': // stand
stand();
break;
case 'r': // rotate left
rotateLeft();
break;
case 'R': // rotate right
rotateRight();
break;
default:
break;
}
}
}

Here is a video of the hexapod walking. It’s not very efficient!

Advertisements

2 thoughts on “Arduino based hexapod robot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s