3

Can anyone give any idea to calculate the angle by which a compass needle should be rotated to point in the direction of gravity from accelerometer x, y, z values?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Nijo
  • 45
  • 5

1 Answers1

1

I think X should be 0 and y should be positive while z is near 0 for the compass to point down towards earth. (Which means the phone is held vertical).

In general, from the 0 angle, the compass' angle should be something like

float accelerometerMaxRange = 10; // This is NOT right, but it's a good value to work with
float newAngle = 0;
if (z > 9) {
    // Phone is horizontally flat, can't point towards gravity, really. Do whatever you think is right
} else {
    newAngle  = (float)(x * 90 / accelerometerMaxRange);
    if (y < 0) {
        newAngle = 180 - newAngle;
    }
}
Eliram
  • 606
  • 2
  • 9
  • 21