diff --git a/ESP32/calibration/adc-approx.ods b/ESP32/calibration/adc-approx.ods new file mode 100644 index 0000000..8d54846 Binary files /dev/null and b/ESP32/calibration/adc-approx.ods differ diff --git a/ESP32/calibration/adc-measures.ods b/ESP32/calibration/adc-measures.ods new file mode 100644 index 0000000..2261e2a Binary files /dev/null and b/ESP32/calibration/adc-measures.ods differ diff --git a/ESP32/vehicle-monitor.cpp b/ESP32/vehicle-monitor.cpp index 3523be8..ef5c742 100644 --- a/ESP32/vehicle-monitor.cpp +++ b/ESP32/vehicle-monitor.cpp @@ -9,8 +9,44 @@ AsyncWebServer server(80); +float averageBatteryVoltage = 0.0f; int16_t batteryVoltage = -1; //in mV +struct ADC_CalibrationValue +{ + float Measure; + int16_t ADC_Value; +}; + +const ADC_CalibrationValue calibration[] = { + { 0.118f, 1 }, + { 0.343f, 253 }, + { 0.791f, 801 }, + { 1.512f, 1705 }, + { 2.013f, 2306 }, + { 2.406f, 2796 }, + { 2.606f, 3058 }, + { 2.839f, 3423 }, + { 2.996f, 3726 }, + { 3.16f, 4094 } +}; +const int8_t calibrationNumValues = sizeof(calibration)/sizeof(calibration[0]); + +float getCalibratedVoltage(int16_t adcOutput) +{ + for(int8_t i = 1; i < calibrationNumValues; ++i) + { + if(i == calibrationNumValues - 1 || calibration[i].ADC_Value >= adcOutput) + { + const auto& p = calibration[i - 1]; + const auto& n = calibration[i]; + return (float)(adcOutput - p.ADC_Value) / (float)(n.ADC_Value - p.ADC_Value) * (n.Measure - p.Measure) + p.Measure; + } + } + + return -1.0f; +} + void setup() { Serial.begin(115200); @@ -50,13 +86,22 @@ void setup() Serial.println("HTTP server started"); } +float avgAnalogV = 0.0f; + void loop() { const int potPin = 34; - delay(1000); + const float minV = 0.14f; // 1 + const float maxV = 3.16f; // 4094 + + delay(10); int16_t analogV = analogRead(potPin); - float v = (float)analogV / 4096.0f * 3.3f; - batteryVoltage = (int16_t)(v * 1000.0f + 0.5f); - //Serial.println(potValue); + float v = getCalibratedVoltage(analogV); + + averageBatteryVoltage = averageBatteryVoltage * 0.95f + v * 0.05f; + batteryVoltage = (int16_t)(averageBatteryVoltage * 1000.0f + 0.5f); + + //avgAnalogV = avgAnalogV * 0.9f + (float)analogV * 0.1f; + //batteryVoltage = (int16_t)(avgAnalogV + 0.5f); } diff --git a/WebApp/src/main-page.tsx b/WebApp/src/main-page.tsx index 92dd795..7aafc25 100644 --- a/WebApp/src/main-page.tsx +++ b/WebApp/src/main-page.tsx @@ -23,8 +23,8 @@ export default class MainPage { view() { return this.status ?
-

Tension batterie : {this.status.batteryVoltage.toFixed(1)}V

-

Courant : {this.status.motorCurrent.toFixed(1)}A

+

Tension batterie : {this.status.batteryVoltage.toFixed(3)}V

+

Courant : {this.status.motorCurrent.toFixed(3)}A

Puissance : {(this.status.batteryVoltage * this.status.motorCurrent).toFixed(1)}W

:

Chargement...

;