From 741dbc20427195b98befefe6f047b97cb34173ca Mon Sep 17 00:00:00 2001 From: breadoven <56191411+breadoven@users.noreply.github.com> Date: Mon, 7 Oct 2024 10:58:32 +0100 Subject: [PATCH] allow consecutive calibrations --- src/main/sensors/sensors.c | 13 +++++-------- src/main/sensors/sensors.h | 2 ++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/sensors/sensors.c b/src/main/sensors/sensors.c index e2da66f33ea..b6d4d4dda80 100644 --- a/src/main/sensors/sensors.c +++ b/src/main/sensors/sensors.c @@ -64,22 +64,19 @@ float applySensorTempCompensation(int16_t sensorTemp, float sensorMeasurement, s } if (setting == 51.0f) { // initiate auto calibration - static float referenceMeasurement = 0.0f; - static int16_t lastTemp = 0.0f; - if (sensor_comp_data[sensorType].referenceTemp == sensorTemp) { - referenceMeasurement = sensorMeasurement; - lastTemp = sensorTemp; + sensor_comp_data[sensorType].referenceMeasurement = sensorMeasurement; + sensor_comp_data[sensorType].lastTemp = sensorTemp; startTimeMs = millis(); } float referenceDeltaTemp = ABS(sensorTemp - sensor_comp_data[sensorType].referenceTemp); // centidegrees - if (referenceDeltaTemp > 300 && referenceDeltaTemp > ABS(lastTemp - sensor_comp_data[sensorType].referenceTemp)) { + if (referenceDeltaTemp > 300 && referenceDeltaTemp > ABS(sensor_comp_data[sensorType].lastTemp - sensor_comp_data[sensorType].referenceTemp)) { /* Min 3 deg reference temperature difference required for valid calibration. * Correction adjusted only if temperature difference to reference temperature increasing * Calibration assumes a simple linear relationship */ - lastTemp = sensorTemp; - sensor_comp_data[sensorType].correctionFactor = 0.9f * sensor_comp_data[sensorType].correctionFactor + 0.1f * (sensorMeasurement - referenceMeasurement) / CENTIDEGREES_TO_DEGREES(lastTemp - sensor_comp_data[sensorType].referenceTemp); + sensor_comp_data[sensorType].lastTemp = sensorTemp; + sensor_comp_data[sensorType].correctionFactor = 0.9f * sensor_comp_data[sensorType].correctionFactor + 0.1f * (sensorMeasurement - sensor_comp_data[sensorType].referenceMeasurement) / CENTIDEGREES_TO_DEGREES(sensor_comp_data[sensorType].lastTemp - sensor_comp_data[sensorType].referenceTemp); sensor_comp_data[sensorType].correctionFactor = constrainf(sensor_comp_data[sensorType].correctionFactor, -50.0f, 50.0f); } } else { diff --git a/src/main/sensors/sensors.h b/src/main/sensors/sensors.h index aca0bfb53c9..ac588581a7e 100644 --- a/src/main/sensors/sensors.h +++ b/src/main/sensors/sensors.h @@ -67,7 +67,9 @@ typedef enum { typedef struct sensor_compensation_s { float correctionFactor; + float referenceMeasurement; int16_t referenceTemp; + int16_t lastTemp; sensorTempCalState_e calibrationState; } sensor_compensation_t;