Home » Android » android – javascript 'deviceorientation' event – what sensors does it measure?

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

Posted by: admin May 14, 2020 Leave a comment

Questions:

If I have a simple web page and script that looks like this:

<body>
    <div id="alpha">a</div>
    <div id="beta">b</div>
    <div id="gamma">g</div>
</body>

<script>
window.addEventListener('deviceorientation', function(event) {
    var alpha = event.alpha;
    var beta = event.beta;
    var gamma = event.gamma;

    document.getElementById("alpha").innerHTML = alpha;
    document.getElementById("beta").innerHTML = beta;
    document.getElementById("gamma").innerHTML = gamma;

}, false);
</script>

I can open it up in mobile Firefox for Android and it will output 3 numbers that look like the following:

89.256125
3.109375
0.28125

Where when I rotate the device, the numbers change based on the axis of rotation. 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. I understand that alpha is my heading. I’m curious then, is it getting the “alpha” value from the compass (which has noise issues) and the other two from the gyroscope?

Another issue is when I change the pitch, for some reason the heading changes too, even if I don’t actually change the heading. I’m just curious why this is and how it can be corrected?

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?

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?

Thanks for any insights anyone may have.

How to&Answers:

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.

Answer:

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.

Answer:

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