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