Browse Source

added ADC calibration code

master
Youen Toupin 3 years ago
parent
commit
292b7d7f5a
  1. BIN
      ESP32/calibration/adc-approx.ods
  2. BIN
      ESP32/calibration/adc-measures.ods
  3. 53
      ESP32/vehicle-monitor.cpp
  4. 4
      WebApp/src/main-page.tsx

BIN
ESP32/calibration/adc-approx.ods

Binary file not shown.

BIN
ESP32/calibration/adc-measures.ods

Binary file not shown.

53
ESP32/vehicle-monitor.cpp

@ -9,8 +9,44 @@
AsyncWebServer server(80); AsyncWebServer server(80);
float averageBatteryVoltage = 0.0f;
int16_t batteryVoltage = -1; //in mV 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() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@ -50,13 +86,22 @@ void setup()
Serial.println("HTTP server started"); Serial.println("HTTP server started");
} }
float avgAnalogV = 0.0f;
void loop() void loop()
{ {
const int potPin = 34; 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); int16_t analogV = analogRead(potPin);
float v = (float)analogV / 4096.0f * 3.3f; float v = getCalibratedVoltage(analogV);
batteryVoltage = (int16_t)(v * 1000.0f + 0.5f);
//Serial.println(potValue); 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);
} }

4
WebApp/src/main-page.tsx

@ -23,8 +23,8 @@ export default class MainPage {
view() { view() {
return this.status return this.status
? <div> ? <div>
<p>Tension batterie : {this.status.batteryVoltage.toFixed(1)}V</p> <p>Tension batterie : {this.status.batteryVoltage.toFixed(3)}V</p>
<p>Courant : {this.status.motorCurrent.toFixed(1)}A</p> <p>Courant : {this.status.motorCurrent.toFixed(3)}A</p>
<p>Puissance : {(this.status.batteryVoltage * this.status.motorCurrent).toFixed(1)}W</p> <p>Puissance : {(this.status.batteryVoltage * this.status.motorCurrent).toFixed(1)}W</p>
</div> </div>
: <p>Chargement...</p>; : <p>Chargement...</p>;

Loading…
Cancel
Save