简体   繁体   中英

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

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?

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

手机的X,Y和Z轴地球的X,Y和Z轴

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), x sin(θ/2), y sin(θ/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

获取电话的当前角度,然后获取比较的角度或属性,并将它们彼此相减。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM