Allows for a GPS module to be connected to a serial port and exposes an easy to use API to get the GPS data.
Fork of MODGPS by
example1.cpp@7:049436bc2225, 2018-09-29 (annotated)
- Committer:
- xanter
- Date:
- Sat Sep 29 20:31:31 2018 +0000
- Revision:
- 7:049436bc2225
- Parent:
- 6:64771e31464e
Fixed compiler errors/warnings; Removed hardcode for LPC17xx
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 2:8aa059e7d8b1 | 1 | #ifdef COMPILE_EXAMPLE1_CODE_MODGPS |
AjK | 1:6aec92e77ad2 | 2 | |
AjK | 1:6aec92e77ad2 | 3 | #include "mbed.h" |
AjK | 1:6aec92e77ad2 | 4 | #include "GPS.h" |
AjK | 1:6aec92e77ad2 | 5 | |
AjK | 1:6aec92e77ad2 | 6 | Serial pc(USBTX, USBRX); |
AjK | 1:6aec92e77ad2 | 7 | DigitalOut led1(LED1); |
AjK | 1:6aec92e77ad2 | 8 | |
AjK | 1:6aec92e77ad2 | 9 | // SET THIS. |
AjK | 1:6aec92e77ad2 | 10 | // Create an instance of the GPS object. You will need to |
AjK | 1:6aec92e77ad2 | 11 | // set p25 to whichever Serial RX pin you have connected |
AjK | 1:6aec92e77ad2 | 12 | // your GPS module to. |
AjK | 1:6aec92e77ad2 | 13 | GPS gps(NC, GPSRX); |
AjK | 1:6aec92e77ad2 | 14 | |
AjK | 6:64771e31464e | 15 | char rmc[GPS_BUFFER_LEN]; |
AjK | 6:64771e31464e | 16 | char gga[GPS_BUFFER_LEN]; |
AjK | 6:64771e31464e | 17 | char vtg[GPS_BUFFER_LEN]; |
AjK | 6:64771e31464e | 18 | char ukn[GPS_BUFFER_LEN]; |
AjK | 6:64771e31464e | 19 | |
AjK | 1:6aec92e77ad2 | 20 | // 0.1 second flash of LED2 |
AjK | 1:6aec92e77ad2 | 21 | DigitalOut led2(LED2); |
AjK | 1:6aec92e77ad2 | 22 | Timeout t2; |
AjK | 1:6aec92e77ad2 | 23 | void t2out(void) { led2 = 0; } |
AjK | 1:6aec92e77ad2 | 24 | void blip2(void) { led2 = 1; t2.attach(&t2out, 0.1); } |
AjK | 1:6aec92e77ad2 | 25 | |
AjK | 1:6aec92e77ad2 | 26 | // 0.1 second flash of LED3 |
AjK | 1:6aec92e77ad2 | 27 | DigitalOut led3(LED3); |
AjK | 1:6aec92e77ad2 | 28 | Timeout t3; |
AjK | 1:6aec92e77ad2 | 29 | void t3out(void) { led3 = 0; } |
AjK | 1:6aec92e77ad2 | 30 | void blip3(void) { led3 = 1; t3.attach(&t3out, 0.1); } |
AjK | 1:6aec92e77ad2 | 31 | |
AjK | 1:6aec92e77ad2 | 32 | // 0.1 second flash of LED4 |
AjK | 1:6aec92e77ad2 | 33 | DigitalOut led4(LED4); |
AjK | 1:6aec92e77ad2 | 34 | |
AjK | 1:6aec92e77ad2 | 35 | Timeout t4; |
AjK | 1:6aec92e77ad2 | 36 | void t4out(void) { led4 = 0; } |
AjK | 1:6aec92e77ad2 | 37 | void blip4(void) { led4 = 1; t4.attach(&t4out, 0.1); } |
AjK | 1:6aec92e77ad2 | 38 | |
AjK | 1:6aec92e77ad2 | 39 | int main() { |
AjK | 1:6aec92e77ad2 | 40 | GPS_Time q1; |
AjK | 1:6aec92e77ad2 | 41 | |
AjK | 1:6aec92e77ad2 | 42 | // SET THIS. |
AjK | 1:6aec92e77ad2 | 43 | // Ensure you set the baud rate to match your serial |
AjK | 1:6aec92e77ad2 | 44 | // communications to your PC/Max/Linux host so you |
AjK | 1:6aec92e77ad2 | 45 | // can read the messages. |
AjK | 1:6aec92e77ad2 | 46 | pc.baud(PCBAUD); |
AjK | 1:6aec92e77ad2 | 47 | |
AjK | 6:64771e31464e | 48 | // Tell MODGPS "we want copies of the NMEA sentences". When a callback |
AjK | 6:64771e31464e | 49 | // is made our buffers will contain a copy of the last received sentence |
AjK | 6:64771e31464e | 50 | // before it was processed/destroyed. |
AjK | 6:64771e31464e | 51 | gps.setRmc(rmc); |
AjK | 6:64771e31464e | 52 | gps.setGga(gga); |
AjK | 6:64771e31464e | 53 | gps.setVtg(vtg); |
AjK | 6:64771e31464e | 54 | gps.setUkn(ukn); |
AjK | 6:64771e31464e | 55 | |
AjK | 1:6aec92e77ad2 | 56 | // SET THIS. |
AjK | 1:6aec92e77ad2 | 57 | // Most GPS modules use 9600,8,n,1 so that's what |
AjK | 1:6aec92e77ad2 | 58 | // we default to here. Ensure your GPS module matches |
AjK | 1:6aec92e77ad2 | 59 | // this, otherwise set it to match. |
AjK | 1:6aec92e77ad2 | 60 | gps.baud(GPSBUAD); |
AjK | 1:6aec92e77ad2 | 61 | gps.format(8, GPS::None, 1); |
AjK | 1:6aec92e77ad2 | 62 | |
AjK | 1:6aec92e77ad2 | 63 | // OPTIONAL |
AjK | 1:6aec92e77ad2 | 64 | // If you GPS has a 1 pulse per second output you can |
AjK | 1:6aec92e77ad2 | 65 | // connect it to an Mbed pin. Here you specify what pin |
AjK | 1:6aec92e77ad2 | 66 | // and on what "edge" teh signal is active. If your GPS |
AjK | 1:6aec92e77ad2 | 67 | // module has a rising edge at the one second point then |
AjK | 1:6aec92e77ad2 | 68 | // use GPS::ppsRise |
AjK | 1:6aec92e77ad2 | 69 | #ifdef PPSPIN |
AjK | 1:6aec92e77ad2 | 70 | gps.ppsAttach(PPSPIN, GPS::ppsFall); |
AjK | 1:6aec92e77ad2 | 71 | #endif |
AjK | 1:6aec92e77ad2 | 72 | |
AjK | 1:6aec92e77ad2 | 73 | // Sample of a callback to a function when the 1PPS activates. |
AjK | 1:6aec92e77ad2 | 74 | // For this example, we flash LED2 for 0.1 second. |
AjK | 1:6aec92e77ad2 | 75 | gps.attach_pps(&blip2); |
AjK | 1:6aec92e77ad2 | 76 | |
AjK | 1:6aec92e77ad2 | 77 | // Sample of a callback to a function when a NMEA GGA message is recieved. |
AjK | 1:6aec92e77ad2 | 78 | // For this example, we flash LED2 for 0.1 second. |
AjK | 1:6aec92e77ad2 | 79 | gps.attach_gga(&blip3); |
AjK | 1:6aec92e77ad2 | 80 | |
AjK | 1:6aec92e77ad2 | 81 | // Sample of a callback to a function when a NMEA RMC message is recieved. |
AjK | 1:6aec92e77ad2 | 82 | // For this example, we flash LED2 for 0.1 second. |
AjK | 1:6aec92e77ad2 | 83 | gps.attach_rmc(&blip4); |
AjK | 1:6aec92e77ad2 | 84 | |
AjK | 1:6aec92e77ad2 | 85 | while(1) { |
AjK | 1:6aec92e77ad2 | 86 | // Every 3 seconds, flip LED1 and print the basic GPS info. |
AjK | 1:6aec92e77ad2 | 87 | wait(3); |
AjK | 1:6aec92e77ad2 | 88 | led1 = 1; |
AjK | 1:6aec92e77ad2 | 89 | |
AjK | 1:6aec92e77ad2 | 90 | // Demonstrate how to find out the GPS location co-ords. |
AjK | 1:6aec92e77ad2 | 91 | pc.printf("Method 1. Lat = %.4f ", gps.latitude()); |
AjK | 1:6aec92e77ad2 | 92 | pc.printf("Lon = %.4f ", gps.longitude()); |
AjK | 1:6aec92e77ad2 | 93 | pc.printf("Alt = %.4f ", gps.altitude()); |
AjK | 1:6aec92e77ad2 | 94 | |
AjK | 1:6aec92e77ad2 | 95 | // Gran a snapshot of the current time. |
AjK | 1:6aec92e77ad2 | 96 | gps.timeNow(&q1); |
AjK | 1:6aec92e77ad2 | 97 | pc.printf("%02d:%02d:%02d %02d/%02d/%04d\r\n", |
AjK | 1:6aec92e77ad2 | 98 | q1.hour, q1.minute, q1.second, q1.day, q1.month, q1.year); |
AjK | 1:6aec92e77ad2 | 99 | |
AjK | 1:6aec92e77ad2 | 100 | // Alternative method that does the same thing. |
AjK | 1:6aec92e77ad2 | 101 | pc.printf("Method 2. Lat = %.4f ", gps.latitude()); |
AjK | 1:6aec92e77ad2 | 102 | pc.printf("Lon = %.4f ", gps.longitude()); |
AjK | 1:6aec92e77ad2 | 103 | pc.printf("Alt = %.4f ", gps.altitude()); |
AjK | 1:6aec92e77ad2 | 104 | |
AjK | 1:6aec92e77ad2 | 105 | GPS_Time *q2 = gps.timeNow(); |
AjK | 1:6aec92e77ad2 | 106 | pc.printf("%02d:%02d:%02d %02d/%02d/%04d\r\n\n", |
AjK | 1:6aec92e77ad2 | 107 | q2->hour, q2->minute, q2->second, q2->day, q2->month, q2->year); |
AjK | 1:6aec92e77ad2 | 108 | delete(q2); |
AjK | 1:6aec92e77ad2 | 109 | led1 = 0; |
AjK | 1:6aec92e77ad2 | 110 | } |
AjK | 1:6aec92e77ad2 | 111 | } |
AjK | 1:6aec92e77ad2 | 112 | |
AjK | 1:6aec92e77ad2 | 113 | #endif |