Browse Source

fixed bug with local variable shadowing a class member variable

fixed interrupt code that could run from flash (because flash cache is disabled during SPIFFS operations)
master
Youen Toupin 2 years ago
parent
commit
c9c5319d7e
  1. 7
      ESP32/platformio.ini
  2. 2
      ESP32/src/ADC.cpp
  3. 26
      ESP32/src/DataLogger.cpp
  4. 5
      ESP32/src/IDECompat.h
  5. 4
      ESP32/src/utils.cpp
  6. 40
      ESP32/src/vehicle-monitor.cpp

7
ESP32/platformio.ini

@ -13,5 +13,8 @@ platform = espressif32
board = nodemcu-32s board = nodemcu-32s
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
upload_protocol = espota #upload_protocol = espota
upload_port = 192.168.1.166 #upload_port = 192.168.1.166
upload_speed = 921600
build_type = debug
monitor_filters = esp32_exception_decoder

2
ESP32/src/ADC.cpp

@ -1,5 +1,7 @@
#include "ADC.h" #include "ADC.h"
#include <limits>
namespace namespace
{ {
const int8_t calibrationNumValues = sizeof(ADC_calibration)/sizeof(ADC_calibration[0]); const int8_t calibrationNumValues = sizeof(ADC_calibration)/sizeof(ADC_calibration[0]);

26
ESP32/src/DataLogger.cpp

@ -4,6 +4,8 @@
#include <Preferences.h> #include <Preferences.h>
#include <limits>
DataLogger DataLogger::mainLogger; DataLogger DataLogger::mainLogger;
DataLogger::DataLogger() DataLogger::DataLogger()
@ -28,24 +30,26 @@ void DataLogger::open()
preferences.end(); preferences.end();
// clear existing files until we have enough free space // 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: "); const unsigned long requiredSpace = 300000; // in bytes
Serial.println(file.name()); unsigned long totalSpace = SPIFFS.totalBytes();
auto logFolder = SPIFFS.open("/log");
SPIFFS.remove(file.name()); auto oldFile = logFolder.openNextFile();
file = 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]; char fileName[32];
sprintf(fileName, "/log/L%05d.csv", logFileIndex); sprintf(fileName, "/log/L%05d.csv", logFileIndex);
file = SPIFFS.open(fileName, FILE_WRITE); 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"); if(!file.print("time,speed,battery voltage,battery output current\n")) Serial.println("DataLogger: failed to write to file");
} }

5
ESP32/src/IDECompat.h

@ -1,5 +0,0 @@
#include <Arduino.h>
#ifndef IRAM_ATTR
#define IRAM_ATTR
#endif

4
ESP32/src/utils.cpp

@ -1,8 +1,10 @@
#include "utils.h" #include "utils.h"
#include <Arduino.h>
namespace utils 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) if(to >= from)
{ {

40
ESP32/src/vehicle-monitor.cpp

@ -1,5 +1,4 @@
#include <Arduino.h> #include <Arduino.h>
#include "IDECompat.h"
#include <WiFi.h> #include <WiFi.h>
#include <WiFiMulti.h> #include <WiFiMulti.h>
@ -21,7 +20,7 @@ AsyncWebServer server(80);
ADC currentSensor(36); ADC currentSensor(36);
ADC batterySensor(39); ADC batterySensor(39);
const int8_t speedSensorPin = 13; const int8_t speedSensorPin = 13;
const int8_t debugLedPin = 12; const int8_t debugLedPin = 2;
const float wheelDiameterInches = 20; const float wheelDiameterInches = 20;
const int numImpulsesPerTurn = 2; const int numImpulsesPerTurn = 2;
@ -58,19 +57,22 @@ void IRAM_ATTR onSpeedSensorChange(bool newState)
else else
{ {
unsigned long impulseDuration = utils::elapsed(speedSensorRiseTime, now); unsigned long impulseDuration = utils::elapsed(speedSensorRiseTime, now);
if(impulseDuration > 1000) return; // impulse was too long, ignore it (maybe magnet stopped near the sensor) if(impulseDuration > 500) return; // impulse was too long, ignore it (maybe magnet stopped near the sensor)
debugLedState = !debugLedState;
digitalWrite(debugLedPin, debugLedState ? HIGH : LOW);
unsigned long timeSinceLastImpulse = utils::elapsed(speedSensorLastImpulseTime, now); unsigned long timeSinceLastImpulse = utils::elapsed(speedSensorLastImpulseTime, now);
speedSensorLastImpulseTime = now; if(timeSinceLastImpulse < 30)
if(timeSinceLastImpulse > 30 && timeSinceLastImpulse < 4000) {
// too little time between impulses, probably some bouncing, ignore it
}
else if(timeSinceLastImpulse < 4000)
{ {
speedSensorLastImpulseTime = now;
speedSensorLastImpulseInterval = timeSinceLastImpulse; speedSensorLastImpulseInterval = timeSinceLastImpulse;
} }
else else
{ {
// too much time between impulses, can't compute speed from that
speedSensorLastImpulseTime = now;
speedSensorLastImpulseInterval = (unsigned long)-1; speedSensorLastImpulseInterval = (unsigned long)-1;
} }
} }
@ -100,7 +102,10 @@ float getSpeed()
float speed = wheelCircumferenceMeters / (float)interval * 1000.0f; // in meters per second 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; return speed;
} }
@ -125,12 +130,12 @@ void connectWifi()
void setup() void setup()
{ {
pinMode(debugLedPin, OUTPUT);
digitalWrite(debugLedPin, HIGH);
pinMode(speedSensorPin, INPUT_PULLUP); pinMode(speedSensorPin, INPUT_PULLUP);
attachInterrupt(speedSensorPin, &onSpeedSensorChange, CHANGE); attachInterrupt(speedSensorPin, &onSpeedSensorChange, CHANGE);
pinMode(debugLedPin, OUTPUT);
digitalWrite(debugLedPin, debugLedState ? HIGH : LOW);
Serial.begin(115200); Serial.begin(115200);
if(!SPIFFS.begin(false)){ if(!SPIFFS.begin(false)){
@ -211,6 +216,8 @@ void setup()
server.begin(); server.begin();
Serial.println("HTTP server started"); Serial.println("HTTP server started");
digitalWrite(debugLedPin, LOW);
} }
void handle_wifi_connection() void handle_wifi_connection()
@ -277,7 +284,7 @@ void handle_ADC_measures()
if(averageV < 0.2f) averageV = 0.0f; if(averageV < 0.2f) averageV = 0.0f;
averageV *= 27.000f; // account for voltage divider to retrieve the input voltage 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); batteryVoltage = (uint16_t)(averageV * 1000.0f + 0.5f);
batteryOutputCurrent = (uint16_t)(averageC * 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) else if(utils::elapsed(stoppedSince, now) > 5 * 60 * 1000)
{ {
Serial.println("Stopping DataLogger"); if(DataLogger::get().isOpen())
DataLogger::get().close(); {
Serial.println("Stopping DataLogger");
DataLogger::get().close();
}
} }
} }
DataLogger::get().log(now, entry); DataLogger::get().log(now, entry);

Loading…
Cancel
Save