Android provides both the rotation vector sensor and the orientation sensor. I know they returns different data, because for vector sensor we have sin of angles, in orientation sensor we have angles. But what’s the conceptual difference? I can’t understand from the docs. Which one provides the orientation of the device in the three-dimensional space? I’m confused!
The older ORIENTATION sensors report orientation using three angles. The problem with this coordinate systems is that it suffers from “gimbal lock”: when the actual orientation vector is close to vertical, one of the coordinates goes to 90 or -90 degrees, and the remaining two coordinates become either uninterpretable or dangerously denormalized.
The newer ROTATION sensors report orientation using quarternion coordinates, which are more complicated to work with, but don’t suffer from the Gimbal lock problem. When orientation is reported using quaternion coordinates, you can determine the precise orientation of the device no matter what the orientation is.
Quaternions are also more computationally efficient. You don’t need to call expensive trig functions to apply a quaternion rotation to a vector. If the w coordinate isn’t supplied you can still compute w with a single sqrt call, compared to three sin and three cos function calls for orientation coordinates in the three angle Euler form.
Short story: the ORIENTATION-style sensors were done wrong. They were fixed in API 9, by replacing them with ROTATION sensors.
ROTATION_VECTOR sensor was introduced in API 9 and represents ‘virtual’ sensor which combines data from different sensors (usually ACCELEROMETER, GEOMAGNETIC_FIELD and GYROSCOPE) and does some smart calculations to provide more accurate data rather than using raw data from ACCEL and GEOMAGNETIC_FIELD sensors. This is called ‘sensor fusion’. More info you can find here
ORIENTATION sensor is deprecated since it is providing not very accurate data. Documentation is suggesting to use raw data from ACCELEROMETER and GEOMAGNETIC_FIELD sensors instead.
Unfortunately, I cannot provide any examples how to use ROTATION_VECTOR sensor data since I’m in process of investigation right now 🙂
Just in case you need some examples how to use raw data – feel free to ask me – I’ll post some examples, but simple googling can help you better 😉
They are conceptually same, just representationally different.
Have a look at the code of orientation sensor here.
The parameter of the function for the orientation sensor is the rotation matrix, which inturn is calculated from the rotation vector(the quats representation)
On cheap Android phones (unlike higher end iPhones) compass will work only when the phone orientation is close to horizontal (i.e. parallel to ground surface).
Technically a good compass (i.e. a floating magnetic sphere) should work at any orientation but the cheap ones don’t. Hence, to use a compass make sure that your phone is horizontal via looking at ACCELEROMETER before you use MAGNETOMETER. Hopefully Google will use better magnetometers in the future!