11

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.

Joey
  • 10,504
  • 16
  • 39
  • 54
  • 3
    Did you read http://dev.w3.org/geo/api/spec-source-orientation.html – mplungjan Jun 16 '12 at 04:52
  • How does it come up with the numbers: [That's how](http://dev.w3.org/geo/api/spec-source-orientation.html#worked-example) (Warning, scary math ahead) – Derek 朕會功夫 Jun 16 '12 at 05:02
  • That W3 spec is definitely helpful. I guess I should have known that existed. I understand the math in it, having taken linear algebra before. – Joey Jun 16 '12 at 05:42
  • I did my best to answer your questions. There is one thing I did not get. Could you please expand on this: "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?" – Ali Jun 16 '12 at 07:25

3 Answers3

3

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.

AlainD
  • 6,187
  • 3
  • 17
  • 31
  • Smoothing over more than two samples could help? – ErikE Dec 30 '12 at 19:26
  • The thing about exponential smoothing is you are smoothing over the smoothed value + a new sample, so practically speaking, you are smoothing over many samples – Ted Brownlow Jun 15 '20 at 18:00
  • Yes, that the exact principle of the exponential smoothing. You can show it is a weighted average with weight w_t = s^t (hence the name exponential). It an average over the whole history with values weighting less and less as times goes by. – AlainD Jun 17 '20 at 16:47
2

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.

Community
  • 1
  • 1
Ali
  • 56,466
  • 29
  • 168
  • 265
  • Thanks for the explanation. I'm actually quite relieved that I don't have to reinvent sensor fusion. Only thing I still can't understand is how it manages to point to true north instead of magnetic north? It doesn't know my latitude or longitutde. – Joey Jun 16 '12 at 08:55
  • Good question, I can only guess. You are right, it can only measure the magnetic north. However, the angle between the gravity and the magnetic north can be used to estimate the latitude. Plus, the GPS or the service provider can tell the device with the (fairly accurate) geographical location. So, I don't know. What makes you think it knows the true north? How much does the true and the magnetic north differ at your place? – Ali Jun 16 '12 at 09:50
  • Well it gives me a reading of north (alpha = 0 or 360) at a few degrees clockwise from where my compass app tells me is north. And I live around Seattle where the difference between magnetic north and true north is significant since magnetic north is over Alaska. – Joey Jun 16 '12 at 19:04
  • Interesting. As I said before, I can only guess, but in principle the device could know its geographical location from the service provider. It was readily available more than 10 years ago. Nowdays, we also have the GPS. My uncle lived in Seattle and I visited him in 1990 :) – Ali Jun 16 '12 at 20:14
0

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