GPS NMEA sentence reader for Nucleo boards.

Dependencies:   mbed MODSERIAL

I had a lot of trouble getting other GPS examples to work. So I took Edoardo De Marchi's code from 2014 and hacked it until it was working on newer hardware.

Advice:

  • Start with confirming the module works in U-Center, and know the baud rate. Many older libraries assume 9600. Newer modules run at higher baud rates
  • Use a good terminal program and confirm you have human readable messages starting with "$GNGGA" or "$GPGSV" or similar.

Comments in code include links to NMEA sentence guides:

Most libraries do not include sentence filters for the types of sentences output by my M8N (and perhaps other modules as well)

Most older versions of the MODSERIAL library do not update properly to support STM32. I have found and included a version of the lib which will compile without errors. However, when using it in my code, the NMEA sentence gets corrupted somehow (for example showing some HUGE [6 digit] Satellite count, etc.

Committer:
edodm85
Date:
Fri Aug 22 12:43:55 2014 +0000
Revision:
1:acd907fbcbae
Parent:
0:ea14ad6794af
Child:
2:c16325a7d225
Added support for LPC4330

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edodm85 0:ea14ad6794af 1 /*
edodm85 0:ea14ad6794af 2 * Author: Edoardo De Marchi
edodm85 1:acd907fbcbae 3 * Date: 22-08-14
edodm85 0:ea14ad6794af 4 * Notes: Firmware for GPS U-Blox NEO-6M
edodm85 0:ea14ad6794af 5 */
edodm85 0:ea14ad6794af 6
edodm85 0:ea14ad6794af 7 #include "main.h"
edodm85 0:ea14ad6794af 8
edodm85 0:ea14ad6794af 9
edodm85 0:ea14ad6794af 10 void Init()
edodm85 0:ea14ad6794af 11 {
edodm85 0:ea14ad6794af 12 gps.baud(9600);
edodm85 0:ea14ad6794af 13 pc.baud(115200);
edodm85 0:ea14ad6794af 14
edodm85 0:ea14ad6794af 15 pc.printf("Init OK\n");
edodm85 0:ea14ad6794af 16 }
edodm85 0:ea14ad6794af 17
edodm85 0:ea14ad6794af 18
edodm85 0:ea14ad6794af 19
edodm85 0:ea14ad6794af 20 int main()
edodm85 0:ea14ad6794af 21 {
edodm85 0:ea14ad6794af 22 Init();
edodm85 0:ea14ad6794af 23 char c;
edodm85 0:ea14ad6794af 24
edodm85 0:ea14ad6794af 25 while(true)
edodm85 0:ea14ad6794af 26 {
edodm85 0:ea14ad6794af 27 if(gps.readable())
edodm85 0:ea14ad6794af 28 {
edodm85 0:ea14ad6794af 29 if(gps.getc() == '$'); // wait a $
edodm85 0:ea14ad6794af 30 {
edodm85 0:ea14ad6794af 31 for(int i=0; i<sizeof(cDataBuffer); i++)
edodm85 0:ea14ad6794af 32 {
edodm85 0:ea14ad6794af 33 c = gps.getc();
edodm85 0:ea14ad6794af 34 if( c == '\r' )
edodm85 0:ea14ad6794af 35 {
edodm85 0:ea14ad6794af 36 //pc.printf("%s\n", cDataBuffer);
edodm85 0:ea14ad6794af 37 parse(cDataBuffer, i);
edodm85 0:ea14ad6794af 38 i = sizeof(cDataBuffer);
edodm85 0:ea14ad6794af 39 }
edodm85 0:ea14ad6794af 40 else
edodm85 0:ea14ad6794af 41 {
edodm85 0:ea14ad6794af 42 cDataBuffer[i] = c;
edodm85 0:ea14ad6794af 43 }
edodm85 0:ea14ad6794af 44 }
edodm85 0:ea14ad6794af 45 }
edodm85 0:ea14ad6794af 46 }
edodm85 0:ea14ad6794af 47 }
edodm85 0:ea14ad6794af 48 }
edodm85 0:ea14ad6794af 49
edodm85 0:ea14ad6794af 50
edodm85 0:ea14ad6794af 51 void parse(char *cmd, int n)
edodm85 0:ea14ad6794af 52 {
edodm85 0:ea14ad6794af 53
edodm85 0:ea14ad6794af 54 char ns, ew, tf, status;
edodm85 0:ea14ad6794af 55 int fq, nst, fix, date; // fix quality, Number of satellites being tracked, 3D fix
edodm85 0:ea14ad6794af 56 float latitude, longitude, timefix, speed, altitude;
edodm85 0:ea14ad6794af 57
edodm85 0:ea14ad6794af 58
edodm85 0:ea14ad6794af 59 // Global Positioning System Fix Data
edodm85 0:ea14ad6794af 60 if(strncmp(cmd,"$GPGGA", 6) == 0)
edodm85 0:ea14ad6794af 61 {
edodm85 0:ea14ad6794af 62 sscanf(cmd, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude);
edodm85 0:ea14ad6794af 63 pc.printf("GPGGA Fix taken at: %f, Latitude: %f %c, Longitude: %f %c, Fix quality: %d, Number of sat: %d, Altitude: %f M\n", timefix, latitude, ns, longitude, ew, fq, nst, altitude);
edodm85 0:ea14ad6794af 64 }
edodm85 0:ea14ad6794af 65
edodm85 0:ea14ad6794af 66 // Satellite status
edodm85 0:ea14ad6794af 67 if(strncmp(cmd,"$GPGSA", 6) == 0)
edodm85 0:ea14ad6794af 68 {
edodm85 0:ea14ad6794af 69 sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst);
edodm85 0:ea14ad6794af 70 pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
edodm85 0:ea14ad6794af 71 }
edodm85 0:ea14ad6794af 72
edodm85 0:ea14ad6794af 73 // Geographic position, Latitude and Longitude
edodm85 0:ea14ad6794af 74 if(strncmp(cmd,"$GPGLL", 6) == 0)
edodm85 0:ea14ad6794af 75 {
edodm85 0:ea14ad6794af 76 sscanf(cmd, "$GPGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix);
edodm85 0:ea14ad6794af 77 pc.printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n", latitude, ns, longitude, ew, timefix);
edodm85 0:ea14ad6794af 78 }
edodm85 0:ea14ad6794af 79
edodm85 0:ea14ad6794af 80 // Geographic position, Latitude and Longitude
edodm85 0:ea14ad6794af 81 if(strncmp(cmd,"$GPRMC", 6) == 0)
edodm85 0:ea14ad6794af 82 {
edodm85 0:ea14ad6794af 83 sscanf(cmd, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date);
edodm85 0:ea14ad6794af 84 pc.printf("GPRMC Fix taken at: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\n", timefix, status, latitude, ns, longitude, ew, speed, date);
edodm85 0:ea14ad6794af 85 }
edodm85 0:ea14ad6794af 86 }
edodm85 0:ea14ad6794af 87
edodm85 0:ea14ad6794af 88
edodm85 0:ea14ad6794af 89
edodm85 0:ea14ad6794af 90