summaryrefslogtreecommitdiff
path: root/cgps.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2011-09-29 21:18:51 -0400
committerEric S. Raymond <esr@thyrsus.com>2011-09-29 21:18:51 -0400
commit44c6a6b7f969e0c31044a6f478a2f6fdad392e92 (patch)
treed6a3b6cdefbb476726c73b3697b7b3025f695328 /cgps.c
parent104f47e1ebf631650bb7be15f2390c4632e82316 (diff)
downloadgpsd-44c6a6b7f969e0c31044a6f478a2f6fdad392e92.tar.gz
Refactoring and splint cleanup.
Diffstat (limited to 'cgps.c')
-rw-r--r--cgps.c80
1 files changed, 7 insertions, 73 deletions
diff --git a/cgps.c b/cgps.c
index e60099ed..8a70a6d0 100644
--- a/cgps.c
+++ b/cgps.c
@@ -131,74 +131,6 @@ static bool compass_flag = false;
#define GPS_ERROR -2 /* low-level failure in GPS read */
#define GPS_TIMEOUT -3 /* low-level failure in GPS waiting */
-/* Convert true heading to magnetic. Taken from the Aviation
- Formulary v1.43. Valid to within two degrees within the
- continiental USA except for the following airports: MO49 MO86 MO50
- 3K6 02K and KOOA. AK correct to better than one degree. Western
- Europe correct to within 0.2 deg.
-
- If you're not in one of these areas, I apologize, I don't have the
- math to compute your varation. This is obviously extremely
- floating-point heavy, so embedded people, beware of using.
-
- Note that there are issues with using magnetic heading. This code
- does not account for the possibility of travelling into or out of
- an area of valid calculation beyond forcing the magnetic conversion
- off. A better way to communicate this to the user is probably
- desirable (in case the don't notice the subtle change from "(mag)"
- to "(true)" on their display).
- */
-static float true2magnetic(double lat, double lon, double heading)
-{
- /* Western Europe */
- /*@ -evalorder +relaxtypes @*/
- if ((lat > 36.0) && (lat < 68.0) && (lon > -10.0) && (lon < 28.0)) {
- heading =
- (10.4768771667158 - (0.507385322418858 * lon) +
- (0.00753170031703826 * pow(lon, 2))
- - (1.40596203924748e-05 * pow(lon, 3)) -
- (0.535560699962353 * lat)
- + (0.0154348808069955 * lat * lon) -
- (8.07756425110592e-05 * lat * pow(lon, 2))
- + (0.00976887198864442 * pow(lat, 2)) -
- (0.000259163929798334 * lon * pow(lat, 2))
- - (3.69056939266123e-05 * pow(lat, 3)) + heading);
- }
- /* USA */
- else if ((lat > 24.0) && (lat < 50.0) && (lon > 66.0) && (lon < 125.0)) {
- lon = 0.0 - lon;
- heading =
- ((-65.6811) + (0.99 * lat) + (0.0128899 * pow(lat, 2)) -
- (0.0000905928 * pow(lat, 3)) + (2.87622 * lon)
- - (0.0116268 * lat * lon) - (0.00000603925 * lon * pow(lat, 2)) -
- (0.0389806 * pow(lon, 2))
- - (0.0000403488 * lat * pow(lon, 2)) +
- (0.000168556 * pow(lon, 3)) + heading);
- }
- /* AK */
- else if ((lat > 54.0) && (lon > 130.0) && (lon < 172.0)) {
- lon = 0.0 - lon;
- heading =
- (618.854 + (2.76049 * lat) - (0.556206 * pow(lat, 2)) +
- (0.00251582 * pow(lat, 3)) - (12.7974 * lon)
- + (0.408161 * lat * lon) + (0.000434097 * lon * pow(lat, 2)) -
- (0.00602173 * pow(lon, 2))
- - (0.00144712 * lat * pow(lon, 2)) +
- (0.000222521 * pow(lon, 3)) + heading);
- } else {
- /* We don't know how to compute magnetic heading for this
- * location. */
- magnetic_flag = false;
- }
-
- /* No negative headings. */
- if (heading < 0.0)
- heading += 360.0;
-
- return (heading);
- /*@ +evalorder -relaxtypes @*/
-}
-
/* Function to call when we're all done. Does a bit of clean-up. */
static void die(int sig)
{
@@ -584,15 +516,17 @@ static void update_gps_panel(struct gps_data_t *gpsdata)
(void)mvwprintw(datawin, 5, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
/* Fill in the heading. */
- if (gpsdata->fix.mode >= MODE_2D && isnan(gpsdata->fix.track) == 0)
- if (!magnetic_flag) {
+ if (gpsdata->fix.mode >= MODE_2D && isnan(gpsdata->fix.track) == 0) {
+ double magheading = true2magnetic(gpsdata->fix.latitude,
+ gpsdata->fix.longitude,
+ gpsdata->fix.track);
+ if (!magnetic_flag || isnan(magheading) != 0) {
(void)snprintf(scr, sizeof(scr), "%.1f deg (true)",
gpsdata->fix.track);
} else {
(void)snprintf(scr, sizeof(scr), "%.1f deg (mag) ",
- true2magnetic(gpsdata->fix.latitude,
- gpsdata->fix.longitude,
- gpsdata->fix.track));
+ magheading);
+ }
} else
(void)snprintf(scr, sizeof(scr), "n/a");
(void)mvwprintw(datawin, 6, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);