![](/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
main.cpp@11:a4096e656762, 2015-07-01 (annotated)
- Committer:
- cmkachur
- Date:
- Wed Jul 01 19:29:54 2015 +0000
- Revision:
- 11:a4096e656762
- Parent:
- 9:15c9bf86d908
- Child:
- 12:7c58c7a6b25c
Add in the 2 ms wait to the AS3935 ISR per the data sheet. Update to FW version 4.
Who changed what in which revision?
User | Revision | Line number | New 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" |
cmkachur | 6:96b0dbe76357 | 7 | #include "ConfigFile.h" |
ftagius | 8:f8830b6c6d9b | 8 | #include "datetime.h" |
ftagius | 8:f8830b6c6d9b | 9 | #include "time.h" |
cmkachur | 6:96b0dbe76357 | 10 | #include <string> |
ftagius | 0:3328df4c3116 | 11 | |
cmkachur | 11:a4096e656762 | 12 | #define FW_VER 4 |
cmkachur | 4:4d26ba1ae0f7 | 13 | |
ftagius | 3:e3974328d808 | 14 | // frdm-kl25z as3935 connections for spi1 |
ftagius | 0:3328df4c3116 | 15 | // ------------------------------------------------ |
ftagius | 0:3328df4c3116 | 16 | // Header -- kl25z -- SD/MMC |
ftagius | 0:3328df4c3116 | 17 | // J2-20 -- PTE1 -- MOSI |
ftagius | 0:3328df4c3116 | 18 | // J9-13 -- PTE4 -- CS |
ftagius | 0:3328df4c3116 | 19 | // J2-14 -- GND -- Vss (GND) |
ftagius | 0:3328df4c3116 | 20 | // J9-9 -- PTE2 -- SCK |
ftagius | 0:3328df4c3116 | 21 | // J9-11 -- PTE3 -- MISO |
ftagius | 0:3328df4c3116 | 22 | |
ftagius | 0:3328df4c3116 | 23 | AS3935 ld(PTE1, PTE3, PTE2, PTE4, "ld", 1000000); // MOSI, MISO, SCK, CS, SPI bus freq (hz) |
ftagius | 0:3328df4c3116 | 24 | InterruptIn IntLightning(PTA12); //IRQ AS3935 |
ftagius | 0:3328df4c3116 | 25 | |
ftagius | 0:3328df4c3116 | 26 | |
ftagius | 0:3328df4c3116 | 27 | // frdm-kl25z sd card connections spi0 |
ftagius | 0:3328df4c3116 | 28 | // ------------------------------------------------ |
ftagius | 0:3328df4c3116 | 29 | // Header -- kl25z -- SPI |
ftagius | 0:3328df4c3116 | 30 | // J2-8 -- PTD2 -- MOSI |
ftagius | 0:3328df4c3116 | 31 | // J2-6 -- PTD0 -- CS |
ftagius | 0:3328df4c3116 | 32 | // J9-12 -- GND -- Vss (GND) |
ftagius | 0:3328df4c3116 | 33 | // J9-4 -- P3V3 -- Vdd (+3.3v) |
ftagius | 0:3328df4c3116 | 34 | // J2-12 -- PTD1 -- SCK |
ftagius | 0:3328df4c3116 | 35 | // J9-14 -- GND -- Vss (GND) |
ftagius | 0:3328df4c3116 | 36 | // J2-10 -- PTD3 -- MISO |
ftagius | 0:3328df4c3116 | 37 | |
ftagius | 0:3328df4c3116 | 38 | SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // MOSI, MISO, SCK, CS |
ftagius | 0:3328df4c3116 | 39 | |
ftagius | 0:3328df4c3116 | 40 | Serial pc(USBTX, USBRX); |
ftagius | 0:3328df4c3116 | 41 | GPS gpsd(PTE20, PTE21); |
ftagius | 0:3328df4c3116 | 42 | DigitalOut red(LED_RED); |
ftagius | 0:3328df4c3116 | 43 | DigitalOut green(LED_GREEN); |
ftagius | 0:3328df4c3116 | 44 | //DigitalOut blue(LED_BLUE); don't use the blue led, due to a board error, writing to the blue led kills spi |
ftagius | 3:e3974328d808 | 45 | bool debug=false; |
cmkachur | 2:3edb129c60b2 | 46 | void writeLogFile(int interruptSource, int distance, long energy); |
ftagius | 1:10d2a051285e | 47 | char logName[]="lightning_data.csv"; |
ftagius | 1:10d2a051285e | 48 | int rdistance, rinterrupt; |
ftagius | 1:10d2a051285e | 49 | char directory[]="/sd/lightning_data"; |
ftagius | 3:e3974328d808 | 50 | int OriginInt=-1; |
cmkachur | 6:96b0dbe76357 | 51 | int gDistance=-1; |
ftagius | 3:e3974328d808 | 52 | long energy=-1; |
ftagius | 8:f8830b6c6d9b | 53 | struct tm Clock; |
ftagius | 0:3328df4c3116 | 54 | |
cmkachur | 6:96b0dbe76357 | 55 | typedef struct |
cmkachur | 6:96b0dbe76357 | 56 | { |
cmkachur | 6:96b0dbe76357 | 57 | unsigned int bOutdoorMode; |
cmkachur | 6:96b0dbe76357 | 58 | unsigned int noiseFloor; |
cmkachur | 6:96b0dbe76357 | 59 | unsigned int wdogThresh; |
cmkachur | 6:96b0dbe76357 | 60 | unsigned int bClrStat; |
cmkachur | 6:96b0dbe76357 | 61 | unsigned int minNumLight; |
cmkachur | 6:96b0dbe76357 | 62 | unsigned int spikeRej; |
cmkachur | 6:96b0dbe76357 | 63 | unsigned int maskDisturber; |
cmkachur | 6:96b0dbe76357 | 64 | }sys_cfg_t; |
cmkachur | 6:96b0dbe76357 | 65 | |
cmkachur | 6:96b0dbe76357 | 66 | // default configuration values |
cmkachur | 6:96b0dbe76357 | 67 | sys_cfg_t sysCfg = { .bOutdoorMode = 1, |
cmkachur | 6:96b0dbe76357 | 68 | .noiseFloor = 2, |
cmkachur | 6:96b0dbe76357 | 69 | .wdogThresh = 2, |
cmkachur | 6:96b0dbe76357 | 70 | .bClrStat = 0, |
cmkachur | 6:96b0dbe76357 | 71 | .minNumLight = 0, |
cmkachur | 6:96b0dbe76357 | 72 | .spikeRej = 2, |
cmkachur | 6:96b0dbe76357 | 73 | .maskDisturber = 0, |
cmkachur | 6:96b0dbe76357 | 74 | }; |
cmkachur | 6:96b0dbe76357 | 75 | |
cmkachur | 6:96b0dbe76357 | 76 | |
ftagius | 0:3328df4c3116 | 77 | void DetectLightning() |
ftagius | 0:3328df4c3116 | 78 | { |
cmkachur | 11:a4096e656762 | 79 | /* After the signal IRQ goes high the external |
cmkachur | 11:a4096e656762 | 80 | unit should wait 2ms before reading the interrupt register. */ |
cmkachur | 11:a4096e656762 | 81 | wait_ms(2); |
ftagius | 0:3328df4c3116 | 82 | OriginInt = ld.interruptSource(); |
cmkachur | 6:96b0dbe76357 | 83 | gDistance = ld.lightningDistanceKm(); |
ftagius | 3:e3974328d808 | 84 | energy = ld.getEnergy(); |
ftagius | 0:3328df4c3116 | 85 | } |
ftagius | 0:3328df4c3116 | 86 | |
cmkachur | 2:3edb129c60b2 | 87 | void writeLogFile(int interruptSource, int distance, long energy) |
ftagius | 0:3328df4c3116 | 88 | { |
ftagius | 1:10d2a051285e | 89 | char logFilePath[128]; |
ftagius | 0:3328df4c3116 | 90 | static bool header=false; |
ftagius | 0:3328df4c3116 | 91 | FILE *fp; |
ftagius | 1:10d2a051285e | 92 | |
ftagius | 1:10d2a051285e | 93 | sprintf(logFilePath, "%s/%s", directory,logName); |
ftagius | 0:3328df4c3116 | 94 | sd.mount(); |
ftagius | 1:10d2a051285e | 95 | fp = fopen(logFilePath, "a"); |
ftagius | 0:3328df4c3116 | 96 | if(fp == NULL) { |
ftagius | 0:3328df4c3116 | 97 | // retry |
ftagius | 3:e3974328d808 | 98 | wait_ms(200); |
ftagius | 1:10d2a051285e | 99 | fp = fopen(logFilePath, "a"); |
ftagius | 0:3328df4c3116 | 100 | if (fp == NULL) |
ftagius | 0:3328df4c3116 | 101 | { |
ftagius | 1:10d2a051285e | 102 | printf("Could not open file %s for writing\r\n",logFilePath); |
ftagius | 0:3328df4c3116 | 103 | sd.unmount(); |
ftagius | 1:10d2a051285e | 104 | printf("unmount sd card \r\n"); |
ftagius | 0:3328df4c3116 | 105 | return; |
ftagius | 0:3328df4c3116 | 106 | } |
ftagius | 0:3328df4c3116 | 107 | } |
ftagius | 3:e3974328d808 | 108 | if (debug) |
ftagius | 3:e3974328d808 | 109 | pc.printf("Opened log file %s\r\n",logFilePath); |
ftagius | 0:3328df4c3116 | 110 | // write the log file header |
ftagius | 0:3328df4c3116 | 111 | if (header == false) |
ftagius | 0:3328df4c3116 | 112 | { |
cmkachur | 2:3edb129c60b2 | 113 | fprintf(fp,"# date,time,raw timestamp,latitude,longitude,distance,interrupt,energy\r\n"); |
ftagius | 0:3328df4c3116 | 114 | header = true; |
ftagius | 0:3328df4c3116 | 115 | } |
ftagius | 0:3328df4c3116 | 116 | // write to the current log file |
ftagius | 8:f8830b6c6d9b | 117 | char time_str[32]; |
ftagius | 8:f8830b6c6d9b | 118 | time_t seconds = time(NULL); |
cmkachur | 11:a4096e656762 | 119 | seconds -= 14400; // 14400 = 60*60*4, kludgy way of setting est |
ftagius | 8:f8830b6c6d9b | 120 | struct tm *tminfo = localtime(&seconds); |
ftagius | 8:f8830b6c6d9b | 121 | strftime(time_str, 32, "%m/%d/%Y,%T", tminfo); |
ftagius | 8:f8830b6c6d9b | 122 | fprintf(fp,"%s,%ld,",time_str,seconds); |
ftagius | 0:3328df4c3116 | 123 | fprintf(fp,"%5.7f,%5.7f,", gpsd.lat_deg, gpsd.lon_deg); |
ftagius | 1:10d2a051285e | 124 | fprintf(fp,"%d,",distance); |
cmkachur | 2:3edb129c60b2 | 125 | fprintf(fp,"%d,",interruptSource); |
cmkachur | 2:3edb129c60b2 | 126 | fprintf(fp,"%ld",energy); |
ftagius | 0:3328df4c3116 | 127 | fprintf(fp,"\r\n"); |
ftagius | 0:3328df4c3116 | 128 | fflush(fp); |
ftagius | 0:3328df4c3116 | 129 | f_sync((FIL*)fp); |
ftagius | 0:3328df4c3116 | 130 | fclose(fp); |
ftagius | 0:3328df4c3116 | 131 | sd.unmount(); |
ftagius | 3:e3974328d808 | 132 | pc.printf("Event: "); |
ftagius | 8:f8830b6c6d9b | 133 | pc.printf(" %s - ", time_str); |
ftagius | 3:e3974328d808 | 134 | switch (interruptSource) |
ftagius | 3:e3974328d808 | 135 | { |
ftagius | 3:e3974328d808 | 136 | case 1: |
ftagius | 3:e3974328d808 | 137 | pc.printf("Noise level too high\r\n"); |
ftagius | 3:e3974328d808 | 138 | break; |
ftagius | 3:e3974328d808 | 139 | case 4: |
ftagius | 3:e3974328d808 | 140 | pc.printf("Disturber\r\n"); |
ftagius | 3:e3974328d808 | 141 | break; |
ftagius | 3:e3974328d808 | 142 | case 8: |
ftagius | 3:e3974328d808 | 143 | pc.printf("Lightning detection, distance=%dkm energy=%ld\r\n", distance, energy); |
cmkachur | 6:96b0dbe76357 | 144 | |
cmkachur | 6:96b0dbe76357 | 145 | if (sysCfg.bClrStat) |
cmkachur | 6:96b0dbe76357 | 146 | ld.clearStats(); |
ftagius | 3:e3974328d808 | 147 | break; |
ftagius | 3:e3974328d808 | 148 | default: |
ftagius | 3:e3974328d808 | 149 | pc.printf("Unknown interrupt %d\r\n", OriginInt); |
ftagius | 3:e3974328d808 | 150 | |
ftagius | 3:e3974328d808 | 151 | } |
ftagius | 3:e3974328d808 | 152 | |
ftagius | 3:e3974328d808 | 153 | if (debug) |
ftagius | 3:e3974328d808 | 154 | pc.printf("Closed log file %s\r\n",logFilePath); |
ftagius | 0:3328df4c3116 | 155 | } |
ftagius | 0:3328df4c3116 | 156 | |
cmkachur | 4:4d26ba1ae0f7 | 157 | |
cmkachur | 4:4d26ba1ae0f7 | 158 | void writeCfgFile(unsigned char *pBuff, unsigned char buffLen, unsigned char fwVer) |
cmkachur | 4:4d26ba1ae0f7 | 159 | { |
cmkachur | 4:4d26ba1ae0f7 | 160 | char cfgFilePath[128]; |
cmkachur | 4:4d26ba1ae0f7 | 161 | FILE *fp; |
cmkachur | 4:4d26ba1ae0f7 | 162 | unsigned char cnt = 0; |
cmkachur | 4:4d26ba1ae0f7 | 163 | |
cmkachur | 4:4d26ba1ae0f7 | 164 | sprintf(cfgFilePath, "%s/%s", directory, "config_data.csv"); |
cmkachur | 4:4d26ba1ae0f7 | 165 | sd.mount(); |
cmkachur | 4:4d26ba1ae0f7 | 166 | fp = fopen(cfgFilePath, "w"); |
cmkachur | 4:4d26ba1ae0f7 | 167 | if(fp == NULL) { |
cmkachur | 4:4d26ba1ae0f7 | 168 | // retry |
cmkachur | 4:4d26ba1ae0f7 | 169 | wait_ms(200); |
cmkachur | 4:4d26ba1ae0f7 | 170 | fp = fopen(cfgFilePath, "w"); |
cmkachur | 4:4d26ba1ae0f7 | 171 | if (fp == NULL) |
cmkachur | 4:4d26ba1ae0f7 | 172 | { |
cmkachur | 4:4d26ba1ae0f7 | 173 | printf("Could not open file %s for writing\r\n",cfgFilePath); |
cmkachur | 4:4d26ba1ae0f7 | 174 | sd.unmount(); |
cmkachur | 4:4d26ba1ae0f7 | 175 | printf("unmount sd card \r\n"); |
cmkachur | 4:4d26ba1ae0f7 | 176 | return; |
cmkachur | 4:4d26ba1ae0f7 | 177 | } |
cmkachur | 4:4d26ba1ae0f7 | 178 | } |
cmkachur | 4:4d26ba1ae0f7 | 179 | if (debug) |
cmkachur | 4:4d26ba1ae0f7 | 180 | pc.printf("Opened log file %s\r\n",cfgFilePath); |
cmkachur | 4:4d26ba1ae0f7 | 181 | |
cmkachur | 4:4d26ba1ae0f7 | 182 | // write the header |
cmkachur | 4:4d26ba1ae0f7 | 183 | fprintf(fp,"# FW_VER,REG0,REG1,REG2,REG3,REG4,REG5,REG6,REG7,REG8\r\n"); |
cmkachur | 4:4d26ba1ae0f7 | 184 | |
cmkachur | 4:4d26ba1ae0f7 | 185 | // write the firmware version |
cmkachur | 4:4d26ba1ae0f7 | 186 | fprintf(fp,"%d,", fwVer); |
cmkachur | 4:4d26ba1ae0f7 | 187 | |
cmkachur | 4:4d26ba1ae0f7 | 188 | // write all the configuration registers |
cmkachur | 4:4d26ba1ae0f7 | 189 | for (cnt = 0; cnt < buffLen && cnt < MAX_CONFIG_REGS; ++cnt) |
cmkachur | 4:4d26ba1ae0f7 | 190 | fprintf(fp,"0x%x,", pBuff[cnt]); |
cmkachur | 4:4d26ba1ae0f7 | 191 | |
cmkachur | 4:4d26ba1ae0f7 | 192 | fflush(fp); |
cmkachur | 4:4d26ba1ae0f7 | 193 | f_sync((FIL*)fp); |
cmkachur | 4:4d26ba1ae0f7 | 194 | fclose(fp); |
cmkachur | 4:4d26ba1ae0f7 | 195 | sd.unmount(); |
cmkachur | 4:4d26ba1ae0f7 | 196 | |
cmkachur | 4:4d26ba1ae0f7 | 197 | if (debug) |
cmkachur | 4:4d26ba1ae0f7 | 198 | pc.printf("Closed cfg file %s\r\n",cfgFilePath); |
cmkachur | 4:4d26ba1ae0f7 | 199 | } |
cmkachur | 4:4d26ba1ae0f7 | 200 | |
cmkachur | 4:4d26ba1ae0f7 | 201 | |
cmkachur | 6:96b0dbe76357 | 202 | bool readCfgFile(char *paDirectory, sys_cfg_t *paSysCfg) |
cmkachur | 6:96b0dbe76357 | 203 | { |
cmkachur | 6:96b0dbe76357 | 204 | bool bRetVal = false; |
ftagius | 8:f8830b6c6d9b | 205 | FILE *fp; |
cmkachur | 6:96b0dbe76357 | 206 | sys_cfg_t lSysCfg; |
cmkachur | 6:96b0dbe76357 | 207 | typedef struct |
cmkachur | 6:96b0dbe76357 | 208 | { |
cmkachur | 6:96b0dbe76357 | 209 | const char *token; |
cmkachur | 6:96b0dbe76357 | 210 | unsigned int *value; |
cmkachur | 6:96b0dbe76357 | 211 | }token_map_t; |
cmkachur | 6:96b0dbe76357 | 212 | |
cmkachur | 6:96b0dbe76357 | 213 | token_map_t tokenMap[] = { {"outdoor_mode", &lSysCfg.bOutdoorMode}, |
cmkachur | 6:96b0dbe76357 | 214 | {"noise_floor", &lSysCfg.noiseFloor}, |
cmkachur | 6:96b0dbe76357 | 215 | {"watchdog_threshold", &lSysCfg.wdogThresh}, |
cmkachur | 6:96b0dbe76357 | 216 | {"clear_stat", &lSysCfg.bClrStat}, |
cmkachur | 6:96b0dbe76357 | 217 | {"minimum_num_light", &lSysCfg.minNumLight}, |
cmkachur | 6:96b0dbe76357 | 218 | {"spike_reject", &lSysCfg.spikeRej}, |
cmkachur | 6:96b0dbe76357 | 219 | {"mask_disturber", &lSysCfg.maskDisturber}, |
cmkachur | 6:96b0dbe76357 | 220 | }; |
cmkachur | 6:96b0dbe76357 | 221 | ConfigFile *plCfgFile; |
cmkachur | 6:96b0dbe76357 | 222 | string fileName; |
cmkachur | 6:96b0dbe76357 | 223 | int count = sizeof(tokenMap) / sizeof(token_map_t); |
cmkachur | 6:96b0dbe76357 | 224 | char szValue[8]; |
cmkachur | 6:96b0dbe76357 | 225 | |
cmkachur | 6:96b0dbe76357 | 226 | if ((NULL == paDirectory) || (NULL == paSysCfg)) |
cmkachur | 6:96b0dbe76357 | 227 | return false; |
cmkachur | 6:96b0dbe76357 | 228 | |
cmkachur | 6:96b0dbe76357 | 229 | // start with whatever configuration params are passed in, in case some params are not found in the file |
cmkachur | 6:96b0dbe76357 | 230 | lSysCfg = *paSysCfg; |
cmkachur | 6:96b0dbe76357 | 231 | |
cmkachur | 6:96b0dbe76357 | 232 | plCfgFile = new ConfigFile(); |
cmkachur | 6:96b0dbe76357 | 233 | fileName = paDirectory; |
cmkachur | 6:96b0dbe76357 | 234 | fileName += "/"; |
cmkachur | 6:96b0dbe76357 | 235 | fileName += "zeus.cfg"; |
ftagius | 8:f8830b6c6d9b | 236 | |
cmkachur | 6:96b0dbe76357 | 237 | sd.mount(); |
ftagius | 8:f8830b6c6d9b | 238 | fp = fopen(fileName.c_str(), "r"); |
ftagius | 9:15c9bf86d908 | 239 | printf ("\n\rReading configuration file [%s]\n\r", fileName.c_str()); |
cmkachur | 6:96b0dbe76357 | 240 | |
cmkachur | 6:96b0dbe76357 | 241 | // try to read values from the configuration file |
cmkachur | 6:96b0dbe76357 | 242 | if (plCfgFile->read((char *)fileName.c_str())) |
cmkachur | 6:96b0dbe76357 | 243 | { |
cmkachur | 6:96b0dbe76357 | 244 | for (int i = 0; i < count; ++i) |
cmkachur | 6:96b0dbe76357 | 245 | { |
cmkachur | 6:96b0dbe76357 | 246 | if (plCfgFile->getValue((char *)tokenMap[i].token, szValue, sizeof(szValue))) |
cmkachur | 6:96b0dbe76357 | 247 | { |
cmkachur | 6:96b0dbe76357 | 248 | if (1 == sscanf(szValue, "%d", tokenMap[i].value)) |
cmkachur | 6:96b0dbe76357 | 249 | { |
cmkachur | 6:96b0dbe76357 | 250 | printf ("Convert success %s[%d]\n\r", tokenMap[i].token, *(tokenMap[i].value)); |
cmkachur | 6:96b0dbe76357 | 251 | } |
cmkachur | 6:96b0dbe76357 | 252 | } |
cmkachur | 6:96b0dbe76357 | 253 | } |
ftagius | 9:15c9bf86d908 | 254 | |
cmkachur | 6:96b0dbe76357 | 255 | // copy out the new found values |
cmkachur | 6:96b0dbe76357 | 256 | *paSysCfg = lSysCfg; |
cmkachur | 6:96b0dbe76357 | 257 | bRetVal = true; |
cmkachur | 6:96b0dbe76357 | 258 | } |
cmkachur | 6:96b0dbe76357 | 259 | else |
cmkachur | 6:96b0dbe76357 | 260 | { |
cmkachur | 6:96b0dbe76357 | 261 | printf ("Failed to read configuration file[%s]\n\r", fileName.c_str()); |
cmkachur | 6:96b0dbe76357 | 262 | |
cmkachur | 6:96b0dbe76357 | 263 | // try to create a default configuration file |
cmkachur | 6:96b0dbe76357 | 264 | for (int i = 0; i < count; ++i) |
cmkachur | 6:96b0dbe76357 | 265 | { |
cmkachur | 6:96b0dbe76357 | 266 | if (snprintf(szValue, sizeof(szValue), "%d", *(tokenMap[i].value))) |
cmkachur | 6:96b0dbe76357 | 267 | { |
cmkachur | 6:96b0dbe76357 | 268 | if (plCfgFile->setValue((char *)tokenMap[i].token, szValue)) |
cmkachur | 6:96b0dbe76357 | 269 | { |
cmkachur | 6:96b0dbe76357 | 270 | printf ("Token creation success %s[%s]\n\r", tokenMap[i].token, szValue); |
cmkachur | 6:96b0dbe76357 | 271 | } |
cmkachur | 6:96b0dbe76357 | 272 | } |
cmkachur | 6:96b0dbe76357 | 273 | } |
cmkachur | 6:96b0dbe76357 | 274 | if (plCfgFile->write((char *)fileName.c_str(), NULL, DOS)) |
cmkachur | 6:96b0dbe76357 | 275 | printf ("Success: Created default configuration file [%s]\n\r", fileName.c_str()); |
cmkachur | 6:96b0dbe76357 | 276 | else |
cmkachur | 6:96b0dbe76357 | 277 | printf ("Failed to create default configuration file [%s]\n\r", fileName.c_str()); |
cmkachur | 6:96b0dbe76357 | 278 | } |
cmkachur | 6:96b0dbe76357 | 279 | |
cmkachur | 6:96b0dbe76357 | 280 | if (plCfgFile) |
cmkachur | 6:96b0dbe76357 | 281 | delete plCfgFile; |
cmkachur | 6:96b0dbe76357 | 282 | |
ftagius | 9:15c9bf86d908 | 283 | if (fp != NULL) |
ftagius | 9:15c9bf86d908 | 284 | fclose(fp); |
ftagius | 9:15c9bf86d908 | 285 | |
cmkachur | 6:96b0dbe76357 | 286 | sd.unmount(); |
cmkachur | 6:96b0dbe76357 | 287 | return bRetVal; |
cmkachur | 6:96b0dbe76357 | 288 | } |
cmkachur | 6:96b0dbe76357 | 289 | |
cmkachur | 4:4d26ba1ae0f7 | 290 | |
ftagius | 0:3328df4c3116 | 291 | int main() |
ftagius | 0:3328df4c3116 | 292 | { |
cmkachur | 6:96b0dbe76357 | 293 | |
cmkachur | 4:4d26ba1ae0f7 | 294 | unsigned char regBuff[MAX_CONFIG_REGS]; |
ftagius | 0:3328df4c3116 | 295 | char c; |
ftagius | 0:3328df4c3116 | 296 | Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? |
ftagius | 0:3328df4c3116 | 297 | const int refresh_Time = 1000; //refresh time in ms |
ftagius | 0:3328df4c3116 | 298 | TSISensor tsi; // touch slider |
ftagius | 0:3328df4c3116 | 299 | unsigned long measFreq; |
ftagius | 1:10d2a051285e | 300 | rdistance=-1; |
ftagius | 1:10d2a051285e | 301 | rinterrupt=-1; |
ftagius | 0:3328df4c3116 | 302 | pc.baud(9600); |
ftagius | 0:3328df4c3116 | 303 | // initializations for gps |
ftagius | 0:3328df4c3116 | 304 | green = 1; |
ftagius | 0:3328df4c3116 | 305 | gpsd.setBaud(9600); |
ftagius | 0:3328df4c3116 | 306 | gpsd.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); |
ftagius | 0:3328df4c3116 | 307 | gpsd.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
ftagius | 0:3328df4c3116 | 308 | gpsd.sendCommand(PGCMD_ANTENNA); |
ftagius | 0:3328df4c3116 | 309 | gpsd.day=01; |
ftagius | 0:3328df4c3116 | 310 | gpsd.month=01; |
ftagius | 0:3328df4c3116 | 311 | gpsd.year=15; |
ftagius | 0:3328df4c3116 | 312 | gpsd.hour=1; |
ftagius | 0:3328df4c3116 | 313 | gpsd.minute=1; |
ftagius | 0:3328df4c3116 | 314 | gpsd.seconds=1; |
ftagius | 0:3328df4c3116 | 315 | red = 1; |
ftagius | 0:3328df4c3116 | 316 | green = 1; |
ftagius | 3:e3974328d808 | 317 | pc.printf("Touch slider to start lightning detector application\r\n"); |
ftagius | 0:3328df4c3116 | 318 | while(1) { |
ftagius | 0:3328df4c3116 | 319 | green = 1; // turn led off |
ftagius | 0:3328df4c3116 | 320 | wait_ms(200); |
ftagius | 0:3328df4c3116 | 321 | if (tsi.readPercentage()) |
ftagius | 0:3328df4c3116 | 322 | break; |
ftagius | 0:3328df4c3116 | 323 | green = 0; // turn led on |
ftagius | 0:3328df4c3116 | 324 | wait_ms(200); |
ftagius | 0:3328df4c3116 | 325 | if (tsi.readPercentage()) |
ftagius | 0:3328df4c3116 | 326 | break; |
ftagius | 0:3328df4c3116 | 327 | } |
cmkachur | 6:96b0dbe76357 | 328 | //Mount the filesystem |
cmkachur | 6:96b0dbe76357 | 329 | sd.mount(); |
cmkachur | 6:96b0dbe76357 | 330 | mkdir(directory, 0777); |
cmkachur | 6:96b0dbe76357 | 331 | sd.unmount(); |
cmkachur | 6:96b0dbe76357 | 332 | |
cmkachur | 6:96b0dbe76357 | 333 | // read configuration values fro SD file system to allow override of defaults in sysCfg |
cmkachur | 6:96b0dbe76357 | 334 | readCfgFile(directory, &sysCfg); |
cmkachur | 6:96b0dbe76357 | 335 | |
ftagius | 3:e3974328d808 | 336 | pc.printf("\r\nInitialize lightning detector\r\n"); |
ftagius | 0:3328df4c3116 | 337 | //initializations for lightning detector |
ftagius | 0:3328df4c3116 | 338 | ld.init(); |
ftagius | 0:3328df4c3116 | 339 | ld.clearStats(); |
ftagius | 1:10d2a051285e | 340 | |
cmkachur | 5:1d4fd419cfb7 | 341 | /* The precision of the calibration will depend on the |
cmkachur | 5:1d4fd419cfb7 | 342 | accuracy of the resonance frequency of the antenna. It is |
cmkachur | 5:1d4fd419cfb7 | 343 | recommended to first trim the receiver antenna before the |
cmkachur | 5:1d4fd419cfb7 | 344 | calibration of both oscillators is done. |
cmkachur | 5:1d4fd419cfb7 | 345 | */ |
cmkachur | 5:1d4fd419cfb7 | 346 | measFreq = ld.tuneAntenna(IntLightning); |
cmkachur | 2:3edb129c60b2 | 347 | ld.calibrateRCOs(IntLightning); |
ftagius | 1:10d2a051285e | 348 | |
cmkachur | 6:96b0dbe76357 | 349 | if (sysCfg.bOutdoorMode) |
cmkachur | 6:96b0dbe76357 | 350 | ld.setOutdoors(); |
cmkachur | 6:96b0dbe76357 | 351 | else |
cmkachur | 6:96b0dbe76357 | 352 | ld.setIndoors(); |
cmkachur | 6:96b0dbe76357 | 353 | |
cmkachur | 6:96b0dbe76357 | 354 | ld.setMinimumLightnings(sysCfg.minNumLight); |
cmkachur | 6:96b0dbe76357 | 355 | ld.setSpikeRejection(sysCfg.spikeRej); |
cmkachur | 6:96b0dbe76357 | 356 | ld.setNoiseFloor(sysCfg.noiseFloor); |
cmkachur | 4:4d26ba1ae0f7 | 357 | |
cmkachur | 6:96b0dbe76357 | 358 | if (sysCfg.maskDisturber) |
cmkachur | 6:96b0dbe76357 | 359 | ld.disableDisturbers(); |
cmkachur | 6:96b0dbe76357 | 360 | else |
cmkachur | 6:96b0dbe76357 | 361 | ld.enableDisturbers(); |
cmkachur | 6:96b0dbe76357 | 362 | |
cmkachur | 6:96b0dbe76357 | 363 | ld.setWatchdogThreshold(sysCfg.wdogThresh); |
ftagius | 0:3328df4c3116 | 364 | IntLightning.rise(&DetectLightning); |
cmkachur | 6:96b0dbe76357 | 365 | |
cmkachur | 6:96b0dbe76357 | 366 | int MinLight = ld.getMinimumLightnings(); |
cmkachur | 6:96b0dbe76357 | 367 | int Noise = ld.getNoiseFloor(); |
cmkachur | 6:96b0dbe76357 | 368 | int TuneCap = ld.getTuneCap(); |
cmkachur | 6:96b0dbe76357 | 369 | int SpikeRej = ld.getSpikeRejection(); |
cmkachur | 6:96b0dbe76357 | 370 | int WatchDog = ld.getWatchdogThreshold(); |
ftagius | 0:3328df4c3116 | 371 | |
cmkachur | 6:96b0dbe76357 | 372 | pc.printf("\r\n Min lightning: %i", MinLight); |
ftagius | 0:3328df4c3116 | 373 | pc.printf("\r\n"); |
ftagius | 0:3328df4c3116 | 374 | pc.printf(" Gain: 0x%02x\r\n",ld.getGain()); |
ftagius | 0:3328df4c3116 | 375 | pc.printf(" Noise: %i", Noise); |
ftagius | 0:3328df4c3116 | 376 | pc.printf("\r\n"); |
ftagius | 0:3328df4c3116 | 377 | pc.printf(" Tune CAP: %i", TuneCap); |
ftagius | 0:3328df4c3116 | 378 | pc.printf("\r\n"); |
ftagius | 0:3328df4c3116 | 379 | pc.printf(" Spike rej: %i", SpikeRej); |
ftagius | 0:3328df4c3116 | 380 | pc.printf("\r\n"); |
ftagius | 0:3328df4c3116 | 381 | pc.printf(" Watchdog: %i", WatchDog); |
ftagius | 0:3328df4c3116 | 382 | pc.printf("\r\n"); |
ftagius | 0:3328df4c3116 | 383 | pc.printf(" LCO calibration: %ld Hz\n\r", measFreq); |
ftagius | 0:3328df4c3116 | 384 | |
ftagius | 0:3328df4c3116 | 385 | refresh_Timer.start(); //starts the clock on the timer |
cmkachur | 4:4d26ba1ae0f7 | 386 | |
cmkachur | 4:4d26ba1ae0f7 | 387 | // get a copy of all config registers |
cmkachur | 4:4d26ba1ae0f7 | 388 | ld.getConfigRegisters(regBuff, sizeof(regBuff)); |
cmkachur | 4:4d26ba1ae0f7 | 389 | |
cmkachur | 6:96b0dbe76357 | 390 | pc.printf("\n\rSystem Registers:\n\r"); |
cmkachur | 6:96b0dbe76357 | 391 | for (int i = 0; i < sizeof(regBuff); ++i) |
cmkachur | 6:96b0dbe76357 | 392 | pc.printf("REG%d [0x%02x]:\n\r", i, regBuff[i]); |
cmkachur | 6:96b0dbe76357 | 393 | pc.printf("\n\r"); |
cmkachur | 6:96b0dbe76357 | 394 | |
cmkachur | 4:4d26ba1ae0f7 | 395 | // write to the config file |
cmkachur | 4:4d26ba1ae0f7 | 396 | writeCfgFile(regBuff, sizeof(regBuff), FW_VER); |
ftagius | 1:10d2a051285e | 397 | |
ftagius | 0:3328df4c3116 | 398 | bool gpsFix=false; |
ftagius | 8:f8830b6c6d9b | 399 | bool rtcRunning=false; |
ftagius | 0:3328df4c3116 | 400 | while (1) |
ftagius | 0:3328df4c3116 | 401 | { |
ftagius | 3:e3974328d808 | 402 | if (OriginInt != -1) |
ftagius | 3:e3974328d808 | 403 | { |
ftagius | 3:e3974328d808 | 404 | // the ld detector generated an interrupt, log the event |
ftagius | 3:e3974328d808 | 405 | IntLightning.disable_irq(); |
cmkachur | 6:96b0dbe76357 | 406 | writeLogFile(OriginInt,gDistance, energy); |
cmkachur | 6:96b0dbe76357 | 407 | //ld.clearStats(); |
ftagius | 3:e3974328d808 | 408 | OriginInt = -1; |
cmkachur | 6:96b0dbe76357 | 409 | gDistance = -1; |
ftagius | 3:e3974328d808 | 410 | energy = -1; |
ftagius | 3:e3974328d808 | 411 | IntLightning.enable_irq(); |
ftagius | 3:e3974328d808 | 412 | } |
ftagius | 3:e3974328d808 | 413 | |
ftagius | 0:3328df4c3116 | 414 | c = gpsd.read(); //queries the GPS |
ftagius | 0:3328df4c3116 | 415 | if (debug) |
ftagius | 0:3328df4c3116 | 416 | { |
ftagius | 0:3328df4c3116 | 417 | if (c) { |
ftagius | 0:3328df4c3116 | 418 | printf("%c", c); //this line will echo the GPS data if not paused |
ftagius | 0:3328df4c3116 | 419 | continue; |
ftagius | 0:3328df4c3116 | 420 | } |
ftagius | 0:3328df4c3116 | 421 | } |
ftagius | 0:3328df4c3116 | 422 | |
ftagius | 0:3328df4c3116 | 423 | //check if we recieved a new message from GPS, if so, attempt to parse it, |
ftagius | 0:3328df4c3116 | 424 | if ( gpsd.newNMEAreceived() ) { |
ftagius | 0:3328df4c3116 | 425 | if ( !gpsd.parse(gpsd.lastNMEA()) ) { |
ftagius | 0:3328df4c3116 | 426 | continue; |
ftagius | 0:3328df4c3116 | 427 | } |
ftagius | 0:3328df4c3116 | 428 | } |
ftagius | 0:3328df4c3116 | 429 | |
ftagius | 8:f8830b6c6d9b | 430 | // if the rtc is not running, update the rtc clock with the latest gps time stamp |
ftagius | 8:f8830b6c6d9b | 431 | if ( rtcRunning == false ) |
ftagius | 8:f8830b6c6d9b | 432 | { |
ftagius | 8:f8830b6c6d9b | 433 | // update rtc with the lastest gps time stamp |
ftagius | 8:f8830b6c6d9b | 434 | SetDateTime(gpsd.year+2000, |
ftagius | 8:f8830b6c6d9b | 435 | gpsd.month-1, |
ftagius | 8:f8830b6c6d9b | 436 | gpsd.day, |
ftagius | 8:f8830b6c6d9b | 437 | gpsd.hour, |
ftagius | 8:f8830b6c6d9b | 438 | gpsd.minute, |
ftagius | 8:f8830b6c6d9b | 439 | gpsd.seconds); |
ftagius | 8:f8830b6c6d9b | 440 | } |
ftagius | 8:f8830b6c6d9b | 441 | |
ftagius | 0:3328df4c3116 | 442 | //check if enough time has passed to warrant printing GPS info to screen |
ftagius | 0:3328df4c3116 | 443 | //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing |
ftagius | 0:3328df4c3116 | 444 | if (refresh_Timer.read_ms() >= refresh_Time) |
ftagius | 0:3328df4c3116 | 445 | { |
ftagius | 0:3328df4c3116 | 446 | if (gpsd.fix) { |
ftagius | 0:3328df4c3116 | 447 | // got a gps fix |
ftagius | 0:3328df4c3116 | 448 | if (gpsFix == false) |
ftagius | 0:3328df4c3116 | 449 | { |
ftagius | 0:3328df4c3116 | 450 | // first time fix obtained |
ftagius | 0:3328df4c3116 | 451 | gpsFix = true; |
ftagius | 8:f8830b6c6d9b | 452 | // bug check - rtc may not be running. check if it is incrementing |
ftagius | 8:f8830b6c6d9b | 453 | time_t seconds_a = time(NULL); // get the current rtc second count |
ftagius | 8:f8830b6c6d9b | 454 | wait(2); // wait two seconds |
ftagius | 8:f8830b6c6d9b | 455 | time_t seconds_b = time(NULL); // get the current rtc second count |
ftagius | 8:f8830b6c6d9b | 456 | if (seconds_a != seconds_b) |
ftagius | 8:f8830b6c6d9b | 457 | { |
ftagius | 8:f8830b6c6d9b | 458 | // rtc must be running |
ftagius | 8:f8830b6c6d9b | 459 | rtcRunning = true; |
ftagius | 8:f8830b6c6d9b | 460 | pc.printf("RTC is running\r\n"); |
ftagius | 8:f8830b6c6d9b | 461 | } |
ftagius | 8:f8830b6c6d9b | 462 | else |
ftagius | 8:f8830b6c6d9b | 463 | { |
ftagius | 8:f8830b6c6d9b | 464 | // rtc is not running, we need to update the rtc every pass through the while loop |
ftagius | 8:f8830b6c6d9b | 465 | rtcRunning = false; // the gps time will update the rtc |
ftagius | 8:f8830b6c6d9b | 466 | pc.printf("RTC is not running\r\n"); |
ftagius | 8:f8830b6c6d9b | 467 | } |
ftagius | 8:f8830b6c6d9b | 468 | |
ftagius | 8:f8830b6c6d9b | 469 | // set the rtc with the latest gps time |
ftagius | 8:f8830b6c6d9b | 470 | SetDateTime(gpsd.year+2000, |
ftagius | 8:f8830b6c6d9b | 471 | gpsd.month-1, |
ftagius | 8:f8830b6c6d9b | 472 | gpsd.day, |
ftagius | 8:f8830b6c6d9b | 473 | gpsd.hour, |
ftagius | 8:f8830b6c6d9b | 474 | gpsd.minute, |
ftagius | 8:f8830b6c6d9b | 475 | gpsd.seconds); |
ftagius | 0:3328df4c3116 | 476 | 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 | 477 | //pc.printf("Touch slider to suspend application\r\n"); |
ftagius | 0:3328df4c3116 | 478 | pc.printf("Waiting for lighting detection...\r\n"); |
ftagius | 0:3328df4c3116 | 479 | } |
ftagius | 0:3328df4c3116 | 480 | |
ftagius | 0:3328df4c3116 | 481 | //red = 1; // turn led off |
ftagius | 0:3328df4c3116 | 482 | //pc.printf("turn green on\r\n"); |
ftagius | 0:3328df4c3116 | 483 | green = 0; // turn led on |
ftagius | 0:3328df4c3116 | 484 | wait_ms(50); |
ftagius | 0:3328df4c3116 | 485 | } |
ftagius | 0:3328df4c3116 | 486 | else |
ftagius | 0:3328df4c3116 | 487 | { |
ftagius | 3:e3974328d808 | 488 | gpsFix = false; |
ftagius | 0:3328df4c3116 | 489 | pc.printf("Waiting for GPS FIX\r\n"); |
ftagius | 0:3328df4c3116 | 490 | red = 0; // turn led on |
ftagius | 0:3328df4c3116 | 491 | } |
ftagius | 0:3328df4c3116 | 492 | |
ftagius | 0:3328df4c3116 | 493 | // restart the timer for the gps print loop |
ftagius | 0:3328df4c3116 | 494 | // writeLogFile(-2); |
ftagius | 0:3328df4c3116 | 495 | refresh_Timer.reset(); |
ftagius | 0:3328df4c3116 | 496 | } |
ftagius | 0:3328df4c3116 | 497 | else |
ftagius | 0:3328df4c3116 | 498 | { |
ftagius | 0:3328df4c3116 | 499 | //red = 0; // turn led on |
ftagius | 0:3328df4c3116 | 500 | //pc.printf("turn green off\r\n"); |
ftagius | 0:3328df4c3116 | 501 | green = 1; // turn green led off |
ftagius | 0:3328df4c3116 | 502 | } // end else refresh timer |
ftagius | 0:3328df4c3116 | 503 | } |
ftagius | 0:3328df4c3116 | 504 | |
ftagius | 0:3328df4c3116 | 505 | } |