Test code for GPS U-blox NEO-6M

Dependencies:   MODSERIAL mbed-src

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 
00007 #include "main.h"
00008 
00009 
00010 void Init()
00011 {
00012     gps.baud(9600);
00013     pc.baud(115200);
00014 
00015     pc.printf("Init OK\n");
00016 }
00017 
00018 
00019 
00020 int main() 
00021 {   
00022     Init();
00023     char c;
00024 
00025     while(true) 
00026     {
00027         if(gps.readable())
00028         { 
00029             if(gps.getc() == '$');           // wait a $
00030             {
00031                 for(int i=0; i<sizeof(cDataBuffer); i++)
00032                 {
00033                     c = gps.getc();
00034                     if( c == '\r' )
00035                     {
00036                         //pc.printf("%s\n", cDataBuffer);
00037                         parse(cDataBuffer, i);
00038                         i = sizeof(cDataBuffer);
00039                     }
00040                     else
00041                     {
00042                         cDataBuffer[i] = c;
00043                     }                 
00044                 }
00045             }
00046          } 
00047     }
00048 }
00049 
00050 
00051 void parse(char *cmd, int n)
00052 {
00053     
00054     char ns, ew, tf, status;
00055     int fq, nst, fix, date;                                     // fix quality, Number of satellites being tracked, 3D fix
00056     float latitude, longitude, timefix, speed, altitude;
00057     
00058     
00059     // Global Positioning System Fix Data
00060     if(strncmp(cmd,"$GPGGA", 6) == 0) 
00061     {
00062         sscanf(cmd, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude);
00063         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);
00064     }
00065     
00066     // Satellite status
00067     if(strncmp(cmd,"$GPGSA", 6) == 0) 
00068     {
00069         sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst);
00070         pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
00071     }
00072     
00073     // Geographic position, Latitude and Longitude
00074     if(strncmp(cmd,"$GPGLL", 6) == 0) 
00075     {
00076         sscanf(cmd, "$GPGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix);
00077         pc.printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n", latitude, ns, longitude, ew, timefix);
00078     }
00079     
00080     // Geographic position, Latitude and Longitude
00081     if(strncmp(cmd,"$GPRMC", 6) == 0) 
00082     {
00083         sscanf(cmd, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date);
00084         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);
00085     }
00086 }
00087 
00088 
00089 
00090