Guide to gyro and accelerometer with Arduino including Kalman filtering

ok ok :slight_smile: tks :slight_smile:

m gettin error like this while running the program.....
wat does it mean..... pls help me......

avrdude: stk500_getsync(): not in sync: resp=0x00
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51

Are you using a programmer? :slight_smile: That error is related to programmers.

Hello Everybody!

Lauszus, I was stuck on page 17 and thought you never replied! I am so silly!

Anyway, thanks for the support. Look at what I have till now : filter.wmv - YouTube

I am using complementary filter.

	 x_angle=(0.993)*(x_angle+GyroRate*dt)+(0.007)*(AccAngle);

I modified the values to take more of the gyro output because if I use the defaults
the robot gets lazy and needs big tilt before trying to react.

 x_angle=(0.98)*(x_angle+GyroRate*dt)+(0.02)*(AccAngle);

How can I also modify the kalman filter to take more out of the gyro?

I think the secret is here... Right?

   float Q_angle  =  0.001; //0.001
    float Q_gyro   =  0.003; //0.003
    float R_angle  =  0.03;  //0.03

But which is which???

#georpo
Q_angle is the process noise covariance for the accelerometer, Q_gyro is the process noise covariance for the gyro and R_angle is the measurment noise covariance.
You should try to lower the Q_gyro value and raise the Q_angle value :slight_smile:

complementary behaves better for me...

still needs some pid tuning....

#georpo
Awesome job!! Where did you buy the belt, gears and the shaft? :slight_smile:

Thanks!

Belts and pulleys are from http://www.econobelt.com/e-store/Pulleys.htm
They are the cheapest I could find after much searching...

Shafts, bearings and bearing housing are all from class 500 RC helicopter ( I also fly RC models helicopters, airplanes etc)
You can find many things at hobbyking

Motor base is made of FR4 (the material we use to make printed circuit boards ) cut with a CNC router that I have at where I work.

Of course the shaft needs cutting and you also need a 8mm adapter for the wheels.

Until now complementary filter works better for me. Do you think that kalman will make a big difference?
Is it worth to spend some more time for it?

For now it seems to balance well enough but it needs more clever way to control the attitude, for example if I push it hard enough
it will start chasing itself and finally fall.. I think that I should add another K for the gyro rate. If we are falling too fast, go faster!

This robot is just a lesson for me because I need all the balance knowledge to make a self flying quadcopter. I hope....

Thanks for the links, might need it some day :slight_smile: Or useful to others.

I don't think you will need a Kalman filter, for instance these guys: The DIY Segway used a complimentary filter just like you.

You can prevent it from falling when pushed, if you add encoders or add body velocity (read gyro) into the PID loop.
What constants did you end up with for the IMU for the complementary filter? I haven't really experimented on that, as I just used some I found on the web.

Nice. A loot of people are trying to do just that, so there's a ton of info out there :slight_smile:

x_angle=(0.993)*(x_angle+GyroRate*dt)+(0.007)*(AccAngle);

My gyro is IDG500 and my accelerometer is MMA6271 both soldered on a custom made pcb.
I am using only 1 axis of the accelerometer and calculate the angle with asin as you suggested.

Okay thanks for the info :slight_smile: Might try to fine-tune the complimentary filter for my IMU some day and see how it perform compared to the Kalman filter.

I tried adding angular velocity and introduced a new K (kv) to the PID.
My dt is fixed at 10mS so it is omitted from the calculation.

    pid_v = Kv *(angle-LastAngle);
    pid_value = (pid_p + pid_i + pid_d + pid_v);

   LastAngle=angle;

This did not work :frowning:

I also tried the same with just the gyro value but no luck.

I just want to get the robot move faster when pushed faster so it will overshoot the balance point and brake.

Hmm, actually I'm building a segway too.
This is an old video:

It balance a bit better now, but I also got problems, when it's being pushed. I would like it to do that before I add remote control with a PS3 Controller (see my github: https://github.com/TKJElectronics/USB_Host_Shield_2.0). You might have a look at this robot:
Earwig balancing robot - YouTube

He replied this, when a user ask how he made it push back:
"the key was to get it to balance just using the angle, only then did I add in body and wheel velocity (and lastly position error) , with just angle error it should stand upright but drift/stop with a? push. the body velocity was calc from 2h*sin(angle change) /dt . so my equation looks just like yours. This feeds balance PID which feeds motor PID with wheel vel also."
But I don't know that "h" stands for?

Hello Lauszus,
I am currently working on a quadrotor, for this im using a 6DOF digital imu(i2c), so i used your code for the kalman filter for it and modified the sensitivity to 14.375 and 256 , i am getting the values in the kalman from -90 to 0 to +90, however the time taken by the kalman filter to reach the final angle is very high, if i tilt the quad in one direction , while tilting it the values are like in the 100 to 200 range but when i rest the quad and after about 2 seconds the correct kalman angle is obtained. As this response is very slow for the quadrotor, how should i modify your code?Please help me Lauszus..

-Arvind Sanjeev

It sounds like it "trust" your gyro to much. Try increasing Q_gyroX. Also try changing Q_angle and R_angleX.

Regards
Lauszus

On changing rangle to a small value, the filtering is fast however on switching on the motors the angle values are not at all stable.

how can I increase the speed of your kalman filter code??...please help

What do you mean by "the angle values are not at all stable"? Do you mean like they are all over the place? That might be because that you are "trusting" the accelerometer too much, as it's really sensitive too acceleration - that's why it's called a accelerometer :slight_smile: So if the motor make the IMU start to vibrate the accelerometer will pick up these vibrations and the angle calculated by the accelerometer will be incorrect.

You should try to experiment with the three noise covariances:
Q_angle // Process noise covariance for the accelerometer - Sw
Q_gyro // Process noise covariance for the gyro - Sw
R_angle // Measurement noise covariance - Sv

By lowering "R_angle" you trust the new angle more. So you should find just the right value, where it's fast enough, but at the same time doesn't get affected by vibrations. Alternatively try raising "Q_angle" as this will "trust" the accelerometer less!

For more info see the following pages: http://academic.csuohio.edu/simond/courses/eec644/kalman.pdf and http://www.cs.unc.edu/~welch/media/pdf/kalman_intro.pdf

These three noise covariances are the key to make it faster and more correct :slight_smile:

Regards
Lauszus

Hi all,

I am using the ADXL345 and itg3200 IMU board from sparkfun.

i have read this thread and it has been very helpful.

i have communicated to both, set them and receiving there output, but my gyros raw X values are always roughly 65533, Y is 65500 +/- 1 for both and Z is roughly 8.

are these values correct? the board is steady on the table. arnt they sapposed to output close to 0 +/- the offset?

can someone show me there raw values from the gyro so i could see if maybe my gyro is not working well.

I am completely comfused with these outputs.

thank you all in advance.

Vince

Also, I am experiencing some discontinuity when i reach a particular angle as so:
18 171.00 153.00
18 170.00 153.00
18 171.00 153.00
19 171.00 153.00
19 171.00 153.00
19 172.00 153.00
27 190.00 153.00
31 189.00 153.00
29 181.00 153.00
28 180.00 153.00
35 197.00 153.00
40 200.00 153.00
45 200.00 153.00
49 200.00 153.00
52 200.00 153.00
57 200.00 153.00
58 200.00 153.00
59 200.00 153.00
60 200.00 153.00
58 200.00 153.00
59 200.00 153.00
59 200.00 153.00
59 200.00 153.00

How can i prevent this?