Emery Premeaux / Mbed 2 deprecated GPS_U-blox_NEO-M8N

Dependencies:   mbed MODSERIAL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Author: Edoardo De Marchi
00003  * Date: 22-08-14
00004  * Notes: Firmware for GPS U-Blox NEO-6M
00005  *
00006  * Modified: Emery Premeaux
00007  * Date: July 2019
00008  * Written for: Nucleo F401RE & NEO-M8N
00009  *
00010  * Updated MODSERIAL library now compiles without errors but data seems corrupted.
00011  *
00012  * NMEA sentences:  https://www.gpsinformation.org/dale/nmea.htm#nmea
00013  * 
00014 */
00015 
00016 #include "mbed.h"
00017 //#include "MODSERIAL.h"
00018 
00019 //MODSERIAL pc(USBTX,USBRX);
00020 //MODSERIAL gps(D8, D2);
00021 
00022 Serial pc(USBTX,USBRX);
00023 Serial gps(D8, D2);
00024 
00025 char cDataBuffer[500];
00026 int i = 0;
00027 int h_time,m_time,s_time;
00028 
00029 void Init();
00030 void parse(char *cmd, int n);
00031 void parseTime (float timeval);
00032 
00033 
00034 void Init()
00035 {
00036     gps.baud(115200);
00037      pc.baud(115200);
00038     pc.printf("Init OK\n");
00039 }
00040 
00041 
00042 
00043 int main() 
00044 {   
00045     Init();
00046     char c;
00047 
00048     while(true) 
00049     {
00050         if(gps.readable())
00051         { 
00052             if(gps.getc() == '$');           // wait a $
00053             {
00054                 for(int i=0; i<sizeof(cDataBuffer); i++)
00055                 {
00056                     c = gps.getc();
00057                     if( c == '\r' )
00058                     {
00059                         //pc.printf("%s\n", cDataBuffer);
00060                         parse(cDataBuffer, i);
00061                         i = sizeof(cDataBuffer);
00062                     }
00063                     else
00064                     {
00065                         cDataBuffer[i] = c;
00066                     }                 
00067                 }
00068             }
00069          } 
00070     }
00071 }
00072 
00073 /* 
00074  * NMEA sentences:  https://www.gpsinformation.org/dale/nmea.htm#nmea
00075  *                  http://navspark.mybigcommerce.com/content/NMEA_Format_v0.1.pdf
00076  */
00077 void parse(char *cmd, int n)
00078 {   
00079     char ns, ew, tf, status;
00080     int fq, nst, fix, date;                                     // fix quality, Number of satellites being tracked, 3D fix
00081     float latitude, longitude, timefix, speed, altitude;
00082     
00083     // Global Positioning System Fix Data
00084     if(strncmp(cmd,"$GPGGA", 6) == 0) 
00085     {
00086         sscanf(cmd, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude);
00087         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);
00088     }
00089     
00090     // Satellite status
00091      else if(strncmp(cmd,"$GPGSA", 6) == 0) 
00092     {
00093         sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst);
00094         pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
00095     }
00096     
00097     // Geographic position, Latitude and Longitude
00098     else if(strncmp(cmd,"$GPGLL", 6) == 0) 
00099     {
00100         sscanf(cmd, "$GPGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix);
00101         pc.printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n", latitude, ns, longitude, ew, timefix);
00102     }
00103     
00104     // Geographic position, Latitude and Longitude
00105     else if(strncmp(cmd,"$GPRMC", 6) == 0) 
00106     {
00107         sscanf(cmd, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date);
00108         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);
00109     }
00110     
00111     // 
00112     else if(strncmp(cmd,"$GNVTG", 6) == 0) 
00113     {
00114      //   pc.printf("its a Vector Track message.\n");
00115     }
00116     
00117     else if(strncmp(cmd,"$GNGGA", 6) == 0) 
00118     {
00119         sscanf(cmd, "$GNGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude);
00120         pc.printf("GNGGA 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);
00121         parseTime(timefix);
00122         pc.printf("Time: %d:%d:%d\n", h_time, m_time, s_time);
00123     }
00124     
00125     else if(strncmp(cmd,"$GNGSA", 6) == 0) 
00126     {
00127         sscanf(cmd, "$GNGSA,%c,%d,%d", &tf, &fix, &nst);
00128         pc.printf("GNGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
00129     }
00130     
00131     else if(strncmp(cmd,"$GPGSV", 6) == 0) 
00132     {
00133      //   pc.printf("its a Satellite details message.\n");
00134     }
00135     
00136     else if(strncmp(cmd,"$GNGLL", 6) == 0) 
00137     {
00138         sscanf(cmd, "$GNGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix);
00139         pc.printf("GNGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n", latitude, ns, longitude, ew, timefix);
00140     }
00141     
00142     else
00143     {
00144    //     pc.printf("Unknown message type\n");
00145     }
00146 }
00147 
00148 void parseTime (float timeval)
00149 {
00150     //format utc time to beijing time,add 8 time zone
00151                 float time = timeval + 80000.00f;
00152                 h_time = int(time) / 10000;
00153                 m_time = (int(time) % 10000) / 100;
00154                 s_time = int(time) % 100;
00155 }
00156 
00157 
00158