Guide to gyro and accelerometer with Arduino including Kalman filtering

@garwalt
Remember to download this file as well: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/MPU6050/Kalman.h

Thanks Lauszus i had thet file but it didnt work, so i downloaded it again and all is well.
I want to add servo controll to this sketch could you give advice on how i might do this?.

Thanks

@garwalt
Simply use the output from the Kalman filter as you input to your servo. See: Servo - Arduino Reference

Thanks Lauszus , sorry to be a pain but this is very new to me, the only thing i have a problem with is, is where in the sketch, is the best place to put the servo write command, at the moment i am using this line from another post.

servo1.writeMicroseconds(1560+kalAngleX*9.55);

i have tried it in various positions within the loop function, and all it does is send the servo full one way , i am getting data on the serial moniter.

Thanks

@garwalt
Simply add:

servo1.write(kalAngleX/2.0);
servo2.write(kalAngleY/2.0);

Just below:

timer = micros();

Thanks Lauszus, all is working as it should.

Hi Laszus the mpu6050 is working fine with your sketch, is there any way to match the angle of the servo to the angle of the gyro, at the moment if the gyro tilts to say 45 degrees the servo only moves to approx 30 degrees.

@garwalt
Just multiply the angle by a constant, as all Servos are a little different.
If you want more control use the writeMicroseconds function: Servo - Arduino Reference.

For instance you could fine the maximum and minimum values and then use map (map() - Arduino Reference) to map the angle to these values.

The post is very informative. Thank you so much.
I am working on something similar to it.

I am working on a self-balancing bicycle which is my final year project.The main idea is to run the bicycle,powered by a motor, in balanced position by measuring the tilt when ever the cycle tend to fall and using steering control to balance it. In my project, I am using two sensors gyro meter

and accelerometer
http://robokits.co.in/shop/index.php?main_page=product_info&cPath=11&products_id=39

to measure the tilt angle.

roll angle of the gyro along the front wheel and 'x-axis' of the accelerometer perpendicular to roll angle,towards the right side of the cycle,parallel to the ground so the roll angle will give the degree/sec and accelerometer will give the displaced angle or tilt angle when the bicycle tends to fall. 'y-axis' of the accelero also comes along the roll angle(I m not using it).

so, firstly I want to know whether the program you used to measure the tilt angle (in your case front and back) is applicable to sensors to accurately measure the tilt angle? (I m not using a imu). It would be of great help if you can make me figure out working of my sensors properly.

nowhere i could get the information about how you placed the axis of gyro and accelero....can you please tell me

please help me

Hi
very interesting post, a bit offtopic but I d need to build a 3 axis angle sensor, which should pass me the pitch and yaw of the sensor in real time. I understand that part of this project iwll do exactly that but I am concerned about multi-turn sensing, so to say. I see the sensor will report easily the angles o a "floating" platform where angles go positive and negative starting for a zero/center, but how will it react when going over 360 degrees? Is i tpossible to sense this kind of rotations? Any hint is much appreciated.
Simone

@madalam
I'm not totally sure what you question is? Are you talking about the physical placement of the gyro and accelerometer or something else?
Btw an IMU is simply a device that reports a crafts velocity, angle etc. In this context consist of the accelerometer and gyro that is used to calculate the angle of the device. See wiki for more information: Inertial measurement unit - Wikipedia.

@io
I had the exact same problem with my Balancing robot. This is how I solved it: Balanduino/Balanduino.ino at 711ef4484173694815e27a78f7cbf0dec4911c90 · TKJElectronics/Balanduino · GitHub.
Also check out the Kickstarter for more information about the project: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit/posts.

Hello guys. I have been following this post for quite a time. Now that i had the gears , i have begun to play with mpu6050 . And i have recently run the code successfully given in this post. Thanks for sharing.
Eventually what i am trying to do with mpu6050 is to measure sea wave data. An make a wave buoy. Since i am only a newbie in electronics and coding. I made some research on the web and found a scientific article about it. In this article they use an accelerometer to measure wave height and period. Main problem is that while buoy drifts with the wave , wave changes its position relative to the ground. So that acc data can not be used to measure wave accurately. They managed to find a way to compensate directional forces applied to buoy and measure z axis . But off course neither the code nor how to do info are given. But equations in this project are shared fortunately. I am adding the image to the post. I am now trying to integrate these equations to the mpu6050 's acc readings. First equation is interesting i think i can measure the acc data with it regardless of tilt or yaw. But i am getting 16xxx something number instead of g. According to mpu 's sensitivity level i am supposed to divide the number to get the g number. If you are interested. Your help would be much appreciated. Thanks in advance.

