Floating-point equality comparison

This commit is contained in:
2026-01-10 11:52:08 +01:00
parent 0236ebcdd1
commit a9ec101bc6

View File

@ -2,10 +2,12 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_log.h" #include "esp_log.h"
#include <string.h> #include <string.h>
#include <math.h>
#include "safety.h" #include "safety.h"
#define PERIODIC_INTERVAL 1U // run safety checks every 1sec #define PERIODIC_INTERVAL 1U // run safety checks every 1sec
#define SENSOR_GRACE_PERIOD (60U * 30U) // period that a sensor can report the same reading in seconds #define SENSOR_GRACE_PERIOD (60U * 30U) // period that a sensor can report the same reading in seconds
#define FLOAT_EPSILON 0.0001f
static const char *TAG = "smart-oil-heater-control-system-safety"; static const char *TAG = "smart-oil-heater-control-system-safety";
static SemaphoreHandle_t xMutexAccessSafety = NULL; static SemaphoreHandle_t xMutexAccessSafety = NULL;
@ -91,7 +93,7 @@ void checkSensorSanity(void)
} }
else else
{ {
if (sCurrentMeasurement.fCurrentValue == sanityChecks[i].fSensorTemperatureLast) if (fabsf(sCurrentMeasurement.fCurrentValue - sanityChecks[i].fSensorTemperatureLast) < FLOAT_EPSILON)
{ {
sanityChecks[i].uUnchangedCounter++; sanityChecks[i].uUnchangedCounter++;
if (sanityChecks[i].uUnchangedCounter >= (SENSOR_GRACE_PERIOD / PERIODIC_INTERVAL)) if (sanityChecks[i].uUnchangedCounter >= (SENSOR_GRACE_PERIOD / PERIODIC_INTERVAL))