Skip to content

Commit

Permalink
allow consecutive calibrations
Browse files Browse the repository at this point in the history
  • Loading branch information
breadoven committed Oct 7, 2024
1 parent f95be06 commit 741dbc2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/main/sensors/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/main/sensors/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 741dbc2

Please sign in to comment.