Bibliothèque pur gps, trame NMEA, conversion des coordonnées au format WSG84

Dependencies:   mbed

Fork of GPSINT by Joseph Bradshaw

Committer:
jebradshaw
Date:
Sat Nov 01 13:27:11 2014 +0000
Revision:
0:f3a7d716faea
Child:
1:c266b90b4c74
GPSINT serial receive interrupt library for parsing GPS NMEA messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 0:f3a7d716faea 1 /* GPSINT.cpp
jebradshaw 0:f3a7d716faea 2 * Copyright (c) 2014, jbradshaw (20141101)
jebradshaw 0:f3a7d716faea 3 *
jebradshaw 0:f3a7d716faea 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
jebradshaw 0:f3a7d716faea 5 * of this software and associated documentation files (the "Software"), to deal
jebradshaw 0:f3a7d716faea 6 * in the Software without restriction, including without limitation the rights
jebradshaw 0:f3a7d716faea 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jebradshaw 0:f3a7d716faea 8 * copies of the Software, and to permit persons to whom the Software is
jebradshaw 0:f3a7d716faea 9 * furnished to do so, subject to the following conditions:
jebradshaw 0:f3a7d716faea 10 *
jebradshaw 0:f3a7d716faea 11 * The above copyright notice and this permission notice shall be included in
jebradshaw 0:f3a7d716faea 12 * all copies or substantial portions of the Software.
jebradshaw 0:f3a7d716faea 13 *
jebradshaw 0:f3a7d716faea 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jebradshaw 0:f3a7d716faea 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jebradshaw 0:f3a7d716faea 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jebradshaw 0:f3a7d716faea 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jebradshaw 0:f3a7d716faea 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jebradshaw 0:f3a7d716faea 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jebradshaw 0:f3a7d716faea 20 * THE SOFTWARE.
jebradshaw 0:f3a7d716faea 21 */
jebradshaw 0:f3a7d716faea 22
jebradshaw 0:f3a7d716faea 23 #include "GPSINT.h"
jebradshaw 0:f3a7d716faea 24
jebradshaw 0:f3a7d716faea 25 GPSINT::GPSINT(PinName tx, PinName rx) : _gps(tx, rx) {
jebradshaw 0:f3a7d716faea 26 _gps.baud(4800);
jebradshaw 0:f3a7d716faea 27 GPSidx=0; // Index for GPS buffer
jebradshaw 0:f3a7d716faea 28 GPSstate=0; // Used to wait for '$' in GPS interrupt
jebradshaw 0:f3a7d716faea 29 _gps.attach(this,&GPSINT::GPSSerialRecvInterrupt, _gps.RxIrq); // Recv interrupt handler
jebradshaw 0:f3a7d716faea 30 }
jebradshaw 0:f3a7d716faea 31
jebradshaw 0:f3a7d716faea 32 int GPSINT::nmea_validate(char *nmeastr){
jebradshaw 0:f3a7d716faea 33 char check[3];
jebradshaw 0:f3a7d716faea 34 char checkcalcstr[3];
jebradshaw 0:f3a7d716faea 35 int i;
jebradshaw 0:f3a7d716faea 36 int calculated_check;
jebradshaw 0:f3a7d716faea 37
jebradshaw 0:f3a7d716faea 38 i=0;
jebradshaw 0:f3a7d716faea 39 calculated_check=0;
jebradshaw 0:f3a7d716faea 40
jebradshaw 0:f3a7d716faea 41 // check to ensure that the string starts with a $
jebradshaw 0:f3a7d716faea 42 if(nmeastr[i] == '$')
jebradshaw 0:f3a7d716faea 43 i++;
jebradshaw 0:f3a7d716faea 44 else
jebradshaw 0:f3a7d716faea 45 return 0;
jebradshaw 0:f3a7d716faea 46
jebradshaw 0:f3a7d716faea 47 //No NULL reached, 75 char largest possible NMEA message, no '*' reached
jebradshaw 0:f3a7d716faea 48 while((nmeastr[i] != 0) && (nmeastr[i] != '*') && (i < 75)){
jebradshaw 0:f3a7d716faea 49 calculated_check ^= nmeastr[i];// calculate the checksum
jebradshaw 0:f3a7d716faea 50 i++;
jebradshaw 0:f3a7d716faea 51 }
jebradshaw 0:f3a7d716faea 52
jebradshaw 0:f3a7d716faea 53 if(i >= 75){
jebradshaw 0:f3a7d716faea 54 return 0;// the string was too long so return an error
jebradshaw 0:f3a7d716faea 55 }
jebradshaw 0:f3a7d716faea 56
jebradshaw 0:f3a7d716faea 57 if (nmeastr[i] == '*'){
jebradshaw 0:f3a7d716faea 58 check[0] = nmeastr[i+1]; //put hex chars in check string
jebradshaw 0:f3a7d716faea 59 check[1] = nmeastr[i+2];
jebradshaw 0:f3a7d716faea 60 check[2] = 0;
jebradshaw 0:f3a7d716faea 61 }
jebradshaw 0:f3a7d716faea 62 else
jebradshaw 0:f3a7d716faea 63 return 0;// no checksum separator found therefor invalid
jebradshaw 0:f3a7d716faea 64
jebradshaw 0:f3a7d716faea 65 sprintf(checkcalcstr,"%02X",calculated_check);
jebradshaw 0:f3a7d716faea 66 return((checkcalcstr[0] == check[0])
jebradshaw 0:f3a7d716faea 67 && (checkcalcstr[1] == check[1])) ? 1 : 0 ;
jebradshaw 0:f3a7d716faea 68 }
jebradshaw 0:f3a7d716faea 69
jebradshaw 0:f3a7d716faea 70 void GPSINT::parseGPSString(char *GPSstrParse){
jebradshaw 0:f3a7d716faea 71 //check if $GPGGA string
jebradshaw 0:f3a7d716faea 72 if(!strncmp(GPSstrParse, "$GPGGA", 6)){
jebradshaw 0:f3a7d716faea 73 if (sscanf(GPSstrParse, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c", &utc_time, &nmea_latitude, &ns, &nmea_longitude, &ew, &lock, &satelites, &hdop, &msl_altitude, &msl_units) >= 1) {
jebradshaw 0:f3a7d716faea 74 // printf("%s", GPSstrParse);
jebradshaw 0:f3a7d716faea 75 return;
jebradshaw 0:f3a7d716faea 76 }
jebradshaw 0:f3a7d716faea 77 else{
jebradshaw 0:f3a7d716faea 78 // printf("BAD parse %s", GPSstrParse);
jebradshaw 0:f3a7d716faea 79 }
jebradshaw 0:f3a7d716faea 80 }
jebradshaw 0:f3a7d716faea 81 // Check if $GPRMC string
jebradshaw 0:f3a7d716faea 82 else if (!strncmp(GPSstrParse, "$GPRMC", 6)){
jebradshaw 0:f3a7d716faea 83 if(sscanf(GPSstrParse, "$GPRMC,%f,%f,%c,%f,%c,%f,%f,%d", &utc_time, &nmea_latitude, &ns, &nmea_longitude, &ew, &speed_k, &course_d, &date) >= 1) {
jebradshaw 0:f3a7d716faea 84 // printf("%s", GPSstrParse);
jebradshaw 0:f3a7d716faea 85 return;
jebradshaw 0:f3a7d716faea 86 }
jebradshaw 0:f3a7d716faea 87 else{
jebradshaw 0:f3a7d716faea 88 // printf("BAD parse %s", GPSstrParse);
jebradshaw 0:f3a7d716faea 89 }
jebradshaw 0:f3a7d716faea 90 }
jebradshaw 0:f3a7d716faea 91 // GLL - Geographic Position-Lat/Lon
jebradshaw 0:f3a7d716faea 92 else if (!strncmp(GPSstrParse, "$GPGLL", 6)){
jebradshaw 0:f3a7d716faea 93 if(sscanf(GPSstrParse, "$GPGLL,%f,%c,%f,%c,%f,%c", &nmea_latitude, &ns, &nmea_longitude, &ew, &utc_time, &gll_status) >= 1) {
jebradshaw 0:f3a7d716faea 94 // printf("%s", GPSstrParse);
jebradshaw 0:f3a7d716faea 95 return;
jebradshaw 0:f3a7d716faea 96 }
jebradshaw 0:f3a7d716faea 97 else{
jebradshaw 0:f3a7d716faea 98 // printf("BAD parse %s", GPSstrParse);
jebradshaw 0:f3a7d716faea 99 }
jebradshaw 0:f3a7d716faea 100 }
jebradshaw 0:f3a7d716faea 101 // VTG-Course Over Ground and Ground Speed
jebradshaw 0:f3a7d716faea 102 else if (!strncmp(GPSstrParse, "$GPVTG", 6)){
jebradshaw 0:f3a7d716faea 103 if(sscanf(GPSstrParse, "$GPVTG,%f,%c,%f,%c,%f,%c,%f,%c", &course_t, &course_t_unit, &course_m, &course_m_unit, &speed_k, &speed_k_unit, &speed_km, &speed_km_unit) >= 1) {
jebradshaw 0:f3a7d716faea 104 // printf("%s", GPSstrParse);
jebradshaw 0:f3a7d716faea 105 return;
jebradshaw 0:f3a7d716faea 106 }
jebradshaw 0:f3a7d716faea 107 else{
jebradshaw 0:f3a7d716faea 108 // printf("BAD parse %s", GPSstrParse);
jebradshaw 0:f3a7d716faea 109 }
jebradshaw 0:f3a7d716faea 110 }
jebradshaw 0:f3a7d716faea 111 }//parseGPSstring()
jebradshaw 0:f3a7d716faea 112
jebradshaw 0:f3a7d716faea 113 void GPSINT::GPSSerialRecvInterrupt(void)
jebradshaw 0:f3a7d716faea 114 {
jebradshaw 0:f3a7d716faea 115 char c;
jebradshaw 0:f3a7d716faea 116 c =_gps.getc(); // On receive interrupt, get the character.
jebradshaw 0:f3a7d716faea 117 // pc.printf("%c", c);
jebradshaw 0:f3a7d716faea 118
jebradshaw 0:f3a7d716faea 119 switch(GPSstate){
jebradshaw 0:f3a7d716faea 120 case 0:
jebradshaw 0:f3a7d716faea 121 if(c =='$'){
jebradshaw 0:f3a7d716faea 122 GPSidx=0;
jebradshaw 0:f3a7d716faea 123 Temp_GPSbuf[GPSidx] = c; //load char in current idx of array
jebradshaw 0:f3a7d716faea 124 GPSidx++;
jebradshaw 0:f3a7d716faea 125 GPSstate = 1;
jebradshaw 0:f3a7d716faea 126 }
jebradshaw 0:f3a7d716faea 127 break;
jebradshaw 0:f3a7d716faea 128 case 1:
jebradshaw 0:f3a7d716faea 129 Temp_GPSbuf[GPSidx] = c; //load char in current idx of array
jebradshaw 0:f3a7d716faea 130 GPSidx++;
jebradshaw 0:f3a7d716faea 131 if(c == '\n'){ //if last char was a newline
jebradshaw 0:f3a7d716faea 132 Temp_GPSbuf[GPSidx] = '\0'; //append a NULL
jebradshaw 0:f3a7d716faea 133 strcpy(GPSbuf, Temp_GPSbuf); //copy temp buf into GPS buf
jebradshaw 0:f3a7d716faea 134 GPSidx=0; //reset index
jebradshaw 0:f3a7d716faea 135 GPSstate = 0; //reset GPS state
jebradshaw 0:f3a7d716faea 136 if(nmea_validate(GPSbuf)){
jebradshaw 0:f3a7d716faea 137 parseGPSString(GPSbuf);
jebradshaw 0:f3a7d716faea 138 }
jebradshaw 0:f3a7d716faea 139 }
jebradshaw 0:f3a7d716faea 140 break;
jebradshaw 0:f3a7d716faea 141
jebradshaw 0:f3a7d716faea 142 default:
jebradshaw 0:f3a7d716faea 143 break;
jebradshaw 0:f3a7d716faea 144
jebradshaw 0:f3a7d716faea 145 }//switch state
jebradshaw 0:f3a7d716faea 146 }
jebradshaw 0:f3a7d716faea 147
jebradshaw 0:f3a7d716faea 148 float GPSINT::nmea_to_dec(float deg_coord, char nsew) {
jebradshaw 0:f3a7d716faea 149 int degree = (int)(deg_coord/100);
jebradshaw 0:f3a7d716faea 150 float minutes = deg_coord - degree*100;
jebradshaw 0:f3a7d716faea 151 float dec_deg = minutes / 60;
jebradshaw 0:f3a7d716faea 152 float decimal = degree + dec_deg;
jebradshaw 0:f3a7d716faea 153 if (nsew == 'S' || nsew == 'W') { // return negative
jebradshaw 0:f3a7d716faea 154 decimal *= -1;
jebradshaw 0:f3a7d716faea 155 }
jebradshaw 0:f3a7d716faea 156 return decimal;
jebradshaw 0:f3a7d716faea 157 }
jebradshaw 0:f3a7d716faea 158
jebradshaw 0:f3a7d716faea 159 // NAVIGATION FUNCTIONS ////////////////////////////////////////////////////////////
jebradshaw 0:f3a7d716faea 160 float GPSINT::calc_course_to(float pointLat, float pontLong) {
jebradshaw 0:f3a7d716faea 161 const double d2r = PI / 180.0;
jebradshaw 0:f3a7d716faea 162 const double r2d = 180.0 / PI;
jebradshaw 0:f3a7d716faea 163 double dlat = abs(pointLat - dec_latitude) * d2r;
jebradshaw 0:f3a7d716faea 164 double dlong = abs(pontLong - dec_longitude) * d2r;
jebradshaw 0:f3a7d716faea 165 double y = sin(dlong) * cos(pointLat * d2r);
jebradshaw 0:f3a7d716faea 166 double x = cos(dec_latitude*d2r)*sin(pointLat*d2r) - sin(dec_latitude*d2r)*cos(pointLat*d2r)*cos(dlong);
jebradshaw 0:f3a7d716faea 167 return atan2(y,x)*r2d;
jebradshaw 0:f3a7d716faea 168 }
jebradshaw 0:f3a7d716faea 169
jebradshaw 0:f3a7d716faea 170 /*
jebradshaw 0:f3a7d716faea 171 var y = Math.sin(dLon) * Math.cos(lat2);
jebradshaw 0:f3a7d716faea 172 var x = Math.cos(lat1)*Math.sin(lat2) -
jebradshaw 0:f3a7d716faea 173 Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
jebradshaw 0:f3a7d716faea 174 var brng = Math.atan2(y, x).toDeg();
jebradshaw 0:f3a7d716faea 175 */
jebradshaw 0:f3a7d716faea 176
jebradshaw 0:f3a7d716faea 177 /*
jebradshaw 0:f3a7d716faea 178 The Haversine formula according to Dr. Math.
jebradshaw 0:f3a7d716faea 179 http://mathforum.org/library/drmath/view/51879.html
jebradshaw 0:f3a7d716faea 180
jebradshaw 0:f3a7d716faea 181 dlon = lon2 - lon1
jebradshaw 0:f3a7d716faea 182 dlat = lat2 - lat1
jebradshaw 0:f3a7d716faea 183 a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
jebradshaw 0:f3a7d716faea 184 c = 2 * atan2(sqrt(a), sqrt(1-a))
jebradshaw 0:f3a7d716faea 185 d = R * c
jebradshaw 0:f3a7d716faea 186
jebradshaw 0:f3a7d716faea 187 Where
jebradshaw 0:f3a7d716faea 188 * dlon is the change in longitude
jebradshaw 0:f3a7d716faea 189 * dlat is the change in latitude
jebradshaw 0:f3a7d716faea 190 * c is the great circle distance in Radians.
jebradshaw 0:f3a7d716faea 191 * R is the radius of a spherical Earth.
jebradshaw 0:f3a7d716faea 192 * The locations of the two points in
jebradshaw 0:f3a7d716faea 193 spherical coordinates (longitude and
jebradshaw 0:f3a7d716faea 194 latitude) are lon1,lat1 and lon2, lat2.
jebradshaw 0:f3a7d716faea 195 */
jebradshaw 0:f3a7d716faea 196 double GPSINT::calc_dist_to_mi(float pointLat, float pontLong) {
jebradshaw 0:f3a7d716faea 197 const double d2r = PI / 180.0;
jebradshaw 0:f3a7d716faea 198 double dlat = pointLat - dec_latitude;
jebradshaw 0:f3a7d716faea 199 double dlong = pontLong - dec_longitude;
jebradshaw 0:f3a7d716faea 200 double a = pow(sin(dlat/2.0),2.0) + cos(dec_latitude*d2r) * cos(pointLat*d2r) * pow(sin(dlong/2.0),2.0);
jebradshaw 0:f3a7d716faea 201 double c = 2.0 * asin(sqrt(abs(a)));
jebradshaw 0:f3a7d716faea 202 double d = 63.765 * c;
jebradshaw 0:f3a7d716faea 203
jebradshaw 0:f3a7d716faea 204 return d;
jebradshaw 0:f3a7d716faea 205 }
jebradshaw 0:f3a7d716faea 206
jebradshaw 0:f3a7d716faea 207 double GPSINT::calc_dist_to_ft(float pointLat, float pontLong) {
jebradshaw 0:f3a7d716faea 208 return calc_dist_to_mi(pointLat, pontLong)*5280.0;
jebradshaw 0:f3a7d716faea 209 }
jebradshaw 0:f3a7d716faea 210
jebradshaw 0:f3a7d716faea 211 double GPSINT::calc_dist_to_km(float pointLat, float pontLong) {
jebradshaw 0:f3a7d716faea 212 return calc_dist_to_mi(pointLat, pontLong)*1.609344;
jebradshaw 0:f3a7d716faea 213 }
jebradshaw 0:f3a7d716faea 214
jebradshaw 0:f3a7d716faea 215 double GPSINT::calc_dist_to_m(float pointLat, float pontLong) {
jebradshaw 0:f3a7d716faea 216 return calc_dist_to_mi(pointLat, pontLong)*1609.344;
jebradshaw 0:f3a7d716faea 217 }