Skip to content

Commit

Permalink
Add raw data logging to SD card (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxothnk423 authored Dec 27, 2024
1 parent ebb4f51 commit 5963f01
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/vario/IMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <ICM_20948.h>
#include "IMU.h"
#include "Leaf_I2C.h"
#include "log.h"
#include "SDcard.h"

#define DEBUG_IMU 0

Expand Down Expand Up @@ -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() {
Expand Down
35 changes: 34 additions & 1 deletion src/vario/SDcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/vario/SDcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 28 additions & 13 deletions src/vario/baro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -27,12 +29,14 @@
LinearRegression<PRESSURE_FILTER_VALS_PREF> 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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -316,7 +310,6 @@
}
// Device Management


// Device reading & data processing
void baro_calculatePressure() {
// calculate temperature (in 100ths of degrees C, from -4000 to 8500)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/vario/gps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "settings.h"
#include "log.h"
#include "baro.h"
#include "SDcard.h"

#define DEBUG_GPS 0

Expand Down Expand Up @@ -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: ");
Expand Down
29 changes: 29 additions & 0 deletions src/vario/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
Expand All @@ -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: ";
Expand Down
4 changes: 4 additions & 0 deletions src/vario/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 5963f01

Please sign in to comment.