add incremental logfile naming

This commit is contained in:
interfisch 2022-06-19 19:41:19 +02:00
parent 3301a3eaed
commit 9987a1693d
1 changed files with 62 additions and 6 deletions

View File

@ -192,6 +192,7 @@ uint8_t controlmode=0;
#include <SD.h> //Format sd cart with FAT or FAT16 #include <SD.h> //Format sd cart with FAT or FAT16
#define SDCHIPSELECT 16 #define SDCHIPSELECT 16
boolean datalogging=true; boolean datalogging=true;
String datalogging_filename="UNKNOWN.txt";
void updateInputs(unsigned long loopmillis); void updateInputs(unsigned long loopmillis);
@ -201,6 +202,8 @@ void display_show_stats2();
void display_show_stats3(); void display_show_stats3();
void display_show_menu(); void display_show_menu();
time_t getTeensy3Time(); time_t getTeensy3Time();
String addLeadingZero(int number);
String getCurrentDatestring();
void setup() { void setup() {
Serial.begin(SERIAL_BAUD); //Debug and Program Serial.begin(SERIAL_BAUD); //Debug and Program
@ -238,6 +241,20 @@ void setup() {
}else{ }else{
Serial.println("Card initialized."); Serial.println("Card initialized.");
display.print(F("OK")); display.display(); display.print(F("OK")); display.display();
if (datalogging){
int filenumber=0;
char buffer[6];
sprintf(buffer, "%04d", filenumber);
datalogging_filename="LOG_"+String(buffer)+".TXT";
while(SD.exists(datalogging_filename) && filenumber<10000) {
Serial.print(datalogging_filename); Serial.println(" exists");
filenumber++;
sprintf(buffer, "%04d", filenumber);
datalogging_filename="LOG_"+String(buffer)+".TXT";
}
Serial.print(datalogging_filename); Serial.println(" is free");
}
} }
analogReadResolution(12); analogReadResolution(12);
@ -272,6 +289,7 @@ void setup() {
} else { } else {
Serial.println("RTC has set the system time"); Serial.println("RTC has set the system time");
display.println(F("OK")); display.display(); display.println(F("OK")); display.display();
} }
@ -576,15 +594,36 @@ void loop() {
static unsigned long last_datalogging_write=0; static unsigned long last_datalogging_write=0;
static boolean logging_headerWritten=false;
if (datalogging) { if (datalogging) {
#define LOGGINGINTERVAL 100 #define LOGGINGINTERVAL 100
if (loopmillis-last_datalogging_write>LOGGINGINTERVAL) if (loopmillis-last_datalogging_write>LOGGINGINTERVAL)
{ {
last_datalogging_write=loopmillis; last_datalogging_write=loopmillis;
File dataFile = SD.open("datalog.txt", FILE_WRITE);
File dataFile = SD.open(datalogging_filename, FILE_WRITE);
if (dataFile) { // if the file is available, write to it if (dataFile) { // if the file is available, write to it
dataFile.println("TEST asdf"); if (!logging_headerWritten) {
dataFile.print("time,cmd_L,cmd_R,");
dataFile.print("current_L,current_R,");
dataFile.print("rpm_L,rpm_R,");
dataFile.print("temp,vbat,");
dataFile.println("trip,currentConsumed");
logging_headerWritten=true;
}
dataFile.print(String(loopmillis)); dataFile.print(";");
dataFile.print(esc.getCmdL()); dataFile.print(";");
dataFile.print(esc.getCmdR()); dataFile.print(";");
dataFile.print(esc.getFiltered_curL(),3); dataFile.print(";");
dataFile.print(esc.getFiltered_curR(),3); dataFile.print(";");
dataFile.print(esc.getFeedback_speedL_meas()); dataFile.print(";");
dataFile.print(esc.getFeedback_speedR_meas()); dataFile.print(";");
dataFile.print(esc.getFeedback_boardTemp()); dataFile.print(";");
dataFile.print(esc.getFeedback_batVoltage()); dataFile.print(";");
dataFile.print(esc.getTrip()); dataFile.print(";");
dataFile.print(esc.getCurrentConsumed(),3);
dataFile.println("");
dataFile.close(); dataFile.close();
} }
} }
@ -779,8 +818,13 @@ void display_show_stats() {
display.print(F("trip=")); display.print(esc.getTrip(),0); display.print(F(", ")); display.print(esc.getCurrentConsumed(),3); display.println(F("Ah")); display.print(F("trip=")); display.print(esc.getTrip(),0); display.print(F(", ")); display.print(esc.getCurrentConsumed(),3); display.println(F("Ah"));
display.print(F("eff.=")); display.print(esc.getTrip()/esc.getCurrentConsumed(),0); display.println(F("m/Ah")); display.print(F("eff.=")); display.print(esc.getTrip()/esc.getCurrentConsumed(),0); display.println(F("m/Ah"));
display.print(F("RTC=")); display.print(now()); display.println(F("")); display.print(F("RTC=")); display.print(getCurrentDatestring()); display.println(F(""));
display.print(F("Logging=")); display.print(datalogging); display.println(F(""));
if (datalogging) {
display.print(datalogging_filename); display.print(F(" ")); display.print(loopmillis/1000); display.println(F("s"));
}else{
display.print("No SD"); display.println(F(""));
}
display.display(); // Show initial text display.display(); // Show initial text
@ -872,3 +916,15 @@ time_t getTeensy3Time()
{ {
return Teensy3Clock.get(); return Teensy3Clock.get();
} }
String addLeadingZero(int number) {
if (number<10) {
return "0"+String(number);
}
return String(number);
}
String getCurrentDatestring() {
return addLeadingZero(year())+addLeadingZero(month())+addLeadingZero(day())+"-"+addLeadingZero(hour())+addLeadingZero(minute())+addLeadingZero(second());
}