#include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" #include "safety.h" #define PERIODIC_INTERVAL 1U // run safety checks every 1sec static const char *TAG = "smart-oil-heater-control-system-safety"; static SemaphoreHandle_t xMutexAccessSafety = NULL; sSensorSanityCheck sanityChecks[NUMBER_OF_SENSOR_SANITY_CHECKS] = { {0, "chamber_temperature", {95.0f, -10.0f}, 0.0f, getChamberTemperature}, {0, "outdoor_temperature", {45.0f, -20.0f}, 0.0f, getOutdoorTemperature}, {0, "inlet_flow_temperature", {95.0f, -10.0f}, 0.0f, getInletFlowTemperature}, {0, "return_flow_temperature", {95.0f, -10.0f}, 0.0f, getReturnFlowTemperature}}; void taskSafety(void *pvParameters); void checkSensorSanity(void); void setSafeState(void); void initSafety(void) { xMutexAccessSafety = xSemaphoreCreateBinary(); if (xMutexAccessSafety == NULL) { ESP_LOGE(TAG, "Unable to create mutex"); } xSemaphoreGive(xMutexAccessSafety); BaseType_t taskCreated = xTaskCreate( taskSafety, // Function to implement the task "taskSafety", // Task name 2048, // Stack size (in words, not bytes) NULL, // Parameters to the task function (none in this case) 5, // Task priority (higher number = higher priority) NULL // Task handle (optional) ); if (taskCreated == pdPASS) { ESP_LOGI(TAG, "Task created successfully!"); } else { ESP_LOGE(TAG, "Failed to create task"); } } void taskSafety(void *pvParameters) { while (1) { vTaskDelay(PERIODIC_INTERVAL * 1000U / portTICK_PERIOD_MS); checkSensorSanity(); } } void checkSensorSanity(void) { for (int i = 0; i < NUMBER_OF_SENSOR_SANITY_CHECKS; i++) { //printf("Check sanity of sensor %s:\n", sanityChecks[i].name); //printf(" Status: %u\n", sanityChecks[i].status); //printf(" Sensor Limits: Max = %.2f, Min = %.2f\n", sanityChecks[i].sSensorLimit.max, sanityChecks[i].sSensorLimit.min); //printf(" Last Sensor Temperature: %.2f\n", sanityChecks[i].fSensorTemperatureLast); const float fSensorTemperatureCurrent = sanityChecks[i].getSensor(CURRENT); if (fSensorTemperatureCurrent == sanityChecks[i].fSensorTemperatureLast) { ESP_LOGE(TAG, "%s Sensor reported unchanged value! %lf == %lf",sanityChecks[i].name, fSensorTemperatureCurrent, sanityChecks[i].fSensorTemperatureLast); sanityChecks[i].status = 1; } else { sanityChecks[i].fSensorTemperatureLast = fSensorTemperatureCurrent; if (fSensorTemperatureCurrent > sanityChecks[i].sSensorLimit.max) { ESP_LOGE(TAG, "%s Sensor reported too high value! %lf > %lf", sanityChecks[i].name, fSensorTemperatureCurrent, sanityChecks[i].sSensorLimit.max); sanityChecks[i].status = 1; } else if (fSensorTemperatureCurrent < sanityChecks[i].sSensorLimit.min) { ESP_LOGE(TAG, "%s Sensor reported too low value! %lf < %lf", sanityChecks[i].name, fSensorTemperatureCurrent, sanityChecks[i].sSensorLimit.min); sanityChecks[i].status = 1; } else { sanityChecks[i].status = 0; } } //printf(" Status: %u\n", sanityChecks[i].status); } } void setSafeState(void) { setCirculationPumpState(DISABLED); setBurnerState(DISABLED); } /* sSafetyStateElement *getSafetyStates(void) { return safetyStates; } */