javascript 'deviceorientation' event - what sensors does it measure? javascript 'deviceorientation' event - what sensors does it measure? android android

javascript 'deviceorientation' event - what sensors does it measure?


All the orientation values are also very noisy for me. Shakily hand, Euler angles, magnetic interference, manufacturing bug, ... Who knows?

I made a small exponential smoothing. That is, I replaced the fluctuating event.alpha by a smoothed value, which was conveniently called alpha:

alpha = event.alpha + s*(alpha - event.alpha), with 0 <= s <= 1;

In other words, each time a new observation is received, the smoothed value is updated with a correction proportional to the error.

  • If s=0, the smoothed is exactly the observed value and there is no smoothing.
  • If s=1, alpha remains constant, which is indeed a too efficient a smoothing.
  • Otherwise alpha is somewhere in between the observed and the smoothed value. In facts, it is a (weighted) average between the last observation and history. It thus follows changes in values with a certain damping effect.
  • If s is small, then the process is near the last observation and adapts quickly to recent changes (and also to random fluctuations). The damping is small.
  • If s is near 1, the process is more viscous. It reacts lazily to random fluctuation (and also to change in the central tendency). The damping is large.
  • Do not try s outside the 0..1 range, as this leads to a negative feedback loop and alpha starts soon to diverge with larger and larger fluctuations.

  • I used s=0.25, after testing that there was no significant difference for s between 0.1 and 0.3.

Important: When using this method, do not forget to initialize alpha, outside the addEventListener function:

var alpha = guestimate (= here 0);

Note that this simple adaptive smoothing works in many other cases, and is really simple programming.


The device orientation is obtained by sensor fusion. Strictly speaking, none of the sensors measures it. The orientation is the result of merging the accelerometer, gyro and magnetometer data in a smart way.

I noticed the values for "alpha" are really noisy - they bounce around non-stop even if the phone is at rest on my desk, while the other two remain steady.

This a common problem with the Euler angles, try to avoid them if you can.

By the way, the Sensor Fusion on Android Devices: A Revolution in Motion Processing video you link to explains it at 38:25.

Also, since the gyroscope measures angular velocity, I presume this event listener is integrating it automatically - is the integration algorithm as good as any? Does it use the accelerometer to correct the drift?

Yes, the gyro drift is corrected with the help of the accelerometer (and magnetometer, if any) readings. This is called sensor fusion.

In this google tech talk video, from 15:00 to 19:00, the speaker talks about correcting the drift inherent in the gyroscope by using the accelermoter, as well as calibrating the orientation with respect to gravity: http://www.youtube.com/watch?v=C7JQ7Rpwn2k How would I go about doing this?

If you have orientation then somebody already did all this for you. You don't have to do anything.


Use a directional cosine matrix or a kalman filter. You can use the accelerometer to plot it or the gyroscope or a combination of both. The drift can be calculated with a bit of machine learning. I think that motion fusion is a part of Texas instruments calibration package. I could be wrong. But its not hard to check. multiply 3 rotational matrices, be grand.. http://www.itu.dk/stud/speciale/segmentering/Matlab6p5/help/toolbox/aeroblks/euleranglestodirectioncosinematrix.html