Showing posts with label ARDUINO. Show all posts
Showing posts with label ARDUINO. Show all posts

Angle measurement using gyro, accelerometer and Arduino

Friday, 26 July 2013
DSC05259.JPG

The device is a rough prototype of what will eventually become a self balance robot, this is the second part of the hole thing (read accelerometer and control a motor to self balance). The first part with only the gyro can be found here.
On this instructable we will measure the angle using both gyro and accelerometer, and using a technique to merge both sensor to get a smooth signal. The technique is called "complementary filter".

previous

Step 1: What we gonna need:

Some part can be replace, and some modification at the code have to be made to fit your hardware.
On this project we will use:
- Breadboard
- Microcontroller, I used the Arduinoboard
- Wire
- Jumper Wires
- Gyroscope XV-8100
- Nunchuck Wii (for the accelerometer)
- Nunchuck Wii adapter for Arduino

Step 2: Building...

The circuit consists of a gyroscope connected direct to port 0 at your arduino and a nunchuck wii connect at the I2C port.

Assembling the gyro:

1. - plug the gyro at the breadboard
2. - wire ever thing:
- Vo pin from gyro connected to analog port0 at arduino (Light orange wire)
- G pin from gyro connected to ground (White wire)
- V+ pin from gyro connected to Vdd(3.3V) (Orange wire)

Assembling the accelerometer:

1. - plug the adapter at the nunchuck
2. - plug the nunchuck at the arduino using the adapter
3. - place the accel sensor like the photo above

Step 3: Some extra information before we proceed

From both sensor we can measure the angle but using two distinct technique.

To measure the angle using the gyro we have to integrate the signal.
But why we have to do this?
Because the gyro give us the angular rate, so a simple way to get the angle is have the angular rate multiplied by the time [angle = angle + w * dt]

To measure the angle using an accelerometer we have to sense the gravity in each axis of the accelerometer, what it means, the projection of the gravity acceleration on each direction of the sensor give us an idea about the angle. [angle_accel = arctg( Ay / sqrt( Ax^2 + Az^2 ) )]

So why we gonna use two sensor instead one?
To take the advantage of both sensor properties. If you look at the graphic the gyro data continuous increase, this is called drift and the accelerometer data change a lot at a small time.

And how to merge both signal?
We will use a technique called complementary filter. I don't know the exact theory behind this, but it works fine. There are some information at the internet, just google it if you need more information. This link has a lot of information and can be useful.

filtered_angle = HPF*( filtered_angle + w* dt) + LPF*(angle_accel); where HPF + LPF = 1

The values of HPF and LPF can be found at this link, on the filter.pdf file. Thanks you guys from "The DIY Segway".
Just for test purpose we gonna set this values like this, HPF = 0.98 and LPF = 0.02.

Step 4: Code:

The code is a adaptation from a code that I used on another project. Probably there are some variables unused.
I used a library to read the nunchuck data from http://todbot.com/blog/. Thank you Tod E. Kurt.
The comments on code are in portuguese, as soon as I have a free time, I will translate it.

The code output trough the serial line some numbers with this pattern:

dt : w : teta : pitch : filter_teta $
time:angular_velocity:gyro_angle:accel_angle:filtered_angle$

So you can save this values at a serial terminal and plot a graphic or use the angle for other things.

If you need any explanation about the code fell free to ask.

The code is ziped. Just unzipe, open and upload it to your arduino.

Step 5: Testing and conclusion

To test the system I saved the data using a program called Termite, then import this data at excel and plot the graphic to see how good my filter is. The results are amazing. Of course you can use the signal to drive a motor, or other things.

Any comments, any doubt, any information missing, just tell me and I correct it.

Please if you like this, rate.

Thank you all.
Read more ...

ArduinoPhone using GPS module

Friday, 19 July 2013

ArduinoPhone

 

 

 

 

 

 

 

 

 

 

PA228564.JPG


interface_all.jpg


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Combining Arduino and other shield modules, we make a mobile phone named Arduino Phone. Meanwhile, we printed a shell for it with the 3D printer. Although it’s not such fine as you think, even a little bit clunky, it’s still very cool. That is the point this is a cell phone made by ourselves.

While, we can’t install Arduino Phone Apps limited by Arduino. So, if you want to play Angry Birds, then you need to do some big modifications on Arduino Phone. :)

Next, I will make a detailed explanation about the steps of making an Arduino Phone, including the hardware connection and software implementation.

Now, let’s begin. 


Step 1: Preparing stuffs

 
 
At the very beginning, we’d better prepare the materials. Of course, most of these modules can be purchased directly, such as Arduino Uno, TFT Touch Shield and GPRS Shield.

Owning to we want to put our Arduino Phone into a 3D printed shell, and in order to adapt the size of this shell, we have to DIY a charging and discharging PCB board (including two parts) and an expansion PCB board which can connect to the headphone jack of GPRS Shield.

