diff --git a/ESP32/platformio.ini b/ESP32/platformio.ini index 67d99bd..5c5f4a3 100644 --- a/ESP32/platformio.ini +++ b/ESP32/platformio.ini @@ -13,5 +13,8 @@ platform = espressif32 board = nodemcu-32s framework = arduino monitor_speed = 115200 -upload_protocol = espota -upload_port = 192.168.1.166 +#upload_protocol = espota +#upload_port = 192.168.1.166 +upload_speed = 921600 +build_type = debug +monitor_filters = esp32_exception_decoder diff --git a/ESP32/src/ADC.cpp b/ESP32/src/ADC.cpp index dc3a86b..cae39a4 100644 --- a/ESP32/src/ADC.cpp +++ b/ESP32/src/ADC.cpp @@ -1,5 +1,7 @@ #include "ADC.h" +#include + namespace { const int8_t calibrationNumValues = sizeof(ADC_calibration)/sizeof(ADC_calibration[0]); diff --git a/ESP32/src/DataLogger.cpp b/ESP32/src/DataLogger.cpp index a03b632..9ec7f29 100644 --- a/ESP32/src/DataLogger.cpp +++ b/ESP32/src/DataLogger.cpp @@ -4,6 +4,8 @@ #include +#include + DataLogger DataLogger::mainLogger; DataLogger::DataLogger() @@ -28,24 +30,26 @@ void DataLogger::open() preferences.end(); // clear existing files until we have enough free space - const size_t requiredSpace = 300000; // in bytes - size_t totalSpace = SPIFFS.totalBytes(); - auto logFolder = SPIFFS.open("/log"); - auto file = logFolder.openNextFile(); - while(file && SPIFFS.usedBytes() + requiredSpace > totalSpace) { - Serial.print("Deleting old log file: "); - Serial.println(file.name()); - - SPIFFS.remove(file.name()); - file = logFolder.openNextFile(); + const unsigned long requiredSpace = 300000; // in bytes + unsigned long totalSpace = SPIFFS.totalBytes(); + auto logFolder = SPIFFS.open("/log"); + auto oldFile = logFolder.openNextFile(); + while(oldFile && SPIFFS.usedBytes() + requiredSpace > totalSpace) + { + Serial.print("Deleting old log file: "); + Serial.println(oldFile.name()); + + SPIFFS.remove(oldFile.name()); + oldFile = logFolder.openNextFile(); + } } char fileName[32]; sprintf(fileName, "/log/L%05d.csv", logFileIndex); file = SPIFFS.open(fileName, FILE_WRITE); - if(!file) Serial.println("DataLogger: failed to open file"); + if(!isOpen()) Serial.println("DataLogger: failed to open file"); if(!file.print("time,speed,battery voltage,battery output current\n")) Serial.println("DataLogger: failed to write to file"); } diff --git a/ESP32/src/IDECompat.h b/ESP32/src/IDECompat.h deleted file mode 100644 index b11d24d..0000000 --- a/ESP32/src/IDECompat.h +++ /dev/null @@ -1,5 +0,0 @@ -#include - -#ifndef IRAM_ATTR -#define IRAM_ATTR -#endif diff --git a/ESP32/src/utils.cpp b/ESP32/src/utils.cpp index 0101de0..0332864 100644 --- a/ESP32/src/utils.cpp +++ b/ESP32/src/utils.cpp @@ -1,8 +1,10 @@ #include "utils.h" +#include + namespace utils { - unsigned long elapsed(unsigned long from, unsigned long to) + unsigned long IRAM_ATTR elapsed(unsigned long from, unsigned long to) { if(to >= from) { diff --git a/ESP32/src/vehicle-monitor.cpp b/ESP32/src/vehicle-monitor.cpp index 56a91c5..fe064db 100644 --- a/ESP32/src/vehicle-monitor.cpp +++ b/ESP32/src/vehicle-monitor.cpp @@ -1,5 +1,4 @@ #include -#include "IDECompat.h" #include #include @@ -21,7 +20,7 @@ AsyncWebServer server(80); ADC currentSensor(36); ADC batterySensor(39); const int8_t speedSensorPin = 13; -const int8_t debugLedPin = 12; +const int8_t debugLedPin = 2; const float wheelDiameterInches = 20; const int numImpulsesPerTurn = 2; @@ -58,19 +57,22 @@ void IRAM_ATTR onSpeedSensorChange(bool newState) else { unsigned long impulseDuration = utils::elapsed(speedSensorRiseTime, now); - if(impulseDuration > 1000) return; // impulse was too long, ignore it (maybe magnet stopped near the sensor) - - debugLedState = !debugLedState; - digitalWrite(debugLedPin, debugLedState ? HIGH : LOW); + if(impulseDuration > 500) return; // impulse was too long, ignore it (maybe magnet stopped near the sensor) unsigned long timeSinceLastImpulse = utils::elapsed(speedSensorLastImpulseTime, now); - speedSensorLastImpulseTime = now; - if(timeSinceLastImpulse > 30 && timeSinceLastImpulse < 4000) + if(timeSinceLastImpulse < 30) + { + // too little time between impulses, probably some bouncing, ignore it + } + else if(timeSinceLastImpulse < 4000) { + speedSensorLastImpulseTime = now; speedSensorLastImpulseInterval = timeSinceLastImpulse; } else { + // too much time between impulses, can't compute speed from that + speedSensorLastImpulseTime = now; speedSensorLastImpulseInterval = (unsigned long)-1; } } @@ -100,7 +102,10 @@ float getSpeed() float speed = wheelCircumferenceMeters / (float)interval * 1000.0f; // in meters per second - if(speed < 0.25f) return 0.0f; // if speed is very low (less than 1km/h) it probably means we've stopped + if(speed < 0.25f) + { + return 0.0f; // if speed is very low (less than 1km/h) it probably means we've stopped + } return speed; } @@ -125,12 +130,12 @@ void connectWifi() void setup() { + pinMode(debugLedPin, OUTPUT); + digitalWrite(debugLedPin, HIGH); + pinMode(speedSensorPin, INPUT_PULLUP); attachInterrupt(speedSensorPin, &onSpeedSensorChange, CHANGE); - pinMode(debugLedPin, OUTPUT); - digitalWrite(debugLedPin, debugLedState ? HIGH : LOW); - Serial.begin(115200); if(!SPIFFS.begin(false)){ @@ -211,6 +216,8 @@ void setup() server.begin(); Serial.println("HTTP server started"); + + digitalWrite(debugLedPin, LOW); } void handle_wifi_connection() @@ -277,7 +284,7 @@ void handle_ADC_measures() if(averageV < 0.2f) averageV = 0.0f; averageV *= 27.000f; // account for voltage divider to retrieve the input voltage - averageC = max(0.0f, averageC - 2.5f) / 0.0238f; // convert voltage to current, according to the sensor linear relation + averageC = std::max(0.0f, averageC - 2.5f) / 0.0238f; // convert voltage to current, according to the sensor linear relation batteryVoltage = (uint16_t)(averageV * 1000.0f + 0.5f); batteryOutputCurrent = (uint16_t)(averageC * 1000.0f + 0.5f); @@ -317,8 +324,11 @@ void loop() } else if(utils::elapsed(stoppedSince, now) > 5 * 60 * 1000) { - Serial.println("Stopping DataLogger"); - DataLogger::get().close(); + if(DataLogger::get().isOpen()) + { + Serial.println("Stopping DataLogger"); + DataLogger::get().close(); + } } } DataLogger::get().log(now, entry);