A GPS serial interrupt service routine that has an on the fly nmea parser. Works with a STM32F411RE and a Adafruit GPS logger.
Dependents: Bicycl_Computer_NUCLEO-F411RE Bicycl_Computer_NUCLEO-L476RG
Fork of GPS by
main.cpp
#include "mbed.h" #include "GPSISR.h" #define PIN_RX_GPS PA_12 //GPS Shield RX pin #define PIN_TX_GPS PA_11 //GPS Shield TX pin Serial pc(USBTX, USBRX); // Set up serial interrupe service handler for gps characters. GPS MyGPS(PIN_TX_GPS,PIN_RX_GPS, 9600); int main() { while (1) { if (MyGPS.dataready()) { MyGPS.read(); pc.printf("NMEA has valid data"); pc.printf("Sats : %d \n", MyGPS.buffer.satellites); pc.printf("%d-%d-%d\n", MyGPS.buffer.month, MyGPS.buffer.day, MyGPS.buffer.year); pc.printf("%d:%d:%d\n", MyGPS.buffer.hours, MyGPS.buffer.minutes, MyGPS.buffer.seconds); } else { pc.printf("NMEA has no valid data"); } } }
nmea.h@5:c5f700c1e1af, 2017-03-01 (annotated)
- Committer:
- trevieze
- Date:
- Wed Mar 01 03:38:03 2017 +0000
- Revision:
- 5:c5f700c1e1af
- Parent:
- 1:4b5ffae743c0
Added NEWS indication to Lon and Lat display
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
trevieze | 1:4b5ffae743c0 | 1 | /* |
trevieze | 1:4b5ffae743c0 | 2 | File: nmea.h |
trevieze | 1:4b5ffae743c0 | 3 | Version: 0.1.0 |
trevieze | 1:4b5ffae743c0 | 4 | Date: Feb. 23, 2013 |
trevieze | 1:4b5ffae743c0 | 5 | License: GPL v2 |
trevieze | 1:4b5ffae743c0 | 6 | |
trevieze | 1:4b5ffae743c0 | 7 | NMEA GPS content parser |
trevieze | 1:4b5ffae743c0 | 8 | |
trevieze | 1:4b5ffae743c0 | 9 | **************************************************************************** |
trevieze | 1:4b5ffae743c0 | 10 | Copyright (C) 2013 Radu Motisan <radu.motisan@gmail.com> |
trevieze | 1:4b5ffae743c0 | 11 | |
trevieze | 1:4b5ffae743c0 | 12 | http://www.pocketmagic.net |
trevieze | 1:4b5ffae743c0 | 13 | |
trevieze | 1:4b5ffae743c0 | 14 | This program is free software; you can redistribute it and/or modify |
trevieze | 1:4b5ffae743c0 | 15 | it under the terms of the GNU General Public License as published by |
trevieze | 1:4b5ffae743c0 | 16 | the Free Software Foundation; either version 2 of the License, or |
trevieze | 1:4b5ffae743c0 | 17 | (at your option) any later version. |
trevieze | 1:4b5ffae743c0 | 18 | |
trevieze | 1:4b5ffae743c0 | 19 | This program is distributed in the hope that it will be useful, |
trevieze | 1:4b5ffae743c0 | 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
trevieze | 1:4b5ffae743c0 | 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
trevieze | 1:4b5ffae743c0 | 22 | GNU General Public License for more details. |
trevieze | 1:4b5ffae743c0 | 23 | |
trevieze | 1:4b5ffae743c0 | 24 | You should have received a copy of the GNU General Public License |
trevieze | 1:4b5ffae743c0 | 25 | along with this program; if not, write to the Free Software |
trevieze | 1:4b5ffae743c0 | 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
trevieze | 1:4b5ffae743c0 | 27 | **************************************************************************** |
trevieze | 1:4b5ffae743c0 | 28 | */ |
trevieze | 1:4b5ffae743c0 | 29 | //#include "../timeout.h" |
trevieze | 1:4b5ffae743c0 | 30 | |
trevieze | 1:4b5ffae743c0 | 31 | |
trevieze | 1:4b5ffae743c0 | 32 | |
trevieze | 1:4b5ffae743c0 | 33 | class NMEA { |
trevieze | 1:4b5ffae743c0 | 34 | |
trevieze | 1:4b5ffae743c0 | 35 | private: |
trevieze | 1:4b5ffae743c0 | 36 | bool m_bFlagRead, // flag used by the parser, when a valid sentence has begun |
trevieze | 1:4b5ffae743c0 | 37 | m_bFlagDataReady; // valid GPS fix and data available, user can call reader functions |
trevieze | 1:4b5ffae743c0 | 38 | char tmp_words[20][15], // hold parsed words for one given NMEA sentence |
trevieze | 1:4b5ffae743c0 | 39 | tmp_szChecksum[15]; // hold the received checksum for one given NMEA sentence |
trevieze | 1:4b5ffae743c0 | 40 | |
trevieze | 1:4b5ffae743c0 | 41 | // will be set to true for characters between $ and * only |
trevieze | 1:4b5ffae743c0 | 42 | bool m_bFlagComputedCks ; // used to compute checksum and indicate valid checksum interval (between $ and * in a given sentence) |
trevieze | 1:4b5ffae743c0 | 43 | int m_nChecksum ; // numeric checksum, computed for a given sentence |
trevieze | 1:4b5ffae743c0 | 44 | bool m_bFlagReceivedCks ; // after getting * we start cuttings the received checksum |
trevieze | 1:4b5ffae743c0 | 45 | int index_received_checksum ; // used to parse received checksum |
trevieze | 1:4b5ffae743c0 | 46 | |
trevieze | 1:4b5ffae743c0 | 47 | // word cutting variables |
trevieze | 1:4b5ffae743c0 | 48 | int m_nWordIdx , // the current word in a sentence |
trevieze | 1:4b5ffae743c0 | 49 | m_nPrevIdx, // last character index where we did a cut |
trevieze | 1:4b5ffae743c0 | 50 | m_nNowIdx ; // current character index |
trevieze | 1:4b5ffae743c0 | 51 | |
trevieze | 1:4b5ffae743c0 | 52 | // globals to store parser results |
trevieze | 1:4b5ffae743c0 | 53 | float res_fLongitude; // GPRMC and GPGGA |
trevieze | 5:c5f700c1e1af | 54 | char res_clon; // E or W |
trevieze | 1:4b5ffae743c0 | 55 | float res_fLatitude; // GPRMC and GPGGA |
trevieze | 5:c5f700c1e1af | 56 | char res_clat; // N or S |
trevieze | 1:4b5ffae743c0 | 57 | unsigned char res_nUTCHour, res_nUTCMin, res_nUTCSec, // GPRMC and GPGGA |
trevieze | 1:4b5ffae743c0 | 58 | res_nUTCDay, res_nUTCMonth, res_nUTCYear; // GPRMC |
trevieze | 1:4b5ffae743c0 | 59 | int res_nSatellitesUsed; // GPGGA |
trevieze | 1:4b5ffae743c0 | 60 | float res_fAltitude; // GPGGA |
trevieze | 1:4b5ffae743c0 | 61 | float res_fSpeed; // GPRMC |
trevieze | 1:4b5ffae743c0 | 62 | float res_fBearing; // GPRMC |
trevieze | 1:4b5ffae743c0 | 63 | |
trevieze | 1:4b5ffae743c0 | 64 | // the parser, currently handling GPRMC and GPGGA, but easy to add any new sentences |
trevieze | 1:4b5ffae743c0 | 65 | void parsedata(); |
trevieze | 1:4b5ffae743c0 | 66 | // aux functions |
trevieze | 1:4b5ffae743c0 | 67 | int digit2dec(char hexdigit); |
trevieze | 1:4b5ffae743c0 | 68 | float string2float(char* s); |
trevieze | 1:4b5ffae743c0 | 69 | int mstrcmp(const char *s1, const char *s2); |
trevieze | 1:4b5ffae743c0 | 70 | float trunc(float v); |
trevieze | 1:4b5ffae743c0 | 71 | |
trevieze | 1:4b5ffae743c0 | 72 | public: |
trevieze | 1:4b5ffae743c0 | 73 | // constructor, initing a few variables |
trevieze | 1:4b5ffae743c0 | 74 | NMEA() { |
trevieze | 1:4b5ffae743c0 | 75 | m_bFlagRead = false; //are we in a sentence? |
trevieze | 1:4b5ffae743c0 | 76 | m_bFlagDataReady = false; //is data available? |
trevieze | 1:4b5ffae743c0 | 77 | } |
trevieze | 1:4b5ffae743c0 | 78 | |
trevieze | 1:4b5ffae743c0 | 79 | /* |
trevieze | 1:4b5ffae743c0 | 80 | * The serial data is assembled on the fly, without using any redundant buffers. |
trevieze | 1:4b5ffae743c0 | 81 | * When a sentence is complete (one that starts with $, ending in EOL), all processing is done on |
trevieze | 1:4b5ffae743c0 | 82 | * this temporary buffer that we've built: checksum computation, extracting sentence "words" (the CSV values), |
trevieze | 1:4b5ffae743c0 | 83 | * and so on. |
trevieze | 1:4b5ffae743c0 | 84 | * When a new sentence is fully assembled using the fusedata function, the code calls parsedata. |
trevieze | 1:4b5ffae743c0 | 85 | * This function in turn, splits the sentences and interprets the data. Here is part of the parser function, |
trevieze | 1:4b5ffae743c0 | 86 | * handling both the $GPRMC NMEA sentence: |
trevieze | 1:4b5ffae743c0 | 87 | */ |
trevieze | 1:4b5ffae743c0 | 88 | int fusedata(char c); |
trevieze | 1:4b5ffae743c0 | 89 | |
trevieze | 1:4b5ffae743c0 | 90 | |
trevieze | 1:4b5ffae743c0 | 91 | // READER functions: retrieving results, call isdataready() first |
trevieze | 1:4b5ffae743c0 | 92 | bool isdataready(); |
trevieze | 1:4b5ffae743c0 | 93 | int getHour(); |
trevieze | 1:4b5ffae743c0 | 94 | int getMinute(); |
trevieze | 1:4b5ffae743c0 | 95 | int getSecond(); |
trevieze | 1:4b5ffae743c0 | 96 | int getDay(); |
trevieze | 1:4b5ffae743c0 | 97 | int getMonth(); |
trevieze | 1:4b5ffae743c0 | 98 | int getYear(); |
trevieze | 1:4b5ffae743c0 | 99 | float getLatitude(); |
trevieze | 5:c5f700c1e1af | 100 | char getlatc(); |
trevieze | 1:4b5ffae743c0 | 101 | float getLongitude(); |
trevieze | 5:c5f700c1e1af | 102 | char getlonc(); |
trevieze | 1:4b5ffae743c0 | 103 | int getSatellites(); |
trevieze | 1:4b5ffae743c0 | 104 | float getAltitude(); |
trevieze | 1:4b5ffae743c0 | 105 | float getSpeed(); |
trevieze | 1:4b5ffae743c0 | 106 | float getBearing(); |
trevieze | 1:4b5ffae743c0 | 107 | }; |
trevieze | 1:4b5ffae743c0 | 108 |