here I would like to tell you how to use the output from sensors to measure the angle that you are looking for.
Firstly, lets see what we exactly want.
we want to sense the rotation of our sensor in three axis. this three axis can be considered as X, Y and Z or pitch, roll and yaw. I prefer X, Y and Z orientation since it is more mathematically represented.
now lets consider that we want to find the angular rotation over each axis using accelerometer. Well, we can find just two angles using accelerometer (if you don't know why don't worry you will see understand soon).
Now lets find the angle for one of the axis:
before we start, you should be agree that gravity is always perpendicular to the surface of earth. I know that the vehicle will have some acceleration to some directions, but compare to g, they are significant, so we can ignore it.
This is the diagram. As you can see, we assumed that gravity is always perpendicular to the earth surface.We know that our readings from accelerometer comes from three different axis. In this case we consider the X and Z axis readings.Now, we have to understand something which is really important in, when we have three axes the vector addition of acceleration in X and Z axis is not equal to gravity. However, in the diagram it may seem that it will be equal.
I talk about how to read your accelerometer here.
now lets say we have the reading in any unit. We call them, X_acceleration, Y_acceleration and Z_acceleration.
We have one variable which is called Gravity, recall from here ( How to read the output from accelerometer? ).
Now we find the angle theta which is equal to Y_angle. (it is rotation over the Y axis of sensor)
from the diagram, you can see that Y_angle = theta =cos-1( X_acceleration / Gravity )
(think of the triangle, I tried but I can't explain in words :( sorry )
we know that cos-1 has a range [0,180], but we may need to have a range from [0,360], in this case we use the Z axis reading. For my quad I used the range equal to [-90,90], this makes it easy for PID control, and much easier to understand.
now, we have Y_angle, in any form that we want from accelerometer.
For the case of gyroscope we need to use what I explained here. (How to use gyroscope?)
Y_angle can be obtain by Y_angle = previous_Y_angle + angle_change_y
previous_Y_angle is the last Y_angle we found which was measured at the last time that loop was repeated.
angle_change_Y is the out put of gyro in Y axis.
Now we have measured Y_angle using gyroscope.
Now, if you think you will notice that we do not have previous_Y_angle for the first time that we start the vehicle. They are two different approach, 1) always start the vehicle when it is flat so the previous_Y_angle is zero 2) using accelerometer to find previous_Y_angle and counting on accelerometer reading. These are the ways comes to my mind and I don't think there is any other way. You may use average or any filter in order to read the data from accelerometer, because as you know accelerometer has a lot of fluctuation. You will read that in detail in Shane's PDF later.
They are many ways to combine these values which are explained here:
http://web.mit.edu/scolton/www/filter.pdf
I have used the complimentary filter, it works fine.
this does not contain the Kalman filter, which I did not understand it myself, but you may understand it from this website: (they are some other parts that they have explained, but I think I have explained better ;) )
http://www.starlino.com/imu_guide.html
I never understood this. It is just so hard.
I think Shane has explained everything in that PDF and everyone will understand it.
The last obvious part is that after that filtering is done you have to do this:
previous_Y_angle = Y_angle_output_from_filter ;
this is necessary for gyroscope future readings.
Now, if you want to test your filter output read this.