I have accelerometer values for the 3 axes (usually when there is only gravity contains data between -1.0 and 1.0 ):
float Rx; float Ry; float Rz;
I make some calculations, then I get the angles for each axis.
float R = sqrt(pow(Rx,2)+pow(Ry,2)+pow(Rz,2)); float Arx = acos(Rx/R)*180/M_PI; float Ary = acos(Ry/R)*180/M_PI; float Arz = acos(Rz/R)*180/M_PI;
Then I set the values for the box angles in opengl
rquad = Arx; yquad = Ary;
Which rotates my box:
glRotatef(yquad,1.0f,0.0f,0.0f); glRotatef(rquad,0.0f,1.0f,0.0f);
It works on a hemisphere. I would like to use the full sphere and I know that I have to use the Arz value to make it work, but I don't know how can I use that for this rotation. Could you help me?
Update: The final answer is in my case:
rquad = -atan2(Rx/R, Rz/R)*180/M_PI; yquad = -atan2(Ry/R, Rz/R)*180/M_PI;
Roll is rotation on the X-axis, which means the object is rotated to the right or left: If the accelerometer is tilted right, the roll will be a positive angle (between 1° to 180°). If the accelerometer is tilted left, the roll will be a negative angle (between -1° to -180°).
If you want to measure tilt in both x and y axis with a 2-axis accelerometer then you can simply use sin-1(a) where a is the output from one axis of the accelerometer. The component of gravity acting on the x axis is a sine function whilst that acting on the y axis is a cosine.
In some applications, where the net acceleration or force on a system over time is gravity, an accelerometer can be used to measure the static angle of tilt or inclination.
Accelerometers can be used for measuring both dynamic and static measurements of acceleration. Tilt is a static measurement where gravity is the acceleration being measured. Therefore, to achieve the highest degree resolution of a tilt measurement, a low-g, high- sensitivity accelerometer is required.
The correct answer is:
Roll = atan2(Y, Z) * 180/M_PI; Pitch = atan2(-X, sqrt(Y*Y + Z*Z)) * 180/M_PI;
Source: https://www.nxp.com/docs/en/application-note/AN3461.pdf (page 10, Eqn. 25 & 26)
uesp's answer is wrong. It looks like an acceptable approximation until pitch and roll both go above 45 degrees.
I may be assuming a different orientation convention, but even if you swap axes and invert values in any consistent way, uesp's computations will never be equivalent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With