diff --git a/src/vario/IMU.cpp b/src/vario/IMU.cpp index 1494d8e..b6140c7 100644 --- a/src/vario/IMU.cpp +++ b/src/vario/IMU.cpp @@ -8,6 +8,8 @@ #include #include "IMU.h" #include "Leaf_I2C.h" +#include "log.h" +#include "SDcard.h" #define DEBUG_IMU 0 @@ -63,6 +65,12 @@ void imu_update() { Serial.print(" z: "); Serial.println(az); } + + if (logbook.dataFileStarted) { + String accelName = "accel,"; + String accelEntry = accelName + String(at); + SDcard_writeData(accelEntry); + } } float IMU_getAccel() { diff --git a/src/vario/SDcard.cpp b/src/vario/SDcard.cpp index 1317c89..4644baa 100644 --- a/src/vario/SDcard.cpp +++ b/src/vario/SDcard.cpp @@ -325,8 +325,41 @@ void SDcard_test(void) { } +// Create & log flight test data for debugging and tuning -// Creating and saving log files to SDCard +String dataSaveDir = "/Data/"; +String currentDataFile; +File dataFile; +String dataFileName; + +bool SDcard_createDataFile(String filename) { + dataFileName = filename; + createDir(SD_MMC, dataSaveDir.c_str()); + currentDataFile = dataSaveDir + filename; + + String header = "millis,sensor, value (lat), lng, alt m, speed mps, heading deg\n"; + + writeFile(SD_MMC, currentDataFile.c_str(), header.c_str()); + dataFile = SD_MMC.open(currentDataFile.c_str(), FILE_APPEND); + + // check if file was written properly, and return false if not + if (!dataFile) { + return false; + } else { + return true; + } +} + +void SDcard_writeData(String data) { + String entry = String(millis()) + "," + data + "\n"; + appendOpenFile(dataFile, entry.c_str()); +} + +void SDcard_closeDataFile() { + dataFile.close(); +} + +// Creating and saving track log files to SDCard String saveDir = "/Tracks/"; String currentTrackFile; diff --git a/src/vario/SDcard.h b/src/vario/SDcard.h index 90c90ea..6f40ffc 100644 --- a/src/vario/SDcard.h +++ b/src/vario/SDcard.h @@ -41,4 +41,8 @@ void appendOpenFile(File &file, const char * message); void SDcard_writeLogHeader(void); void SDcard_writeLogFooter(String trackName, String trackDescription); +bool SDcard_createDataFile(String filename); +void SDcard_writeData(String data); +void SDcard_closeDataFile(void); + #endif diff --git a/src/vario/baro.cpp b/src/vario/baro.cpp index de6774f..332984d 100644 --- a/src/vario/baro.cpp +++ b/src/vario/baro.cpp @@ -9,6 +9,8 @@ #include "Leaf_I2C.h" #include "tempRH.h" #include "buttons.h" +#include "log.h" +#include "SDcard.h" #define DEBUG_BARO 0 // flag for printing serial debugging messages @@ -27,12 +29,14 @@ LinearRegression pressure_lr; // probably gonna delete these + /* #define VARIO_SENSITIVITY 3 // not sure what this is yet :) #define PfilterSize 6 // pressure alt filter values (minimum 1, max 10) int32_t varioVals[25]; int32_t climbVals[31]; int32_t climbSecVals[9]; + */ // Baro Values BARO baro; // struct for common baro values we need all over the place @@ -141,17 +145,7 @@ // Device Management //Initialize the baro sensor - void baro_init(void) - { - // probably don't need these - uint8_t i=0; - // initialize averaging arrays - for (i=0;i<31;i++) { - if (i<25) varioVals[i] = 0; - if (i<9) climbSecVals[i]=0; - climbVals[i] = 0; - } - + void baro_init(void) { // recover saved altimeter setting if (ALT_SETTING > 28.0 && ALT_SETTING < 32.0) baro.altimeterSetting = ALT_SETTING; @@ -316,7 +310,6 @@ } // Device Management - // Device reading & data processing void baro_calculatePressure() { // calculate temperature (in 100ths of degrees C, from -4000 to 8500) @@ -350,8 +343,30 @@ // calculate temperature compensated pressure (in 100ths of mbars) baro.pressure = ((uint64_t)D1_P * SENS1 / (int64_t)pow(2,21) - OFF1)/pow(2,15); + + // record datapoint on SD card if datalogging is turned on + if (logbook.dataFileStarted) { + String baroName = "baro mb*100,"; + String baroEntry = baroName + String(baro.pressure); + SDcard_writeData(baroEntry); + } } + + // Filter Pressure Values + // + // To reduce noise, we perform a moving average of the last N data points. We create an array of datapoints pressureFilterVals[] with size FILTER_VALS_MAX + 1, + // saving one spot [0] for an index/bookmark to where the next value should be stored. + // The value of the bookmark rises from 1 up to FILTER_VALS_MAX, then wraps around to 1 again, overwriting old data. + // For proper behavior on startup, the each element of the array is loaded with the current (first) pressure reading. + // + // When filtering the pressure values, the most recent N values are summed up (from the bookmarked spot back to N spots before that), then divided by N to get the average. + // (where N is presently PRESSURE_FILTER_VALS_PREF == FILTER_VALS_MAX) + // + // TODO: Eventually we will support the user adjusting the sensitivity by reducing the size of PRESSURE_FILTER_VALS_PREF to average over fewer samples, if higher sensitivity (less noise reduction) is desired. + // + // NOTE: We have also explored performing a linear regeression on the last N samples to get a more accurate filtered value. This is still in testing, and not currently being used in the code. + // void baro_filterPressure(void) { // new way with regression: pressure_lr.update((double)millis(), (double)baro.alt); @@ -410,7 +425,7 @@ } baro.climbRateFiltered /= CLIMB_FILTER_VALS_PREF; // divide to get the filtered climb rate - // now calculate the longer-running average climb value + // now calculate the longer-running average climb value (this is a smoother, slower-changing value for things like glide ratio, etc) int32_t total_samples = CLIMB_AVERAGE * FILTER_VALS_MAX; // CLIMB_AVERAGE seconds * 20 samples per second = total samples to average over // current averaege * weighted by total samples (minus 1) + one more new sample / total samples diff --git a/src/vario/gps.cpp b/src/vario/gps.cpp index 3c1dd06..45d2af0 100644 --- a/src/vario/gps.cpp +++ b/src/vario/gps.cpp @@ -12,6 +12,7 @@ #include "settings.h" #include "log.h" #include "baro.h" +#include "SDcard.h" #define DEBUG_GPS 0 @@ -223,6 +224,20 @@ void gps_update() { gps_updateSatList(); gps_calculateGlideRatio(); + if (logbook.dataFileStarted) { + String gpsName = "gps,"; + String gpsEntryString = gpsName + + String(gps.location.lat(),8) + ',' + + String(gps.location.lng(),8) + ',' + + String(gps.altitude.meters()) + ',' + + String(gps.speed.mps()) + ',' + + String(gps.course.deg()); + + SDcard_writeData(gpsEntryString); + } + + + /* Serial.print("Valid: "); diff --git a/src/vario/log.cpp b/src/vario/log.cpp index d33a16b..41a9a5f 100644 --- a/src/vario/log.cpp +++ b/src/vario/log.cpp @@ -10,6 +10,8 @@ #include "SDcard.h" #include "tempRH.h" +bool DATAFILE = true; // set to false to disable data logging to SDcard + // Flight Timer uint32_t flightTimerSec = 0; bool flightTimerRunning = 0; @@ -66,6 +68,11 @@ void log_update() { } } + // start the data file logging if needed + if (DATAFILE && !logbook.dataFileStarted) { + logbook.dataFileStarted = SDcard_createDataFile(log_createTestDataFileName()); + } + uint32_t logSave_time = micros(); // save datapoints to the track @@ -227,6 +234,12 @@ void log_update() { SDcard_writeLogFooter(log_createTrackFileName(), log_createTrackDescription()); //TODO: other KML file additions; maybe adding description and min/max and other stuff } + + //if a flight data file was started + if (logbook.dataFileStarted) { + logbook.dataFileStarted = false; + SDcard_closeDataFile(); + } } flightTimerRunning = 0; } @@ -409,6 +422,7 @@ String log_getKMLCoordinates() { return logPointStr; } +// filename for Flight Track log file (GPS points every second) String log_createFileName() { String fileTitle = "FlightTrack"; String fileDate = String(gps_getLocalDate()); @@ -431,6 +445,21 @@ String log_createFileName() { return fileName; } +// file name for Test Data file (all flight sensor data saved in real time -- for debugging and tuning purposes) +String log_createTestDataFileName() { + String fileTitle = "TestData"; + String fileDate = String(gps_getLocalDate()); + int32_t timeInMinutes = (gps.time.hour()*60 + gps.time.minute() + 24*60 + TIME_ZONE) % (24*60); + uint8_t timeHours = timeInMinutes/60; + uint8_t timeMinutes = timeInMinutes % 60; + String fileTime = String(timeHours/10) + String(timeHours % 10) + String(timeMinutes / 10) + String(timeMinutes % 10); + + String fileName = fileTitle + "_" + fileDate + "_" + fileTime + ".csv"; + Serial.println(fileName); + + return fileName; +} + String log_createTrackFileName() { Serial.print("creating track file name..."); String trackname = "Flight Time: "; diff --git a/src/vario/log.h b/src/vario/log.h index fdde4f2..4363aeb 100644 --- a/src/vario/log.h +++ b/src/vario/log.h @@ -38,6 +38,9 @@ String log_createTrackFileName(void); String log_createTrackDescription(void); +// Test data file (all sensor values dumped realtime to csv) + String log_createTestDataFileName(void); + void log_captureValues(void); void log_checkMinMaxValues(void); bool flightTimer_autoStop(void); @@ -46,6 +49,7 @@ // baro struct to hold most values struct LOGBOOK { bool flightTrackStarted = false; + bool dataFileStarted = false; int32_t alt = 0; int32_t alt_start = 0;