basic lightning detector with gps and sd card logging
Dependencies: AS3935 AdafruitGPS SDFileSystem TSI mbed ConfigFile
Diff: main.cpp
- Revision:
- 6:96b0dbe76357
- Parent:
- 5:1d4fd419cfb7
- Child:
- 8:f8830b6c6d9b
diff -r 1d4fd419cfb7 -r 96b0dbe76357 main.cpp --- a/main.cpp Wed Jun 24 18:18:43 2015 +0000 +++ b/main.cpp Tue Jun 30 16:21:39 2015 +0000 @@ -4,8 +4,10 @@ #include "TSISensor.h" #include "SDFileSystem.h" #include "AS3935.h" +#include "ConfigFile.h" +#include <string> -#define FW_VER 2 +#define FW_VER 3 // frdm-kl25z as3935 connections for spi1 // ------------------------------------------------ @@ -45,13 +47,35 @@ int rdistance, rinterrupt; char directory[]="/sd/lightning_data"; int OriginInt=-1; -int distance=-1; +int gDistance=-1; long energy=-1; +typedef struct +{ +unsigned int bOutdoorMode; +unsigned int noiseFloor; +unsigned int wdogThresh; +unsigned int bClrStat; +unsigned int minNumLight; +unsigned int spikeRej; +unsigned int maskDisturber; +}sys_cfg_t; + +// default configuration values +sys_cfg_t sysCfg = { .bOutdoorMode = 1, + .noiseFloor = 2, + .wdogThresh = 2, + .bClrStat = 0, + .minNumLight = 0, + .spikeRej = 2, + .maskDisturber = 0, + }; + + void DetectLightning() { OriginInt = ld.interruptSource(); - distance = ld.lightningDistanceKm(); + gDistance = ld.lightningDistanceKm(); energy = ld.getEnergy(); } @@ -108,7 +132,9 @@ break; case 8: pc.printf("Lightning detection, distance=%dkm energy=%ld\r\n", distance, energy); - ld.clearStats(); + + if (sysCfg.bClrStat) + ld.clearStats(); break; default: pc.printf("Unknown interrupt %d\r\n", OriginInt); @@ -164,9 +190,93 @@ } +bool readCfgFile(char *paDirectory, sys_cfg_t *paSysCfg) +{ + bool bRetVal = false; + sys_cfg_t lSysCfg; + typedef struct + { + const char *token; + unsigned int *value; + }token_map_t; + + token_map_t tokenMap[] = { {"outdoor_mode", &lSysCfg.bOutdoorMode}, + {"noise_floor", &lSysCfg.noiseFloor}, + {"watchdog_threshold", &lSysCfg.wdogThresh}, + {"clear_stat", &lSysCfg.bClrStat}, + {"minimum_num_light", &lSysCfg.minNumLight}, + {"spike_reject", &lSysCfg.spikeRej}, + {"mask_disturber", &lSysCfg.maskDisturber}, + }; + ConfigFile *plCfgFile; + string fileName; + int count = sizeof(tokenMap) / sizeof(token_map_t); + char szValue[8]; + + if ((NULL == paDirectory) || (NULL == paSysCfg)) + return false; + + // start with whatever configuration params are passed in, in case some params are not found in the file + lSysCfg = *paSysCfg; + + plCfgFile = new ConfigFile(); + fileName = paDirectory; + fileName += "/"; + fileName += "zeus.cfg"; + + sd.mount(); + printf ("\n\rReading configuration file[%s]\n\r", fileName.c_str()); + + // try to read values from the configuration file + if (plCfgFile->read((char *)fileName.c_str())) + { + for (int i = 0; i < count; ++i) + { + if (plCfgFile->getValue((char *)tokenMap[i].token, szValue, sizeof(szValue))) + { + if (1 == sscanf(szValue, "%d", tokenMap[i].value)) + { + printf ("Convert success %s[%d]\n\r", tokenMap[i].token, *(tokenMap[i].value)); + } + } + } + + // copy out the new found values + *paSysCfg = lSysCfg; + bRetVal = true; + } + else + { + printf ("Failed to read configuration file[%s]\n\r", fileName.c_str()); + + // try to create a default configuration file + for (int i = 0; i < count; ++i) + { + if (snprintf(szValue, sizeof(szValue), "%d", *(tokenMap[i].value))) + { + if (plCfgFile->setValue((char *)tokenMap[i].token, szValue)) + { + printf ("Token creation success %s[%s]\n\r", tokenMap[i].token, szValue); + } + } + } + if (plCfgFile->write((char *)fileName.c_str(), NULL, DOS)) + printf ("Success: Created default configuration file [%s]\n\r", fileName.c_str()); + else + printf ("Failed to create default configuration file [%s]\n\r", fileName.c_str()); + } + + if (plCfgFile) + delete plCfgFile; + + sd.unmount(); + return bRetVal; +} + int main() { + unsigned char regBuff[MAX_CONFIG_REGS]; char c; Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? @@ -201,6 +311,14 @@ if (tsi.readPercentage()) break; } + //Mount the filesystem + sd.mount(); + mkdir(directory, 0777); + sd.unmount(); + + // read configuration values fro SD file system to allow override of defaults in sysCfg + readCfgFile(directory, &sysCfg); + pc.printf("\r\nInitialize lightning detector\r\n"); //initializations for lightning detector @@ -215,21 +333,30 @@ measFreq = ld.tuneAntenna(IntLightning); ld.calibrateRCOs(IntLightning); - ld.setOutdoors(); - ld.setMinimumLightnings(0); - ld.setSpikeRejection(2); - ld.setNoiseFloor(2); + if (sysCfg.bOutdoorMode) + ld.setOutdoors(); + else + ld.setIndoors(); + + ld.setMinimumLightnings(sysCfg.minNumLight); + ld.setSpikeRejection(sysCfg.spikeRej); + ld.setNoiseFloor(sysCfg.noiseFloor); - ld.enableDisturbers(); - ld.setWatchdogThreshold(4); + if (sysCfg.maskDisturber) + ld.disableDisturbers(); + else + ld.enableDisturbers(); + + ld.setWatchdogThreshold(sysCfg.wdogThresh); IntLightning.rise(&DetectLightning); - int MinBlysk = ld.getMinimumLightnings(); - int Noise = ld.getNoiseFloor(); - int TuneCap = ld.getTuneCap(); - int SpikeRej = ld.getSpikeRejection(); - int WatchDog = ld.getWatchdogThreshold(); + + int MinLight = ld.getMinimumLightnings(); + int Noise = ld.getNoiseFloor(); + int TuneCap = ld.getTuneCap(); + int SpikeRej = ld.getSpikeRejection(); + int WatchDog = ld.getWatchdogThreshold(); - pc.printf("\r\n Min wylad: %i", MinBlysk); + pc.printf("\r\n Min lightning: %i", MinLight); pc.printf("\r\n"); pc.printf(" Gain: 0x%02x\r\n",ld.getGain()); pc.printf(" Noise: %i", Noise); @@ -242,16 +369,16 @@ 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); - sd.unmount(); // get a copy of all config registers ld.getConfigRegisters(regBuff, sizeof(regBuff)); + pc.printf("\n\rSystem Registers:\n\r"); + for (int i = 0; i < sizeof(regBuff); ++i) + pc.printf("REG%d [0x%02x]:\n\r", i, regBuff[i]); + pc.printf("\n\r"); + // write to the config file writeCfgFile(regBuff, sizeof(regBuff), FW_VER); @@ -262,10 +389,10 @@ { // the ld detector generated an interrupt, log the event IntLightning.disable_irq(); - writeLogFile(OriginInt,distance, energy); - ld.clearStats(); + writeLogFile(OriginInt,gDistance, energy); + //ld.clearStats(); OriginInt = -1; - distance = -1; + gDistance = -1; energy = -1; IntLightning.enable_irq(); }