Buggy Geolocator drivers: How to calculate Heading when your driver doesn't

I've been evaluating the location sensors in a number of Windows 8 tablet and ultrabook systems recently, and as you might expect I have come across the occasional buggy driver. Sometimes the bugs are severe enough that the location sensor doesn't function reliably or even function at all, but I have seen cases where the sensor returns partial data in the Geocoordinate class. To pick a recent example, the other day I was testing a tablet that embeds a GNSS: a Global Navigation Satellite System receiver, which is capable of using GPS, GLONASS, Galileo, or Beidou navigation systems. This tablet should have provided complete location data to Windows Store apps that include position, speed, altitude, heading, and so on, but when I ran my tests I got results like this:

45.54203 N, -122.96043 E, 109 m Alt, 1.2 m/s, <null> deg, +/-32 m, +/-10 m Alt

Notice the part where the heading is reported as "<null> deg". This is an example of a bug where the driver is returning incomplete data: we have a position and altitude, but even though we have a speed we don't have a heading.

The bad news is that, as a developer, you will have to deal with this in your apps. The tablet I was using was a consumer device purchased at retail and it shipped with a driver that has a bug, and any location-aware app running on this system will not report a valid heading. You don't have any control over who will install your app, or where, and it's very possible (if not guaranteed) that it will get run on systems with buggy drivers. The end user won't know or care why your app doesn't work properly: they'll just know that it doesn't.

The good news is, little problems like this one are pretty easy to work around. It just requires a little program logic and a bit of math.

How to calculate heading when the location provider doesn't

The first step is, of course, to determine whether or not the system you are running on has a problem. How do you know if you are not getting proper heading information? You run both of the following tests once you get a valid position fix:

  1. The Heading field of the Geocoordinate object should be non-NULL if the speed is non-zero (and non-NULL). In plain English, if you are moving then you should have a valid heading.
  2. Ensure that the reported Heading is sensible.

The first test is pretty easy, and might look something like this in C#:

if ( coordinate.Speed > 0 && coordinate.Heading == NULL ) broken_heading= true;

The second test is a bit more involved: you must first calculate the heading value yourself, and then compare it to the reported value. If the two are off by a significant margin, then the system's driver might be reporting bogus data. In theory, your calculated heading and the software driver's reported heading should be very close since they use the same method (more on that below), but in practice the embedded GPS might be employing some filtering or other post-processing of the data, or doing its calculations at a slightly different time. To play it safe, you should validate the heading information only when it is not in flux (i.e., the user appears to be moving in a striaght line).

If both tests fail, then you should calculate heading yourself and present that value to the user.

Calculating heading

GPS devices calculate their heading from the last two position reports: they take where you were before and where you are now, and find the bearing from the former to the latter. Unlike an magnetic or electronic compass, a GPS compass requires the device to be moving, which is why GPS devices report heading-- a direction of movement-- specifically, and not a bearing. Since the GPS is just looking at the previous and current position reports to determine the heading, you can do the same thing and arrive at, in theory, the exact same value.

In my blog post "Calculating a bearing between points in location-aware apps" I provide a method for calculating a bearing between two points that we can use to get our heading, but that's really overkill in this situation. Most GPS devices update their position every 1 or 2 seconds, and a real-world object-- even a high speed one such as an airplane-- travels a very short distance relative to the circumference of the earth in such a short amount of time. That means we can use a local, flat earth approximation and get our result using fewer trig functions.

Given latitudes Φ1 and Φ2, and longitudes λ1 and λ2 (you'll need to convert them to radians):

Θ = atan2( sin(Φ1) * Δλ, ΔΦ)

This function will return the angle in radians from -π to π but what we want is an angle in degrees from 0 to 360. To accomplish this, we convert to degrees, add 360, and take the modulo 360:

Θd = ( Θ * 180 / π + 360 ) % 360

I compared the results of this formula with the output from a working location driver (for clarity, I have limited the coordinates to 3 decimal places):

Point Latitude Longitude Speed Heading Calculated Heading
1 45.577 -122.97 24.8 6.8 n/a
2 45.577 -122.97 24.1 9.6 9.6
3 45.577 -122.97 23.8 12.7 12.3
4 45.577 -122.97 22.6 16.4 17.1
5 45.577 -122.969 21.4 20.8 22
6 45.578 -122.969 20.5 24.1 24.9
7 45.578 -122.969 20.1 25.7 25.9
8 45.578 -122.969 17.5 27.6 27.9
9 45.578 -122.969 15 27.7 26.9
10 45.578 -122.969 12.5 28.2 27.2
11 45.578 -122.969 10.8 29.6 29.3
12 45.578 -122.969 9.1 42.5 42.8
13 45.578 -122.969 8.6 46.2 51
14 45.578 -122.969 8.3 79.9 78.6
15 45.578 -122.968 8.7 95 95.9
16 45.578 -122.968 10.7 108 111.2
17 45.578 -122.968 12.5 109.2 110.5
18 45.578 -122.968 14.2 108.8 110.1
19 45.578 -122.968 15.5 107.7 109.8
20 45.578 -122.968 16.5 106.1 107.1
21 45.578 -122.967 18.3 104.3 104.7
22 45.578 -122.967 18.9 103.1 103.2
23 45.578 -122.967 20.3 102.2 103
24 45.578 -122.967 21 101.7 101.9
25 45.578 -122.966 21.4 101.5 102
26 45.578 -122.966 22.2 102.9 103.2
27 45.578 -122.966 22.6 104.7 105
28 45.578 -122.965 22.7 105.9 105.5
29 45.578 -122.965 23 107.3 106.9
30 45.578 -122.965 23.6 109.4 109.1

As you can see, the heading values line up quite well, to within a degree in most instances. Only when the heading was changing rapidly was there a significant variation.

What this shows is that with just a few additions to your code you can detect, and compensate for, buggy location drivers that don't properly report a heading to the Location API. It's extra work, yes, but it's work that will pay off when, invariably, your app runs on an affected system. This extra level of robustness can mean the difference between an app that works properly and one that does not, and it might just give your app an edge over your competitors'.

Part 2: Calculating speed

§

Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione