read gps data

Fork of NEO-6m-GPS by t k

Committer:
sofianeamazouz
Date:
Tue Mar 20 13:16:40 2018 +0000
Revision:
4:fa5fed5771c0
Parent:
3:e37fc1ccd812
gps code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
8fromPi 1:1d60e6a0ffd9 1 /* mbed GPS Module Library
simon 0:15611c7938a3 2 * Copyright (c) 2008-2010, sford
8fromPi 1:1d60e6a0ffd9 3 * Copyright (c) 2013, B.Adryan
simon 0:15611c7938a3 4 *
simon 0:15611c7938a3 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:15611c7938a3 6 * of this software and associated documentation files (the "Software"), to deal
simon 0:15611c7938a3 7 * in the Software without restriction, including without limitation the rights
simon 0:15611c7938a3 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:15611c7938a3 9 * copies of the Software, and to permit persons to whom the Software is
simon 0:15611c7938a3 10 * furnished to do so, subject to the following conditions:
simon 0:15611c7938a3 11 *
simon 0:15611c7938a3 12 * The above copyright notice and this permission notice shall be included in
simon 0:15611c7938a3 13 * all copies or substantial portions of the Software.
simon 0:15611c7938a3 14 *
simon 0:15611c7938a3 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:15611c7938a3 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:15611c7938a3 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:15611c7938a3 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:15611c7938a3 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:15611c7938a3 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:15611c7938a3 21 * THE SOFTWARE.
simon 0:15611c7938a3 22 */
sampleCode 2:7350eda5fa8c 23
simon 0:15611c7938a3 24 #include "GPS.h"
simon 0:15611c7938a3 25
sampleCode 2:7350eda5fa8c 26 GPS::GPS(PinName tx, PinName rx, int Baud) : _gps(tx, rx)
sampleCode 2:7350eda5fa8c 27 {
sampleCode 2:7350eda5fa8c 28 _gps.baud(Baud);
simon 0:15611c7938a3 29 longitude = 0.0;
sampleCode 2:7350eda5fa8c 30 latitude = 0.0;
simon 0:15611c7938a3 31 }
simon 0:15611c7938a3 32
sampleCode 2:7350eda5fa8c 33 int GPS::sample()
sampleCode 2:7350eda5fa8c 34 {
sofianeamazouz 3:e37fc1ccd812 35
simon 0:15611c7938a3 36 int lock;
simon 0:15611c7938a3 37
sampleCode 2:7350eda5fa8c 38 while(1) {
sofianeamazouz 4:fa5fed5771c0 39
simon 0:15611c7938a3 40 getline();
simon 0:15611c7938a3 41
simon 0:15611c7938a3 42 // Check if it is a GPGGA msg (matches both locked and non-locked msg)
sampleCode 2:7350eda5fa8c 43 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) {
simon 0:15611c7938a3 44 if(!lock) {
8fromPi 1:1d60e6a0ffd9 45 time = 0.0;
simon 0:15611c7938a3 46 longitude = 0.0;
8fromPi 1:1d60e6a0ffd9 47 latitude = 0.0;
8fromPi 1:1d60e6a0ffd9 48 sats = 0;
8fromPi 1:1d60e6a0ffd9 49 hdop = 0.0;
8fromPi 1:1d60e6a0ffd9 50 alt = 0.0;
sampleCode 2:7350eda5fa8c 51 geoid = 0.0;
simon 0:15611c7938a3 52 return 0;
simon 0:15611c7938a3 53 } else {
8fromPi 1:1d60e6a0ffd9 54 //GPGGA format according http://aprs.gids.nl/nmea/#gga
8fromPi 1:1d60e6a0ffd9 55 // time (float), lat (f), (N/S) (c), long (f), (E/W) (c), fix (d), sats (d),
sampleCode 2:7350eda5fa8c 56 // hdop (float), altitude (float), M, geoid (float), M, , ,
8fromPi 1:1d60e6a0ffd9 57 //GPGGA,092010.000,5210.9546,N,00008.8913,E,1,07,1.3,9.7,M,47.0,M,,0000*5D
sampleCode 2:7350eda5fa8c 58
sampleCode 2:7350eda5fa8c 59 //format utc time to beijing time,add 8 time zone
sampleCode 2:7350eda5fa8c 60 time = time + 80000.00f;
sampleCode 2:7350eda5fa8c 61 hour = int(time) / 10000;
sampleCode 2:7350eda5fa8c 62 minute = (int(time) % 10000) / 100;
sampleCode 2:7350eda5fa8c 63 seconed = int(time) % 100;
sampleCode 2:7350eda5fa8c 64
simon 0:15611c7938a3 65 return 1;
simon 0:15611c7938a3 66 }
simon 0:15611c7938a3 67 }
simon 0:15611c7938a3 68 }
simon 0:15611c7938a3 69 }
simon 0:15611c7938a3 70
sampleCode 2:7350eda5fa8c 71 float GPS::trunc(float v)
sampleCode 2:7350eda5fa8c 72 {
simon 0:15611c7938a3 73 if(v < 0.0) {
simon 0:15611c7938a3 74 v*= -1.0;
simon 0:15611c7938a3 75 v = floor(v);
simon 0:15611c7938a3 76 v*=-1.0;
simon 0:15611c7938a3 77 } else {
simon 0:15611c7938a3 78 v = floor(v);
simon 0:15611c7938a3 79 }
simon 0:15611c7938a3 80 return v;
simon 0:15611c7938a3 81 }
simon 0:15611c7938a3 82
sampleCode 2:7350eda5fa8c 83 void GPS::getline()
sampleCode 2:7350eda5fa8c 84 {
sofianeamazouz 3:e37fc1ccd812 85 while(_gps.getc() != '$');
sofianeamazouz 4:fa5fed5771c0 86
sofianeamazouz 3:e37fc1ccd812 87 // wait for the start of a line
simon 0:15611c7938a3 88 for(int i=0; i<256; i++) {
simon 0:15611c7938a3 89 msg[i] = _gps.getc();
simon 0:15611c7938a3 90 if(msg[i] == '\r') {
simon 0:15611c7938a3 91 msg[i] = 0;
sofianeamazouz 3:e37fc1ccd812 92
simon 0:15611c7938a3 93 return;
simon 0:15611c7938a3 94 }
simon 0:15611c7938a3 95 }
simon 0:15611c7938a3 96 error("Overflowed message limit");
8fromPi 1:1d60e6a0ffd9 97 }