First complete (Basic!) SD logging

This commit is contained in:
Josh Stewart 2021-10-13 17:10:05 +11:00
parent f62195d511
commit ef59fbabf0
5 changed files with 113 additions and 71 deletions

View File

@ -1302,17 +1302,18 @@ page = 13
onboard_log_trigger_prot = bits, U08, 117, [4:4], "Disabled", "Enabled"
onboard_log_trigger_Vbat = bits, U08, 117, [5:5], "Disabled", "Enabled"
onboard_log_trigger_Epin = bits, U08, 117, [6:7], "Disabled", "polling", "toggle" , "INVALID"
onboard_log_tr2_thr_on = scalar, U08, 118, "RPM", 100.0, 0.0, 0, 10000, 0
onboard_log_tr2_thr_off = scalar, U08, 119, "RPM", 100.0, 0.0, 0, 10000, 0
onboard_log_tr3_thr_RPM = bits, U08, 120, [0:0], "Disabled", "Enabled"
onboard_log_tr3_thr_MAP = bits, U08, 120, [1:1], "Disabled", "Enabled"
onboard_log_tr3_thr_Oil = bits, U08, 120, [2:2], "Disabled", "Enabled"
onboard_log_tr3_thr_AFR = bits, U08, 120, [3:3], "Disabled", "Enabled"
onboard_log_tr4_thr_on = scalar, U08, 121, "V", 0.1, 0.0, 0.0, 15.90, 2 ; * ( 1 byte)
onboard_log_tr4_thr_off = scalar, U08, 122, "V", 0.1, 0.0, 0.0, 14.90, 2 ; * ( 1 byte)
onboard_log_tr5_thr_on = scalar, U08, 123, "pin", 1.0, 0.0, 0.0, 255, 0 ;
onboard_log_tr1_duration = scalar, U16, 118, "s", 1.000, 0.0, 0, 65000, 0
onboard_log_tr2_thr_on = scalar, U08, 120, "RPM", 100.0, 0.0, 0, 10000, 0
onboard_log_tr2_thr_off = scalar, U08, 121, "RPM", 100.0, 0.0, 0, 10000, 0
onboard_log_tr3_thr_RPM = bits, U08, 122, [0:0], "Disabled", "Enabled"
onboard_log_tr3_thr_MAP = bits, U08, 122, [1:1], "Disabled", "Enabled"
onboard_log_tr3_thr_Oil = bits, U08, 122, [2:2], "Disabled", "Enabled"
onboard_log_tr3_thr_AFR = bits, U08, 122, [3:3], "Disabled", "Enabled"
onboard_log_tr4_thr_on = scalar, U08, 123, "V", 0.1, 0.0, 0.0, 15.90, 2 ; * ( 1 byte)
onboard_log_tr4_thr_off = scalar, U08, 124, "V", 0.1, 0.0, 0.0, 14.90, 2 ; * ( 1 byte)
onboard_log_tr5_thr_on = scalar, U08, 125, "pin", 1.0, 0.0, 0.0, 255, 0 ;
unused12_125_127 = array, U08, 124, [4], "%", 1.0, 0.0, 0.0, 255, 0
unused12_125_127 = array, U08, 126, [2], "%", 1.0, 0.0, 0.0, 255, 0
;--------------------------------------------------
;Second ignition map (Page 14)
@ -1563,7 +1564,8 @@ page = 14
defaultValue = onboard_log_trigger_RPM, 0
defaultValue = onboard_log_trigger_prot, 0
defaultValue = onboard_log_trigger_Vbat, 0
defaultValue = onboard_log_trigger_Epin, 0
defaultValue = onboard_log_trigger_Epin, 0
defaultValue = onboard_log_tr1_duration, 60
defaultValue = onboard_log_tr2_thr_on, 600
defaultValue = onboard_log_tr2_thr_off,400
defaultValue = onboard_log_tr3_thr_RPM, 0
@ -2169,6 +2171,7 @@ menuDialog = main
onboard_log_trigger_prot = "[engine protection] the logger is started one of the corresponding bits in the engine protection is set"
onboard_log_trigger_Vbat = "[Battery Voltage] the logger is started when the battery voltage is above the on treshold and stopped below the off threshold"
onboard_log_trigger_Epin = "[Ext pin [polling]] the logger is running for as long the chosen pin is high and stopped when it is low. When set to [Ext pin [toggle]] the logger is toggled on/off with a pulse on this chosen pin number"
onboard_log_tr1_duration = "When logging from boot is enabled, this is the duration the log will run for"
onboard_log_tr2_thr_on = "When the engine RPM is above this threshold the datalogger is started"
onboard_log_tr2_thr_off = "When the engine RPM is below this threshold the datalogger is stopped"
onboard_log_tr3_thr_RPM = "When the bits of the RPM engine protetion function are set the datalogger is started when no set anymore the logger is stopped"
@ -3950,9 +3953,9 @@ menuDialog = main
dialog = onboard_log_basic_setup, "Basics"
field = "Logger type", onboard_log_file_style
field = "CSV separator", onboard_log_csv_separator {onboard_log_file_style == 1}
;field = "CSV separator", onboard_log_csv_separator {onboard_log_file_style == 1}
field = "Log rate", onboard_log_file_rate {onboard_log_file_style}
field = "Filename", onboard_log_filenaming {onboard_log_file_style}
;field = "Filename", onboard_log_filenaming {onboard_log_file_style}
; field = "Storage", onboard_log_storage {onboard_log_file_style}
dialog = onboard_log_trigger_boot, "On boot"
@ -3985,6 +3988,7 @@ menuDialog = main
dialog = onboard_log_trigger, "Triggers"
field = "On Boot", onboard_log_trigger_boot
field = "On Boot log duration", onboard_log_tr1_duration, {onboard_log_file_style && onboard_log_trigger_boot}
field = "Above RPM", onboard_log_trigger_RPM
field = "With Engine Protection", onboard_log_trigger_prot
field = "With battery", onboard_log_trigger_Vbat

