error handling and cleanup
This commit is contained in:
@ -1,3 +1,8 @@
|
||||
/**
|
||||
* @file safety.c
|
||||
* @brief Implementation of safety monitoring module.
|
||||
*/
|
||||
|
||||
#include "safety.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
@ -7,53 +12,68 @@
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#define PERIODIC_INTERVAL 1U // run safety checks every 1sec
|
||||
#define SENSOR_GRACE_PERIOD (CONFIG_SENSOR_GRACE_PERIOD_MINUTES * 60U) // period that a sensor can report the same reading in seconds
|
||||
/** @brief Task interval in seconds. */
|
||||
#define PERIODIC_INTERVAL 1U
|
||||
|
||||
/** @brief Grace period for unchanged sensor readings (seconds). */
|
||||
#define SENSOR_GRACE_PERIOD (CONFIG_SENSOR_GRACE_PERIOD_MINUTES * 60U)
|
||||
|
||||
/** @brief Epsilon for float comparison. */
|
||||
#define FLOAT_EPSILON 0.0001f
|
||||
static const char *TAG = "smart-oil-heater-control-system-safety";
|
||||
|
||||
static const char *TAG = "safety";
|
||||
|
||||
static SemaphoreHandle_t xMutexAccessSafety = NULL;
|
||||
|
||||
/** @brief Sensor sanity check configurations. */
|
||||
static sSensorSanityCheck sanityChecks[NUMBER_OF_SENSOR_SANITY_CHECKS] = {
|
||||
{SENSOR_NO_ERROR, "chamber_temperature", {SENSOR_LIMIT_CHAMBER_MAX, SENSOR_LIMIT_CHAMBER_MIN}, 0.0f, 0U, getChamberTemperature},
|
||||
{SENSOR_NO_ERROR, "outdoor_temperature", {SENSOR_LIMIT_OUTDOOR_MAX, SENSOR_LIMIT_OUTDOOR_MIN}, 0.0f, 0U, getOutdoorTemperature},
|
||||
{SENSOR_NO_ERROR, "inlet_flow_temperature", {SENSOR_LIMIT_INLET_MAX, SENSOR_LIMIT_INLET_MIN}, 0.0f, 0U, getInletFlowTemperature},
|
||||
{SENSOR_NO_ERROR, "return_flow_temperature", {SENSOR_LIMIT_RETURN_MAX, SENSOR_LIMIT_RETURN_MIN}, 0.0f, 0U, getReturnFlowTemperature}};
|
||||
|
||||
static eSafetyState sSafetyState = SAFETY_NO_ERROR;
|
||||
|
||||
void taskSafety(void *pvParameters);
|
||||
void checkSensorSanity(void);
|
||||
void setSafeState(void);
|
||||
/* Private function prototypes */
|
||||
static void taskSafety(void *pvParameters);
|
||||
static void checkSensorSanity(void);
|
||||
static void setSafeState(void);
|
||||
|
||||
void initSafety(void)
|
||||
esp_err_t initSafety(void)
|
||||
{
|
||||
xMutexAccessSafety = xSemaphoreCreateRecursiveMutex();
|
||||
if (xMutexAccessSafety == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Unable to create mutex");
|
||||
ESP_LOGE(TAG, "Failed to create mutex");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
xSemaphoreGiveRecursive(xMutexAccessSafety);
|
||||
|
||||
BaseType_t taskCreated = xTaskCreate(
|
||||
taskSafety, // Function to implement the task
|
||||
"taskSafety", // Task name
|
||||
4096, // 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)
|
||||
);
|
||||
taskSafety,
|
||||
"taskSafety",
|
||||
4096,
|
||||
NULL,
|
||||
5,
|
||||
NULL);
|
||||
|
||||
if (taskCreated == pdPASS)
|
||||
{
|
||||
ESP_LOGI(TAG, "Task created successfully!");
|
||||
}
|
||||
else
|
||||
if (taskCreated != pdPASS)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to create task");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
setSafeState(); // Set inital state
|
||||
setSafeState();
|
||||
|
||||
ESP_LOGI(TAG, "Initialized successfully");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void taskSafety(void *pvParameters)
|
||||
/**
|
||||
* @brief Safety monitoring task.
|
||||
* @param pvParameters Task parameters (unused).
|
||||
*/
|
||||
static void taskSafety(void *pvParameters)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
@ -61,7 +81,6 @@ void taskSafety(void *pvParameters)
|
||||
|
||||
if (xSemaphoreTakeRecursive(xMutexAccessSafety, portMAX_DELAY) == pdTRUE)
|
||||
{
|
||||
|
||||
checkSensorSanity();
|
||||
|
||||
if (sSafetyState != SAFETY_NO_ERROR)
|
||||
@ -74,7 +93,10 @@ void taskSafety(void *pvParameters)
|
||||
}
|
||||
}
|
||||
|
||||
void checkSensorSanity(void)
|
||||
/**
|
||||
* @brief Check all sensor readings for sanity.
|
||||
*/
|
||||
static void checkSensorSanity(void)
|
||||
{
|
||||
sSafetyState = SAFETY_NO_ERROR;
|
||||
for (int i = 0; i < NUMBER_OF_SENSOR_SANITY_CHECKS; i++)
|
||||
@ -130,7 +152,10 @@ void checkSensorSanity(void)
|
||||
}
|
||||
}
|
||||
|
||||
void setSafeState(void)
|
||||
/**
|
||||
* @brief Set system to safe state (burner off, pump on).
|
||||
*/
|
||||
static void setSafeState(void)
|
||||
{
|
||||
setCirculationPumpState(ENABLED); // To cool down system
|
||||
setBurnerState(DISABLED); // Deactivate burner
|
||||
|
||||
Reference in New Issue
Block a user