example program demonstrating use of the adafruit gps and logging to an sd card
Dependencies: AdafruitGPS SDFileSystem TSI mbed
main.cpp@0:b23bd622e0cc, 2015-06-16 (annotated)
- Committer:
- ftagius
- Date:
- Tue Jun 16 12:05:21 2015 +0000
- Revision:
- 0:b23bd622e0cc
example program to interface with an adafruit ultimate gps and to log gps data to an sd card
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ftagius | 0:b23bd622e0cc | 1 | #include "mbed.h" |
ftagius | 0:b23bd622e0cc | 2 | #include "GPS.h" |
ftagius | 0:b23bd622e0cc | 3 | #include "main.h" |
ftagius | 0:b23bd622e0cc | 4 | #include "TSISensor.h" |
ftagius | 0:b23bd622e0cc | 5 | #include "SDFileSystem.h" |
ftagius | 0:b23bd622e0cc | 6 | |
ftagius | 0:b23bd622e0cc | 7 | Serial pc(USBTX, USBRX); |
ftagius | 0:b23bd622e0cc | 8 | GPS gpsd(PTE20, PTE21); |
ftagius | 0:b23bd622e0cc | 9 | // frdm-kl25z sd card connections |
ftagius | 0:b23bd622e0cc | 10 | // ------------------------------------------------ |
ftagius | 0:b23bd622e0cc | 11 | // Header -- kl25z -- SD/MMC |
ftagius | 0:b23bd622e0cc | 12 | // J2-8 -- PTD2 -- MOSI |
ftagius | 0:b23bd622e0cc | 13 | // J2-6 -- PTD0 -- CS |
ftagius | 0:b23bd622e0cc | 14 | // J9-12 -- GND -- Vss (GND) |
ftagius | 0:b23bd622e0cc | 15 | // J9-4 -- P3V3 -- Vdd (+3.3v) |
ftagius | 0:b23bd622e0cc | 16 | // J2-12 -- PTD1 -- SCK |
ftagius | 0:b23bd622e0cc | 17 | // J9-14 -- GND -- Vss (GND) |
ftagius | 0:b23bd622e0cc | 18 | // J2-10 -- PTD3 -- MISO |
ftagius | 0:b23bd622e0cc | 19 | |
ftagius | 0:b23bd622e0cc | 20 | SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // MOSI, MISO, SCK, CS |
ftagius | 0:b23bd622e0cc | 21 | DigitalOut red(LED_RED); |
ftagius | 0:b23bd622e0cc | 22 | DigitalOut green(LED_GREEN); |
ftagius | 0:b23bd622e0cc | 23 | //DigitalOut blue(LED_BLUE); don't use the blue led, due to a board error, writing to the blue led kills spi |
ftagius | 0:b23bd622e0cc | 24 | bool debug; |
ftagius | 0:b23bd622e0cc | 25 | |
ftagius | 0:b23bd622e0cc | 26 | bool gpsEnabled = true; |
ftagius | 0:b23bd622e0cc | 27 | |
ftagius | 0:b23bd622e0cc | 28 | int main() |
ftagius | 0:b23bd622e0cc | 29 | { |
ftagius | 0:b23bd622e0cc | 30 | bool debug=false; |
ftagius | 0:b23bd622e0cc | 31 | bool logOpened=false; |
ftagius | 0:b23bd622e0cc | 32 | char logFile[128]; |
ftagius | 0:b23bd622e0cc | 33 | char directory[]="/sd/gps_data"; |
ftagius | 0:b23bd622e0cc | 34 | char c; |
ftagius | 0:b23bd622e0cc | 35 | FILE *fp; |
ftagius | 0:b23bd622e0cc | 36 | Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? |
ftagius | 0:b23bd622e0cc | 37 | const int refresh_Time = 1000; //refresh time in ms |
ftagius | 0:b23bd622e0cc | 38 | TSISensor tsi; // touch slider |
ftagius | 0:b23bd622e0cc | 39 | int write_count=0; |
ftagius | 0:b23bd622e0cc | 40 | pc.baud(9600); |
ftagius | 0:b23bd622e0cc | 41 | |
ftagius | 0:b23bd622e0cc | 42 | red = 1; |
ftagius | 0:b23bd622e0cc | 43 | green = 1; |
ftagius | 0:b23bd622e0cc | 44 | pc.printf("Touch slider to start application\r\n"); |
ftagius | 0:b23bd622e0cc | 45 | while(1) { |
ftagius | 0:b23bd622e0cc | 46 | green = 1; // turn led off |
ftagius | 0:b23bd622e0cc | 47 | wait_ms(200); |
ftagius | 0:b23bd622e0cc | 48 | green = 0; // turn led on |
ftagius | 0:b23bd622e0cc | 49 | wait_ms(200); |
ftagius | 0:b23bd622e0cc | 50 | if (tsi.readPercentage()) |
ftagius | 0:b23bd622e0cc | 51 | break; |
ftagius | 0:b23bd622e0cc | 52 | } |
ftagius | 0:b23bd622e0cc | 53 | green = 1; |
ftagius | 0:b23bd622e0cc | 54 | |
ftagius | 0:b23bd622e0cc | 55 | gpsd.setBaud(9600); |
ftagius | 0:b23bd622e0cc | 56 | gpsd.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); |
ftagius | 0:b23bd622e0cc | 57 | gpsd.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
ftagius | 0:b23bd622e0cc | 58 | gpsd.sendCommand(PGCMD_ANTENNA); |
ftagius | 0:b23bd622e0cc | 59 | gpsd.day=01; |
ftagius | 0:b23bd622e0cc | 60 | gpsd.month=01; |
ftagius | 0:b23bd622e0cc | 61 | gpsd.year=15; |
ftagius | 0:b23bd622e0cc | 62 | gpsd.hour=1; |
ftagius | 0:b23bd622e0cc | 63 | gpsd.minute=1; |
ftagius | 0:b23bd622e0cc | 64 | gpsd.seconds=1; |
ftagius | 0:b23bd622e0cc | 65 | refresh_Timer.start(); //starts the clock on the timer |
ftagius | 0:b23bd622e0cc | 66 | //Mount the filesystem |
ftagius | 0:b23bd622e0cc | 67 | sd.mount(); |
ftagius | 0:b23bd622e0cc | 68 | mkdir(directory, 0777); |
ftagius | 0:b23bd622e0cc | 69 | pc.printf("Starting GPS App\r\n"); |
ftagius | 0:b23bd622e0cc | 70 | while (1) |
ftagius | 0:b23bd622e0cc | 71 | { |
ftagius | 0:b23bd622e0cc | 72 | c = gpsd.read(); //queries the GPS |
ftagius | 0:b23bd622e0cc | 73 | if (debug) |
ftagius | 0:b23bd622e0cc | 74 | { |
ftagius | 0:b23bd622e0cc | 75 | if (c) { |
ftagius | 0:b23bd622e0cc | 76 | printf("%c", c); //this line will echo the GPS data if not paused |
ftagius | 0:b23bd622e0cc | 77 | continue; |
ftagius | 0:b23bd622e0cc | 78 | } |
ftagius | 0:b23bd622e0cc | 79 | } |
ftagius | 0:b23bd622e0cc | 80 | |
ftagius | 0:b23bd622e0cc | 81 | //check if we recieved a new message from GPS, if so, attempt to parse it, |
ftagius | 0:b23bd622e0cc | 82 | if ( gpsd.newNMEAreceived() ) { |
ftagius | 0:b23bd622e0cc | 83 | if ( !gpsd.parse(gpsd.lastNMEA()) ) { |
ftagius | 0:b23bd622e0cc | 84 | continue; |
ftagius | 0:b23bd622e0cc | 85 | } |
ftagius | 0:b23bd622e0cc | 86 | } |
ftagius | 0:b23bd622e0cc | 87 | |
ftagius | 0:b23bd622e0cc | 88 | //check if enough time has passed to warrant printing GPS info to screen |
ftagius | 0:b23bd622e0cc | 89 | //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing |
ftagius | 0:b23bd622e0cc | 90 | if (refresh_Timer.read_ms() >= refresh_Time) |
ftagius | 0:b23bd622e0cc | 91 | { |
ftagius | 0:b23bd622e0cc | 92 | if (gpsd.fix) { |
ftagius | 0:b23bd622e0cc | 93 | // got a gps fix |
ftagius | 0:b23bd622e0cc | 94 | |
ftagius | 0:b23bd622e0cc | 95 | // is the log file open? if not, open it |
ftagius | 0:b23bd622e0cc | 96 | if ( !logOpened ) |
ftagius | 0:b23bd622e0cc | 97 | { |
ftagius | 0:b23bd622e0cc | 98 | logOpened = true; |
ftagius | 0:b23bd622e0cc | 99 | // create a log file name with the current date/time stamp |
ftagius | 0:b23bd622e0cc | 100 | sprintf(logFile, "%s%s%02d_%02d_20%02d_%02d_%02d_%02d.csv", directory,"/gps_",gpsd.day,gpsd.month,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds); |
ftagius | 0:b23bd622e0cc | 101 | sd.mount(); |
ftagius | 0:b23bd622e0cc | 102 | fp = fopen(logFile, "w"); |
ftagius | 0:b23bd622e0cc | 103 | if(fp == NULL) { |
ftagius | 0:b23bd622e0cc | 104 | // retry |
ftagius | 0:b23bd622e0cc | 105 | wait_ms(500); |
ftagius | 0:b23bd622e0cc | 106 | fp = fopen(logFile, "w"); |
ftagius | 0:b23bd622e0cc | 107 | if (fp == NULL) |
ftagius | 0:b23bd622e0cc | 108 | error("Could not open file %s for writing\r\n",logFile); |
ftagius | 0:b23bd622e0cc | 109 | } |
ftagius | 0:b23bd622e0cc | 110 | else |
ftagius | 0:b23bd622e0cc | 111 | { |
ftagius | 0:b23bd622e0cc | 112 | pc.printf("Opened log file %s\r\n",logFile); |
ftagius | 0:b23bd622e0cc | 113 | } |
ftagius | 0:b23bd622e0cc | 114 | // write the log file header |
ftagius | 0:b23bd622e0cc | 115 | fprintf(fp,"# date,time,raw timestamp,latitude,longitude\r\n"); |
ftagius | 0:b23bd622e0cc | 116 | } |
ftagius | 0:b23bd622e0cc | 117 | |
ftagius | 0:b23bd622e0cc | 118 | pc.printf("Date: %02d/%02d/20%02d ", gpsd.month, gpsd.day, gpsd.year); |
ftagius | 0:b23bd622e0cc | 119 | pc.printf("%02d:%02d:%02d ", gpsd.hour, gpsd.minute, gpsd.seconds); |
ftagius | 0:b23bd622e0cc | 120 | pc.printf("Rawdate: %7.0f ",gpsd.timef); |
ftagius | 0:b23bd622e0cc | 121 | //pc.printf("Fix: %d ", (int) gpsd.fix); |
ftagius | 0:b23bd622e0cc | 122 | //pc.printf("Quality: %d ", (int) gpsd.fixquality); |
ftagius | 0:b23bd622e0cc | 123 | //pc.printf("Loc: %5.2f%c, %5.2f%c ", gpsd.latitude, gpsd.lat, gpsd.longitude, gpsd.lon); |
ftagius | 0:b23bd622e0cc | 124 | pc. printf("Loc: %5.7f, %5.7f ", gpsd.lat_deg, gpsd.lon_deg); |
ftagius | 0:b23bd622e0cc | 125 | //pc.printf("Speed: %5.2f knots ", gpsd.speed); |
ftagius | 0:b23bd622e0cc | 126 | //pc.printf("Angle: %5.2f ", gpsd.angle); |
ftagius | 0:b23bd622e0cc | 127 | //pc.printf("Altitude: %5.2f ", gpsd.altitude); |
ftagius | 0:b23bd622e0cc | 128 | pc. printf("Sat: %d\r\n", gpsd.satellites); |
ftagius | 0:b23bd622e0cc | 129 | |
ftagius | 0:b23bd622e0cc | 130 | //red = 1; // turn led off |
ftagius | 0:b23bd622e0cc | 131 | green = 0; // turn led on |
ftagius | 0:b23bd622e0cc | 132 | //wait_ms(100); |
ftagius | 0:b23bd622e0cc | 133 | // keep the log files small by keeping track of write_count. |
ftagius | 0:b23bd622e0cc | 134 | // when write_count exceeds a threshold, close the log file. |
ftagius | 0:b23bd622e0cc | 135 | // a new log file will be openeded at the start of the next loop. |
ftagius | 0:b23bd622e0cc | 136 | if (write_count < 1000) |
ftagius | 0:b23bd622e0cc | 137 | { |
ftagius | 0:b23bd622e0cc | 138 | // write to the current log file |
ftagius | 0:b23bd622e0cc | 139 | fprintf(fp,"%02d/%02d/20%02d,", gpsd.month, gpsd.day, gpsd.year); |
ftagius | 0:b23bd622e0cc | 140 | fprintf(fp,"%02d:%02d:%02d,", gpsd.hour, gpsd.minute, gpsd.seconds); |
ftagius | 0:b23bd622e0cc | 141 | fprintf(fp,"%7.0f,",gpsd.timef); |
ftagius | 0:b23bd622e0cc | 142 | fprintf(fp,"%5.7f,%5.7f", gpsd.lat_deg, gpsd.lon_deg); |
ftagius | 0:b23bd622e0cc | 143 | fprintf(fp,"\r\n"); |
ftagius | 0:b23bd622e0cc | 144 | fflush(fp); |
ftagius | 0:b23bd622e0cc | 145 | f_sync((FIL*)fp); |
ftagius | 0:b23bd622e0cc | 146 | fclose(fp); |
ftagius | 0:b23bd622e0cc | 147 | fp = fopen(logFile, "a"); |
ftagius | 0:b23bd622e0cc | 148 | //wait_ms(100); |
ftagius | 0:b23bd622e0cc | 149 | if(fp == NULL) |
ftagius | 0:b23bd622e0cc | 150 | { |
ftagius | 0:b23bd622e0cc | 151 | // retry |
ftagius | 0:b23bd622e0cc | 152 | wait_ms(500); |
ftagius | 0:b23bd622e0cc | 153 | fp = fopen(logFile, "a"); |
ftagius | 0:b23bd622e0cc | 154 | if (fp == NULL) |
ftagius | 0:b23bd622e0cc | 155 | error("Could not open file for writing 2\r\n"); |
ftagius | 0:b23bd622e0cc | 156 | } |
ftagius | 0:b23bd622e0cc | 157 | //else |
ftagius | 0:b23bd622e0cc | 158 | // pc.printf("file closed then reopened\r\n"); |
ftagius | 0:b23bd622e0cc | 159 | write_count++; |
ftagius | 0:b23bd622e0cc | 160 | |
ftagius | 0:b23bd622e0cc | 161 | } |
ftagius | 0:b23bd622e0cc | 162 | else |
ftagius | 0:b23bd622e0cc | 163 | { |
ftagius | 0:b23bd622e0cc | 164 | // close the current log file |
ftagius | 0:b23bd622e0cc | 165 | fclose(fp); |
ftagius | 0:b23bd622e0cc | 166 | write_count = 0; |
ftagius | 0:b23bd622e0cc | 167 | logOpened = false; |
ftagius | 0:b23bd622e0cc | 168 | //Unmount the filesystem |
ftagius | 0:b23bd622e0cc | 169 | sd.unmount(); |
ftagius | 0:b23bd622e0cc | 170 | } |
ftagius | 0:b23bd622e0cc | 171 | |
ftagius | 0:b23bd622e0cc | 172 | } |
ftagius | 0:b23bd622e0cc | 173 | else |
ftagius | 0:b23bd622e0cc | 174 | { |
ftagius | 0:b23bd622e0cc | 175 | pc.printf("Waiting for GPS FIX\r\n"); |
ftagius | 0:b23bd622e0cc | 176 | red = 0; // turn led on |
ftagius | 0:b23bd622e0cc | 177 | } |
ftagius | 0:b23bd622e0cc | 178 | |
ftagius | 0:b23bd622e0cc | 179 | // restart the timer for the gps print loop |
ftagius | 0:b23bd622e0cc | 180 | refresh_Timer.reset(); |
ftagius | 0:b23bd622e0cc | 181 | } |
ftagius | 0:b23bd622e0cc | 182 | else |
ftagius | 0:b23bd622e0cc | 183 | { |
ftagius | 0:b23bd622e0cc | 184 | //red = 0; // turn led on |
ftagius | 0:b23bd622e0cc | 185 | green = 1; // turn green led off |
ftagius | 0:b23bd622e0cc | 186 | // check the slider. if touched, close the log file and wait for another slider touch |
ftagius | 0:b23bd622e0cc | 187 | if (tsi.readPercentage()) |
ftagius | 0:b23bd622e0cc | 188 | { |
ftagius | 0:b23bd622e0cc | 189 | // close the current log file |
ftagius | 0:b23bd622e0cc | 190 | fclose(fp); |
ftagius | 0:b23bd622e0cc | 191 | write_count = 0; |
ftagius | 0:b23bd622e0cc | 192 | logOpened = false; |
ftagius | 0:b23bd622e0cc | 193 | pc.printf("Closed log file %s\r\n", logFile); |
ftagius | 0:b23bd622e0cc | 194 | pc.printf("Touch slider to continue application\r\n"); |
ftagius | 0:b23bd622e0cc | 195 | while (1) |
ftagius | 0:b23bd622e0cc | 196 | { |
ftagius | 0:b23bd622e0cc | 197 | |
ftagius | 0:b23bd622e0cc | 198 | green = 1; // turn led off |
ftagius | 0:b23bd622e0cc | 199 | wait_ms(200); |
ftagius | 0:b23bd622e0cc | 200 | green = 0; // turn led on |
ftagius | 0:b23bd622e0cc | 201 | wait_ms(200); |
ftagius | 0:b23bd622e0cc | 202 | // break of of the loop if the slider is touched |
ftagius | 0:b23bd622e0cc | 203 | if (tsi.readPercentage()) |
ftagius | 0:b23bd622e0cc | 204 | { |
ftagius | 0:b23bd622e0cc | 205 | green = 1; |
ftagius | 0:b23bd622e0cc | 206 | red = 1; |
ftagius | 0:b23bd622e0cc | 207 | break; |
ftagius | 0:b23bd622e0cc | 208 | } |
ftagius | 0:b23bd622e0cc | 209 | |
ftagius | 0:b23bd622e0cc | 210 | } // end while 1 for slider |
ftagius | 0:b23bd622e0cc | 211 | } // end if tsi read |
ftagius | 0:b23bd622e0cc | 212 | } // end else refresh timer |
ftagius | 0:b23bd622e0cc | 213 | |
ftagius | 0:b23bd622e0cc | 214 | } |
ftagius | 0:b23bd622e0cc | 215 | |
ftagius | 0:b23bd622e0cc | 216 | |
ftagius | 0:b23bd622e0cc | 217 | |
ftagius | 0:b23bd622e0cc | 218 | } |