Guide to gyro and accelerometer with Arduino including Kalman filtering

julio79:
@freak174

I'm sorry .. I use the same sensor.

How did you find these values?

double zeroValue[5] = {950, -400, 13500, -100, -500};

you can edit your skecth?

Hello! I dont have the sketch no more, I partly gave up on this sensos hehe.
However, if you look at Lazarus sketch it is basically the same BalancingRobotArduino/BalancingRobotArduino.ino at master · TKJElectronics/BalancingRobotArduino · GitHub

But Id recommend you to use this sketch instead as it is made for your sensor https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/Examples/MPU6050_DMP6/MPU6050_DMP6.ino#L95

When I used the later mentioned sketch I got it to work with the yaw,pitch,roll values but when I try to combine it to work with a servo I get fifo overflow and the code gets screwd up.

cheers

This is awesome, and probably just what i need!

I'm planning to create a gyroscopic platform on which i will mount a GoPro camera or a Contour camera. I will be mounting this onto a motorbike on an actual race track, so it will go through some pretty intense forces. Then i want to compensate the camera angles using e few servos. I only need to compensate for the pitch and roll at this time.

I have experience with arduino's and the coding but im not familiar with the gyro and accelerator sensors.

I do have a few questions:

Offcourse the first question is; considering the forces applied on the sensors by putting it on a motorbike. Will this still work?

and; is using a gyro and accelero enough to fully remove the drift or will i also need the magnetometer (i need the platform to remain absolutely level to the human eye)?

and; are the resulting values usable to compensate using servo's?

i will probably be using this board with my arduino:

Thanks in advance,

Niels

@APH3X2N
It should work perfectly fine if you can find some servos that are strong enough to handle force.
You don't need to use a magnetometer an accelerometer and gyro should be fine.

I actually got some example code for that IMU: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/ITG3205_ADXL345/ITG3205_ADXL345.ino

Also take a look at this project a guy posted at this post a while ago: Guide to gyro and accelerometer with Arduino including Kalman filtering - #96 by Rivello - Sensors - Arduino Forum
And his post as well: http://arduino.cc/forum/index.php/topic,68755.0.html

Regards
Lauszus

@Lauszus

Thanks for the reply!

And offcourse for the samples.

I''ll be using some pretty strong servos which i'll be using inside brackets to hold the force and weight.

Thanks,

Niels

Hi, Sparkfun doesn't sell IMU Analog Combo Board Razor - 6DOF Ultra-Thin IMU - SEN-10010 - SparkFun Electronics? any more.
it's been replaced by SparkFun 6 Degrees of Freedom IMU Digital Combo Board - ITG3200/ADXL345 - SEN-10121 - SparkFun Electronics.

is your code compatible with the new product ? if yes what modifications should I make .
Thank you

Take a look at this code: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/IMU6DOF/ITG3205_ADXL345, it's for a ITG3205 and a ADXL345. I know the gyro is a bit different, but it should work just fine with your as well.

Hallo Lauszus, Thanks so much for this great blog. I need ur help with something. I'm using itg 3200 and bma 180, I applied your code and it works fine but when rotating around roll for exaple with 90 degrees, the output reading is 70 and the same for pitch although before that, i was applying a comp filter (explained by starlino) and it was giving an output of 90 when rotating with 90 degrees. Do you have any idea what might be causing that?

@totyforever
Try this code I wrote: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/ITG3205_ADXL345/ITG3205_ADXL345.ino and tell me if that works.

Man, you are great :slight_smile: , the problem was in this function:-
R = sqrt(pow(accXval,2)+pow(accYval,2)+pow(accZval,2));
accXangle = acos(accXval/R)*RAD_TO_DEG-90;
accYangle = acos(accYval/R)*RAD_TO_DEG-90;

I think it's not accurate or something as I was applying it in my old code and instead of (accXval/R), it was (Rxest/R) where Rxest is calculated using both acc and gyro readings. However, after I changed it with the one u sent me in the previous post, it worked so fine. Thank you so much.

@totyforever
Nice. You welcome :slight_smile:
atan2 will also give you 360 degrees resolution!

Yup I noticed that :), but shouldn't I use the acc sensitivity in this function and for the Y angle as well :
double getXangle() {
double accXval = Accx-zeroValue[0];
double accZval = Accz-zeroValue[2];
double angle = (atan2(accXval,accZval))*RAD_TO_DEG;
return angle;

I mean would be like this: (4095 = sens in 2g range)

double getXangle() {
double accXval = (Accx-zeroValue[0])/4095;
double accZval = (Accz-zeroValue[2])/4095;
double angle = (atan2(accXval,accZval))*RAD_TO_DEG;
return angle;

It does not make any difference.

For instance take a look at this picture: http://upload.wikimedia.org/wikipedia/commons/7/7e/Trigonometry_triangle.svg
You can get the angle using atan(a/b).

For instance if a=100 and b=500, then the angle would be:
atan(100/500)=11.31deg
Then if you divide both of them with the same constant (this could be the sensitivity) lets say it's 100. Then a=100/100=1 and b=500/100=5, so:
atan(1/5)=11.31deg

atan2 is used to get 360 resolution and is dealt within the function. See this wiki page: atan2 - Wikipedia

yes got it :slight_smile: Thanks Lauszus a lot for ur efforts, wish u all the best

@totyforever
Your welcome and you too :slight_smile:

Hi mate, I've seen you videos on Youtube and thats how I've got here!

I need a 2 axis gyro and 2 axis accelerometer for what I need, wich board do you suggest?

Thanks!

@lparisi

One of the most popular at the moment is the MPU-6050 from InvenSense: http://www.invensense.com/mems/gyro/mpu6050.html. I got a one as well and the performance is pretty good and you can also do a lot of advances stuff with it.
There is a breakout board available form Sparkfun: https://www.sparkfun.com/products/11028 and a lot of other places like ebay.

Here is a basic example sketch I wrote: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/IMU6DOF/MPU6050

If you need to work with the onboard DMP, I recommend the following library: i2cdevlib/Arduino/MPU6050 at master · jrowberg/i2cdevlib · GitHub

What is the Arduino that you used for this project and do you think that there would any problems with coding the gyro/accelerometer module that you used for a Nano v3.0?

@Swoop132
I originally used a Duemilanove, but I have since used the same code for Arduino Uno, Mega etc.
You can use it with a Nano just fine.

Hi Lauszus,

Like the many others, I got my IMU working by studying at lot of your hard work, just wanted you to know that I really appreciate it!
I have a question, with the project I'm working on - I need to filter out the accel data that is affecting the
Kalman output such as when the IMU is under accel in any of the 3 axis. For example while placing the IMU
on a rotating platform, the addition of of the X or Y accel vectors start to affect the roll
and pitch angles. What do you recommend to eliminate these bias errors for when the IMU is not stationary and under accel/decel?

-futureM

@futureM
That's the whole purpose of the Kalman filter. If you are not satisfied with the performance as it is right now, then you have to tune the values.
See my blog for more information: TKJ Electronics » A practical approach to Kalman filter and how to implement it