tags:

views:

104

answers:

1

Hi, I have accelerometer values for the 3 axis(usually when there is only gravity contains data between -1.0 and 1.0 ):

  float Rx;
  float Ry;
  float Rz;

I make soma 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 work on 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;
A: 

I use the following calculations to convert our accelerometer readings into roll and pitch values:

Roll = atan2( sqrt(Y*Y + X*X), Z) * 180/M_PI;
Pitch = atan2( sqrt(X*X + Z*Z), Y) * 180/M_PI;

You may need to swap the X/Y/Z values or translate the Roll/Pitch depending on how your accelerometers are defined. To use them in the display them it is a simple matter of:

glRotatef (Pitch, 0.0f, 0.0f, 1.0f);
glRotatef (Roll,  1.0f, 0.0f, 0.0f);
uesp
Thanks for answering, but I tried it and I get 0°-180° for pitch and roll. I would like to get 0°-360°
Roland Soós
I tried again with clear head :) It works well
Roland Soós