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@0:db98027c0bbb, 2010-11-15 (annotated)
- Committer:
- AjK
- Date:
- Mon Nov 15 20:11:16 2010 +0000
- Revision:
- 0:db98027c0bbb
- Child:
- 1:6aec92e77ad2
1.8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 0:db98027c0bbb | 1 | #ifdef COMPILE_EXAMPLE_CODE_MODGPS |
AjK | 0:db98027c0bbb | 2 | |
AjK | 0:db98027c0bbb | 3 | #include "mbed.h" |
AjK | 0:db98027c0bbb | 4 | #include "GPS.h" |
AjK | 0:db98027c0bbb | 5 | |
AjK | 0:db98027c0bbb | 6 | Serial pc(USBTX, USBRX); |
AjK | 0:db98027c0bbb | 7 | DigitalOut led1(LED1); |
AjK | 0:db98027c0bbb | 8 | |
AjK | 0:db98027c0bbb | 9 | // SET THIS. |
AjK | 0:db98027c0bbb | 10 | // Create an instance of the GPS object. You will need to |
AjK | 0:db98027c0bbb | 11 | // set p25 to whichever Serial RX pin you have connected |
AjK | 0:db98027c0bbb | 12 | // your GPS module to. |
AjK | 0:db98027c0bbb | 13 | GPS gps(NC, GPSRX); |
AjK | 0:db98027c0bbb | 14 | |
AjK | 0:db98027c0bbb | 15 | // 0.1 second flash of LED2 |
AjK | 0:db98027c0bbb | 16 | DigitalOut led2(LED2); |
AjK | 0:db98027c0bbb | 17 | Timeout t2; |
AjK | 0:db98027c0bbb | 18 | void t2out(void) { led2 = 0; } |
AjK | 0:db98027c0bbb | 19 | void blip2(void) { led2 = 1; t2.attach(&t2out, 0.1); } |
AjK | 0:db98027c0bbb | 20 | |
AjK | 0:db98027c0bbb | 21 | // 0.1 second flash of LED3 |
AjK | 0:db98027c0bbb | 22 | DigitalOut led3(LED3); |
AjK | 0:db98027c0bbb | 23 | Timeout t3; |
AjK | 0:db98027c0bbb | 24 | void t3out(void) { led3 = 0; } |
AjK | 0:db98027c0bbb | 25 | void blip3(void) { led3 = 1; t3.attach(&t3out, 0.1); } |
AjK | 0:db98027c0bbb | 26 | |
AjK | 0:db98027c0bbb | 27 | // 0.1 second flash of LED4 |
AjK | 0:db98027c0bbb | 28 | DigitalOut led4(LED4); |
AjK | 0:db98027c0bbb | 29 | |
AjK | 0:db98027c0bbb | 30 | Timeout t4; |
AjK | 0:db98027c0bbb | 31 | void t4out(void) { led4 = 0; } |
AjK | 0:db98027c0bbb | 32 | void blip4(void) { led4 = 1; t4.attach(&t4out, 0.1); } |
AjK | 0:db98027c0bbb | 33 | |
AjK | 0:db98027c0bbb | 34 | int main() { |
AjK | 0:db98027c0bbb | 35 | GPS_Time q1; |
AjK | 0:db98027c0bbb | 36 | |
AjK | 0:db98027c0bbb | 37 | // SET THIS. |
AjK | 0:db98027c0bbb | 38 | // Ensure you set the baud rate to match your serial |
AjK | 0:db98027c0bbb | 39 | // communications to your PC/Max/Linux host so you |
AjK | 0:db98027c0bbb | 40 | // can read the messages. |
AjK | 0:db98027c0bbb | 41 | pc.baud(PCBAUD); |
AjK | 0:db98027c0bbb | 42 | |
AjK | 0:db98027c0bbb | 43 | // SET THIS. |
AjK | 0:db98027c0bbb | 44 | // Most GPS modules use 9600,8,n,1 so that's what |
AjK | 0:db98027c0bbb | 45 | // we default to here. Ensure your GPS module matches |
AjK | 0:db98027c0bbb | 46 | // this, otherwise set it to match. |
AjK | 0:db98027c0bbb | 47 | gps.baud(GPSBUAD); |
AjK | 0:db98027c0bbb | 48 | gps.format(8, GPS::None, 1); |
AjK | 0:db98027c0bbb | 49 | |
AjK | 0:db98027c0bbb | 50 | // OPTIONAL |
AjK | 0:db98027c0bbb | 51 | // If you GPS has a 1 pulse per second output you can |
AjK | 0:db98027c0bbb | 52 | // connect it to an Mbed pin. Here you specify what pin |
AjK | 0:db98027c0bbb | 53 | // and on what "edge" teh signal is active. If your GPS |
AjK | 0:db98027c0bbb | 54 | // module has a rising edge at the one second point then |
AjK | 0:db98027c0bbb | 55 | // use GPS::ppsRise |
AjK | 0:db98027c0bbb | 56 | #ifdef PPSPIN |
AjK | 0:db98027c0bbb | 57 | gps.ppsAttach(PPSPIN, GPS::ppsFall); |
AjK | 0:db98027c0bbb | 58 | #endif |
AjK | 0:db98027c0bbb | 59 | |
AjK | 0:db98027c0bbb | 60 | // Sample of a callback to a function when the 1PPS activates. |
AjK | 0:db98027c0bbb | 61 | // For this example, we flash LED2 for 0.1 second. |
AjK | 0:db98027c0bbb | 62 | gps.attach_pps(&blip2); |
AjK | 0:db98027c0bbb | 63 | |
AjK | 0:db98027c0bbb | 64 | // Sample of a callback to a function when a NMEA GGA message is recieved. |
AjK | 0:db98027c0bbb | 65 | // For this example, we flash LED2 for 0.1 second. |
AjK | 0:db98027c0bbb | 66 | gps.attach_gga(&blip3); |
AjK | 0:db98027c0bbb | 67 | |
AjK | 0:db98027c0bbb | 68 | // Sample of a callback to a function when a NMEA RMC message is recieved. |
AjK | 0:db98027c0bbb | 69 | // For this example, we flash LED2 for 0.1 second. |
AjK | 0:db98027c0bbb | 70 | gps.attach_rmc(&blip4); |
AjK | 0:db98027c0bbb | 71 | |
AjK | 0:db98027c0bbb | 72 | while(1) { |
AjK | 0:db98027c0bbb | 73 | // Every 3 seconds, flip LED1 and print the basic GPS info. |
AjK | 0:db98027c0bbb | 74 | wait(3); |
AjK | 0:db98027c0bbb | 75 | led1 = 1; |
AjK | 0:db98027c0bbb | 76 | |
AjK | 0:db98027c0bbb | 77 | // Demonstrate how to find out the GPS location co-ords. |
AjK | 0:db98027c0bbb | 78 | pc.printf("Method 1. Lat = %.4f ", gps.latitude()); |
AjK | 0:db98027c0bbb | 79 | pc.printf("Lon = %.4f ", gps.longitude()); |
AjK | 0:db98027c0bbb | 80 | pc.printf("Alt = %.4f ", gps.altitude()); |
AjK | 0:db98027c0bbb | 81 | |
AjK | 0:db98027c0bbb | 82 | // Gran a snapshot of the current time. |
AjK | 0:db98027c0bbb | 83 | gps.timeNow(&q1); |
AjK | 0:db98027c0bbb | 84 | pc.printf("%02d:%02d:%02d %02d/%02d/%04d\r\n", |
AjK | 0:db98027c0bbb | 85 | q1.hour, q1.minute, q1.second, q1.day, q1.month, q1.year); |
AjK | 0:db98027c0bbb | 86 | |
AjK | 0:db98027c0bbb | 87 | // Alternative method that does the same thing. |
AjK | 0:db98027c0bbb | 88 | pc.printf("Method 2. Lat = %.4f ", gps.latitude()); |
AjK | 0:db98027c0bbb | 89 | pc.printf("Lon = %.4f ", gps.longitude()); |
AjK | 0:db98027c0bbb | 90 | pc.printf("Alt = %.4f ", gps.altitude()); |
AjK | 0:db98027c0bbb | 91 | |
AjK | 0:db98027c0bbb | 92 | GPS_Time *q2 = gps.timeNow(); |
AjK | 0:db98027c0bbb | 93 | pc.printf("%02d:%02d:%02d %02d/%02d/%04d\r\n\n", |
AjK | 0:db98027c0bbb | 94 | q2->hour, q2->minute, q2->second, q2->day, q2->month, q2->year); |
AjK | 0:db98027c0bbb | 95 | delete(q2); |
AjK | 0:db98027c0bbb | 96 | led1 = 0; |
AjK | 0:db98027c0bbb | 97 | } |
AjK | 0:db98027c0bbb | 98 | } |
AjK | 0:db98027c0bbb | 99 | |
AjK | 0:db98027c0bbb | 100 | #endif |