If you want to make a shell by yourself, you can directly take Lipo Rider, or something like this, as the charge sheet. Thus, you do not need to DIY PCB board in order to meet the size of the shell.

What you need:

1.Arduino Uno
2.TFT Touch Shield
3.GPRS Shield
4.RTC
5.Custom ArduinoPhone Charge Circuit (or Lipo Rider)
6.Li-po battery
7.A shell (with 3D printer)
All components at here.

Step 2: Hardware Connection

After preparing the materials, we need to assemble the above electronic components to lay the foundation functional part of Arduino Phone.

1.Plug GPRS Shield into Arduino UNI, then, connect the TFT Touch Shield to GPRS.
2.Connect RTC module to Arduino UNO.
2.Plug into the power module, and connect your earphone to the headphone jack on GPRS.

Well, if you choose to use Lipo Rider, you can refer to this sort of connection.
Ok, now, we are going to get into the software program part.

Step 3: Programming and Test




This Arduino Phone contains the following main functions.
1. receive & send message, letter input
2. dial & answer calls
3. real time clock display
4. A convenient and concise UI, You can switch function by sliding your finger on the screen. A standard 12 key input method for inputting message.
You can get all of the Arduino Phone code from the Github, including the dependent libraries.

Before uploading code to the Arduino, you need to copy all the files in Libraries directory of source
code to Arduino-1.0.4\libraries folder.

However, a much more difficult problem is that this Arduino Phone hasn’t physical buttons
(except the Reset button switch and GPRS). So, how to manage UI will be a challenging task.

Fortunately, TFT Touch not only provides a display function, also a touch screen function.
Thus, we can manage the UI through gesture, like left-swipe and right-swipe.

It this step, we will show how ArduinoPhone works. And the picture  indicates the workflow of ArduinoPhone.

Finally, opening ArduinoPhone.ino with Arduino IDE, then uplaod source code to ArduinoPhone.

Step 4: Assembling




shell2.png





shoe_shell.jpg




In order to make it look like a phone, we printed a shell with 3D printer as shown below. Then, assemble them carefully.
This is a troublesome but interesting process. Actually, maybe it will be more interesting if you put them into other shells, such as in shoes( see the picture :) ).
We are planning to do Arduino upgraded version. If you have any ideas or are willing to help us design a more perfect UI, welcome to contact me.
Read more ...

Simple 18dof Hexapod, Arduino nano

Monday, 1 July 2013

Simple 18dof Hexapod, Arduino nano                 

The content and the project is by orangenarwhals
                                                                                 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Hello,
Here is a simple hexapod that can be built by hand very quickly. The mechanical design is not great, but it is very much in the KISS (keep it stupidly simple) style and should be doable in a weekend for builders of novice to medium experience.

I won't be improving this project any time soon, and people seem to visit my blog from pololu, so I thought I'd go ahead and document it as is. I built this for a sophomore mechanical engineering class at MIT. The wires and six legs make it look complicated, but since the legs are just the same thing repeated 6 times, it's simpler than it appears. Additionally, I did not implement remote controls so all the code runs autonomously (zero input, multiple output system).

