Calculate compass heading from DeviceOrientation Event API
firefox, google-chrome, html, math
Solution
The steps you need to determine the compass heading according to the worked example provided in the specification* are as follows:
- Convert returned DeviceOrientation `alpha`, `beta` and `gamma` values from degrees to radians as `alphaRad`, `betaRad`, `gammaRad`.
- Compute rotationA (`rA`) and rotationB (`rB`) components per the worked example in the specification using `alphaRad`, `betaRad` and `gammaRad` (as shown in the example code below).
- Compute `compassHeading = Math.atan(rA / rB)`.
- Convert returned half unit circle headings to whole unit circle headings in the range [0-360) degrees.
- Convert `compassHeading` from radians back to degrees (optional).
Here is the worked example from the specification implemented in JavaScript:
function compassHeading(alpha, beta, gamma) {
// Convert degrees to radians
var alphaRad = alpha * (Math.PI / 180);
var betaRad = beta * (Math.PI / 180);
var gammaRad = gamma * (Math.PI / 180);
// Calculate equation components
var cA = Math.cos(alphaRad);
var sA = Math.sin(alphaRad);
var cB = Math.cos(betaRad);
var sB = Math.sin(betaRad);
var cG = Math.cos(gammaRad);
var sG = Math.sin(gammaRad);
// Calculate A, B, C rotation components
var rA = - cA * sG - sA * sB * cG;
var rB = - sA * sG + cA * sB * cG;
var rC = - cB * cG;
// Calculate compass heading
var compassHeading = Math.atan(rA / rB);
// Convert from half unit circle to whole unit circle
if(rB < 0) {
compassHeading += Math.PI;
}else if(rA < 0) {
compassHeading += 2 * Math.PI;
}
// Convert radians to degrees
compassHeading *= 180 / Math.PI;
return compassHeading;
}
window.addEventListener('deviceorientation', function(evt) {
var heading = null;
if(evt.absolute === true && evt.alpha !== null) {
heading = compassHeading(evt.alpha, evt.beta, evt.gamma);
}
// Do something with 'heading'...
}, false);
You can also view a demo of the code provided above.
As of the time of writing (17th Feb 2014) this currently works in:
- Google Chrome for Android
- Opera Mobile for Android
- Firefox Beta for Android
Other browsers do not yet conform to the DeviceOrientation calibration described in the DeviceOrientation Event specification and/or do not provide `absolute` DeviceOrientation data values making it impossible to determine `compassHeading` with non-complete data.
* Determining the compass heading of the horizontal component of a vector which is orthogonal to the device's screen and pointing out of the back of the screen.
Problem
For an augmented reality web app for smartphones I'm trying to calculate the compass heading when the user is holding the device in their hand, with the screen in a vertical plane and the top of the screen pointing upwards. I have taken the suggested formula from http://dev.w3.org/geo/api/spec-source-orientation (see Worked Example) and implemented the following function: ``` function compassHeading(alpha, beta, gamma) { var a1, a2, b1, b2; if ( beta !== 0 || gamma !== 0 ) { a1 = -Math.cos(alpha) * Math.sin(gamma); a2 = Math.sin(alpha) * Math.sin(beta) * Math.cos(gamma); b1 = -Math.sin(alpha) * Math.sin(gamma); b2 = Math.cos(alpha) * Math.sin(beta) * Math.cos(gamma); return Math.atan((a1 - a2) / (b1 + b2)).toDeg(); } else { return 0; } } ``` while .toDeg() is a Number object extension courtesy http://www.movable-type.co.uk/scripts/latlong.html ``` /** Converts radians to numeric (signed) degrees */ if (typeof Number.prototype.toDeg == 'undefined') { Number.prototype.toDeg = function() { return this * 180 / Math.PI; }; } ``` However, the problem is that the calculated compass heading value jumps from about -75 to 80 even if the device (Google Galaxy Nexus) is mounted to hold a static position. This seems to happen in both Google Chrome BETA and FF BETA 23. Does somebody see an error in my approach or know a more reliable way to calculate the compass heading?