![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
basic lightning detector with gps and sd card logging
Dependencies: AS3935 AdafruitGPS SDFileSystem TSI mbed ConfigFile
Diff: main.cpp
- Revision:
- 1:10d2a051285e
- Parent:
- 0:3328df4c3116
- Child:
- 2:3edb129c60b2
--- a/main.cpp Fri Jun 19 11:29:59 2015 +0000 +++ b/main.cpp Mon Jun 22 20:39:32 2015 +0000 @@ -37,67 +37,67 @@ DigitalOut green(LED_GREEN); //DigitalOut blue(LED_BLUE); don't use the blue led, due to a board error, writing to the blue led kills spi bool debug; -//bool gpsEnabled = true; int day, month,year,hour,minute,seconds; -void writeLogFile(int distance); -//char logFile[]="lightning_data.csv"; +void writeLogFile(int interruptSource, int distance); +char logName[]="lightning_data.csv"; +int rdistance, rinterrupt; +char directory[]="/sd/lightning_data"; void DetectLightning() { int OriginInt; + int distance=-1; wait_ms(2); // OriginInt = ld.interruptSource(); pc.printf("%02d/%02d/20%02d_%02d:%02d:%02d ",month,day,year,hour,minute,seconds); switch (OriginInt) { case 1: - pc.printf("Noise level too high\r\n"); - break; + pc.printf("Noise level too high\r\n"); + break; case 4: - pc.printf("Disturber\r\n"); - writeLogFile(-1); // use -1 for distance to distinguish disturbances from strikes - break; + pc.printf("Disturber\r\n"); + break; case 8: - int distance = ld.lightningDistanceKm(); + distance = ld.lightningDistanceKm(); pc.printf("Lightning detection, distance=%dkm\r\n", distance); - writeLogFile(distance); ld.clearStats(); - break; + break; default: - pc.printf("unknown interrupt %d\r\n", OriginInt); + pc.printf("Unknown interrupt %d\r\n", OriginInt); } + writeLogFile(OriginInt,distance); + pc.printf("Resume wait for lightning detection...\r\n"); } - void writeLogFile(int distance) + void writeLogFile(int interruptSource, int distance) { - char logFile[128]; - char directory[]="/sd/lightning_data"; + char logFilePath[128]; static bool header=false; FILE *fp; - // create a log file name with the current date/time stamp - //sprintf(logFile, "%s%s%02d_%02d_20%02d_%02d_%02d_%02d.csv", directory,"/gps_",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds); - sprintf(logFile, "%s%s", directory,"/lightning_data.csv"); - + + sprintf(logFilePath, "%s/%s", directory,logName); sd.mount(); - fp = fopen(logFile, "a"); + fp = fopen(logFilePath, "a"); if(fp == NULL) { // retry wait_ms(500); - fp = fopen(logFile, "a"); + fp = fopen(logFilePath, "a"); if (fp == NULL) { - error("Could not open file %s for writing\r\n",logFile); + printf("Could not open file %s for writing\r\n",logFilePath); sd.unmount(); + printf("unmount sd card \r\n"); return; } } - pc.printf("Opened log file %s\r\n",logFile); + pc.printf("Opened log file %s\r\n",logFilePath); // write the log file header if (header == false) { - fprintf(fp,"# date,time,raw timestamp,latitude,longitude,distance\r\n"); + fprintf(fp,"# date,time,raw timestamp,latitude,longitude,distance,interrupt\r\n"); header = true; } // write to the current log file @@ -105,23 +105,26 @@ fprintf(fp,"%02d:%02d:%02d,", gpsd.hour, gpsd.minute, gpsd.seconds); fprintf(fp,"%7.0f,",gpsd.timef); fprintf(fp,"%5.7f,%5.7f,", gpsd.lat_deg, gpsd.lon_deg); - fprintf(fp,"%d",distance); + fprintf(fp,"%d,",distance); + fprintf(fp,"%d",interruptSource); fprintf(fp,"\r\n"); fflush(fp); f_sync((FIL*)fp); fclose(fp); sd.unmount(); + pc.printf("Closed log file %s\r\n",logFilePath); } int main() { bool debug=false; - char directory[]="/sd/lightning_data"; char c; Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? const int refresh_Time = 1000; //refresh time in ms TSISensor tsi; // touch slider unsigned long measFreq; + rdistance=-1; + rinterrupt=-1; pc.baud(9600); // initializations for gps green = 1; @@ -151,15 +154,15 @@ pc.printf("\r\nstart lightning detector\r\n"); //initializations for lightning detector - //initializations ld.init(); ld.clearStats(); - //ld.setTuneCap(5); // 500kHz + measFreq = ld.tuneAntenna(IntLightning); + ld.powerUp(); ld.calibrateRCOs(IntLightning); - ld.setIndoors(); - //ld.setOutdoors(); + //ld.setIndoors(); + ld.setOutdoors(); ld.setMinimumLightnings(0); ld.setSpikeRejection(2); ld.setNoiseFloor(2); @@ -169,6 +172,7 @@ IntLightning.rise(&DetectLightning); int MinBlysk = ld.getMinimumLightnings(); int Noise = ld.getNoiseFloor(); + // ld.setTuneCap(5); // 500kHz int TuneCap = ld.getTuneCap(); int SpikeRej = ld.getSpikeRejection(); int WatchDog = ld.getWatchdogThreshold(); @@ -191,6 +195,8 @@ //Mount the filesystem sd.mount(); mkdir(directory, 0777); + sd.unmount(); + bool gpsFix=false; pc.printf("Starting GPS App\r\n"); while (1) @@ -230,7 +236,7 @@ // first time fix obtained gpsFix = true; 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); - pc.printf("Touch slider to suspend application\r\n"); + //pc.printf("Touch slider to suspend application\r\n"); pc.printf("Waiting for lighting detection...\r\n"); } @@ -254,10 +260,11 @@ //red = 0; // turn led on //pc.printf("turn green off\r\n"); green = 1; // turn green led off + #if 0 // check the slider. if touched, disable lighting interrupts and wait for another slider touch if (tsi.readPercentage()) { - IntLightning.disable_irq(); + //IntLightning.disable_irq(); pc.printf("Touch slider to continue application\r\n"); while (1) { @@ -270,7 +277,7 @@ green = 1; red = 1; pc.printf("Now monitoring for lightning strikes\r\n"); - IntLightning.enable_irq(); + //IntLightning.enable_irq(); break; } green = 0; // turn led on @@ -281,12 +288,13 @@ green = 1; red = 1; pc.printf("Now monitoring for lightning strikes\r\n"); - IntLightning.enable_irq(); + //IntLightning.enable_irq(); break; } } // end while 1 for slider } // end if tsi read + #endif } // end else refresh timer }