Components / SerialGPS

Dependents:   Sparkfun_GPS_Shield My_GPS Teste_GPS_luis STM32L152RE ... more

Fork of SerialGPS by Components

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialGPS.cpp Source File

SerialGPS.cpp

00001 /* mbed GPS Module Library
00002  * Copyright (c) 2008-2010, sford
00003  * Copyright (c) 2013, B.Adryan
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  */
00023  
00024 #include "SerialGPS.h"
00025 
00026 SerialGPS::SerialGPS(PinName tx, PinName rx, int Baud) : _gps(tx, rx) {
00027     _gps.baud(Baud);    
00028     longitude = 0.0;
00029     latitude = 0.0;        
00030 }
00031 
00032 int SerialGPS::sample() {
00033     char ns, ew, unit;
00034     int lock;
00035 
00036     while(1) {        
00037         getline();
00038 
00039         // Check if it is a GPGGA msg (matches both locked and non-locked msg)
00040         if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c,%f", &time, &latitude, &ns, &longitude, &ew, &lock, &sats, &hdop, &alt, &unit, &geoid) >= 1) { 
00041             if(!lock) {
00042                 time = 0.0;
00043                 longitude = 0.0;
00044                 latitude = 0.0;
00045                 sats = 0;
00046                 hdop = 0.0;
00047                 alt = 0.0;
00048                 geoid = 0.0;        
00049                 return 0;
00050             } else {
00051                 //GPGGA format according http://aprs.gids.nl/nmea/#gga
00052                 // time (float), lat (f), (N/S) (c), long (f), (E/W) (c), fix (d), sats (d),
00053                 // hdop (float), altitude (float), M, geoid (float), M, , ,  
00054                 //GPGGA,092010.000,5210.9546,N,00008.8913,E,1,07,1.3,9.7,M,47.0,M,,0000*5D
00055                 
00056                 if(ns == 'S') {    latitude  *= -1.0; }
00057                 if(ew == 'W') {    longitude *= -1.0; }
00058                 float degrees = trunc(latitude / 100.0f);
00059                 float minutes = latitude - (degrees * 100.0f);
00060                 latitude = degrees + minutes / 60.0f;    
00061                 degrees = trunc(longitude / 100.0f * 0.01f);
00062                 minutes = longitude - (degrees * 100.0f);
00063                 longitude = degrees + minutes / 60.0f;
00064                 return 1;
00065             }
00066         }
00067     }
00068 }
00069 
00070 float SerialGPS::trunc(float v) {
00071     if(v < 0.0) {
00072         v*= -1.0;
00073         v = floor(v);
00074         v*=-1.0;
00075     } else {
00076         v = floor(v);
00077     }
00078     return v;
00079 }
00080 
00081 void SerialGPS::getline() {
00082     while(_gps.getc() != '$');    // wait for the start of a line
00083 
00084     for(int i=0; i<256; i++) {
00085         msg[i] = _gps.getc();
00086         if(msg[i] == '\r') {
00087             msg[i] = 0;
00088             return;
00089         }
00090     }
00091     error("Overflowed message limit");
00092 }