Guide to gyro and accelerometer with Arduino including Kalman filtering

Interesting. I need to look over this compensation for drift then. However, Im still not sure why one cant get acceleration on the Z axis (Im British though, so Z axis means depth to me but in this case, I believe we mean yaw). There is a yaw gyro indeed but it is claimed there is a yaw accelerometer as well. Why is it there if it can't be used? It seems confusing. Clearly I must be missing something.

Yes it is yaw (the rotation around the z-axis). The reason why you can not measure yaw with a accelerometer is because the gravitational force from earth does not change when you rotate the IMU horizontal. In theory you will not measure any difference at all, in pratical however you see the acceleration from the movement. I use the z-value from the accelerometer together with pitch and roll to calculate the force vector (this is theoretical 1, but in practice there is a small difference). I also use the z-value from the accelerometer to know when the IMU is tilted more than 90 degrees to both side, so I can get 360 degree resolution instead of only 180 :slight_smile:

  • Lauszus

.."while the accelerometer measures Earth gravitational acceleration (g) in three dimensions"..
not only, it measures the acceleration of the unit as well..P. :slight_smile:

Yes, I understand in the case of the accelerometer now. That does make sense. So, do you use the gyro at all for the Yaw calculation?

Pito: exactly! That is why you can not count on the accelerometer on short terms, because it is to unstable. For example it will jump all over the place if you shake it. That is why you have to combine it with a gyro, that do not change when you for example shake it.

Onidaito: I do not use it right now, but if you look in my code I actualy calculate the yaw. Just write Serial.println(gyroZangle); and you will see the yaw, but remember it will drift!

If it is an ITG a simple offset calibration is enought to cancel the drift, or almost all of it.

What do you mean? Will you just keep calibratrating all the time, to compensate for drift?

  • Lauszus

hey Lauszus,

I got my sensor (nice and fast too,thanks for the germany tip!)

I have it all connected and it seems to be working. One thing i notice though is that tilting forward on the y-axis gives me values up to 60 but backwards on the y-axis i only get as high as 20. on the x-axis then i an even 40 on each side.

I notice you wrote to add in a piece of code to make it read full 360, is that why mine isn't reading right?

i tried putting in that code after
" R = sqrt(pow(accXval,2)+pow(accYval,2)+pow(accZval,2));//the force vector
accXangle = acos(accXval/R)*RAD_TO_DEG-90;
accYangle = acos(accYval/R)*RAD_TO_DEG-90;"

But didnt change anything.

also i'm not connecting the z-axis because i don't need it. I assumed it wouldnt make a difference.

Just wanted to say thanks again for the code. Looks like my project will become what i dreamed :slight_smile: I'll post a video up here when its done

Glad I could help. Have you connected the accelerometers z-value? If not that is why you data is getting weird. Remember that the code uses the z-value from the accelerometer to calculate the force vector.
Looking forward to see your project in action :slight_smile:

  • Lauszus

no i didn't plug it in because i didnt need to measure the x-axis.

I am using a pressure sensor also, and using an arduino Uno so only have 6 inputs :~

so it needs to be used for calculation even if you dont plan on printing the value?

Don't you mean the z-axis (yaw)? You need it for my code, but yes it it possible to calculate the angle with just one two values I think, haven't really researched on that, because it hasn't been a problem to me.

  • Lauszus

yes sorry i meant z-axis.

ok, i'll try research into that. Or else i might be able to get an arduino mega.

Worst case scenario i can still work with the values i am getting

Okay. Please upload the code for calculating the angle with only two values from the accelerometer when you get it working :slight_smile:

  • Lauszus

I thought a bit about it, and actually you can calculate the axis with just one value.

In the code it says:

accXangle = acos(accXval/R)*RAD_TO_DEG-90;
accYangle = acos(accYval/R)*RAD_TO_DEG-90;

Instead you could write:

accXangle = asin(accXval)*RAD_TO_DEG;
accYangle = asin(accYval)*RAD_TO_DEG;

Then you should get an angle very close to the first one!! It is not as precise as the first one, but that kind of precision is only important if you are building a balancing robot or something like that.
The resolution will also only be 180 degrees.

That should fix your problem :slight_smile:

  • Lauszus

Guys, I really think I should be getting better yaw values from the Gyro. I found this:

http://code.google.com/p/imumargalgorithm30042010sohm/

This shows some pretty decent output which leads me to believe that my unit could be broken as the Yaw value doesn't change at all when I rotate around that axis. I noticed that on the sparkfun page for this item, someone else had a similar problem. I've emailed the chap from the link above in the hopes that I can figure this out. At the moment, the lack of decent/any yaw (no need to use a magnometer just yet) is halting my project :frowning: Im wondering if there is something else im missing here. I've tried the kalman filter approach as well and that seems worse than just using the raw gyro values :S

You can not use the kalman filter for the yaw value, because the accelerometer can not measure the yaw :slight_smile:
Hope you get it working!

  • Lauszus

Yeah, I've done a little more research and I see what you mean. I guess i just need to integrate over time for yaw off the gyro and then reset when the drift gets too much... until maybe I get a magnetometer as well!

Thanks though! Its been a big help!

Hey guys,

Is it possible to also use the kalman filter for only an accelerometer? It will only use one axis (of the three available). I just need a smooth movement of the servo I'm using

You can not use the kalman filter with only one input. Sorry but you need a gyro too, that is why it called "sensor fusion" :slight_smile:
You could perhaps use some other kind of smoothing algorithm.

  • Lauszus

Hey Lauszus, I finished my final year project in college. I thought I would show it on here so people can see what can be done with the code and sensor. Thanks again for you help!