basic lightning detector with gps and sd card logging

Dependencies:   AS3935 AdafruitGPS SDFileSystem TSI mbed ConfigFile

Committer:
ftagius
Date:
Mon Jun 22 20:39:32 2015 +0000
Revision:
1:10d2a051285e
Parent:
0:3328df4c3116
Child:
2:3edb129c60b2
basic lightning detector, with gps and sd card logging

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ftagius 0:3328df4c3116 1 #include "mbed.h"
ftagius 0:3328df4c3116 2 #include "GPS.h"
ftagius 0:3328df4c3116 3 #include "main.h"
ftagius 0:3328df4c3116 4 #include "TSISensor.h"
ftagius 0:3328df4c3116 5 #include "SDFileSystem.h"
ftagius 0:3328df4c3116 6 #include "AS3935.h"
ftagius 0:3328df4c3116 7
ftagius 0:3328df4c3116 8 // frdm-kl25z sd card connections for spi1
ftagius 0:3328df4c3116 9 // ------------------------------------------------
ftagius 0:3328df4c3116 10 // Header -- kl25z -- SD/MMC
ftagius 0:3328df4c3116 11 // J2-20 -- PTE1 -- MOSI
ftagius 0:3328df4c3116 12 // J9-13 -- PTE4 -- CS
ftagius 0:3328df4c3116 13 // J2-14 -- GND -- Vss (GND)
ftagius 0:3328df4c3116 14 // J9-9 -- PTE2 -- SCK
ftagius 0:3328df4c3116 15 // J9-11 -- PTE3 -- MISO
ftagius 0:3328df4c3116 16
ftagius 0:3328df4c3116 17 AS3935 ld(PTE1, PTE3, PTE2, PTE4, "ld", 1000000); // MOSI, MISO, SCK, CS, SPI bus freq (hz)
ftagius 0:3328df4c3116 18 InterruptIn IntLightning(PTA12); //IRQ AS3935
ftagius 0:3328df4c3116 19
ftagius 0:3328df4c3116 20
ftagius 0:3328df4c3116 21 // frdm-kl25z sd card connections spi0
ftagius 0:3328df4c3116 22 // ------------------------------------------------
ftagius 0:3328df4c3116 23 // Header -- kl25z -- SPI
ftagius 0:3328df4c3116 24 // J2-8 -- PTD2 -- MOSI
ftagius 0:3328df4c3116 25 // J2-6 -- PTD0 -- CS
ftagius 0:3328df4c3116 26 // J9-12 -- GND -- Vss (GND)
ftagius 0:3328df4c3116 27 // J9-4 -- P3V3 -- Vdd (+3.3v)
ftagius 0:3328df4c3116 28 // J2-12 -- PTD1 -- SCK
ftagius 0:3328df4c3116 29 // J9-14 -- GND -- Vss (GND)
ftagius 0:3328df4c3116 30 // J2-10 -- PTD3 -- MISO
ftagius 0:3328df4c3116 31
ftagius 0:3328df4c3116 32 SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // MOSI, MISO, SCK, CS
ftagius 0:3328df4c3116 33
ftagius 0:3328df4c3116 34 Serial pc(USBTX, USBRX);
ftagius 0:3328df4c3116 35 GPS gpsd(PTE20, PTE21);
ftagius 0:3328df4c3116 36 DigitalOut red(LED_RED);
ftagius 0:3328df4c3116 37 DigitalOut green(LED_GREEN);
ftagius 0:3328df4c3116 38 //DigitalOut blue(LED_BLUE); don't use the blue led, due to a board error, writing to the blue led kills spi
ftagius 0:3328df4c3116 39 bool debug;
ftagius 0:3328df4c3116 40 int day, month,year,hour,minute,seconds;
ftagius 1:10d2a051285e 41 void writeLogFile(int interruptSource, int distance);
ftagius 1:10d2a051285e 42 char logName[]="lightning_data.csv";
ftagius 1:10d2a051285e 43 int rdistance, rinterrupt;
ftagius 1:10d2a051285e 44 char directory[]="/sd/lightning_data";
ftagius 0:3328df4c3116 45
ftagius 0:3328df4c3116 46 void DetectLightning()
ftagius 0:3328df4c3116 47 {
ftagius 0:3328df4c3116 48 int OriginInt;
ftagius 1:10d2a051285e 49 int distance=-1;
ftagius 0:3328df4c3116 50 wait_ms(2); //
ftagius 0:3328df4c3116 51 OriginInt = ld.interruptSource();
ftagius 0:3328df4c3116 52 pc.printf("%02d/%02d/20%02d_%02d:%02d:%02d ",month,day,year,hour,minute,seconds);
ftagius 0:3328df4c3116 53 switch (OriginInt)
ftagius 0:3328df4c3116 54 {
ftagius 0:3328df4c3116 55 case 1:
ftagius 1:10d2a051285e 56 pc.printf("Noise level too high\r\n");
ftagius 1:10d2a051285e 57 break;
ftagius 0:3328df4c3116 58 case 4:
ftagius 1:10d2a051285e 59 pc.printf("Disturber\r\n");
ftagius 1:10d2a051285e 60 break;
ftagius 0:3328df4c3116 61 case 8:
ftagius 1:10d2a051285e 62 distance = ld.lightningDistanceKm();
ftagius 0:3328df4c3116 63 pc.printf("Lightning detection, distance=%dkm\r\n", distance);
ftagius 0:3328df4c3116 64 ld.clearStats();
ftagius 1:10d2a051285e 65 break;
ftagius 0:3328df4c3116 66 default:
ftagius 1:10d2a051285e 67 pc.printf("Unknown interrupt %d\r\n", OriginInt);
ftagius 0:3328df4c3116 68
ftagius 0:3328df4c3116 69 }
ftagius 1:10d2a051285e 70 writeLogFile(OriginInt,distance);
ftagius 1:10d2a051285e 71 pc.printf("Resume wait for lightning detection...\r\n");
ftagius 0:3328df4c3116 72
ftagius 0:3328df4c3116 73 }
ftagius 0:3328df4c3116 74
ftagius 1:10d2a051285e 75 void writeLogFile(int interruptSource, int distance)
ftagius 0:3328df4c3116 76 {
ftagius 1:10d2a051285e 77 char logFilePath[128];
ftagius 0:3328df4c3116 78 static bool header=false;
ftagius 0:3328df4c3116 79 FILE *fp;
ftagius 1:10d2a051285e 80
ftagius 1:10d2a051285e 81 sprintf(logFilePath, "%s/%s", directory,logName);
ftagius 0:3328df4c3116 82 sd.mount();
ftagius 1:10d2a051285e 83 fp = fopen(logFilePath, "a");
ftagius 0:3328df4c3116 84 if(fp == NULL) {
ftagius 0:3328df4c3116 85 // retry
ftagius 0:3328df4c3116 86 wait_ms(500);
ftagius 1:10d2a051285e 87 fp = fopen(logFilePath, "a");
ftagius 0:3328df4c3116 88 if (fp == NULL)
ftagius 0:3328df4c3116 89 {
ftagius 1:10d2a051285e 90 printf("Could not open file %s for writing\r\n",logFilePath);
ftagius 0:3328df4c3116 91 sd.unmount();
ftagius 1:10d2a051285e 92 printf("unmount sd card \r\n");
ftagius 0:3328df4c3116 93 return;
ftagius 0:3328df4c3116 94 }
ftagius 0:3328df4c3116 95 }
ftagius 1:10d2a051285e 96 pc.printf("Opened log file %s\r\n",logFilePath);
ftagius 0:3328df4c3116 97 // write the log file header
ftagius 0:3328df4c3116 98 if (header == false)
ftagius 0:3328df4c3116 99 {
ftagius 1:10d2a051285e 100 fprintf(fp,"# date,time,raw timestamp,latitude,longitude,distance,interrupt\r\n");
ftagius 0:3328df4c3116 101 header = true;
ftagius 0:3328df4c3116 102 }
ftagius 0:3328df4c3116 103 // write to the current log file
ftagius 0:3328df4c3116 104 fprintf(fp,"%02d/%02d/20%02d,", gpsd.month, gpsd.day, gpsd.year);
ftagius 0:3328df4c3116 105 fprintf(fp,"%02d:%02d:%02d,", gpsd.hour, gpsd.minute, gpsd.seconds);
ftagius 0:3328df4c3116 106 fprintf(fp,"%7.0f,",gpsd.timef);
ftagius 0:3328df4c3116 107 fprintf(fp,"%5.7f,%5.7f,", gpsd.lat_deg, gpsd.lon_deg);
ftagius 1:10d2a051285e 108 fprintf(fp,"%d,",distance);
ftagius 1:10d2a051285e 109 fprintf(fp,"%d",interruptSource);
ftagius 0:3328df4c3116 110 fprintf(fp,"\r\n");
ftagius 0:3328df4c3116 111 fflush(fp);
ftagius 0:3328df4c3116 112 f_sync((FIL*)fp);
ftagius 0:3328df4c3116 113 fclose(fp);
ftagius 0:3328df4c3116 114 sd.unmount();
ftagius 1:10d2a051285e 115 pc.printf("Closed log file %s\r\n",logFilePath);
ftagius 0:3328df4c3116 116 }
ftagius 0:3328df4c3116 117
ftagius 0:3328df4c3116 118 int main()
ftagius 0:3328df4c3116 119 {
ftagius 0:3328df4c3116 120 bool debug=false;
ftagius 0:3328df4c3116 121 char c;
ftagius 0:3328df4c3116 122 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
ftagius 0:3328df4c3116 123 const int refresh_Time = 1000; //refresh time in ms
ftagius 0:3328df4c3116 124 TSISensor tsi; // touch slider
ftagius 0:3328df4c3116 125 unsigned long measFreq;
ftagius 1:10d2a051285e 126 rdistance=-1;
ftagius 1:10d2a051285e 127 rinterrupt=-1;
ftagius 0:3328df4c3116 128 pc.baud(9600);
ftagius 0:3328df4c3116 129 // initializations for gps
ftagius 0:3328df4c3116 130 green = 1;
ftagius 0:3328df4c3116 131 gpsd.setBaud(9600);
ftagius 0:3328df4c3116 132 gpsd.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
ftagius 0:3328df4c3116 133 gpsd.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
ftagius 0:3328df4c3116 134 gpsd.sendCommand(PGCMD_ANTENNA);
ftagius 0:3328df4c3116 135 gpsd.day=01;
ftagius 0:3328df4c3116 136 gpsd.month=01;
ftagius 0:3328df4c3116 137 gpsd.year=15;
ftagius 0:3328df4c3116 138 gpsd.hour=1;
ftagius 0:3328df4c3116 139 gpsd.minute=1;
ftagius 0:3328df4c3116 140 gpsd.seconds=1;
ftagius 0:3328df4c3116 141 red = 1;
ftagius 0:3328df4c3116 142 green = 1;
ftagius 0:3328df4c3116 143 pc.printf("Touch slider to start application\r\n");
ftagius 0:3328df4c3116 144 while(1) {
ftagius 0:3328df4c3116 145 green = 1; // turn led off
ftagius 0:3328df4c3116 146 wait_ms(200);
ftagius 0:3328df4c3116 147 if (tsi.readPercentage())
ftagius 0:3328df4c3116 148 break;
ftagius 0:3328df4c3116 149 green = 0; // turn led on
ftagius 0:3328df4c3116 150 wait_ms(200);
ftagius 0:3328df4c3116 151 if (tsi.readPercentage())
ftagius 0:3328df4c3116 152 break;
ftagius 0:3328df4c3116 153 }
ftagius 0:3328df4c3116 154
ftagius 0:3328df4c3116 155 pc.printf("\r\nstart lightning detector\r\n");
ftagius 0:3328df4c3116 156 //initializations for lightning detector
ftagius 0:3328df4c3116 157 ld.init();
ftagius 0:3328df4c3116 158 ld.clearStats();
ftagius 1:10d2a051285e 159
ftagius 0:3328df4c3116 160 measFreq = ld.tuneAntenna(IntLightning);
ftagius 1:10d2a051285e 161
ftagius 0:3328df4c3116 162 ld.powerUp();
ftagius 0:3328df4c3116 163 ld.calibrateRCOs(IntLightning);
ftagius 1:10d2a051285e 164 //ld.setIndoors();
ftagius 1:10d2a051285e 165 ld.setOutdoors();
ftagius 0:3328df4c3116 166 ld.setMinimumLightnings(0);
ftagius 0:3328df4c3116 167 ld.setSpikeRejection(2);
ftagius 0:3328df4c3116 168 ld.setNoiseFloor(2);
ftagius 0:3328df4c3116 169 //ld.disableDisturbers();
ftagius 0:3328df4c3116 170 ld.enableDisturbers();
ftagius 0:3328df4c3116 171 ld.setWatchdogThreshold(4);
ftagius 0:3328df4c3116 172 IntLightning.rise(&DetectLightning);
ftagius 0:3328df4c3116 173 int MinBlysk = ld.getMinimumLightnings();
ftagius 0:3328df4c3116 174 int Noise = ld.getNoiseFloor();
ftagius 1:10d2a051285e 175 // ld.setTuneCap(5); // 500kHz
ftagius 0:3328df4c3116 176 int TuneCap = ld.getTuneCap();
ftagius 0:3328df4c3116 177 int SpikeRej = ld.getSpikeRejection();
ftagius 0:3328df4c3116 178 int WatchDog = ld.getWatchdogThreshold();
ftagius 0:3328df4c3116 179
ftagius 0:3328df4c3116 180 pc.printf(" Min wylad: %i", MinBlysk);
ftagius 0:3328df4c3116 181 pc.printf("\r\n");
ftagius 0:3328df4c3116 182 pc.printf(" Gain: 0x%02x\r\n",ld.getGain());
ftagius 0:3328df4c3116 183 pc.printf(" Noise: %i", Noise);
ftagius 0:3328df4c3116 184 pc.printf("\r\n");
ftagius 0:3328df4c3116 185 pc.printf(" Tune CAP: %i", TuneCap);
ftagius 0:3328df4c3116 186 pc.printf("\r\n");
ftagius 0:3328df4c3116 187 pc.printf(" Spike rej: %i", SpikeRej);
ftagius 0:3328df4c3116 188 pc.printf("\r\n");
ftagius 0:3328df4c3116 189 pc.printf(" Watchdog: %i", WatchDog);
ftagius 0:3328df4c3116 190 pc.printf("\r\n");
ftagius 0:3328df4c3116 191 pc.printf(" LCO calibration: %ld Hz\n\r", measFreq);
ftagius 0:3328df4c3116 192
ftagius 0:3328df4c3116 193
ftagius 0:3328df4c3116 194 refresh_Timer.start(); //starts the clock on the timer
ftagius 0:3328df4c3116 195 //Mount the filesystem
ftagius 0:3328df4c3116 196 sd.mount();
ftagius 0:3328df4c3116 197 mkdir(directory, 0777);
ftagius 1:10d2a051285e 198 sd.unmount();
ftagius 1:10d2a051285e 199
ftagius 0:3328df4c3116 200 bool gpsFix=false;
ftagius 0:3328df4c3116 201 pc.printf("Starting GPS App\r\n");
ftagius 0:3328df4c3116 202 while (1)
ftagius 0:3328df4c3116 203 {
ftagius 0:3328df4c3116 204 c = gpsd.read(); //queries the GPS
ftagius 0:3328df4c3116 205 if (debug)
ftagius 0:3328df4c3116 206 {
ftagius 0:3328df4c3116 207 if (c) {
ftagius 0:3328df4c3116 208 printf("%c", c); //this line will echo the GPS data if not paused
ftagius 0:3328df4c3116 209 continue;
ftagius 0:3328df4c3116 210 }
ftagius 0:3328df4c3116 211 }
ftagius 0:3328df4c3116 212
ftagius 0:3328df4c3116 213 //check if we recieved a new message from GPS, if so, attempt to parse it,
ftagius 0:3328df4c3116 214 if ( gpsd.newNMEAreceived() ) {
ftagius 0:3328df4c3116 215 if ( !gpsd.parse(gpsd.lastNMEA()) ) {
ftagius 0:3328df4c3116 216 continue;
ftagius 0:3328df4c3116 217 }
ftagius 0:3328df4c3116 218 }
ftagius 0:3328df4c3116 219
ftagius 0:3328df4c3116 220 // update globals with the lastest gps time stamp
ftagius 0:3328df4c3116 221 day=gpsd.day;
ftagius 0:3328df4c3116 222 month=gpsd.month;
ftagius 0:3328df4c3116 223 year=gpsd.year;
ftagius 0:3328df4c3116 224 hour=gpsd.hour;
ftagius 0:3328df4c3116 225 minute=gpsd.minute;
ftagius 0:3328df4c3116 226 seconds=gpsd.seconds;
ftagius 0:3328df4c3116 227
ftagius 0:3328df4c3116 228 //check if enough time has passed to warrant printing GPS info to screen
ftagius 0:3328df4c3116 229 //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing
ftagius 0:3328df4c3116 230 if (refresh_Timer.read_ms() >= refresh_Time)
ftagius 0:3328df4c3116 231 {
ftagius 0:3328df4c3116 232 if (gpsd.fix) {
ftagius 0:3328df4c3116 233 // got a gps fix
ftagius 0:3328df4c3116 234 if (gpsFix == false)
ftagius 0:3328df4c3116 235 {
ftagius 0:3328df4c3116 236 // first time fix obtained
ftagius 0:3328df4c3116 237 gpsFix = true;
ftagius 0:3328df4c3116 238 pc.printf("GPS fix obtained on %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds);
ftagius 1:10d2a051285e 239 //pc.printf("Touch slider to suspend application\r\n");
ftagius 0:3328df4c3116 240 pc.printf("Waiting for lighting detection...\r\n");
ftagius 0:3328df4c3116 241 }
ftagius 0:3328df4c3116 242
ftagius 0:3328df4c3116 243 //red = 1; // turn led off
ftagius 0:3328df4c3116 244 //pc.printf("turn green on\r\n");
ftagius 0:3328df4c3116 245 green = 0; // turn led on
ftagius 0:3328df4c3116 246 wait_ms(50);
ftagius 0:3328df4c3116 247 }
ftagius 0:3328df4c3116 248 else
ftagius 0:3328df4c3116 249 {
ftagius 0:3328df4c3116 250 pc.printf("Waiting for GPS FIX\r\n");
ftagius 0:3328df4c3116 251 red = 0; // turn led on
ftagius 0:3328df4c3116 252 }
ftagius 0:3328df4c3116 253
ftagius 0:3328df4c3116 254 // restart the timer for the gps print loop
ftagius 0:3328df4c3116 255 // writeLogFile(-2);
ftagius 0:3328df4c3116 256 refresh_Timer.reset();
ftagius 0:3328df4c3116 257 }
ftagius 0:3328df4c3116 258 else
ftagius 0:3328df4c3116 259 {
ftagius 0:3328df4c3116 260 //red = 0; // turn led on
ftagius 0:3328df4c3116 261 //pc.printf("turn green off\r\n");
ftagius 0:3328df4c3116 262 green = 1; // turn green led off
ftagius 1:10d2a051285e 263 #if 0
ftagius 0:3328df4c3116 264 // check the slider. if touched, disable lighting interrupts and wait for another slider touch
ftagius 0:3328df4c3116 265 if (tsi.readPercentage())
ftagius 0:3328df4c3116 266 {
ftagius 1:10d2a051285e 267 //IntLightning.disable_irq();
ftagius 0:3328df4c3116 268 pc.printf("Touch slider to continue application\r\n");
ftagius 0:3328df4c3116 269 while (1)
ftagius 0:3328df4c3116 270 {
ftagius 0:3328df4c3116 271
ftagius 0:3328df4c3116 272 green = 1; // turn led off
ftagius 0:3328df4c3116 273 wait_ms(200);
ftagius 0:3328df4c3116 274 // break of of the loop if the slider is touched
ftagius 0:3328df4c3116 275 if (tsi.readPercentage())
ftagius 0:3328df4c3116 276 {
ftagius 0:3328df4c3116 277 green = 1;
ftagius 0:3328df4c3116 278 red = 1;
ftagius 0:3328df4c3116 279 pc.printf("Now monitoring for lightning strikes\r\n");
ftagius 1:10d2a051285e 280 //IntLightning.enable_irq();
ftagius 0:3328df4c3116 281 break;
ftagius 0:3328df4c3116 282 }
ftagius 0:3328df4c3116 283 green = 0; // turn led on
ftagius 0:3328df4c3116 284 wait_ms(200);
ftagius 0:3328df4c3116 285 // break of of the loop if the slider is touched
ftagius 0:3328df4c3116 286 if (tsi.readPercentage())
ftagius 0:3328df4c3116 287 {
ftagius 0:3328df4c3116 288 green = 1;
ftagius 0:3328df4c3116 289 red = 1;
ftagius 0:3328df4c3116 290 pc.printf("Now monitoring for lightning strikes\r\n");
ftagius 1:10d2a051285e 291 //IntLightning.enable_irq();
ftagius 0:3328df4c3116 292 break;
ftagius 0:3328df4c3116 293 }
ftagius 0:3328df4c3116 294
ftagius 0:3328df4c3116 295 } // end while 1 for slider
ftagius 0:3328df4c3116 296 } // end if tsi read
ftagius 1:10d2a051285e 297 #endif
ftagius 0:3328df4c3116 298 } // end else refresh timer
ftagius 0:3328df4c3116 299
ftagius 0:3328df4c3116 300 }
ftagius 0:3328df4c3116 301
ftagius 0:3328df4c3116 302
ftagius 0:3328df4c3116 303
ftagius 0:3328df4c3116 304 }