![](/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:
- 0:3328df4c3116
- Child:
- 1:10d2a051285e
diff -r 000000000000 -r 3328df4c3116 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jun 19 11:29:59 2015 +0000 @@ -0,0 +1,296 @@ +#include "mbed.h" +#include "GPS.h" +#include "main.h" +#include "TSISensor.h" +#include "SDFileSystem.h" +#include "AS3935.h" + + // frdm-kl25z sd card connections for spi1 + // ------------------------------------------------ + // Header -- kl25z -- SD/MMC + // J2-20 -- PTE1 -- MOSI + // J9-13 -- PTE4 -- CS + // J2-14 -- GND -- Vss (GND) + // J9-9 -- PTE2 -- SCK + // J9-11 -- PTE3 -- MISO + +AS3935 ld(PTE1, PTE3, PTE2, PTE4, "ld", 1000000); // MOSI, MISO, SCK, CS, SPI bus freq (hz) +InterruptIn IntLightning(PTA12); //IRQ AS3935 + + + // frdm-kl25z sd card connections spi0 + // ------------------------------------------------ + // Header -- kl25z -- SPI + // J2-8 -- PTD2 -- MOSI + // J2-6 -- PTD0 -- CS + // J9-12 -- GND -- Vss (GND) + // J9-4 -- P3V3 -- Vdd (+3.3v) + // J2-12 -- PTD1 -- SCK + // J9-14 -- GND -- Vss (GND) + // J2-10 -- PTD3 -- MISO + +SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // MOSI, MISO, SCK, CS + +Serial pc(USBTX, USBRX); +GPS gpsd(PTE20, PTE21); +DigitalOut red(LED_RED); +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 DetectLightning() +{ + int OriginInt; + 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; + case 4: + pc.printf("Disturber\r\n"); + writeLogFile(-1); // use -1 for distance to distinguish disturbances from strikes + break; + case 8: + int distance = ld.lightningDistanceKm(); + pc.printf("Lightning detection, distance=%dkm\r\n", distance); + writeLogFile(distance); + ld.clearStats(); + break; + default: + pc.printf("unknown interrupt %d\r\n", OriginInt); + + } + +} + + void writeLogFile(int distance) +{ + char logFile[128]; + char directory[]="/sd/lightning_data"; + 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"); + + sd.mount(); + fp = fopen(logFile, "a"); + if(fp == NULL) { + // retry + wait_ms(500); + fp = fopen(logFile, "a"); + if (fp == NULL) + { + error("Could not open file %s for writing\r\n",logFile); + sd.unmount(); + return; + } + } + pc.printf("Opened log file %s\r\n",logFile); + // write the log file header + if (header == false) + { + fprintf(fp,"# date,time,raw timestamp,latitude,longitude,distance\r\n"); + header = true; + } + // write to the current log file + fprintf(fp,"%02d/%02d/20%02d,", gpsd.month, gpsd.day, gpsd.year); + 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,"\r\n"); + fflush(fp); + f_sync((FIL*)fp); + fclose(fp); + sd.unmount(); +} + +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; + pc.baud(9600); + // initializations for gps + green = 1; + gpsd.setBaud(9600); + gpsd.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); + gpsd.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); + gpsd.sendCommand(PGCMD_ANTENNA); + gpsd.day=01; + gpsd.month=01; + gpsd.year=15; + gpsd.hour=1; + gpsd.minute=1; + gpsd.seconds=1; + red = 1; + green = 1; + pc.printf("Touch slider to start application\r\n"); + while(1) { + green = 1; // turn led off + wait_ms(200); + if (tsi.readPercentage()) + break; + green = 0; // turn led on + wait_ms(200); + if (tsi.readPercentage()) + break; + } + + 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.setMinimumLightnings(0); + ld.setSpikeRejection(2); + ld.setNoiseFloor(2); + //ld.disableDisturbers(); + ld.enableDisturbers(); + ld.setWatchdogThreshold(4); + IntLightning.rise(&DetectLightning); + int MinBlysk = ld.getMinimumLightnings(); + int Noise = ld.getNoiseFloor(); + int TuneCap = ld.getTuneCap(); + int SpikeRej = ld.getSpikeRejection(); + int WatchDog = ld.getWatchdogThreshold(); + + pc.printf(" Min wylad: %i", MinBlysk); + pc.printf("\r\n"); + pc.printf(" Gain: 0x%02x\r\n",ld.getGain()); + pc.printf(" Noise: %i", Noise); + pc.printf("\r\n"); + pc.printf(" Tune CAP: %i", TuneCap); + pc.printf("\r\n"); + pc.printf(" Spike rej: %i", SpikeRej); + pc.printf("\r\n"); + pc.printf(" Watchdog: %i", WatchDog); + pc.printf("\r\n"); + pc.printf(" LCO calibration: %ld Hz\n\r", measFreq); + + + refresh_Timer.start(); //starts the clock on the timer + //Mount the filesystem + sd.mount(); + mkdir(directory, 0777); + bool gpsFix=false; + pc.printf("Starting GPS App\r\n"); + while (1) + { + c = gpsd.read(); //queries the GPS + if (debug) + { + if (c) { + printf("%c", c); //this line will echo the GPS data if not paused + continue; + } + } + + //check if we recieved a new message from GPS, if so, attempt to parse it, + if ( gpsd.newNMEAreceived() ) { + if ( !gpsd.parse(gpsd.lastNMEA()) ) { + continue; + } + } + + // update globals with the lastest gps time stamp + day=gpsd.day; + month=gpsd.month; + year=gpsd.year; + hour=gpsd.hour; + minute=gpsd.minute; + seconds=gpsd.seconds; + + //check if enough time has passed to warrant printing GPS info to screen + //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing + if (refresh_Timer.read_ms() >= refresh_Time) + { + if (gpsd.fix) { + // got a gps fix + if (gpsFix == false) + { + // 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("Waiting for lighting detection...\r\n"); + } + + //red = 1; // turn led off + //pc.printf("turn green on\r\n"); + green = 0; // turn led on + wait_ms(50); + } + else + { + pc.printf("Waiting for GPS FIX\r\n"); + red = 0; // turn led on + } + + // restart the timer for the gps print loop + // writeLogFile(-2); + refresh_Timer.reset(); + } + else + { + //red = 0; // turn led on + //pc.printf("turn green off\r\n"); + green = 1; // turn green led off + // check the slider. if touched, disable lighting interrupts and wait for another slider touch + if (tsi.readPercentage()) + { + IntLightning.disable_irq(); + pc.printf("Touch slider to continue application\r\n"); + while (1) + { + + green = 1; // turn led off + wait_ms(200); + // break of of the loop if the slider is touched + if (tsi.readPercentage()) + { + green = 1; + red = 1; + pc.printf("Now monitoring for lightning strikes\r\n"); + IntLightning.enable_irq(); + break; + } + green = 0; // turn led on + wait_ms(200); + // break of of the loop if the slider is touched + if (tsi.readPercentage()) + { + green = 1; + red = 1; + pc.printf("Now monitoring for lightning strikes\r\n"); + IntLightning.enable_irq(); + break; + } + + } // end while 1 for slider + } // end if tsi read + } // end else refresh timer + + } + + + +}