Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 513
Joey Avatar asked Jun 16 '12 04:06

Joey


People also ask

How to detect device orientation?

Detecting Orientation Changes in Javascript Should you need to simply detect when a user changes orientation, you can use the following event listener: screen. orientation. addEventListener("change", function(e) { // Do something on change });

What is the difference between the device Motion API and the orientation API?

The device orientation event returns rotation data, which includes how much the device is leaning front-to-back, side-to-side, and, if the phone or laptop has a compass, the direction the device is facing. Use device motion for when the current motion of the device is needed.

What is device motion?

The first-generation device motions support is a part of Device Orientation API. It allows Web applications to access the accelerometer data expressed as acceleration (in m/s2) and gyroscope data expressed as rotation angle change (in °/s) for each of the three dimensions, provided as events.


2 Answers

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.

like image 118
AlainD Avatar answered Nov 10 '22 00:11

AlainD


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.

like image 41
Ali Avatar answered Nov 10 '22 01:11

Ali