Allows for a GPS module to be connected to a serial port and exposes an easy to use API to get the GPS data.

Fork of MODGPS by Andy K

Committer:
xanter
Date:
Sat Sep 29 20:31:31 2018 +0000
Revision:
7:049436bc2225
Parent:
0:db98027c0bbb
Fixed compiler errors/warnings; Removed hardcode for LPC17xx

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:db98027c0bbb 1 /*
AjK 0:db98027c0bbb 2 Copyright (c) 2010 Andy Kirkham
AjK 0:db98027c0bbb 3
AjK 0:db98027c0bbb 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:db98027c0bbb 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:db98027c0bbb 6 in the Software without restriction, including without limitation the rights
AjK 0:db98027c0bbb 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:db98027c0bbb 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:db98027c0bbb 9 furnished to do so, subject to the following conditions:
AjK 0:db98027c0bbb 10
AjK 0:db98027c0bbb 11 The above copyright notice and this permission notice shall be included in
AjK 0:db98027c0bbb 12 all copies or substantial portions of the Software.
AjK 0:db98027c0bbb 13
AjK 0:db98027c0bbb 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:db98027c0bbb 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:db98027c0bbb 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:db98027c0bbb 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:db98027c0bbb 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:db98027c0bbb 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:db98027c0bbb 20 THE SOFTWARE.
AjK 0:db98027c0bbb 21 */
AjK 0:db98027c0bbb 22
AjK 0:db98027c0bbb 23 #include "GPS_Geodetic.h"
AjK 0:db98027c0bbb 24
AjK 0:db98027c0bbb 25 void
AjK 0:db98027c0bbb 26 GPS_Geodetic::nmea_gga(char *s) {
AjK 0:db98027c0bbb 27 char *token;
AjK 0:db98027c0bbb 28 int token_counter = 0;
AjK 0:db98027c0bbb 29 char *latitude = (char *)NULL;
AjK 0:db98027c0bbb 30 char *longitude = (char *)NULL;
AjK 0:db98027c0bbb 31 char *lat_dir = (char *)NULL;
AjK 0:db98027c0bbb 32 char *lon_dir = (char *)NULL;
AjK 0:db98027c0bbb 33 char *qual = (char *)NULL;
AjK 0:db98027c0bbb 34 char *altitude = (char *)NULL;
AjK 0:db98027c0bbb 35 char *sats = (char *)NULL;
AjK 0:db98027c0bbb 36
AjK 0:db98027c0bbb 37 token = strtok(s, ",");
AjK 0:db98027c0bbb 38 while (token) {
AjK 0:db98027c0bbb 39 switch (token_counter) {
AjK 0:db98027c0bbb 40 case 2: latitude = token; break;
AjK 0:db98027c0bbb 41 case 4: longitude = token; break;
AjK 0:db98027c0bbb 42 case 3: lat_dir = token; break;
AjK 0:db98027c0bbb 43 case 5: lon_dir = token; break;
AjK 0:db98027c0bbb 44 case 6: qual = token; break;
AjK 0:db98027c0bbb 45 case 7: sats = token; break;
AjK 0:db98027c0bbb 46 case 9: altitude = token; break;
AjK 0:db98027c0bbb 47 }
AjK 0:db98027c0bbb 48 token = strtok((char *)NULL, ",");
AjK 0:db98027c0bbb 49 token_counter++;
AjK 0:db98027c0bbb 50 }
AjK 0:db98027c0bbb 51
AjK 0:db98027c0bbb 52 // If the fix quality is valid set our location information.
AjK 0:db98027c0bbb 53 if (latitude && longitude && altitude && sats) {
AjK 0:db98027c0bbb 54 lat = convert_lat_coord(latitude, lat_dir[0]);
AjK 0:db98027c0bbb 55 lon = convert_lon_coord(longitude, lon_dir[0]);
AjK 0:db98027c0bbb 56 alt = convert_height(altitude);
AjK 0:db98027c0bbb 57 num_of_gps_sats = atoi(sats);
AjK 0:db98027c0bbb 58 gps_satellite_quality = atoi(qual);
AjK 0:db98027c0bbb 59 }
AjK 0:db98027c0bbb 60 else {
AjK 0:db98027c0bbb 61 gps_satellite_quality = 0;
AjK 0:db98027c0bbb 62 }
AjK 0:db98027c0bbb 63 }
AjK 0:db98027c0bbb 64
AjK 0:db98027c0bbb 65 double
AjK 0:db98027c0bbb 66 GPS_Geodetic::convert_lat_coord(char *s, char north_south)
AjK 0:db98027c0bbb 67 {
AjK 0:db98027c0bbb 68 int deg, min, sec;
AjK 0:db98027c0bbb 69 double fsec, val;
AjK 0:db98027c0bbb 70
AjK 0:db98027c0bbb 71 deg = ( (s[0] - '0') * 10) + s[1] - '0';
AjK 0:db98027c0bbb 72 min = ( (s[2] - '0') * 10) + s[3] - '0';
AjK 0:db98027c0bbb 73 sec = ( ((s[5] - '0') * 1000) + ((s[6] - '0') * 100) + ((s[7] - '0') * 10) + (s[8] - '0'));
AjK 0:db98027c0bbb 74 fsec = (double)((double)sec /10000.0);
AjK 0:db98027c0bbb 75 val = (double)deg + ((double)((double)min/60.0)) + (fsec/60.0);
AjK 0:db98027c0bbb 76 if (north_south == 'S') { val *= -1.0; }
AjK 0:db98027c0bbb 77 lat = val;
AjK 0:db98027c0bbb 78 return val;
AjK 0:db98027c0bbb 79 }
AjK 0:db98027c0bbb 80
AjK 0:db98027c0bbb 81 double
AjK 0:db98027c0bbb 82 GPS_Geodetic::convert_lon_coord(char *s, char east_west)
AjK 0:db98027c0bbb 83 {
AjK 0:db98027c0bbb 84 int deg, min, sec;
AjK 0:db98027c0bbb 85 double fsec, val;
AjK 0:db98027c0bbb 86
AjK 0:db98027c0bbb 87 deg = ( (s[0] - '0') * 100) + ((s[1] - '0') * 10) + (s[2] - '0');
AjK 0:db98027c0bbb 88 min = ( (s[3] - '0') * 10) + s[4] - '0';
AjK 0:db98027c0bbb 89 sec = ( ((s[6] - '0') * 1000) + ((s[7] - '0') * 100) + ((s[8] - '0') * 10) + (s[9] - '0'));
AjK 0:db98027c0bbb 90 fsec = (double)((double)sec /10000.0);
AjK 0:db98027c0bbb 91 val = (double)deg + ((double)((double)min/60.0)) + (fsec/60.0);
AjK 0:db98027c0bbb 92 if (east_west == 'W') { val *= -1.0; }
AjK 0:db98027c0bbb 93 lon = val;
AjK 0:db98027c0bbb 94 return val;
AjK 0:db98027c0bbb 95
AjK 0:db98027c0bbb 96 }
AjK 0:db98027c0bbb 97
AjK 0:db98027c0bbb 98 double
AjK 0:db98027c0bbb 99 GPS_Geodetic::convert_height(char *s)
AjK 0:db98027c0bbb 100 {
AjK 0:db98027c0bbb 101 double val = (double)(atof(s) / 1000.0);
AjK 0:db98027c0bbb 102 alt = val;
AjK 0:db98027c0bbb 103 return val;
AjK 0:db98027c0bbb 104 }
AjK 0:db98027c0bbb 105
AjK 0:db98027c0bbb 106