added ADC calibration code
This commit is contained in:
parent
48d8ca4ac2
commit
292b7d7f5a
BIN
ESP32/calibration/adc-approx.ods
Normal file
BIN
ESP32/calibration/adc-approx.ods
Normal file
Binary file not shown.
BIN
ESP32/calibration/adc-measures.ods
Normal file
BIN
ESP32/calibration/adc-measures.ods
Normal file
Binary file not shown.
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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…
Reference in New Issue
Block a user