From f5cfe91fcbd2f2e0c406478ac15545a738f09305 Mon Sep 17 00:00:00 2001 From: Fisch Date: Sat, 13 Jul 2024 19:19:10 +0200 Subject: [PATCH] add sensor serial command --- controller_teensy/include/definitions.h | 3 +- controller_teensy/include/display.h | 9 ++- controller_teensy/include/logging.h | 84 ++++++++++++++++++++----- controller_teensy/src/main.cpp | 53 +++------------- 4 files changed, 85 insertions(+), 64 deletions(-) diff --git a/controller_teensy/include/definitions.h b/controller_teensy/include/definitions.h index 149fb54..90d849b 100644 --- a/controller_teensy/include/definitions.h +++ b/controller_teensy/include/definitions.h @@ -70,7 +70,8 @@ bool control_buttonB=false; unsigned long loopmillis; -unsigned long looptime_duration; +unsigned long looptime_duration_min; +unsigned long looptime_duration_max; #define ADSREADPERIOD 3 //set slightly higher as actual read time to avoid unnecessary register query diff --git a/controller_teensy/include/display.h b/controller_teensy/include/display.h index ea34fb8..fb0b81e 100644 --- a/controller_teensy/include/display.h +++ b/controller_teensy/include/display.h @@ -318,7 +318,9 @@ void display_standingDisarmedDisplay(ESCSerialComm& escFront, ESCSerialComm& esc display.setFont(); display.setCursor(0,0); - if (datalogging) { + //Row1 + + if (getDatalogging()) { display.print(getLogFilename()); }else{ display.print("LOG DISABLED"); @@ -328,12 +330,15 @@ void display_standingDisarmedDisplay(ESCSerialComm& escFront, ESCSerialComm& esc display.print(F("s")); display.println(); + //Row2 + display.print(F("ESC F=")); display.print(escFront.getControllerConnected()); display.print(F(" R=")); display.print(escRear.getControllerConnected()); display.println(); + //Row3 display.print("throttle="); dtostrf(ads_throttle_A_raw,1,0,buf); @@ -343,6 +348,8 @@ void display_standingDisarmedDisplay(ESCSerialComm& escFront, ESCSerialComm& esc display.print((String)buf); display.println(); + //Row 4 + display.print("brake="); dtostrf(ads_brake_raw,1,0,buf); display.print((String)buf); diff --git a/controller_teensy/include/logging.h b/controller_teensy/include/logging.h index 6170732..15ed904 100644 --- a/controller_teensy/include/logging.h +++ b/controller_teensy/include/logging.h @@ -11,16 +11,21 @@ boolean sdcard_available=false; boolean datalogging=true; String datalogging_filename="UNKNOWN.txt"; +#define LOGGINGINTERVAL 100 + bool serialCommandEcho_Enabled=true; bool initLogging(); void loggingLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& escRear); + + void writeLogComment(unsigned long time, String msg); void printFileListing(); void printDirectory(File dir, int numTabs,String parent); void printFile(String filename); void removeFile(String filename); -void serialCommandLoop(); +void serialCommandLoop(unsigned long loopmillis, ESCSerialComm& escFront, ESCSerialComm& escRear); +float getBatteryVoltage(ESCSerialComm& escFront, ESCSerialComm& escRear); bool initLogging() { @@ -58,7 +63,7 @@ bool initLogging() { Serial.print(datalogging_filename); Serial.println(" is free"); display.print(datalogging_filename); if (!datalogging) { //datalogging is disabled during boot - display.print( "NO LOG"); + display.print(" OFF"); } display.println(); display.display(); @@ -71,23 +76,23 @@ void loggingLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm static unsigned long last_datalogging_write=0; static boolean logging_headerWritten=false; - if (datalogging && sdcard_available) { - #define LOGGINGINTERVAL 100 - if (loopmillis-last_datalogging_write>LOGGINGINTERVAL) - { - last_datalogging_write=loopmillis; - - File dataFile = SD.open(datalogging_filename.c_str(), FILE_WRITE); + + if (loopmillis-last_datalogging_write>LOGGINGINTERVAL) + { + last_datalogging_write=loopmillis; + if (datalogging && sdcard_available) { + + File dataFile = SD.open(datalogging_filename.c_str(), FILE_WRITE); if (dataFile) { // if the file is available, write to it if (!logging_headerWritten) { dataFile.print("time,cmd_FrontL,cmd_FrontR,cmd_RearL,cmd_RearR,"); dataFile.print("current_FrontL,current_FrontR,current_RearL,current_RearR,"); dataFile.print("rpm_FrontL,rpm_FrontR,rpm_RearL,rpm_RearR,"); - dataFile.print("temp_Front,temp_Rear,vbat_Front,vbat_Rear,"); + dataFile.print("temp_ESCFront,temp_ESCRear,vbat_Front,vbat_Rear,"); dataFile.print("currentAll,throttle,brake,speed,trip_Front,trip_Rear,trip,currentConsumed_Front,currentConsumed_Rear,currentConsumed,watthoursConsumed,"); - dataFile.println("temp_ESCFront,temp_ESCRear,temp_Air,looptime_duration"); + dataFile.println("temp_Front,temp_Rear,temp_Air,looptime_duration"); dataFile.print("#TIMESTAMP:"); dataFile.println(now()); logging_headerWritten=true; } @@ -122,8 +127,12 @@ void loggingLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm dataFile.print(temp_ESCFront,2); dataFile.print(","); dataFile.print(temp_ESCRear,2); dataFile.print(","); dataFile.print(temp_Air,2); dataFile.print(","); - dataFile.print(looptime_duration); //dataFile.print(","); - looptime_duration=0; //reset + dataFile.print(looptime_duration_min); dataFile.print(","); + dataFile.print(looptime_duration_max); //dataFile.print(","); + looptime_duration_max=0; //reset + looptime_duration_min=1000; //reset + + dataFile.println(""); dataFile.close(); @@ -131,6 +140,10 @@ void loggingLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm error_sdfile_unavailable=true; } } + + looptime_duration_max=0; //reset + looptime_duration_min=1000; //reset + } } @@ -154,6 +167,10 @@ String getLogFilename() { return datalogging_filename; } +bool getDatalogging() { + return datalogging; +} + bool loadTripSD() { //stats.txt containt lines with driven distance or consume current. @@ -222,13 +239,17 @@ void writeTrip(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& myFile.print("owh="); myFile.println(overallWatthoursConsumed); myFile.print("vlt="); - float _voltage=(escFront.getFeedback_batVoltage()+escRear.getFeedback_batVoltage())/2.0; + float _voltage=getBatteryVoltage(escFront, escRear); myFile.println(_voltage,2); myFile.close(); } } } +float getBatteryVoltage(ESCSerialComm& escFront, ESCSerialComm& escRear) { + return (escFront.getFeedback_batVoltage()+escRear.getFeedback_batVoltage())/2.0; +} + void resetTrip() { if (datalogging && sdcard_available) { File myFile = SD.open("stats.txt", FILE_WRITE); @@ -251,7 +272,7 @@ void resetTrip() { -void serialCommandLoop() { +void serialCommandLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& escRear) { #define MAX_MESSAGE_LENGTH 80 uint8_t timeoutcounter=MAX_MESSAGE_LENGTH; @@ -299,15 +320,46 @@ void serialCommandLoop() { }else if(smessage.equals("ls")) { printFileListing(); }else if(smessage.startsWith("cat")) { - printFile(smessage.substring(4)); + String _filename=smessage.substring(4); + if (_filename.length()<3){ //if no filename given, use current log file + _filename=getLogFilename(); + } + printFile(_filename); }else if(smessage.startsWith("rm")) { removeFile(smessage.substring(3)); }else if(smessage.equals("log off")) { + writeLogComment(loopmillis, "Datalogging disabled by serial command"); datalogging=false; Serial.print("Log disabled: "); Serial.println(datalogging_filename); }else if(smessage.equals("log on")) { datalogging=true; + writeLogComment(loopmillis, "Datalogging enabled by serial command"); Serial.print("Log enabled: "); Serial.println(datalogging_filename); + }else if(smessage.equals("show stats")) { + Serial.print("overallTrip="); Serial.println(overallTrip); + Serial.print("overallCurrentConsumed="); Serial.println(overallCurrentConsumed); + Serial.print("overallWatthoursConsumed="); Serial.println(overallWatthoursConsumed); + Serial.print("voltage="); Serial.println(getBatteryVoltage(escFront,escRear)); + }else if(smessage.equals("show sensors")) { + Serial.print("loopmillis(s) \t"); Serial.println(loopmillis/1000.0,3); + Serial.print("temp_ESCFront \t"); Serial.println(escFront.getFeedback_boardTemp()); + Serial.print("temp_ESCRear \t"); Serial.println(escRear.getFeedback_boardTemp()); + Serial.print("vbat_Front \t"); Serial.println(escFront.getFeedback_batVoltage()); + Serial.print("vbat_Rear \t"); Serial.println(escRear.getFeedback_batVoltage()); + Serial.print("throttle \t"); Serial.println(throttle_pos); + Serial.print("brake \t"); Serial.println(brake_pos); + Serial.print("trip_Front \t"); Serial.println(escFront.getTrip()); + Serial.print("trip_Rear \t"); Serial.println(escRear.getTrip()); + Serial.print("trip \t"); Serial.println(trip); + Serial.print("currentConsumed_Front \t"); Serial.println(escFront.getCurrentConsumed(),3); + Serial.print("currentConsumed_Rear \t"); Serial.println(escRear.getCurrentConsumed(),3); + Serial.print("currentConsumed \t"); Serial.println(currentConsumed,3); + Serial.print("watthoursConsumed \t"); Serial.println(watthoursConsumed,3); + Serial.print("temp_Front \t"); Serial.println(temp_ESCFront,3); + Serial.print("temp_Rear \t"); Serial.println(temp_ESCRear,3); + Serial.print("temp_Air \t"); Serial.println(temp_Air,3); + Serial.print("looptime_duration_min \t"); Serial.println(looptime_duration_min); + Serial.print("looptime_duration_max \t"); Serial.println(looptime_duration_max); } message_pos = 0; diff --git a/controller_teensy/src/main.cpp b/controller_teensy/src/main.cpp index 678a005..4e2be45 100644 --- a/controller_teensy/src/main.cpp +++ b/controller_teensy/src/main.cpp @@ -16,8 +16,9 @@ #include "led.h" #include "temperature.h" -//#include "comms.h" + String getLogFilename(); +bool getDatalogging(); #include "display.h" #include "logging.h" @@ -161,7 +162,6 @@ void setup() writeLogComment(millis(), "Setup Finished"); - led_simpeProgress(15,true); led_simpleProgressWait(); //wait longer if any errors were displayed with led_simpeProgress() @@ -176,46 +176,6 @@ void loop() { loopmillis=millis(); //read millis for this cycle - - - /* - // ____ Debugging pending serial byted for feedback - static int s2availmax=0; - int _a2=Serial2.available(); - if (_a2>s2availmax) { - s2availmax=_a2; - //Serial.print("new s2availmax"); Serial.println(s2availmax); - String _text="Serial2 Bytes Available Max="; - _text+=s2availmax; - writeLogComment(Serial1,loopmillis, _text); - } - - static int s3availmax=0; - int _a3=Serial3.available(); - if (_a3>s3availmax) { - s3availmax=_a3; - //Serial.print("new s3availmax"); Serial.println(s3availmax); - String _text="Serial3 Bytes Available Max="; - _text+=s3availmax; - writeLogComment(Serial1,loopmillis, _text); - } - // ----- End of debug - - - - bool newData2=ReceiveSerial(SerialcomFront,FeedbackFront, NewFeedbackFront, Serial2); - bool newData3=ReceiveSerial(SerialcomRear,FeedbackRear, NewFeedbackRear, Serial3); - //Max (40) or 22 available/pending bytes - - if (newData2) { - updateMotorparams(motorparamsFront,FeedbackFront,loopmillis); - } - if (newData3) { - updateMotorparams(motorparamsRear,FeedbackRear,loopmillis); - } - */ - - @@ -362,12 +322,13 @@ void loop() { } - - - serialCommandLoop(); + + + serialCommandLoop(loopmillis,escFront,escRear); - looptime_duration=max(looptime_duration,millis()-loopmillis); + looptime_duration_min=min(looptime_duration_min,millis()-loopmillis); + looptime_duration_max=max(looptime_duration_max,millis()-loopmillis); }