basic lightning detector with gps and sd card logging

Dependencies:   AS3935 AdafruitGPS SDFileSystem TSI mbed ConfigFile

Committer:
cmkachur
Date:
Wed Jun 24 16:36:55 2015 +0000
Revision:
4:4d26ba1ae0f7
Parent:
3:e3974328d808
Child:
5:1d4fd419cfb7
Store all configuration register values and firmware version to config_data.csv file at startup. Delete obsolete code.

Who changed what in which revision?

UserRevisionLine numberNew 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"
ftagius 0:3328df4c3116 7
cmkachur 4:4d26ba1ae0f7 8 #define FW_VER 1
cmkachur 4:4d26ba1ae0f7 9
ftagius 3:e3974328d808 10 // frdm-kl25z as3935 connections for spi1
ftagius 0:3328df4c3116 11 // ------------------------------------------------
ftagius 0:3328df4c3116 12 // Header -- kl25z -- SD/MMC
ftagius 0:3328df4c3116 13 // J2-20 -- PTE1 -- MOSI
ftagius 0:3328df4c3116 14 // J9-13 -- PTE4 -- CS
ftagius 0:3328df4c3116 15 // J2-14 -- GND -- Vss (GND)
ftagius 0:3328df4c3116 16 // J9-9 -- PTE2 -- SCK
ftagius 0:3328df4c3116 17 // J9-11 -- PTE3 -- MISO
ftagius 0:3328df4c3116 18
ftagius 0:3328df4c3116 19 AS3935 ld(PTE1, PTE3, PTE2, PTE4, "ld", 1000000); // MOSI, MISO, SCK, CS, SPI bus freq (hz)
ftagius 0:3328df4c3116 20 InterruptIn IntLightning(PTA12); //IRQ AS3935
ftagius 0:3328df4c3116 21
ftagius 0:3328df4c3116 22
ftagius 0:3328df4c3116 23 // frdm-kl25z sd card connections spi0
ftagius 0:3328df4c3116 24 // ------------------------------------------------
ftagius 0:3328df4c3116 25 // Header -- kl25z -- SPI
ftagius 0:3328df4c3116 26 // J2-8 -- PTD2 -- MOSI
ftagius 0:3328df4c3116 27 // J2-6 -- PTD0 -- CS
ftagius 0:3328df4c3116 28 // J9-12 -- GND -- Vss (GND)
ftagius 0:3328df4c3116 29 // J9-4 -- P3V3 -- Vdd (+3.3v)
ftagius 0:3328df4c3116 30 // J2-12 -- PTD1 -- SCK
ftagius 0:3328df4c3116 31 // J9-14 -- GND -- Vss (GND)
ftagius 0:3328df4c3116 32 // J2-10 -- PTD3 -- MISO
ftagius 0:3328df4c3116 33
ftagius 0:3328df4c3116 34 SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // MOSI, MISO, SCK, CS
ftagius 0:3328df4c3116 35
ftagius 0:3328df4c3116 36 Serial pc(USBTX, USBRX);
ftagius 0:3328df4c3116 37 GPS gpsd(PTE20, PTE21);
ftagius 0:3328df4c3116 38 DigitalOut red(LED_RED);
ftagius 0:3328df4c3116 39 DigitalOut green(LED_GREEN);
ftagius 0:3328df4c3116 40 //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 41 bool debug=false;
ftagius 0:3328df4c3116 42 int day, month,year,hour,minute,seconds;
cmkachur 2:3edb129c60b2 43 void writeLogFile(int interruptSource, int distance, long energy);
ftagius 1:10d2a051285e 44 char logName[]="lightning_data.csv";
ftagius 1:10d2a051285e 45 int rdistance, rinterrupt;
ftagius 1:10d2a051285e 46 char directory[]="/sd/lightning_data";
ftagius 3:e3974328d808 47 int OriginInt=-1;
ftagius 3:e3974328d808 48 int distance=-1;
ftagius 3:e3974328d808 49 long energy=-1;
ftagius 0:3328df4c3116 50
ftagius 0:3328df4c3116 51 void DetectLightning()
ftagius 0:3328df4c3116 52 {
ftagius 0:3328df4c3116 53 OriginInt = ld.interruptSource();
ftagius 3:e3974328d808 54 distance = ld.lightningDistanceKm();
ftagius 3:e3974328d808 55 energy = ld.getEnergy();
ftagius 0:3328df4c3116 56 }
ftagius 0:3328df4c3116 57
cmkachur 2:3edb129c60b2 58 void writeLogFile(int interruptSource, int distance, long energy)
ftagius 0:3328df4c3116 59 {
ftagius 1:10d2a051285e 60 char logFilePath[128];
ftagius 0:3328df4c3116 61 static bool header=false;
ftagius 0:3328df4c3116 62 FILE *fp;
ftagius 1:10d2a051285e 63
ftagius 1:10d2a051285e 64 sprintf(logFilePath, "%s/%s", directory,logName);
ftagius 0:3328df4c3116 65 sd.mount();
ftagius 1:10d2a051285e 66 fp = fopen(logFilePath, "a");
ftagius 0:3328df4c3116 67 if(fp == NULL) {
ftagius 0:3328df4c3116 68 // retry
ftagius 3:e3974328d808 69 wait_ms(200);
ftagius 1:10d2a051285e 70 fp = fopen(logFilePath, "a");
ftagius 0:3328df4c3116 71 if (fp == NULL)
ftagius 0:3328df4c3116 72 {
ftagius 1:10d2a051285e 73 printf("Could not open file %s for writing\r\n",logFilePath);
ftagius 0:3328df4c3116 74 sd.unmount();
ftagius 1:10d2a051285e 75 printf("unmount sd card \r\n");
ftagius 0:3328df4c3116 76 return;
ftagius 0:3328df4c3116 77 }
ftagius 0:3328df4c3116 78 }
ftagius 3:e3974328d808 79 if (debug)
ftagius 3:e3974328d808 80 pc.printf("Opened log file %s\r\n",logFilePath);
ftagius 0:3328df4c3116 81 // write the log file header
ftagius 0:3328df4c3116 82 if (header == false)
ftagius 0:3328df4c3116 83 {
cmkachur 2:3edb129c60b2 84 fprintf(fp,"# date,time,raw timestamp,latitude,longitude,distance,interrupt,energy\r\n");
ftagius 0:3328df4c3116 85 header = true;
ftagius 0:3328df4c3116 86 }
ftagius 0:3328df4c3116 87 // write to the current log file
ftagius 0:3328df4c3116 88 fprintf(fp,"%02d/%02d/20%02d,", gpsd.month, gpsd.day, gpsd.year);
ftagius 0:3328df4c3116 89 fprintf(fp,"%02d:%02d:%02d,", gpsd.hour, gpsd.minute, gpsd.seconds);
ftagius 0:3328df4c3116 90 fprintf(fp,"%7.0f,",gpsd.timef);
ftagius 0:3328df4c3116 91 fprintf(fp,"%5.7f,%5.7f,", gpsd.lat_deg, gpsd.lon_deg);
ftagius 1:10d2a051285e 92 fprintf(fp,"%d,",distance);
cmkachur 2:3edb129c60b2 93 fprintf(fp,"%d,",interruptSource);
cmkachur 2:3edb129c60b2 94 fprintf(fp,"%ld",energy);
ftagius 0:3328df4c3116 95 fprintf(fp,"\r\n");
ftagius 0:3328df4c3116 96 fflush(fp);
ftagius 0:3328df4c3116 97 f_sync((FIL*)fp);
ftagius 0:3328df4c3116 98 fclose(fp);
ftagius 0:3328df4c3116 99 sd.unmount();
ftagius 3:e3974328d808 100 pc.printf("Event: ");
ftagius 3:e3974328d808 101 switch (interruptSource)
ftagius 3:e3974328d808 102 {
ftagius 3:e3974328d808 103 case 1:
ftagius 3:e3974328d808 104 pc.printf("Noise level too high\r\n");
ftagius 3:e3974328d808 105 break;
ftagius 3:e3974328d808 106 case 4:
ftagius 3:e3974328d808 107 pc.printf("Disturber\r\n");
ftagius 3:e3974328d808 108 break;
ftagius 3:e3974328d808 109 case 8:
ftagius 3:e3974328d808 110 pc.printf("Lightning detection, distance=%dkm energy=%ld\r\n", distance, energy);
ftagius 3:e3974328d808 111 ld.clearStats();
ftagius 3:e3974328d808 112 break;
ftagius 3:e3974328d808 113 default:
ftagius 3:e3974328d808 114 pc.printf("Unknown interrupt %d\r\n", OriginInt);
ftagius 3:e3974328d808 115
ftagius 3:e3974328d808 116 }
ftagius 3:e3974328d808 117
ftagius 3:e3974328d808 118 if (debug)
ftagius 3:e3974328d808 119 pc.printf("Closed log file %s\r\n",logFilePath);
ftagius 0:3328df4c3116 120 }
ftagius 0:3328df4c3116 121
cmkachur 4:4d26ba1ae0f7 122
cmkachur 4:4d26ba1ae0f7 123 void writeCfgFile(unsigned char *pBuff, unsigned char buffLen, unsigned char fwVer)
cmkachur 4:4d26ba1ae0f7 124 {
cmkachur 4:4d26ba1ae0f7 125 char cfgFilePath[128];
cmkachur 4:4d26ba1ae0f7 126 FILE *fp;
cmkachur 4:4d26ba1ae0f7 127 unsigned char cnt = 0;
cmkachur 4:4d26ba1ae0f7 128
cmkachur 4:4d26ba1ae0f7 129 sprintf(cfgFilePath, "%s/%s", directory, "config_data.csv");
cmkachur 4:4d26ba1ae0f7 130 sd.mount();
cmkachur 4:4d26ba1ae0f7 131 fp = fopen(cfgFilePath, "w");
cmkachur 4:4d26ba1ae0f7 132 if(fp == NULL) {
cmkachur 4:4d26ba1ae0f7 133 // retry
cmkachur 4:4d26ba1ae0f7 134 wait_ms(200);
cmkachur 4:4d26ba1ae0f7 135 fp = fopen(cfgFilePath, "w");
cmkachur 4:4d26ba1ae0f7 136 if (fp == NULL)
cmkachur 4:4d26ba1ae0f7 137 {
cmkachur 4:4d26ba1ae0f7 138 printf("Could not open file %s for writing\r\n",cfgFilePath);
cmkachur 4:4d26ba1ae0f7 139 sd.unmount();
cmkachur 4:4d26ba1ae0f7 140 printf("unmount sd card \r\n");
cmkachur 4:4d26ba1ae0f7 141 return;
cmkachur 4:4d26ba1ae0f7 142 }
cmkachur 4:4d26ba1ae0f7 143 }
cmkachur 4:4d26ba1ae0f7 144 if (debug)
cmkachur 4:4d26ba1ae0f7 145 pc.printf("Opened log file %s\r\n",cfgFilePath);
cmkachur 4:4d26ba1ae0f7 146
cmkachur 4:4d26ba1ae0f7 147 // write the header
cmkachur 4:4d26ba1ae0f7 148 fprintf(fp,"# FW_VER,REG0,REG1,REG2,REG3,REG4,REG5,REG6,REG7,REG8\r\n");
cmkachur 4:4d26ba1ae0f7 149
cmkachur 4:4d26ba1ae0f7 150 // write the firmware version
cmkachur 4:4d26ba1ae0f7 151 fprintf(fp,"%d,", fwVer);
cmkachur 4:4d26ba1ae0f7 152
cmkachur 4:4d26ba1ae0f7 153 // write all the configuration registers
cmkachur 4:4d26ba1ae0f7 154 for (cnt = 0; cnt < buffLen && cnt < MAX_CONFIG_REGS; ++cnt)
cmkachur 4:4d26ba1ae0f7 155 fprintf(fp,"0x%x,", pBuff[cnt]);
cmkachur 4:4d26ba1ae0f7 156
cmkachur 4:4d26ba1ae0f7 157 fflush(fp);
cmkachur 4:4d26ba1ae0f7 158 f_sync((FIL*)fp);
cmkachur 4:4d26ba1ae0f7 159 fclose(fp);
cmkachur 4:4d26ba1ae0f7 160 sd.unmount();
cmkachur 4:4d26ba1ae0f7 161
cmkachur 4:4d26ba1ae0f7 162 if (debug)
cmkachur 4:4d26ba1ae0f7 163 pc.printf("Closed cfg file %s\r\n",cfgFilePath);
cmkachur 4:4d26ba1ae0f7 164 }
cmkachur 4:4d26ba1ae0f7 165
cmkachur 4:4d26ba1ae0f7 166
cmkachur 4:4d26ba1ae0f7 167
ftagius 0:3328df4c3116 168 int main()
ftagius 0:3328df4c3116 169 {
cmkachur 4:4d26ba1ae0f7 170 unsigned char regBuff[MAX_CONFIG_REGS];
ftagius 0:3328df4c3116 171 char c;
ftagius 0:3328df4c3116 172 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
ftagius 0:3328df4c3116 173 const int refresh_Time = 1000; //refresh time in ms
ftagius 0:3328df4c3116 174 TSISensor tsi; // touch slider
ftagius 0:3328df4c3116 175 unsigned long measFreq;
ftagius 1:10d2a051285e 176 rdistance=-1;
ftagius 1:10d2a051285e 177 rinterrupt=-1;
ftagius 0:3328df4c3116 178 pc.baud(9600);
ftagius 0:3328df4c3116 179 // initializations for gps
ftagius 0:3328df4c3116 180 green = 1;
ftagius 0:3328df4c3116 181 gpsd.setBaud(9600);
ftagius 0:3328df4c3116 182 gpsd.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
ftagius 0:3328df4c3116 183 gpsd.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
ftagius 0:3328df4c3116 184 gpsd.sendCommand(PGCMD_ANTENNA);
ftagius 0:3328df4c3116 185 gpsd.day=01;
ftagius 0:3328df4c3116 186 gpsd.month=01;
ftagius 0:3328df4c3116 187 gpsd.year=15;
ftagius 0:3328df4c3116 188 gpsd.hour=1;
ftagius 0:3328df4c3116 189 gpsd.minute=1;
ftagius 0:3328df4c3116 190 gpsd.seconds=1;
ftagius 0:3328df4c3116 191 red = 1;
ftagius 0:3328df4c3116 192 green = 1;
ftagius 3:e3974328d808 193 pc.printf("Touch slider to start lightning detector application\r\n");
ftagius 0:3328df4c3116 194 while(1) {
ftagius 0:3328df4c3116 195 green = 1; // turn led off
ftagius 0:3328df4c3116 196 wait_ms(200);
ftagius 0:3328df4c3116 197 if (tsi.readPercentage())
ftagius 0:3328df4c3116 198 break;
ftagius 0:3328df4c3116 199 green = 0; // turn led on
ftagius 0:3328df4c3116 200 wait_ms(200);
ftagius 0:3328df4c3116 201 if (tsi.readPercentage())
ftagius 0:3328df4c3116 202 break;
ftagius 0:3328df4c3116 203 }
ftagius 0:3328df4c3116 204
ftagius 3:e3974328d808 205 pc.printf("\r\nInitialize lightning detector\r\n");
ftagius 0:3328df4c3116 206 //initializations for lightning detector
ftagius 0:3328df4c3116 207 ld.init();
ftagius 0:3328df4c3116 208 ld.clearStats();
ftagius 1:10d2a051285e 209
cmkachur 2:3edb129c60b2 210 ld.calibrateRCOs(IntLightning);
ftagius 0:3328df4c3116 211 measFreq = ld.tuneAntenna(IntLightning);
ftagius 1:10d2a051285e 212
ftagius 1:10d2a051285e 213 ld.setOutdoors();
ftagius 0:3328df4c3116 214 ld.setMinimumLightnings(0);
ftagius 0:3328df4c3116 215 ld.setSpikeRejection(2);
ftagius 0:3328df4c3116 216 ld.setNoiseFloor(2);
cmkachur 4:4d26ba1ae0f7 217
ftagius 0:3328df4c3116 218 ld.enableDisturbers();
ftagius 0:3328df4c3116 219 ld.setWatchdogThreshold(4);
ftagius 0:3328df4c3116 220 IntLightning.rise(&DetectLightning);
ftagius 0:3328df4c3116 221 int MinBlysk = ld.getMinimumLightnings();
ftagius 0:3328df4c3116 222 int Noise = ld.getNoiseFloor();
ftagius 0:3328df4c3116 223 int TuneCap = ld.getTuneCap();
ftagius 0:3328df4c3116 224 int SpikeRej = ld.getSpikeRejection();
ftagius 0:3328df4c3116 225 int WatchDog = ld.getWatchdogThreshold();
ftagius 0:3328df4c3116 226
ftagius 3:e3974328d808 227 pc.printf("\r\n Min wylad: %i", MinBlysk);
ftagius 0:3328df4c3116 228 pc.printf("\r\n");
ftagius 0:3328df4c3116 229 pc.printf(" Gain: 0x%02x\r\n",ld.getGain());
ftagius 0:3328df4c3116 230 pc.printf(" Noise: %i", Noise);
ftagius 0:3328df4c3116 231 pc.printf("\r\n");
ftagius 0:3328df4c3116 232 pc.printf(" Tune CAP: %i", TuneCap);
ftagius 0:3328df4c3116 233 pc.printf("\r\n");
ftagius 0:3328df4c3116 234 pc.printf(" Spike rej: %i", SpikeRej);
ftagius 0:3328df4c3116 235 pc.printf("\r\n");
ftagius 0:3328df4c3116 236 pc.printf(" Watchdog: %i", WatchDog);
ftagius 0:3328df4c3116 237 pc.printf("\r\n");
ftagius 0:3328df4c3116 238 pc.printf(" LCO calibration: %ld Hz\n\r", measFreq);
ftagius 0:3328df4c3116 239
ftagius 0:3328df4c3116 240
ftagius 0:3328df4c3116 241 refresh_Timer.start(); //starts the clock on the timer
ftagius 0:3328df4c3116 242 //Mount the filesystem
ftagius 0:3328df4c3116 243 sd.mount();
ftagius 0:3328df4c3116 244 mkdir(directory, 0777);
ftagius 1:10d2a051285e 245 sd.unmount();
cmkachur 4:4d26ba1ae0f7 246
cmkachur 4:4d26ba1ae0f7 247 // get a copy of all config registers
cmkachur 4:4d26ba1ae0f7 248 ld.getConfigRegisters(regBuff, sizeof(regBuff));
cmkachur 4:4d26ba1ae0f7 249
cmkachur 4:4d26ba1ae0f7 250 // write to the config file
cmkachur 4:4d26ba1ae0f7 251 writeCfgFile(regBuff, sizeof(regBuff), FW_VER);
ftagius 1:10d2a051285e 252
ftagius 0:3328df4c3116 253 bool gpsFix=false;
ftagius 0:3328df4c3116 254 while (1)
ftagius 0:3328df4c3116 255 {
ftagius 3:e3974328d808 256 if (OriginInt != -1)
ftagius 3:e3974328d808 257 {
ftagius 3:e3974328d808 258 // the ld detector generated an interrupt, log the event
ftagius 3:e3974328d808 259 IntLightning.disable_irq();
ftagius 3:e3974328d808 260 writeLogFile(OriginInt,distance, energy);
ftagius 3:e3974328d808 261 ld.clearStats();
ftagius 3:e3974328d808 262 OriginInt = -1;
ftagius 3:e3974328d808 263 distance = -1;
ftagius 3:e3974328d808 264 energy = -1;
ftagius 3:e3974328d808 265 IntLightning.enable_irq();
ftagius 3:e3974328d808 266 }
ftagius 3:e3974328d808 267
ftagius 0:3328df4c3116 268 c = gpsd.read(); //queries the GPS
ftagius 0:3328df4c3116 269 if (debug)
ftagius 0:3328df4c3116 270 {
ftagius 0:3328df4c3116 271 if (c) {
ftagius 0:3328df4c3116 272 printf("%c", c); //this line will echo the GPS data if not paused
ftagius 0:3328df4c3116 273 continue;
ftagius 0:3328df4c3116 274 }
ftagius 0:3328df4c3116 275 }
ftagius 0:3328df4c3116 276
ftagius 0:3328df4c3116 277 //check if we recieved a new message from GPS, if so, attempt to parse it,
ftagius 0:3328df4c3116 278 if ( gpsd.newNMEAreceived() ) {
ftagius 0:3328df4c3116 279 if ( !gpsd.parse(gpsd.lastNMEA()) ) {
ftagius 0:3328df4c3116 280 continue;
ftagius 0:3328df4c3116 281 }
ftagius 0:3328df4c3116 282 }
ftagius 0:3328df4c3116 283
ftagius 0:3328df4c3116 284 // update globals with the lastest gps time stamp
ftagius 0:3328df4c3116 285 day=gpsd.day;
ftagius 0:3328df4c3116 286 month=gpsd.month;
ftagius 0:3328df4c3116 287 year=gpsd.year;
ftagius 0:3328df4c3116 288 hour=gpsd.hour;
ftagius 0:3328df4c3116 289 minute=gpsd.minute;
ftagius 0:3328df4c3116 290 seconds=gpsd.seconds;
ftagius 0:3328df4c3116 291
ftagius 0:3328df4c3116 292 //check if enough time has passed to warrant printing GPS info to screen
ftagius 0:3328df4c3116 293 //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing
ftagius 0:3328df4c3116 294 if (refresh_Timer.read_ms() >= refresh_Time)
ftagius 0:3328df4c3116 295 {
ftagius 0:3328df4c3116 296 if (gpsd.fix) {
ftagius 0:3328df4c3116 297 // got a gps fix
ftagius 0:3328df4c3116 298 if (gpsFix == false)
ftagius 0:3328df4c3116 299 {
ftagius 0:3328df4c3116 300 // first time fix obtained
ftagius 0:3328df4c3116 301 gpsFix = true;
ftagius 0:3328df4c3116 302 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 303 //pc.printf("Touch slider to suspend application\r\n");
ftagius 0:3328df4c3116 304 pc.printf("Waiting for lighting detection...\r\n");
ftagius 0:3328df4c3116 305 }
ftagius 0:3328df4c3116 306
ftagius 0:3328df4c3116 307 //red = 1; // turn led off
ftagius 0:3328df4c3116 308 //pc.printf("turn green on\r\n");
ftagius 0:3328df4c3116 309 green = 0; // turn led on
ftagius 0:3328df4c3116 310 wait_ms(50);
ftagius 0:3328df4c3116 311 }
ftagius 0:3328df4c3116 312 else
ftagius 0:3328df4c3116 313 {
ftagius 3:e3974328d808 314 gpsFix = false;
ftagius 0:3328df4c3116 315 pc.printf("Waiting for GPS FIX\r\n");
ftagius 0:3328df4c3116 316 red = 0; // turn led on
ftagius 0:3328df4c3116 317 }
ftagius 0:3328df4c3116 318
ftagius 0:3328df4c3116 319 // restart the timer for the gps print loop
ftagius 0:3328df4c3116 320 // writeLogFile(-2);
ftagius 0:3328df4c3116 321 refresh_Timer.reset();
ftagius 0:3328df4c3116 322 }
ftagius 0:3328df4c3116 323 else
ftagius 0:3328df4c3116 324 {
ftagius 0:3328df4c3116 325 //red = 0; // turn led on
ftagius 0:3328df4c3116 326 //pc.printf("turn green off\r\n");
ftagius 0:3328df4c3116 327 green = 1; // turn green led off
ftagius 0:3328df4c3116 328 } // end else refresh timer
ftagius 0:3328df4c3116 329 }
ftagius 0:3328df4c3116 330
ftagius 0:3328df4c3116 331 }