Accelerometer angles

by para
Tags: accelerometer, angles
 P: 2 Hi, I'm new to the forum. I have a 3 axis accelerometer that I am trying to obtain roll and pitch from. Besides the accelerometer itself, I was provided the raw data values of what each axis will report when it has 0g being applied to it and when it has 1g being applied to it. Using this I am able to find and normalize how many gravity forces are currently being applied to each axis. In the rest of this post assume x, y, and z are normalized; so if x=1 then 1g is applied to the x-axis in the positive direction. I'm using the following equations: $$roll = atan(\frac{z}{x})$$ $$pitch = atan(\frac{z}{y})$$ If the accelerometer is lying flat, with the z-axis facing up, and I rotate it over the y-axis (roll), then I can use the above equation to obtain the roll from 0 to 360 degrees. Same with pitch. However I run into two problems:When either roll or pitch reach ~$$\pm$$90 degrees then the other angle experiences unusual behavior. For instance, if I roll it over to 90 degrees the pitch may suddenly become 45, 92, 120, etc (it's unstable). It also appears that if I both roll and pitch the device at the same time the values are not correct. Is there a special way to calculate angles based on this type of data, or am I just going about this wrong? It's been a while since I've done any 3d geometric math and I don't remember a whole lot. Thanks.