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".
Step 1: What we gonna need:
Step 2: Building...
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
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:
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 $
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
Any comments, any doubt, any information missing, just tell me and I correct it.
Please if you like this, rate.
Thank you all.