Compare commits

...

2 Commits

Author SHA1 Message Date
interfisch 9987a1693d add incremental logfile naming 2022-06-19 19:41:19 +02:00
interfisch 3301a3eaed add rtc 2022-06-19 15:20:49 +02:00
1 changed files with 116 additions and 26 deletions

View File

@ -4,6 +4,10 @@
unsigned long loopmillis;
unsigned long last_loopmillis;
#include <TimeLib.h> //for teensy rtc
time_t getTeensy3Time();
#include <OneButton.h>
#include <SPI.h>
@ -188,6 +192,7 @@ uint8_t controlmode=0;
#include <SD.h> //Format sd cart with FAT or FAT16
#define SDCHIPSELECT 16
boolean datalogging=true;
String datalogging_filename="UNKNOWN.txt";
void updateInputs(unsigned long loopmillis);
@ -196,20 +201,60 @@ void display_show_stats();
void display_show_stats2();
void display_show_stats3();
void display_show_menu();
time_t getTeensy3Time();
String addLeadingZero(int number);
String getCurrentDatestring();
void setup() {
Serial.begin(SERIAL_BAUD); //Debug and Program
Wire.begin();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Init. ESC"));
display.display(); // Show initial text
esc.init();
display.print(F("Init. SD.. ")); display.display();
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(SDCHIPSELECT)) {
Serial.println("Card failed, or not present");
display.print(F("Fail!")); display.display();
datalogging=false; //disable logging
delay(1000);
}else{
Serial.println("Card initialized.");
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);
@ -233,29 +278,29 @@ void setup() {
});
Wire.begin();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
display.print(F("Init. RTC.. ")); display.display();
setSyncProvider(getTeensy3Time); //See https://www.pjrc.com/teensy/td_libs_Time.html#teensy3
if (timeStatus()!= timeSet) {
Serial.println("Unable to sync with the RTC");
display.println(F("Fail")); display.display();
delay(1000);
} else {
Serial.println("RTC has set the system time");
display.println(F("OK")); display.display();
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.println(F("Radio Init"));
display.display(); // Show initial text
display.println(F("Init. NRF24")); display.display();
radio.begin();
Serial.println("RF24 set rate");
radio.setDataRate( RF24_250KBPS ); //set to slow data rate. default was 1MBPS
if (!radio.setDataRate( RF24_250KBPS )){ //set to slow data rate. default was 1MBPS
display.println(F(" Fail Data Rate!")); display.display();
}
//radio.setDataRate( RF24_1MBPS );
//Serial.println("set channel");
@ -272,12 +317,11 @@ void setup() {
Serial.println("start listening");
radio.startListening();
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.println(F("Started"));
display.println(F("Finished"));
display.display(); // Show initial text
}
@ -550,15 +594,36 @@ void loop() {
static unsigned long last_datalogging_write=0;
static boolean logging_headerWritten=false;
if (datalogging) {
#define LOGGINGINTERVAL 100
if (loopmillis-last_datalogging_write>LOGGINGINTERVAL)
{
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
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();
}
}
@ -744,7 +809,7 @@ void display_show_stats() {
static float mincurR=0;
mincurL=min(mincurL,esc.getFiltered_curL());
mincurR=min(mincurR,esc.getFiltered_curR());*/
display.print(F("Bat=")); display.print(esc.getFeedback_batVoltage()); display.print(F(" min=")); display.println(esc.getMinBatVoltage());
display.print(F("Temp=")); display.print(esc.getFeedback_boardTemp()); display.print(F(" max=")); display.println(esc.getMaxBoardTemp());
@ -753,6 +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("eff.=")); display.print(esc.getTrip()/esc.getCurrentConsumed(),0); display.println(F("m/Ah"));
display.print(F("RTC=")); display.print(getCurrentDatestring()); 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
@ -837,4 +909,22 @@ void display_show_menu() {
}
display.display(); // Show initial text
}
}
time_t getTeensy3Time()
{
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());
}