Allows for a GPS module to be connected to a serial port and exposes an easy to use API to get the GPS data. New feature, added Mbed/LPC17xx RTC synchronisation

Dependents:   SatGPS AntiTheftGPS FLIGHT_CONTROL_AND_COMMUNICATIONS_SYSTEM GPS-Lora ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example1.cpp Source File

example1.cpp

00001 #ifdef COMPILE_EXAMPLE1_CODE_MODGPS
00002 
00003 #include "mbed.h"
00004 #include "GPS.h"
00005 
00006 Serial pc(USBTX, USBRX);
00007 DigitalOut led1(LED1);
00008 
00009 // SET THIS.
00010 // Create an instance of the GPS object. You will need to
00011 // set p25 to whichever Serial RX pin you have connected
00012 // your GPS module to.
00013 GPS gps(NC, GPSRX);
00014 
00015 char rmc[GPS_BUFFER_LEN];
00016 char gga[GPS_BUFFER_LEN];
00017 char vtg[GPS_BUFFER_LEN];
00018 char ukn[GPS_BUFFER_LEN];
00019 
00020 // 0.1 second flash of LED2
00021 DigitalOut led2(LED2);
00022 Timeout t2;
00023 void t2out(void) { led2 = 0; }
00024 void blip2(void) { led2 = 1; t2.attach(&t2out, 0.1); }
00025 
00026 // 0.1 second flash of LED3
00027 DigitalOut led3(LED3);
00028 Timeout t3;
00029 void t3out(void) { led3 = 0; }
00030 void blip3(void) { led3 = 1; t3.attach(&t3out, 0.1); }
00031 
00032 // 0.1 second flash of LED4
00033 DigitalOut led4(LED4);
00034 
00035 Timeout t4;
00036 void t4out(void) { led4 = 0; }
00037 void blip4(void) { led4 = 1; t4.attach(&t4out, 0.1); }
00038 
00039 int main() {
00040     GPS_Time q1;
00041     
00042     // SET THIS.
00043     // Ensure you set the baud rate to match your serial
00044     // communications to your PC/Max/Linux host so you
00045     // can read the messages.
00046     pc.baud(PCBAUD);
00047     
00048     // Tell MODGPS "we want copies of the NMEA sentences". When a callback
00049     // is made our buffers will contain a copy of the last received sentence
00050     // before it was processed/destroyed.    
00051     gps.setRmc(rmc);
00052     gps.setGga(gga);
00053     gps.setVtg(vtg);
00054     gps.setUkn(ukn);
00055     
00056     // SET THIS.
00057     // Most GPS modules use 9600,8,n,1 so that's what
00058     // we default to here. Ensure your GPS module matches
00059     // this, otherwise set it to match.
00060     gps.baud(GPSBUAD);
00061     gps.format(8, GPS::None, 1);
00062     
00063     // OPTIONAL
00064     // If you GPS has a 1 pulse per second output you can
00065     // connect it to an Mbed pin. Here you specify what pin
00066     // and on what "edge" teh signal is active. If your GPS
00067     // module has a rising edge at the one second point then
00068     // use GPS::ppsRise
00069     #ifdef PPSPIN
00070     gps.ppsAttach(PPSPIN, GPS::ppsFall );
00071     #endif
00072 
00073     // Sample of a callback to a function when the 1PPS activates.
00074     // For this example, we flash LED2 for 0.1 second.
00075     gps.attach_pps(&blip2);
00076     
00077     // Sample of a callback to a function when a NMEA GGA message is recieved.
00078     // For this example, we flash LED2 for 0.1 second.
00079     gps.attach_gga(&blip3);
00080     
00081     // Sample of a callback to a function when a NMEA RMC message is recieved.
00082     // For this example, we flash LED2 for 0.1 second.
00083     gps.attach_rmc(&blip4);
00084 
00085     while(1) {
00086         // Every 3 seconds, flip LED1 and print the basic GPS info.
00087         wait(3);
00088         led1 = 1;
00089         
00090         // Demonstrate how to find out the GPS location co-ords.
00091         pc.printf("Method 1. Lat = %.4f ", gps.latitude());
00092         pc.printf("Lon = %.4f ", gps.longitude());
00093         pc.printf("Alt = %.4f ", gps.altitude());
00094         
00095         // Gran a snapshot of the current time.
00096         gps.timeNow(&q1);
00097         pc.printf("%02d:%02d:%02d %02d/%02d/%04d\r\n", 
00098             q1.hour, q1.minute, q1.second, q1.day, q1.month, q1.year);
00099             
00100         // Alternative method that does the same thing.        
00101         pc.printf("Method 2. Lat = %.4f ", gps.latitude());
00102         pc.printf("Lon = %.4f ", gps.longitude());
00103         pc.printf("Alt = %.4f ", gps.altitude());
00104         
00105         GPS_Time *q2 = gps.timeNow();
00106         pc.printf("%02d:%02d:%02d %02d/%02d/%04d\r\n\n", 
00107             q2->hour, q2->minute, q2->second, q2->day, q2->month, q2->year);
00108         delete(q2);        
00109         led1 = 0;
00110     }
00111 }
00112 
00113 #endif