@tamayaytam
The MPU-6050 will output it's values as a 16-bit signed number (-32768 to 32767). Depending on the which scaling you use 1g is a certain value. The default is +/-2g. Then 1g would be equal to 16384. Simply divide your readings by 16384 if you want to convert the reading into g's and are using the default setting.
For more information see the datasheet: http://www.invensense.com/mems/gyro/documents/PS-MPU-6000A.pdf at page 13.

Hi!, I'm using IMUs for my final project, and I've been studying Kalman filters implementations for a while.

Studying Lauszus code, I see that Kalman filter gain (K0 and K1) is obtained from the error covariance matrix (P00, P01, P10 and P11) multiplied by the time between readings (dt) and added some constants.

If I see how Kalman gains evolve in time, I see that K0 approachs to 1 and, if time between readings remain constant, K1 approachs to -1/dt. If this is OK, where does Kalman filter improve the Complementary Filter?

Thanks and greetings from Argentina

@juanv
You are totally right, see this comment on my blog: TKJ Electronics » A practical approach to Kalman filter and how to implement it down to this one: TKJ Electronics » A practical approach to Kalman filter and how to implement it.

Hi, I'm a newbie, but i would like try with this IMU Pololu - AltIMU-10 Gyro, Accelerometer, Compass, and Altimeter (L3GD20, LSM303DLHC, and LPS331AP Carrier).
What's is your opinion?. Can I implement your algorithm with this IMU?

Albert

@AlbertG
You can use any IMU you like :slight_smile:

Hello, im doing my little Self Balancing robot project, got 4 days left to show it to the pubblic, ive calculated the PID for the motors (using DC motors) and this forum was the GOD for me, better said Lazarus is God! So i wondered if this forum is thread is still opened and you still answer newbie quesitons. Anways ill just ask away, ive obtained the data from my sensor, ill try to attach it, all the action is on the X axis, do i need the kalman Y calculus to be done? or how does it work? From what ive read, i only have to pass to my motors the kalman X value through PWM, thats analogWrite(kalmanX, pin number); ... wait... this is where i have the problem... where does my PID loop go? i should pass the kalman calculus value to the PID and then pass the PID calculus to the motors... no? Im confused, ive read so many to get this thing working, now my sensors are accurate have accurate readings thanks to you, ive calculated the pid loop, now i have to put it all together and imm a little confused. Ima try attaching that .txt sensor reading, i hope youre still here and still have some time to post a reply :slight_smile:
Thanks in advance for the answer!
Best regards,
Cata

Data Lazarus.txt (298 KB)

@LillSlick
You should check out my robot at Kickstarter: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit.
The source code is available at Github: Balanduino/Firmware/Balanduino at master · TKJElectronics/Balanduino · GitHub. It will pretty much answer all your questions :wink:

WOW thanks for the reply on such a short notice! Your code is very elaborate, youre very thorough. I dont need complicated stuff, only want to balance the damn thing :slight_smile: no time for other stuff, i have to present my work in 4 days or else im screwed, it is my first robotics project, im an automation engineer and this project was proposed by my boss and was stupid enough to tell a deadline without even having the hardware, they gave me all the hardware 3-4 days ago and they said 1week ... so now after ive done the phisical part i would like to get the code done, ill use your data acquisition and kalman filtering and i have to get the motors to balance the robot. I had a look on your code and saw you have much control over the motors, im not using the encoders, i dont care if one of the wheels will have a different caracteristic, plus im using two PmodHB5 h-bridges, the pins: Dir/En/Vcc/GND ive merged them and i thoat ill send the same command for them all, if dir is 1, only 1 pin is high and bolth h-bridges should behave in the same manner, and the PWM is the same for both ill send 1 pwm to 1 pin which runs both h-bridges at the same time... thats what i thoat, and wanna get this sorted as soon as possible so i dont get buttfucked by my bosses :)). Its kinda hard putting up with 10 hours of work and after that, concentrate on the "other project" but they dont care. Can u have some pointers, how should i implement a simplified version of ur code so that the robot balances itsself based on the kalman calculus uve done in the mpu6050 file and thats about it... is it complicated? jasons done some cool stuff with the motor control, he has an if decision there which does two types of commands for the motors, one aggressive command and one conservative command, thats kinda nice but i wont have the time to experiment with it. So, i only need the basics working, after that, ill build on it as you built on your own project which frankly is a super-project! Im very interested in this project, i love it, i want to take it to a whole new level, but for my own, not for some fool who thinks hes gonna unemploy me. Sorry for misspelling ive done so far, hope you can give me some pointers :). Thanks! Best regards, Cata PS: ill post the code i was talking about :slight_smile:

BalancingRobot.rar (4.11 KB)