How do I get the angle of the phone relative to the Earth's magnetic field?

2.8k views Asked by At

I was planning to calculate the angle the phone is relative to the earth's magnetic field. For this I planned to use Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR. In Geomagnetic Rotation Vector, it explains that event.values return the same values as in Rotation Vector which are,

  • x * sin(θ/2),
  • y * sin(θ/2),
  • z * sin(θ/2),
  • cos(θ/2)

What is x,y,z values in above given values? By using these values Can i calculate the angles between phone's X axis and earth's X axis, phone's Y axis and earth's Y axis, and phone's Z axis and earth's Z axis?

P.S.: θ is described as an angle θ around an axis (x, y, or z)

Phone's X,Y and Z axis Earth's X,Y and Z axis

2

There are 2 answers

0
Keith Aylwin On BEST ANSWER

For Rotation vectors:

The x value is the "pitch" of the phone: At 0 it is face up, at -1, 1 it is facing the ground. This rotates around the x-axis in the diagram on the right above.

The y value is the "Yaw" of the phone: Decreasing the yaw rotates counter-clockwise, increasing it rotates clockwise around the y-axis.

The z value is the "Roll" of the phone: Increasing or decreasing the roll rotates the phone around the z-axis.

Each of the coordinates are a value from -1 to 1 because of they are trigonometric functions. The Android Documentation states that they x,y,z values are equal to the last three components of a unit quaternion (cos(θ/2), xsin(θ/2), ysin(θ/2), z*sin(θ/2)). Using quaternions is beyond the scope of my knowledge but here's a tutorial if you would like to learn how they work. http://3dgep.com/understanding-quaternions/

The way I figured out how to understand these values came from playing with the pitch/yaw/roll of the phone in this API for simulating movement on android phones https://code.google.com/p/openintents/wiki/SensorSimulator

For example a phone with a Yaw of 0, a pitch of 0, and a Roll of zero is lying flat, with the screen upwards (towards the sky) with the top of the phone pointed north (Again play with the SensorSimulator and you'll be better able to visualize this, it's really hard to explain through words)

If you want to have an easy way to calculate the reference to the earths coordinate system call getRotationMatrix() on your SensorManager: Android doc

Here is a demo of a program using the rotation vectors: https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/os/RotationVectorDemo.java

0
StupidHackerGuy On

Get the phones current angle, then get the angle or property of comparison and subtract them from each other.