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?
Asked
Active
Viewed 2,071 times
1 Answers
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
-
The top question is, how much is the `accelerometerMaxRange` ;) – Tony Bogdanov Jun 25 '13 at 16:35