View File

@ -45,7 +45,8 @@ uint8_t SD_status = SD_STATUS_OFF;
void initSD();
void writeSDLogEntry();
void writetSDLogHeader();
void endSD();
void beginSDLogging();
void endSDLogging();
void setTS_SD_status();
void formatExFat();
bool createLogFile();

View File

@ -11,7 +11,7 @@ void initSD()
SD_status = SD_STATUS_READY;
//Set the RTC callback. This is used to set the correct timestamp on file creation and sync operations
//FsDateTime::setCallback(dateTime);
FsDateTime::setCallback(dateTime);
// Initialize the SD.
if (!sd.begin(SD_CONFIG))
@ -60,7 +60,33 @@ bool createLogFile()
return returnValue;
}
void endSD()
void beginSDLogging()
{
if(SD_status == SD_STATUS_READY)
{
SD_status = SD_STATUS_ACTIVE; //Set the status as being active so that entries will beging to be written. This will be updated below if there is an error
// Open or create file - truncate existing file.
if (!createLogFile())
{
SD_status = SD_STATUS_ERROR_NO_WRITE;
}
//Perform pre-allocation on card. This dramatically inproves write speed
if (!logFile.preAllocate(SD_LOG_FILE_SIZE))
{
SD_status = SD_STATUS_ERROR_NO_SPACE;
}
//initialize the RingBuf.
rb.begin(&logFile);
//Write a header row
writeSDLogHeader();
}
}
void endSDLogging()
{
if(SD_status > 0)
{
@ -70,20 +96,17 @@ void endSD()
logFile.rewind();
logFile.close();
SD_status = SD_STATUS_OFF;
setTS_SD_status();
SD_status = SD_STATUS_READY;
}
}
void writeSDLogEntry()
{
//uint8_t logEntry[LOG_ENTRY_SIZE];
//Check if we're already running a log
if(SD_status == SD_STATUS_READY)
{
//Log not currently running, check if it should be
checkForSDStart();
}
if(SD_status == SD_STATUS_ACTIVE)
@ -95,46 +118,39 @@ void writeSDLogEntry()
}
rb.println("");
//Check if write to SD from ringbuffer is needed
//We write to SD when there is more than 1 sector worth of data in the ringbuffer and there is not already a write being performed
if( (rb.bytesUsed() >= SD_SECTOR_SIZE) && !logFile.isBusy() )
{
uint16_t bytesWritten = rb.writeOut(SD_SECTOR_SIZE);
//Make sure that the entire sector was written successfully
if (SD_SECTOR_SIZE != bytesWritten)
{
SD_status = SD_STATUS_ERROR_WRITE_FAIL;
}
}
//Check whether we should stop logging
checkForSDStop();
}
//Check if write to SD from ringbuffer is needed
//We write to SD when there is more than 1 sector worth of data in the ringbuffer and there is not already a write being performed
if( (rb.bytesUsed() >= SD_SECTOR_SIZE) && !logFile.isBusy() )
{
uint16_t bytesWritten = rb.writeOut(SD_SECTOR_SIZE);
//Make sure that the entire sector was written successfully
if (SD_SECTOR_SIZE != bytesWritten)
{
SD_status = SD_STATUS_ERROR_WRITE_FAIL;
setTS_SD_status();
}
}
setTS_SD_status();
}
void writeSDLogHeader()
{
if(SD_status == SD_STATUS_READY)
for(byte x=0; x<SD_LOG_NUM_FIELDS; x++)
{
for(byte x=0; x<SD_LOG_NUM_FIELDS; x++)
//for(byte x=0; x<4; x++)
{
#ifdef CORE_AVR
//This will probably never be used
char buffer[30];
strcpy_P(buffer, (char *)pgm_read_word(&(header_table[x])));
rb.print(buffer);
#else
rb.print(header_table[x]);
#endif
if(x < (SD_LOG_NUM_FIELDS - 1)) { rb.print(","); }
}
rb.println("");
#ifdef CORE_AVR
//This will probably never be used
char buffer[30];
strcpy_P(buffer, (char *)pgm_read_word(&(header_table[x])));
rb.print(buffer);
#else
rb.print(header_table[x]);
#endif
if(x < (SD_LOG_NUM_FIELDS - 1)) { rb.print(","); }
}
SD_status = SD_STATUS_ACTIVE;
rb.println("");
}
//Sets the status variable for TunerStudio
@ -158,28 +174,41 @@ void setTS_SD_status()
void checkForSDStart()
{
//Logging can only start if we're in the ready state
if(SD_status == SD_STATUS_READY)
//We must check the SD_status each time to prevent trying to init a new log file multiple times
//Check for enable at boot
if( (configPage13.onboard_log_trigger_boot) && (SD_status == SD_STATUS_READY) )
{
// Open or create file - truncate existing file.
if (!createLogFile())
//Check that we're not already finished the logging
if((millis() / 1000) < configPage13.onboard_log_tr1_duration)
{
SD_status = SD_STATUS_ERROR_NO_WRITE;
}
beginSDLogging(); //Setup the log file, prallocation, header row
}
}
//Perform pre-allocation on card. This dramatically inproves write speed
if (!logFile.preAllocate(SD_LOG_FILE_SIZE))
//Check for RPM based Enable
if( (configPage13.onboard_log_trigger_RPM) && (SD_status == SD_STATUS_READY) )
{
if(currentStatus.RPMdiv100 >= configPage13.onboard_log_tr2_thr_on)
{
SD_status = SD_STATUS_ERROR_NO_SPACE;
beginSDLogging(); //Setup the log file, prallocation, header row
}
}
//initialize the RingBuf.
rb.begin(&logFile);
//Check for engine protection based enable
if((configPage13.onboard_log_trigger_prot) && (SD_status == SD_STATUS_READY) )
{
}
if( (configPage13.onboard_log_trigger_Vbat) && (SD_status == SD_STATUS_READY) )
{
}
if(( configPage13.onboard_log_trigger_Epin) && (SD_status == SD_STATUS_READY) )
{
//Write a header row
if(SD_status == SD_STATUS_READY)
{
writeSDLogHeader();
}
}
}
@ -188,10 +217,18 @@ void checkForSDStart()
*/
void checkForSDStop()
{
//Logging can only start if we're in the ready state
//Logging only needs to be stopped if already active
if(SD_status == SD_STATUS_ACTIVE)
{
//Check for enable at boot
if(configPage13.onboard_log_trigger_boot)
{
//Check if we're past the logging duration
if((millis() / 1000) > configPage13.onboard_log_tr1_duration)
{
endSDLogging(); //Setup the log file, prallocation, header row
}
}
}
}