[edit: Please vote for me in the epilog contest if you think this is worthy! I don't think this is lasercutter worthy, but I actually just really want an instructables shirt. :D ]

Again, this is documentation of the exact steps involved in a semi-working project. No theoretical underpinnings for designing your own hexapod are really explained here.

A complete picture set of the build process exists here: 2.007 Hexapod (Spring 2011). The first few pictures on there are from Aluminum Hexalinkagepod, based off of the Parallax boebot hexapod.
A set of blog posts exists here:http://j.gs/2Tgu I would specifically recommend this post:http://j.gs/2Tgv A video explaining the design process in 7 minutes (this instructables goes into the construction but not the design): http://j.gs/2Tgw and here is a video of it at the end:


Required items (hardware):
~ Vertical bandsaw (unless you have a lot of patience with a razor)
~ 1/4'' plastic sheet (any reasonable thickness such that the plastic is fairly rigid is fine. I used 1/4'' ABS)
~ About 6'' of
~ 18 R/C servos (I recommend standard size, I've seen hexapods with the tiny 9g servos, but I think that it would be hard to cut out the holes for such servos by hand), complete with the "+" shaped servo horns and servo center screws that should come with the kit.
I used 6 Hitec-311 and 12 Vigor VS-2 servos because that was what I could scavenge for.
~ Screwdriver
~ 4-40 bolts and locknuts (about 48 of them), or whatever bolts fit through your servo flange holes (the side holes). At least 3/8'' long (enough for the 1/4'' plastic or Al and a locknut to fit on there).
~ Drill and drill bit, ideally also a drill press
~ Ratchet or socket wrench for 4-40 bolts
~ Ideally, a vice or clamp
~ Ideally, a horizontal bandsaw
~Optional: Scrap 2x4 wood
~ Measuring instrument, ruler or vastly preferably calipers
~ Optional: Deburring tool


Required items (electronics): 
~ Arduino nano + breadboard + male headers (for the servos)
~ Either 6 Y-splitter servo cables or a pololu serial servo controller (because the default arduino library only supports 12 servos). I bought an 24ch one, but obviously didn't need all 24 ch, not sure why I did that. >.<;; but I am a conservative person and tend to make large purchases just in case. I'm working on fixing this.
I guess another option is to use an arduino mega.
~ Laptop and usb programming cable appropriate for your flavor of arduino
Male Headers
~ Jumper wires (or single core wire
 suitable for breadboards)
~ Potentially some servo extension cables, female-to-female (and you stick headers in them to make them female-to-male) will come in handy.
~ Breadboard (probably a standard 700 point one is best if you are putting the nano onto the breadboard)
~ Battery pack (I recommend a rechargeable battery flavor of battery pack, as the 18 servos are power hungry)
~~ so you could use a 4xAA battery pack and it'd be fine (the servos are nominally 5V servos but they will run fine at 6v, they will just be a bit twitchy because their circuitry/feedback+controls are designed for 5v use), but if they were alkaline batteries (~3000mAh) they would run out after an hour or less of use

~~alternatively, use a LM7805 chip to regulate a lipo battery pack, which runs at 7.4v, down to 5V. These linear power regulators dissipate the extra energy as heat. For how to use one, please google "7805 tutorial". For instance, see: http://jumptuck.com/2011/11/15/voltage-regulation-friend-7805/.


Time required: 1 weekend if you just follow my design. I encourage you to design your own hexapod though, once you see how easy it is!


What I used also included a 2.007 (that's a course at MIT) carrier board (looks like https://sites.google.com/site/2007arduino/). This just brings the servo pins out for easy access, as well as has a built in breadboard and a switching power regulator* that can supply up to 3A at 5v, which is probably enough for the 18 servos. It seemed to function a-okay, but my code only ever had 6 servos moving at any given time.

I also made my own battery pack out of some Sanyo UR18650U batteries that were donated to MITERS by Tesla. They are 3.6V, so I made a 2-series, 3 parallel battery pack for a 7.2V 3.3Ah battery pack. This is obviously overkill. At 3.3 Ah and continuously drawing 3A, I could run my hexapod for over an hour. I've found that 10-20 minutes is plenty of runtime for hexapod.

*as opposed to a LM7805 linear power regulator, a switching power regulator is much more efficient.

Note:The hexapod CAD files are for Solidworks 2012 and do not detail nuts and bolts, they are for reference only. I also made a diameter versus radius error on the body, so the body is too big. 

Step 1: Make the Paper Templates for Femur and Tibia Segments

 
 
 
 
 
 
 
 
 
 
 
We'll be using paper templates to mark out six of the same thing (six legs).

My tibia ended up (after a few iterations) being 1.4x3.75 inches, with holes marked appropriately for where the servo horns would go.

My coxa ended up being 1.42x4.83 inches, with an appropriately sized hole cut out for the servo. The curve on the leg is an arbitrary "it looks nice" curve I cut out directly on the bandsaw (I started out with rectangular legs).

The CAD model you see in the pictures is a later and better (read: smaller and lighter, more appropriate for the servos) revision than the paper templates, and should give you some idea of how it the hexapod is assembled. So ignore the dimensions written on the paper template in the pictures.

Once you are done making the templates, mark them onto the plastic sheet in preparation for cutting. For marking the holes, sharpie bleeds through paper so just line the paper templates up with the plastic and mark the template with sharpie and it should show up on the plastic. I actually cut the legs out and then marked the holes; you can do it in any order.

Step 2: Cut out the Femur and Tibia Segments

I used a vertical bandsaw for cutting out the femur and tibia segments. Make sure to wear safety glasses and take appropriate safety precautions (no loose hair/jewelry/clothing, etc.).

For the tibia and cutting out where the servo fits through, since the bandsaw blade can't just turn around when you hit the inside corners, instead of one continuous cut, you make two half-moon cuts from opposite sides and then go through and cut the back edge. See the first picture.

Step 3: Drill out the Servo Horns and Leg Segments

The servo horns and  should have holes the same size for the bolts to drop through and be locked in place with locknuts. Look up "tap and drill sizing chart" on google, then clearance or through hole fits, for whatever size screw you ended up getting. Basically, the holes should be the major diameter of the screws, so that the body of the screw fits through but not the cap.

For instance, for 4-40 screws (#4 size screws with 40 threads per inch), I look up http://www.physics.ncsu.edu/pearl/Tap_Drill_Chart.html and see that I can use either a size 32 or size 30 drill bit for a clearance hole. Probably I will use a size 32, in case my hole is drilled off-center, I have some leeway for the bolt to still fit through even if the femur/tibia segment holes are not lined up perfectly with the servo flange holes.

To prevent too much burring on the back side and give the vice something thick to clamp on to, I put a block of wood behind the servo horns. These holes can all be done free-form with just a hand-drill because the plastic is so soft, especially with a center drill for the femur/tibia segments, but you just won't get nice cuts (and be careful not to send things flying if you don't press down with your hand hard enough). Actually, I really can't recommend doing it without clamps or a vice, for safety's sake.

To make it easier the next time around: I would have used self-tapping screws, or perhaps even just normal screws as the plastic is pretty soft, and then drilled sizing for aluminum (75% thread), e.g. for 4-40 I would have used a size 43 bit. This way, I don't have to deal with the whole locknut thing which takes extra tools and forever to tighten. I would have to have self-tapping screws of the right length, though, since they have pointy ends which might hurt if they stuck out of the plastic.

Yea, locknuts are much better than just two nuts tightened together, which always loosen unless you use locktite, but if you mess up something they take forever to un-tighten and re-tighten.

Step 4: Attach Servos to Femur and Tibia

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Time to break out the screwdriver and socket wrench!

I used bins to keep all the servo stuff organized, but that's entirely optional.I used bins to keep all the servo stuff organized, but that's entirely optional.

It's easy to zone out and put one or the other servo horn facing the wrong way or flipped or whatever -- again, locknuts are a pain to redo.

Make sure to use the center screw to screw into the servo. This keeps the leg segment from falling off of the servo.

Step 5: Bandsaw and Drill the Coxa Segments

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
These segments are cut out of the 1/4'' angle aluminum stock. They should be just longer than the servo flanges. I did this on the horizontal bandsaw and cut them to by 0.45'' wide. You could in theory do it on the vertical bandsaw if you had parallels set up on one side and a push stick. Definitely don't try cutting this out just free form with two hands, because 0.45'' is way too close to the blade for you to not lose a finger.

I measured the distance to each of the two holes by using calipers on the servo and estimating by eye where the centers of the holes were.

I gently scribed where each hole would go by setting my calipers to the right distance and then locking it, then brushing the tip of my el cheapo calipers across the aluminum.  See http://www.botlanta.org/uploads/pdf/caliper_abuse.pdf for more details on how to do this (written by my friend, charles guan / teamtestbot / etotheipiplusone / e0designs). Sharpie may be applied beforehand so you can see the scribed line better afterwards.

Then I went in with a center punch and center punch all of the holes.

Finally I went in with a drill press and drilled out all of the holes, then used a deburring tool on all the edges.

Step 6: Attach Servos to Coxa Segments

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
What the title says.
If any of the holes are off-center, remember that you really only need to attach two screws (one on either side) to the servo, not all four flange screws.

Repeat six times. Yea, you see what I mean about using self-tapping screws instead of bolts+locknuts now, because that is a _lot_ of locknuts to tighten.

Step 7: Mark out the Body

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
If you can, print out the template I provided. Otherwise, as follows:

Design: Again, no real math went into the design of this hexapod. I simply eyeballed how close together the legs could be and drew out a circle that big. See the paper on the right in the first picture for how I thought about it.

I treated it as an exercise to mark out that circle with just calipers, no compass. To do so, refer to www.botlanta.org/uploads/pdf/caliper_abuse.pdf again, starting on page 27.

Specifically, for a hexagon, referring to (http://media.photobucket.com/user/oldtiffie/media/Black_book/Polygon_measure1.jpg), the side length is just one-half the diameter.

So using the caliper technique (scribing two arcs of 1/2*D and finding the center point to determine the next point on the hexagon), I marked out six dots and then draw arcs between them to complete a circle (diameter = 5.9'').

Then I placed the servo horn on there and marked out where the center of the servo horn would go, then marked out another circle that big (diameter = 5.3'') with 6 points. Finally, I marked out two holes for each of the servo horns (four is really overkill) by just placing the servo horn center over the center dot and then orienting like an "X" instead of a "+" and using pencil to mark the two holes.

Tape the template onto the plastic, bandsaw in a circle, drill out the holes. Done!

Step 8: Assemble Legs onto Body

Make sure to put the servo horns on the body first, assembling with bolts+locknuts, and then attach the servo with the center screw. Because it doesn't go on the other way!

The first picture shows some posts I hot glued on. These didn't last. But that brings to mind
Revision: I would have included some standoffs for the controlboard to sit on and with space underneath for the battery pack to velcro in.

The second picture is from an older revision, but shows the order of installation.

Congratulations! You have a complete hexapod body! That wasn't so bad.


Step 9: Wire 'er up! 12 servo output version

 
 
 
 
 
 
 
 
 
 
 
 
 
 
Note: Servos go Ground-Power-Signal, usually black-red-yellowororange, with convention on most PCBs placing the GND facing out.

12 SERVO OUTPUT VERSION 
Suitable for limitations of default Arduino servo library. See first two pictures for wiring with the 2.007 nano carrier board.

Breadboard Setup
If you don't have a 2.007 carrier board (not for sale as of 24 June 2013, although that may change), put the nano on the breadboard and wire accordingly, with the 5V source in the picture being replaced by your 5v source (e.g. LM7805 with a 4xAA battery pack as described in the first step). You don't want to just put 4xAA into your arduino on "VIN" and then use the "5V" on the arduino because that puts it through a regulator that cannot handle enough current to supply all 18 servos -- your hexapod will be droopy and sleepy as overcurrent protection kicks in on the arduino, unless you just fry your arduino. You must supply both the arduino and the lm7805 circuit from the 4xAA battery pack and then use the LM7805 circuit output to supply your servos in order to not starve them of current.

See  http://www.instructables.com/id/How-to-use-a-breadboard/ if you've never used a breadboard before

Servo Wiring
Okay, so we have six Y-splitter cables. They will be controlling 12 servos total (2 each). Then we have 6 more independent legs, for 12 servo outputs required total. Now pick a face to be the front of the hexapod, and mentally split the legs evenly down either side. The front and back servos (coxa, femur, tibia) of each side will be wired together, and the middle legs will be independent.

Thus, in the code, we will only need 12 servo outputs, like so:

/*
~front~
A  D
B  E
C  F
~back~
*/

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;

Note: In the picture, I have the letters placed incorrectly around the circular hexapod. Notice that it doesn't matter for explaining how the tripod gait works.

Step 10: Wire 'er up! Pololu Serial servo controller, 18 servo version

18 SERVO OUTPUT VERSION
Pololu


The following instructions are for the 2.007 board, however, they are detailed enough that they should explain what you need to do. Essentially, you need 5 wires: a single "servo output" (just an output pin, plus ground and power for the pololu board) and VSRV and GND for the servos. The two grounds should be connected somewhere (anywhere) in your circuit.

1. Look at labeled picture http://www.pololu.com/docs/0J40/1.b

2. What you need on pololu-side: serial going through to RX pin on pololu, power to VSRV and VIN, ground to GND, and lots of servos

'3. According to our eventual code:
#include <NewSoftSerial.h> #NOTE: July 2013: NewSoftSerial is included by default now, do not use this code -- see later steps in this instructables
#define txPin 2
NewSoftSerial mySerial(rxPin, txPin);
void setup(){
  mySerial.begin(9600);
  delay(1000);
}
we should take a servo female-female wire, put one end on servo male header pins for pin 2 of Arduino.

4. RX/TX and Microntroller power: Put other end white wire (or yellow or whatever wire is SIG) on RX on maestro, and red (VIN) and black (GND). Should look like the second picture, the wire-with-masking-tape, with black facing "out" toward the USB port.

5. Servo power: see first picture, the two non-servo cables (the red wire and black wire going to the breadboard) are screwed into the blue terminal block on the maestro. The breadboard has 5V and GND from the 8.4V battery going through the linear regulator on the carrier board. I'm actually stealing 5V from a servo pin. See the black wire soldered to Dig9Output, 5V in the upper left of this pic (which is actually carrying 5V, not GND) (via a female header pin so I wasn't soldering straight to the carrier board pins) (ignore the gazillion extraneous wires)

6. Remove VSRV=VIN jumper
Why remove the jumper? Well, a. Makes the pololu RX pin happier (compare to setup below) b. Setting that jumper seems to current-limit the power going to the servos, leading to my sad-servo symptoms. aka unable-to-walk hexapod.

Whether you will find this to be true, I don't know, but certainly this was a very frustrating point for me.

Servo Wiring
Straightforward. Plug in your 18 servos. (Yep, an 18ch maestro would have worked just fine instead of the 24ch one I bought. Even a 12ch one would work, but you would have to split your servo code between arduino and pololu servos and that could get messy).

Step 11: 12 Servo Code

This is for a simple tripod gait. Note that I just fix the tibia joints in place, which is not ideal, but makes the code very understandable. I would say this code is a good jumping off point. The servos in this code are attached on pins 2 through 13.

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);

The only values you should need to adjust are AC_up, AC_down COXA_CW, COXA_CCW, and potentially TIBIA. Probably you don't need to adjust them much if you are also using standard 180deg servos like I am. These I experimentally determined to be the limits if I didn't want the legs crashing into each other or into the main body of the hexapod (going too far up).

https://github.com/nouyang/18-servo-hexapod/blob/master/arduino_may13_2011.pde
Or, as copied below:
======================

#include <Servo.h>

#define TIBIA 45
#define DELAY 300

#define COXA_CCW 70
#define COXA_CW 105


/*
~front~
A  D
B  E
C  F
~back~
*/

#define AC_UP 92
#define AC_DOWN 125


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;

void setup()
{
  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()
{
  for (int i=0; i<=2; i++){
    walkfwd();
  }
  for (int j=0; j<=2; j++){
    walkbwd();
  }
  for (int k=0; k<=2; k++){
    turnleft();
  }
  for (int l=0; l<=2; l++){
    turnright();
  }



void walkbwd() {
  tibia();
  b1();
  b2();
  b3();
  b4();
}
void walkfwd() {
  tibia();
  tri1();
  tri2();
  tri3();
  tri4();
}

void turnleft() {
  tibia();
  l1();
  l2();
  l3();
  l4();
}

void turnright() {
  tibia();
  r1();
  r2();
  r3();
  r4();
}
void tibia() {
  AC_tibia.write(TIBIA);
  B_tibia.write(TIBIA);
  DF_tibia.write(TIBIA);
  E_tibia.write(TIBIA);
}

void tri1() {
  AC_coxa.write(COXA_CW);
  E_coxa.write(COXA_CCW);

  DF_coxa.write(COXA_CW);
  B_coxa.write(COXA_CCW);

  delay(DELAY);
};
void tri2() {
  AC_femur.write(AC_DOWN);
  E_femur.write(DOWN);

  DF_femur.write(UP);
  B_femur.write(UP);

  delay(DELAY);
};

void tri3() {
  AC_coxa.write(COXA_CCW);
  E_coxa.write(COXA_CW);

  DF_coxa.write(COXA_CCW);
  B_coxa.write(COXA_CW);

  delay(DELAY);
};
void tri4() {
  AC_femur.write(AC_UP);
  E_femur.write(UP);

  DF_femur.write(DOWN);
  B_femur.write(DOWN);

  delay(DELAY);
};


void b1() {
  AC_coxa.write(COXA_CCW);
  E_coxa.write(COXA_CW);

  DF_coxa.write(COXA_CCW);
  B_coxa.write(COXA_CW);

  delay(DELAY);
};
void b2() {
  AC_femur.write(AC_DOWN);
  E_femur.write(DOWN);

  DF_femur.write(UP);
  B_femur.write(UP);

  delay(DELAY);
};

void b3() {
  AC_coxa.write(COXA_CW);
  E_coxa.write(COXA_CCW);

  DF_coxa.write(COXA_CW);
  B_coxa.write(COXA_CCW);

  delay(DELAY);
};
void b4() {
  AC_femur.write(AC_UP);
  E_femur.write(UP);

  DF_femur.write(DOWN);
  B_femur.write(DOWN);

  delay(DELAY);
};


void l1() {
  AC_coxa.write(COXA_CCW);
  E_coxa.write(COXA_CCW);

  DF_coxa.write(COXA_CW);
  B_coxa.write(COXA_CW);

  delay(DELAY);
};
void l2() {
  AC_femur.write(DOWN);
  E_femur.write(DOWN);

  DF_femur.write(UP);
  B_femur.write(UP);

  delay(DELAY);
};

void l3() {
  AC_coxa.write(COXA_CW);
  E_coxa.write(COXA_CW);

  DF_coxa.write(COXA_CCW);
  B_coxa.write(COXA_CCW);

  delay(DELAY);
};
void l4() {
  AC_femur.write(UP);
  E_femur.write(UP);

  DF_femur.write(DOWN);
  B_femur.write(DOWN);

  delay(DELAY);
};



void r1() {
  AC_coxa.write(COXA_CW);
  E_coxa.write(COXA_CW);

  DF_coxa.write(COXA_CCW);
  B_coxa.write(COXA_CCW);

  delay(DELAY);
};
void r2() {
  AC_femur.write(DOWN);
  E_femur.write(DOWN);

  DF_femur.write(UP);
  B_femur.write(UP);

  delay(DELAY);
};

void r3() {
  AC_coxa.write(COXA_CCW);
  E_coxa.write(COXA_CCW);

  DF_coxa.write(COXA_CW);
  B_coxa.write(COXA_CW);

  delay(DELAY);
};
void r4() {
  AC_femur.write(UP);
  E_femur.write(UP);

  DF_femur.write(DOWN);
  B_femur.write(DOWN);

  delay(DELAY);
};


Step 12: 18 Servo Code

DSCN2352.JPG
See "12 servo" for notes on adjusting the constants. Code is at
https://github.com/nouyang/18-servo-hexapod/blob/master/pololu_aug17-2012.pde.
Explanation:
See documentation at  http://www.pololu.com/docs/0J40/5.ehttp://q.gs/4UGL6

a. BYTE is a parameter that pecifies the base (format) to us http://www.arduino.cc/en/Serial/Print

b. target is a non-negative integer less than 8192 (it can be written in binary notation with 14 or fewer digits)
e.g. 6000, or in binary: 01011101110000

c. 0x7F is 01111111 in binary, which infinity zeros to the left, so "&"ing (bitwise AND) it masks out all the digits in target (when target is written in binary) except the last 7 digits (only 1 AND 1 == 1. all other combinations == 0)
  01011101110000
& 00000001111111
= 00000001110000

d. right shift operator, shifts last seven digits (numbers 7 through 13) in target off into empty space and so now the "new" last seven digits were originally bits #0 to 6 (see color-coded pololu doc). Mask with 0x7F again, just to be sure.
  01011101110000,>>7 to:
  00000000101110, then:
& 00000001111111
= 00000000101110

The main difference from the default code is that I mapped the values so that I could mindlessly port code from arduino-"Servo.write()"-style to pololu-"settarget()"-style.

void settarget(unsigned char servo, unsigned int target)
{
  target = map(target, 0, 180, 2400, 9500);






My code, up on github, is also copied below:
======================
#include <SoftwareSerial.h>
#define txPin 2
#define rxPin 3

#define TIBIA 25
#define DELAY 150

#define CW 70
#define CCW 105

#define UP 92
#define DOWN 125

// ~~~~~~~~~~~~~~~~~~~~ //

#define A_COX 1
#define A_FEM 2
#define A_TIB 3

#define B_COX 4
#define B_FEM 5
#define B_TIB 6

#define C_COX 7
#define C_FEM 8
#define C_TIB 9

#define D_COX 10
#define D_FEM 11
#define D_TIB 12
#define E_COX 13
#define E_FEM 14
#define E_TIB 15

#define F_COX 16
#define F_FEM 17
#define F_TIB 18

int numtimes = 3;

SoftwareSerial mySerial(rxPin, txPin);

void setup()// run once, when the sketch starts
{
  mySerial.begin(9600);
  delay(1000);
}
void loop()
{
  for (int i=0; i<=4; i++){
    walkfwd();
  }
  for (int j=0; j<=4; j++){
    walkbwd();
  }
  for (int k=0; k<=2; k++){
    turnleft();
  }
  for (int l=0; l<=2; l++){
    turnright();
  }
}

void tibia() {
  settarget(A_TIB, TIBIA);
  settarget(B_TIB, TIBIA);
  settarget(C_TIB, TIBIA);
  settarget(D_TIB, TIBIA);
  settarget(E_TIB, TIBIA);
  settarget(F_TIB, TIBIA);
}


// ~~~~~~~~~~fwd~~~~~~~~~~ //

void f1() {
  // [COXA] changed
  settarget(A_COX, CW);
  settarget(C_COX, CW);
  settarget(E_COX, CCW);

  settarget(D_COX, CW);
  settarget(F_COX, CW);
  settarget(B_COX, CCW);

  delay(DELAY);
}

void f2() {
  // [FEMUR] changed
  settarget(A_FEM, DOWN);
  settarget(C_FEM, DOWN);
  settarget(E_FEM, DOWN);

  settarget(D_FEM, UP);
  settarget(F_FEM, UP);
  settarget(B_FEM, UP);

  delay(DELAY);
}

void f3() {
  // [COXA] changed
  settarget(A_COX, CCW);
  settarget(C_COX, CCW);
  settarget(E_COX, CW);

  settarget(D_COX, CCW);
  settarget(F_COX, CCW);
  settarget(B_COX, CW);

  delay(DELAY);
}

void f4() {
  // [FEMUR] changed
  settarget(A_FEM, UP);
  settarget(C_FEM, UP);
  settarget(E_FEM, UP);

  settarget(D_FEM, DOWN);
  settarget(F_FEM, DOWN);
  settarget(B_FEM, DOWN );

  delay(DELAY);
}

// ~~~~~~~~~bwd~~~~~~~~~~~ //


void b1() {
  // [COXA] changed
  settarget(A_COX, CCW);
  settarget(C_COX, CCW);
  settarget(E_COX, CW);

  settarget(D_COX, CCW);
  settarget(F_COX, CCW);
  settarget(B_COX, CW);

  delay(DELAY);
}

void b2() {
  // [FEMUR] changed
  settarget(A_FEM, DOWN);
  settarget(C_FEM, DOWN);
  settarget(E_FEM, DOWN);

  settarget(D_FEM, UP);
  settarget(F_FEM, UP);
  settarget(B_FEM, UP);

  delay(DELAY);
}

void b3() {
  // [COXA] changed
  settarget(A_COX, CW);
  settarget(C_COX, CW);
  settarget(E_COX, CCW);

  settarget(D_COX, CW);
  settarget(F_COX, CW);
  settarget(B_COX, CCW);

  delay(DELAY);
}

void b4() {
  // [FEMUR] changed
  settarget(A_FEM, UP);
  settarget(C_FEM, UP);
  settarget(E_FEM, UP);

  settarget(D_FEM, DOWN);
  settarget(F_FEM, DOWN);
  settarget(B_FEM, DOWN );

  delay(DELAY);
}

// ~~~~~~~~~left~~~~~~~~~~~ //

void l1() {
  // [COXA] changed
  settarget(A_COX, CCW);
  settarget(C_COX, CCW);
  settarget(E_COX, CCW);

  settarget(D_COX, CW);
  settarget(F_COX, CW);
  settarget(B_COX, CW);

  delay(DELAY);
}

void l2() {
  // [FEMUR] changed
  settarget(A_FEM, DOWN);
  settarget(C_FEM, DOWN);
  settarget(E_FEM, DOWN);

  settarget(D_FEM, UP);
  settarget(F_FEM, UP);
  settarget(B_FEM, UP);

  delay(DELAY);
}

void l3() {
  // [COXA] changed
  settarget(A_COX, CW);
  settarget(C_COX, CW);
  settarget(E_COX, CW);

  settarget(D_COX, CCW);
  settarget(F_COX, CCW);
  settarget(B_COX, CCW);

  delay(DELAY);
}

void l4() {
  // [FEMUR] changed
  settarget(A_FEM, UP);
  settarget(C_FEM, UP);
  settarget(E_FEM, UP);

  settarget(D_FEM, DOWN);
  settarget(F_FEM, DOWN);
  settarget(B_FEM, DOWN );

  delay(DELAY);
}

// ~~~~~~~~~right~~~~~~~~~~~ //

void r1() {
  // [COXA] changed
  settarget(A_COX, CW);
  settarget(C_COX, CW);
  settarget(E_COX, CW);

  settarget(D_COX, CCW);
  settarget(F_COX, CCW);
  settarget(B_COX, CCW);

  delay(DELAY);
}

void r2() {
  // [FEMUR] changed
  settarget(A_FEM, DOWN);
  settarget(C_FEM, DOWN);
  settarget(E_FEM, DOWN);

  settarget(D_FEM, UP);
  settarget(F_FEM, UP);
  settarget(B_FEM, UP);

  delay(DELAY);
}

void r3() {
  // [COXA] changed
  settarget(A_COX, CCW);
  settarget(C_COX, CCW);
  settarget(E_COX, CCW);

  settarget(D_COX, CW);
  settarget(F_COX, CW);
  settarget(B_COX, CW);

  delay(DELAY);
}

void r4() {
  // [FEMUR] changed
  settarget(A_FEM, UP);
  settarget(C_FEM, UP);
  settarget(E_FEM, UP);

  settarget(D_FEM, DOWN);
  settarget(F_FEM, DOWN);
  settarget(B_FEM, DOWN );

  delay(DELAY);
}

// ~~~~~~~~~~~~~~~~~~~~ //


void walkbwd() {
  tibia();
  b1();
  b2();
  b3();
  b4();
}
void walkfwd() {
  tibia();
  f1();
  f2();
  f3();
  f4();
}

void turnleft() {
  tibia();
  l1();
  l2();
  l3();
  l4();
}

void turnright() {
  tibia();
  r1();
  r2();
  r3();
  r4();
}

// ~~~~~~~~~~~~~~~~~~~~ //


//Send a Set Target command to the Maestro.
//Target is in units of quarter microseconds, so the normal range is 4000 to 8000.
void settarget(unsigned char servo, unsigned int target)
{
  target = map(target, 0, 180, 2400, 9500);
  mySerial.write(0xAA); //start byte
  mySerial.write(0x0C) ; //device id
  mySerial.write(0x04); //command number
  mySerial.write(servo); //servo number
  mySerial.write(target & 0x7F);
  mySerial.write((target >> 7) & 0x7F);
}

Step 13: Further Reading

Congratulations on finishing! Now pose for a picture with your hexapod as it dances! :D

Further Reading
This whole hexapod shenanigans was inspired by youtube: dancing hexapods...



For more hexapod craziness, see my (a bit elderly) gallery here of ones around the internet here: http://orangenarwhals.blogspot.com/2011/07/strange-and-beautiful-hexapods-spider.html.

See also academic papers a reading group I led read here: http://ieee.scripts.mit.edu/urgewiki/index.php?title=S2012_-_Hexapods_and_Other_Cool_Things

An overview of the design process and other things I'd like to implement as of two years ago: http://orangenarwhals.blogspot.com/2011/05/dreaming-of-dancing-hexapods-2007.html
A video explaining the design process in 7 minutes (this instructables goes into the construction but not the design): http://youtu.be/qTh-OGA_LeM



Gaits
Tripod gaits are the simplest. Go out and read more about the different gaits out there. For instance, can you figure out which gait Stompy is using and why?

Forums
There are also many people on the forums, in particular:
pololu e.g. (http://forum.pololu.com/viewtopic.php?f=16&t=6188&p=29520#p29520)
trossenrobotics
lynxmotion
arcbotics e.g. (http://forum.arcbotics.com/viewtopic.php?f=12&t=386)

Hexacon
hexacon2013.mit.edu
On May 4th, 2013, the world's first hexapod conference was held at MIT. If I'm still around MIT in a year or so (I just graduated and am working on my own startup), there will be a second one (or do any of you all want to organize one? Let me know!).

===
Read more ...