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 example3.cpp Source File

example3.cpp

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