The accelerometer is sensing the pull of gravity. If the accelerometer is flat the X and Y accelerations are 0 and the Z acceleration is -1g. As the accelerometer tilts the Z acceleration gets closer to 0 and the X and/or Y accelerations go up or down. Using some trig you can determine the pitch and roll angles.First thing to do is to calibrate the accelerometer so you can subtract the zero offset and calculate acceleration in g.
When you tilt the X axis up past 90 degrees, the Z axis goes through zero and then starts going positive. Since yrot is based on the X and Z measurements, when Z goes from negative to positive you get a different answer, even though Y is staying near zero.To get pitch and roll, try:Code: [Select]pitch=atan2(ax,sqrt((ay*ay)+(az*az)))roll=atan2(ay,sqrt((ax*ax)+(az*az)))Note that the result is in Radians so if you want degrees you still have to convert. You shouldn't get a problem with both arguments to atan2() being zero unless your accelerometer is in free-fall.
sqrt((x*x)+(y*y)) calculates the length of the hypotenuse of a right triangle with sides X and Y (Pythagorean Theorem)Unfortunately it gives the absolute magnitude and destroys information about quadrant. I guess you need the sign information for the various forces to determine pitch and roll correctly.
Please enter a valid email to subscribe
We need to confirm your email address.
To complete the subscription, please click the link in the
email we just sent you.
Thank you for subscribing!
via Egeo 16