View File

@ -1402,6 +1402,7 @@ struct config13 {
byte onboard_log_trigger_prot :1; // "Disabled", "Enabled"
byte onboard_log_trigger_Vbat :1; // "Disabled", "Enabled"
byte onboard_log_trigger_Epin :2; // "Disabled", "polling", "toggle" , "INVALID"
uint16_t onboard_log_tr1_duration; // Duration of logging that starts on boot
byte onboard_log_tr2_thr_on; // "RPM", 100.0, 0.0, 0, 10000, 0
byte onboard_log_tr2_thr_off; // "RPM", 100.0, 0.0, 0, 10000, 0
byte onboard_log_tr3_thr_RPM :1; // "Disabled", "Enabled"
@ -1413,7 +1414,7 @@ struct config13 {
byte onboard_log_tr5_thr_on; // "pin", 0, 0, 0, 1, 255, 0 ;
byte unused12_125_127[4];
byte unused12_125_127[2];
#if defined(CORE_AVR)
};

View File

@ -315,7 +315,6 @@ void loop()
#ifdef SD_LOGGING
if(configPage13.onboard_log_file_rate == LOGGER_RATE_4HZ) { writeSDLogEntry(); }
if(currentStatus.secl == 10) { endSD(); }
#endif
currentStatus.fuelPressure = getFuelPressure();