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
example3.cpp@7:049436bc2225, 2018-09-29 (annotated)
- Committer:
- xanter
- Date:
- Sat Sep 29 20:31:31 2018 +0000
- Revision:
- 7:049436bc2225
- Parent:
- 2:8aa059e7d8b1
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_EXAMPLE3_CODE_MODGPS |
AjK | 2:8aa059e7d8b1 | 2 | |
AjK | 2:8aa059e7d8b1 | 3 | // See forum post http://mbed.org/forum/mbed/topic/2151/ |
AjK | 2:8aa059e7d8b1 | 4 | |
AjK | 2:8aa059e7d8b1 | 5 | #include "mbed.h" |
AjK | 2:8aa059e7d8b1 | 6 | #include "GPS.h" |
AjK | 2:8aa059e7d8b1 | 7 | |
AjK | 2:8aa059e7d8b1 | 8 | Serial pc(USBTX, USBRX); |
AjK | 2:8aa059e7d8b1 | 9 | DigitalOut led1(LED1); |
AjK | 2:8aa059e7d8b1 | 10 | |
AjK | 2:8aa059e7d8b1 | 11 | // SET THIS. |
AjK | 2:8aa059e7d8b1 | 12 | // Create an instance of the GPS object. You will need to |
AjK | 2:8aa059e7d8b1 | 13 | // set p25 to whichever Serial RX pin you have connected |
AjK | 2:8aa059e7d8b1 | 14 | // your GPS module to. |
AjK | 2:8aa059e7d8b1 | 15 | GPS gps(NC, GPSRX); |
AjK | 2:8aa059e7d8b1 | 16 | |
AjK | 2:8aa059e7d8b1 | 17 | // 0.05 second flash of LED2 |
AjK | 2:8aa059e7d8b1 | 18 | DigitalOut led2(LED2); |
AjK | 2:8aa059e7d8b1 | 19 | Timeout t2; |
AjK | 2:8aa059e7d8b1 | 20 | void t2out(void) { led2 = 0; } |
AjK | 2:8aa059e7d8b1 | 21 | void blip2(void) { led2 = 1; t2.attach(&t2out, 0.05); } |
AjK | 2:8aa059e7d8b1 | 22 | |
AjK | 2:8aa059e7d8b1 | 23 | // 0.05 second flash of LED3 |
AjK | 2:8aa059e7d8b1 | 24 | DigitalOut led3(LED3); |
AjK | 2:8aa059e7d8b1 | 25 | Timeout t3; |
AjK | 2:8aa059e7d8b1 | 26 | void t3out(void) { led3 = 0; } |
AjK | 2:8aa059e7d8b1 | 27 | void blip3(void) { led3 = 1; t3.attach(&t3out, 0.05); } |
AjK | 2:8aa059e7d8b1 | 28 | |
AjK | 2:8aa059e7d8b1 | 29 | // 0.05 second flash of LED4 |
AjK | 2:8aa059e7d8b1 | 30 | DigitalOut led4(LED4); |
AjK | 2:8aa059e7d8b1 | 31 | |
AjK | 2:8aa059e7d8b1 | 32 | Timeout t4; |
AjK | 2:8aa059e7d8b1 | 33 | void t4out(void) { led4 = 0; } |
AjK | 2:8aa059e7d8b1 | 34 | void blip4(void) { led4 = 1; t4.attach(&t4out, 0.05); } |
AjK | 2:8aa059e7d8b1 | 35 | |
AjK | 2:8aa059e7d8b1 | 36 | int main() { |
AjK | 2:8aa059e7d8b1 | 37 | GPS_Time q1; |
AjK | 2:8aa059e7d8b1 | 38 | GPS_VTG v1; |
AjK | 2:8aa059e7d8b1 | 39 | |
AjK | 2:8aa059e7d8b1 | 40 | // SET THIS. |
AjK | 2:8aa059e7d8b1 | 41 | // Ensure you set the baud rate to match your serial |
AjK | 2:8aa059e7d8b1 | 42 | // communications to your PC/Max/Linux host so you |
AjK | 2:8aa059e7d8b1 | 43 | // can read the messages. |
AjK | 2:8aa059e7d8b1 | 44 | pc.baud(PCBAUD); |
AjK | 2:8aa059e7d8b1 | 45 | |
AjK | 2:8aa059e7d8b1 | 46 | // SET THIS. |
AjK | 2:8aa059e7d8b1 | 47 | // Most GPS modules use 9600,8,n,1 so that's what |
AjK | 2:8aa059e7d8b1 | 48 | // we default to here. Ensure your GPS module matches |
AjK | 2:8aa059e7d8b1 | 49 | // this, otherwise set it to match. |
AjK | 2:8aa059e7d8b1 | 50 | gps.baud(GPSBUAD); |
AjK | 2:8aa059e7d8b1 | 51 | gps.format(8, GPS::None, 1); |
AjK | 2:8aa059e7d8b1 | 52 | |
AjK | 2:8aa059e7d8b1 | 53 | // OPTIONAL |
AjK | 2:8aa059e7d8b1 | 54 | // If you GPS has a 1 pulse per second output you can |
AjK | 2:8aa059e7d8b1 | 55 | // connect it to an Mbed pin. Here you specify what pin |
AjK | 2:8aa059e7d8b1 | 56 | // and on what "edge" teh signal is active. If your GPS |
AjK | 2:8aa059e7d8b1 | 57 | // module has a rising edge at the one second point then |
AjK | 2:8aa059e7d8b1 | 58 | // use GPS::ppsRise |
AjK | 2:8aa059e7d8b1 | 59 | #ifdef PPSPIN |
AjK | 2:8aa059e7d8b1 | 60 | gps.ppsAttach(PPSPIN, GPS::ppsFall); |
AjK | 2:8aa059e7d8b1 | 61 | #endif |
AjK | 2:8aa059e7d8b1 | 62 | |
AjK | 2:8aa059e7d8b1 | 63 | // Sample of a callback to a function when a NMEA GGA message is recieved. |
AjK | 2:8aa059e7d8b1 | 64 | // For this example, we flash LED2 for 0.05 second. |
AjK | 2:8aa059e7d8b1 | 65 | gps.attach_gga(&blip2); |
AjK | 2:8aa059e7d8b1 | 66 | |
AjK | 2:8aa059e7d8b1 | 67 | // Sample of a callback to a function when a NMEA RMC message is recieved. |
AjK | 2:8aa059e7d8b1 | 68 | // For this example, we flash LED3 for 0.05 second. |
AjK | 2:8aa059e7d8b1 | 69 | gps.attach_rmc(&blip3); |
AjK | 2:8aa059e7d8b1 | 70 | |
AjK | 2:8aa059e7d8b1 | 71 | // Sample of a callback to a function when a NMEA VTG message is recieved. |
AjK | 2:8aa059e7d8b1 | 72 | // For this example, we flash LED4 for 0.05 second. |
AjK | 2:8aa059e7d8b1 | 73 | gps.attach_vtg(&blip4); |
AjK | 2:8aa059e7d8b1 | 74 | |
AjK | 2:8aa059e7d8b1 | 75 | while(1) { |
AjK | 2:8aa059e7d8b1 | 76 | // Every 3 seconds, flip LED1 and print the basic GPS info. |
AjK | 2:8aa059e7d8b1 | 77 | wait(3); |
AjK | 2:8aa059e7d8b1 | 78 | led1 = 1; |
AjK | 2:8aa059e7d8b1 | 79 | |
AjK | 2:8aa059e7d8b1 | 80 | // Demonstrate how to find out the GPS location co-ords. |
AjK | 2:8aa059e7d8b1 | 81 | pc.printf("Method 1. Lat = %.4f ", gps.latitude()); |
AjK | 2:8aa059e7d8b1 | 82 | pc.printf("Lon = %.4f ", gps.longitude()); |
AjK | 2:8aa059e7d8b1 | 83 | pc.printf("Alt = %.4f ", gps.altitude()); |
AjK | 2:8aa059e7d8b1 | 84 | |
AjK | 2:8aa059e7d8b1 | 85 | // Grab a snapshot of the current time. |
AjK | 2:8aa059e7d8b1 | 86 | gps.timeNow(&q1); |
AjK | 2:8aa059e7d8b1 | 87 | pc.printf("%c %02d:%02d:%02d %02d/%02d/%04d\r\n", |
AjK | 2:8aa059e7d8b1 | 88 | q1.status, q1.hour, q1.minute, q1.second, q1.day, q1.month, q1.year); |
AjK | 2:8aa059e7d8b1 | 89 | |
AjK | 2:8aa059e7d8b1 | 90 | gps.vtg(&v1); |
AjK | 2:8aa059e7d8b1 | 91 | pc.printf("Method 1. Vector data, Speed (knots):%lf, Speed(kph):%lf, Track(true):%lf, Track(mag)%lf\r\n", |
AjK | 2:8aa059e7d8b1 | 92 | v1.velocity_knots(), v1.velocity_kph(), v1.track_true(), v1.track_mag()); |
AjK | 2:8aa059e7d8b1 | 93 | |
AjK | 2:8aa059e7d8b1 | 94 | // Alternative method that does the same thing. |
AjK | 2:8aa059e7d8b1 | 95 | pc.printf("Method 2. Lat = %.4f ", gps.latitude()); |
AjK | 2:8aa059e7d8b1 | 96 | pc.printf("Lon = %.4f ", gps.longitude()); |
AjK | 2:8aa059e7d8b1 | 97 | pc.printf("Alt = %.4f ", gps.altitude()); |
AjK | 2:8aa059e7d8b1 | 98 | |
AjK | 2:8aa059e7d8b1 | 99 | GPS_Time *q2 = gps.timeNow(); |
AjK | 2:8aa059e7d8b1 | 100 | pc.printf("%c %02d:%02d:%02d %02d/%02d/%04d\r\n", |
AjK | 2:8aa059e7d8b1 | 101 | q2->status, q2->hour, q2->minute, q2->second, q2->day, q2->month, q2->year); |
AjK | 2:8aa059e7d8b1 | 102 | delete(q2); |
AjK | 2:8aa059e7d8b1 | 103 | |
AjK | 2:8aa059e7d8b1 | 104 | GPS_VTG *v2 = gps.vtg(); |
AjK | 2:8aa059e7d8b1 | 105 | pc.printf("Method 2. Vector data, Speed (knots):%lf, Speed(kph):%lf, Track(true):%lf, Track(mag):%lf\r\n\n", |
AjK | 2:8aa059e7d8b1 | 106 | v2->velocity_knots(), v2->velocity_kph(), v2->track_true(), v2->track_mag()); |
AjK | 2:8aa059e7d8b1 | 107 | delete(v2); |
AjK | 2:8aa059e7d8b1 | 108 | |
AjK | 2:8aa059e7d8b1 | 109 | led1 = 0; |
AjK | 2:8aa059e7d8b1 | 110 | } |
AjK | 2:8aa059e7d8b1 | 111 | } |
AjK | 2:8aa059e7d8b1 | 112 | |
AjK | 2:8aa059e7d8b1 | 113 | #endif |