Compare commits
	
		
			1 Commits
		
	
	
		
			main
			...
			feature/wi
		
	
	| Author | SHA256 | Date | |
|---|---|---|---|
| d14ae528c0 | 
							
								
								
									
										239
									
								
								main/control.c
									
									
									
									
									
								
							
							
						
						
									
										239
									
								
								main/control.c
									
									
									
									
									
								
							| @ -8,35 +8,29 @@ | ||||
| #include "safety.h" | ||||
| #include "sntp.h" | ||||
|  | ||||
| #define PERIODIC_INTERVAL 1U // Run control loop every 1 second | ||||
| #define PERIODIC_INTERVAL 1U // run control loop every 1sec | ||||
|  | ||||
| // Temperature thresholds | ||||
| #define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY 30.0f | ||||
| #define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT 25.0f | ||||
| #define CHAMBER_TEMPERATURE_TARGET 80.0f             // Max cutoff temperature | ||||
| #define CHAMBER_TEMPERATURE_THRESHOLD 45.0f          // Min threshold for burner enable | ||||
| #define SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH 20.0f // Summer mode will be activated | ||||
| #define SUMMER_MODE_TEMPERATURE_THRESHOLD_LOW 15.0f  // Summer mode will be deactivated --> Heating starts | ||||
| #define CIRCULATION_PUMP_TEMPERATURE_THRESHOLD 30.0f // Min threshold of chamber for circulation pump enable | ||||
| #define BURNER_FAULT_DETECTION_THRESHOLD (60U * 4U)  // Burner fault detection after 4 minutes | ||||
| #define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY 30.0 | ||||
| #define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT 25.0 | ||||
| #define CHAMPER_TEMPERATURE_TARGET 80.0 | ||||
| #define BURNER_FAULT_DETECTION_THRESHOLD (60U * 3U) // Detect burner fault if after 3 minutes no burner start detected | ||||
|  | ||||
| static const char *TAG = "smart-oil-heater-control-system-control"; | ||||
| static eControlState sControlState = CONTROL_STARTING; | ||||
| // Control table for daily schedules | ||||
| static const sControlDay aControlTable[] = { | ||||
|     {MONDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, | ||||
|     {TUESDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, | ||||
|     {WEDNESDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, | ||||
|     {THURSDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, | ||||
|     {FRIDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{23, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, | ||||
|     {SATURDAY, 2U, {{{6, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{23, 30}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, | ||||
|     {SUNDAY, 2U, {{{6, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{22, 30}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, | ||||
| }; | ||||
| static sControlTemperatureEntry currentControlEntry = aControlTable[0].aTemperatureEntries[0]; | ||||
|  | ||||
| // Function prototypes | ||||
| static sControlDay aControlTable[] = { | ||||
|     {MONDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}}, | ||||
|     {TUESDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}}, | ||||
|     {WEDNESDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}}, | ||||
|     {THURSDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}}, | ||||
|     {FRIDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{23, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}}, | ||||
|     {SATURDAY, 2U, {{{6, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{23, 30}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}}, | ||||
|     {SUNDAY, 2U, {{{6, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{22, 30}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}}, | ||||
| }; | ||||
|  | ||||
| void taskControl(void *pvParameters); | ||||
| void findControlCurrentTemperatureEntry(void); | ||||
| eControlWeekday getCurrentWeekday(void); | ||||
| sControlTemperatureEntry getCurrentTemperatureEntry(void); | ||||
|  | ||||
| void initControl(void) | ||||
| { | ||||
| @ -62,71 +56,92 @@ void initControl(void) | ||||
| void taskControl(void *pvParameters) | ||||
| { | ||||
|     bool bHeatingInAction = false; | ||||
|     bool bSummerMode = false; | ||||
|     eBurnerState eBurnerState = BURNER_UNKNOWN; | ||||
|     bool bBurnerFaultDetected = false; | ||||
|     int64_t i64BurnerEnableTimestamp = esp_timer_get_time(); | ||||
|  | ||||
|     time_t now; | ||||
|  | ||||
|     while (1) | ||||
|     { | ||||
|         // Get the current time | ||||
|         time(&now); | ||||
|         ESP_LOGW(TAG, "Control loop time: %lli", now); | ||||
|         vTaskDelay(PERIODIC_INTERVAL * 1000U / portTICK_PERIOD_MS); | ||||
|  | ||||
|         // Check for safety faults | ||||
|         if (getSafetyState() != SAFETY_NO_ERROR) | ||||
|         { | ||||
|             ESP_LOGW(TAG, "Control not possible due to safety fault!"); | ||||
|             //ESP_LOGW(TAG, "Control not possible due to safety fault!"); | ||||
|             sControlState = CONTROL_FAULT_SAFETY; | ||||
|             if (bHeatingInAction) | ||||
|             if (bHeatingInAction == true) | ||||
|             { | ||||
|                 ESP_LOGW(TAG, "Disabling burner due to safety fault"); | ||||
|                 ESP_LOGW(TAG, "Control not possible due to safety fault: Disable burner"); | ||||
|                 bHeatingInAction = false; | ||||
|                 setCirculationPumpState(ENABLED); | ||||
|                 setBurnerState(DISABLED); | ||||
|                 setSafetyControlState(ENABLED); | ||||
|             } | ||||
|             continue; | ||||
|         } | ||||
|  | ||||
|         // Check for SNTP faults | ||||
|         if (getSntpState() != SYNC_SUCCESSFUL) | ||||
|         { | ||||
|             ESP_LOGW(TAG, "Control not possible due to SNTP fault!"); | ||||
|             ESP_LOGW(TAG, "Control not possible due to sntp fault!"); | ||||
|             sControlState = CONTROL_FAULT_SNTP; | ||||
|             if (bHeatingInAction) | ||||
|             if (bHeatingInAction == true) | ||||
|             { | ||||
|                 ESP_LOGW(TAG, "Disabling burner due to SNTP fault"); | ||||
|                 ESP_LOGW(TAG, "Control not possible due to sntp fault: Disable burner"); | ||||
|                 bHeatingInAction = false; | ||||
|                 setCirculationPumpState(ENABLED); | ||||
|                 setBurnerState(DISABLED); | ||||
|                 setSafetyControlState(ENABLED); | ||||
|             } | ||||
|             continue; | ||||
|         } | ||||
|  | ||||
|         sControlTemperatureEntry currentControlEntry = getControlCurrentTemperatureEntry(); | ||||
|         sControlTemperatureEntry currentControlEntry = getCurrentTemperatureEntry(); | ||||
|         // ESP_LOGI(TAG, "Control Entry Hour: %i Minute: %i ChamberTemp: %lf ReturnFlowTemp: %lf", currentControlEntry.timestamp.hour, currentControlEntry.timestamp.minute, currentControlEntry.fChamberTemperature, currentControlEntry.fReturnFlowTemperature); | ||||
|  | ||||
|         if (getOutdoorTemperature().fDampedValue >= SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH) | ||||
|         if (bHeatingInAction == true) | ||||
|         { | ||||
|             bSummerMode = true; | ||||
|         } | ||||
|         else if (getOutdoorTemperature().fDampedValue <= SUMMER_MODE_TEMPERATURE_THRESHOLD_LOW) | ||||
|             if ((getChamberTemperature().fCurrentValue >= currentControlEntry.fChamberTemperature) || (getChamberTemperature().predict60s.fValue >= currentControlEntry.fChamberTemperature)) | ||||
|             { | ||||
|             bSummerMode = false; | ||||
|         } | ||||
|  | ||||
|         // Enable burner if outdoor temperature is low and return flow temperature is cooled down | ||||
|         if (!bHeatingInAction && (eBurnerState != BURNER_FAULT)) | ||||
|         { | ||||
|             if (bSummerMode) | ||||
|             { | ||||
|                 // ESP_LOGI(TAG, "Outdoor temperature too warm: Disabling heating"); | ||||
|                 ESP_LOGI(TAG, "Chamber Target Temperature reached: Disable burner"); | ||||
|                 bHeatingInAction = false; | ||||
|                 setCirculationPumpState(ENABLED); | ||||
|                 setBurnerState(DISABLED); | ||||
|                 setSafetyControlState(DISABLED); | ||||
|                 sControlState = CONTROL_OUTDOOR_TOO_WARM; | ||||
|                 setSafetyControlState(ENABLED); | ||||
|             } | ||||
|             else if ((getReturnFlowTemperature().average60s.fValue <= currentControlEntry.fReturnFlowTemperature) && | ||||
|                      (getChamberTemperature().fCurrentValue <= CHAMBER_TEMPERATURE_THRESHOLD)) | ||||
|             else | ||||
|             { | ||||
|                 ESP_LOGI(TAG, "Enabling burner: Return flow temperature target reached"); | ||||
|                 eBurnerState = BURNER_UNKNOWN; | ||||
|                 if (bHeatingInAction) | ||||
|                 { | ||||
|                     int64_t i64Delta = esp_timer_get_time() - i64BurnerEnableTimestamp; | ||||
|  | ||||
|                     if ((i64Delta / 1000000U) >= BURNER_FAULT_DETECTION_THRESHOLD) | ||||
|                     { | ||||
|                         if (getBurnerError() == FAULT) | ||||
|                         { | ||||
|                             ESP_LOGW(TAG, "Detected burner fault after %lli seconds!", (i64Delta / 1000000U)); | ||||
|                             ESP_LOGW(TAG, "Control not possible due to burner fault: Disable burner"); | ||||
|                             sControlState = CONTROL_FAULT_BURNER; | ||||
|                             bHeatingInAction = false; | ||||
|                             bBurnerFaultDetected = true; | ||||
|                             setCirculationPumpState(ENABLED); | ||||
|                             setBurnerState(DISABLED); | ||||
|                             setSafetyControlState(ENABLED); | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if ((bHeatingInAction == false) && (bBurnerFaultDetected == false)) | ||||
|         { | ||||
|             if ((getReturnFlowTemperature().average60s.fValue <= currentControlEntry.fReturnFlowTemperature) && (getChamberTemperature().fCurrentValue <= 45.0)) | ||||
|             { | ||||
|                 ESP_LOGI(TAG, "Return Flow Target Temperature reached: Enable Burner"); | ||||
|                 bHeatingInAction = true; | ||||
|                 setCirculationPumpState(ENABLED); | ||||
|                 setBurnerState(ENABLED); | ||||
|                 setSafetyControlState(ENABLED); | ||||
|                 i64BurnerEnableTimestamp = esp_timer_get_time(); | ||||
| @ -134,56 +149,10 @@ void taskControl(void *pvParameters) | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 // ESP_LOGI(TAG, "Return flow temperature too warm: Disabling heating"); | ||||
|                 sControlState = CONTROL_RETURN_FLOW_TOO_WARM; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         // Disable burner if target temperature is reached or a fault occurred | ||||
|         if (bHeatingInAction) | ||||
|         { | ||||
|             if ((getChamberTemperature().fCurrentValue >= currentControlEntry.fChamberTemperature) || | ||||
|                 (getChamberTemperature().predict60s.fValue >= currentControlEntry.fChamberTemperature)) | ||||
|             { | ||||
|                 ESP_LOGI(TAG, "Chamber target temperature reached: Disabling burner"); | ||||
|                 bHeatingInAction = false; | ||||
|                 setBurnerState(DISABLED); | ||||
|                 setSafetyControlState(ENABLED); | ||||
|     } | ||||
|             else if (esp_timer_get_time() - i64BurnerEnableTimestamp >= BURNER_FAULT_DETECTION_THRESHOLD * 1000000U) | ||||
|             { | ||||
|                 if (eBurnerState == BURNER_UNKNOWN) | ||||
|                 { | ||||
|                     if (getBurnerError() == FAULT) | ||||
|                     { | ||||
|                         // ESP_LOGW(TAG, "Burner fault detected: Disabling burner"); | ||||
|                         bHeatingInAction = false; | ||||
|                         eBurnerState = BURNER_FAULT; | ||||
|                         sControlState = CONTROL_FAULT_BURNER; | ||||
|                         setBurnerState(DISABLED); | ||||
|                         setSafetyControlState(ENABLED); | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
|                         // ESP_LOGI(TAG, "No burner fault detected: Marking burner as fired"); | ||||
|                         eBurnerState = BURNER_FIRED; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         // Manage circulation pump | ||||
|         if (getChamberTemperature().fCurrentValue <= CIRCULATION_PUMP_TEMPERATURE_THRESHOLD) | ||||
|         { | ||||
|             // ESP_LOGI(TAG, "Burner cooled down: Disabling circulation pump"); | ||||
|             setCirculationPumpState(DISABLED); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             // ESP_LOGI(TAG, "Burner heated: Enabling circulation pump"); | ||||
|             setCirculationPumpState(ENABLED); | ||||
|         } | ||||
|     } // End of while(1) | ||||
| } | ||||
|  | ||||
| eControlState getControlState(void) | ||||
| @ -191,46 +160,78 @@ eControlState getControlState(void) | ||||
|     return sControlState; | ||||
| } | ||||
|  | ||||
| eControlWeekday getControlCurrentWeekday(void) | ||||
| eControlWeekday getCurrentWeekday(void) | ||||
| { | ||||
|     time_t now; | ||||
|     struct tm *timeinfo; | ||||
|  | ||||
|     // Get the current time | ||||
|     time(&now); | ||||
|     timeinfo = localtime(&now); | ||||
|     timeinfo = localtime(&now); // Convert to local time | ||||
|  | ||||
|     // Get the day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday) | ||||
|     int day = timeinfo->tm_wday; | ||||
|     return (eControlWeekday)((day == 0) ? 6 : day - 1); | ||||
|  | ||||
|     // Adjust so that Monday = 0, Sunday = 6 | ||||
|     if (day == 0) | ||||
|     { | ||||
|         day = 6; // Sunday becomes 6 | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         day -= 1; // Shift other days to make Monday = 0 | ||||
|     } | ||||
|  | ||||
| void findControlCurrentTemperatureEntry(void) | ||||
| { | ||||
|     eControlWeekday currentDay = getControlCurrentWeekday(); | ||||
|     return (eControlWeekday)day; | ||||
| } | ||||
|  | ||||
| sControlTemperatureEntry getCurrentTemperatureEntry(void) | ||||
| { | ||||
|     sControlTemperatureEntry result = aControlTable[0].aTemperatureEntries[0]; | ||||
|     eControlWeekday currentDay = getCurrentWeekday(); | ||||
|     time_t now; | ||||
|     struct tm timeinfo; | ||||
|     time(&now); | ||||
|     localtime_r(&now, &timeinfo); | ||||
|  | ||||
|     int hour = timeinfo.tm_hour; | ||||
|     int minute = timeinfo.tm_min; | ||||
|     // Get the current time | ||||
|     time(&now); | ||||
|     // Convert to local time structure | ||||
|     localtime_r(&now, &timeinfo); | ||||
|     // Extract hour and minute | ||||
|     int hour = timeinfo.tm_hour;  // Hour (0-23) | ||||
|     int minute = timeinfo.tm_min; // Minute (0-59)u | ||||
|  | ||||
|     // ESP_LOGI(TAG, "Current Day: %i Hour: %i Minute: %i", currentDay, hour, minute); | ||||
|  | ||||
|     for (int i = 0; i < sizeof(aControlTable) / sizeof(aControlTable[0]); i++) | ||||
|     { | ||||
|         /// loops through days | ||||
|         // ESP_LOGI(TAG, "Day %d: %d", i + 1, aControlTable[i].day); | ||||
|         // int numberOfEntries = aControlTable[i].entryCount; | ||||
|         // ESP_LOGI(TAG, "Number of entries: %i", numberOfEntries); | ||||
|  | ||||
|         for (int j = 0; j < aControlTable[i].entryCount; j++) | ||||
|         { | ||||
|             if ((aControlTable[i].day > currentDay) || | ||||
|                 (aControlTable[i].day == currentDay && aControlTable[i].aTemperatureEntries[j].timestamp.hour > hour) || | ||||
|                 (aControlTable[i].day == currentDay && aControlTable[i].aTemperatureEntries[j].timestamp.hour == hour && aControlTable[i].aTemperatureEntries[j].timestamp.minute >= minute)) | ||||
|             if ((aControlTable[i].day) > currentDay) | ||||
|             { | ||||
|                 currentControlEntry = aControlTable[i].aTemperatureEntries[j]; | ||||
|             } | ||||
|             currentControlEntry = aControlTable[i].aTemperatureEntries[j]; | ||||
|         } | ||||
|     } | ||||
|                 // ESP_LOGI(TAG, "DAY Return Control Entry Day: %i Hour: %i Minute: %i ChamberTemp: %lf ReturnFlowTemp: %lf", aControlTable[i].day, aControlTable[i].aTemperatureEntries[j].timestamp.hour, aControlTable[i].aTemperatureEntries[j].timestamp.minute, aControlTable[i].aTemperatureEntries[j].fChamberTemperature, aControlTable[i].aTemperatureEntries[j].fReturnFlowTemperature); | ||||
|                 return result; | ||||
|             } | ||||
|  | ||||
| sControlTemperatureEntry getControlCurrentTemperatureEntry(void) | ||||
|             if ((aControlTable[i].day == currentDay) && (aControlTable[i].aTemperatureEntries[j].timestamp.hour > hour)) | ||||
|             { | ||||
|     return currentControlEntry; | ||||
|                 // ESP_LOGI(TAG, "HOUR Return Control Entry Day: %i Hour: %i Minute: %i ChamberTemp: %lf ReturnFlowTemp: %lf", aControlTable[i].day, aControlTable[i].aTemperatureEntries[j].timestamp.hour, aControlTable[i].aTemperatureEntries[j].timestamp.minute, aControlTable[i].aTemperatureEntries[j].fChamberTemperature, aControlTable[i].aTemperatureEntries[j].fReturnFlowTemperature); | ||||
|                 return result; | ||||
|             } | ||||
|  | ||||
|             if ((aControlTable[i].day == currentDay) && (aControlTable[i].aTemperatureEntries[j].timestamp.hour == hour) && (aControlTable[i].aTemperatureEntries[j].timestamp.minute == minute)) | ||||
|             { | ||||
|                 // ESP_LOGI(TAG, "MINUTE Return Control Entry Day: %i Hour: %i Minute: %i ChamberTemp: %lf ReturnFlowTemp: %lf", aControlTable[i].day, aControlTable[i].aTemperatureEntries[j].timestamp.hour, aControlTable[i].aTemperatureEntries[j].timestamp.minute, aControlTable[i].aTemperatureEntries[j].fChamberTemperature, aControlTable[i].aTemperatureEntries[j].fReturnFlowTemperature); | ||||
|                 return result; | ||||
|             } | ||||
|  | ||||
|             // ESP_LOGI(TAG, "SET Return Control Entry Day: %i Hour: %i Minute: %i ChamberTemp: %lf ReturnFlowTemp: %lf", aControlTable[i].day, aControlTable[i].aTemperatureEntries[j].timestamp.hour, aControlTable[i].aTemperatureEntries[j].timestamp.minute, aControlTable[i].aTemperatureEntries[j].fChamberTemperature, aControlTable[i].aTemperatureEntries[j].fReturnFlowTemperature); | ||||
|             result = aControlTable[i].aTemperatureEntries[j]; | ||||
|         } | ||||
|     } | ||||
|     return result; | ||||
| } | ||||
| @ -14,13 +14,6 @@ typedef enum _ControlState | ||||
|     CONTROL_FAULT_SNTP, | ||||
| } eControlState; | ||||
|  | ||||
| typedef enum _BurnerState | ||||
| { | ||||
|     BURNER_UNKNOWN, // Burner is disabled or state after enabling is still unkown | ||||
|     BURNER_FIRED,   // Burner fired successfully | ||||
|     BURNER_FAULT    // Burner was unable to fire successfully | ||||
| } eBurnerState; | ||||
|  | ||||
| typedef enum _ControlWeekday | ||||
| { | ||||
|     MONDAY, | ||||
| @ -54,5 +47,3 @@ typedef struct _ControlDay | ||||
|  | ||||
| void initControl(void); | ||||
| eControlState getControlState(void); | ||||
| eControlWeekday getControlCurrentWeekday(void); | ||||
| sControlTemperatureEntry getControlCurrentTemperatureEntry(void); | ||||
|  | ||||
| @ -17,7 +17,7 @@ const uint8_t uBurnerFaultPin = 19U; | ||||
| const uint8_t uDS18B20Pin = 4U; | ||||
|  | ||||
| const onewire_addr_t uChamperTempSensorAddr = 0xd00000108cd01d28; | ||||
| const onewire_addr_t uOutdoorTempSensorAddr = 0xd70000108a9b9128; | ||||
| const onewire_addr_t uOutdoorTempSensorAddr = 0x78000000c6c2f728; | ||||
| const onewire_addr_t uInletFlowTempSensorAddr = 0x410000108b8c0628; | ||||
| const onewire_addr_t uReturnFlowTempSensorAddr = 0x90000108cc77c28; | ||||
|  | ||||
| @ -88,23 +88,22 @@ void initMeasurement(sMeasurement *pMeasurement) | ||||
|         return; | ||||
|  | ||||
|     pMeasurement->state = MEASUREMENT_FAULT; | ||||
|     pMeasurement->fCurrentValue = INITIALISATION_VALUE; | ||||
|     pMeasurement->fDampedValue = INITIALISATION_VALUE; | ||||
|     pMeasurement->fCurrentValue = 0.0f; | ||||
|  | ||||
|     pMeasurement->average10s.fValue = INITIALISATION_VALUE; | ||||
|     pMeasurement->average10s.fValue = 0.0f; | ||||
|     pMeasurement->average10s.bufferCount = 0U; | ||||
|     pMeasurement->average10s.bufferIndex = 0U; | ||||
|     memset(pMeasurement->average10s.samples, 0U, AVG10S_SAMPLE_SIZE); | ||||
|     memset(pMeasurement->average10s.samples, 0U, AVG10_SAMPLE_SIZE); | ||||
|  | ||||
|     pMeasurement->average60s.fValue = INITIALISATION_VALUE; | ||||
|     pMeasurement->average60s.fValue = 0.0f; | ||||
|     pMeasurement->average60s.bufferCount = 0U; | ||||
|     pMeasurement->average60s.bufferIndex = 0U; | ||||
|     memset(pMeasurement->average60s.samples, 0U, AVG60S_SAMPLE_SIZE); | ||||
|     memset(pMeasurement->average60s.samples, 0U, AVG60_SAMPLE_SIZE); | ||||
|  | ||||
|     pMeasurement->predict60s.fValue = INITIALISATION_VALUE; | ||||
|     pMeasurement->predict60s.fValue = 0.0f; | ||||
|     pMeasurement->predict60s.bufferCount = 0U; | ||||
|     pMeasurement->predict60s.bufferIndex = 0U; | ||||
|     memset(pMeasurement->predict60s.samples, 0U, PRED60S_SAMPLE_SIZE); | ||||
|     memset(pMeasurement->predict60s.samples, 0U, PRED60_SAMPLE_SIZE); | ||||
| } | ||||
|  | ||||
| void updateAverage(sMeasurement *pMeasurement) | ||||
| @ -114,9 +113,9 @@ void updateAverage(sMeasurement *pMeasurement) | ||||
|  | ||||
|     // Average form the last 10sec | ||||
|     pMeasurement->average10s.samples[pMeasurement->average10s.bufferIndex] = pMeasurement->fCurrentValue; | ||||
|     pMeasurement->average10s.bufferIndex = (pMeasurement->average10s.bufferIndex + 1) % AVG10S_SAMPLE_SIZE; | ||||
|     pMeasurement->average10s.bufferIndex = (pMeasurement->average10s.bufferIndex + 1) % AVG10_SAMPLE_SIZE; | ||||
|  | ||||
|     if (pMeasurement->average10s.bufferCount < AVG10S_SAMPLE_SIZE) | ||||
|     if (pMeasurement->average10s.bufferCount < AVG10_SAMPLE_SIZE) | ||||
|     { | ||||
|         pMeasurement->average10s.bufferCount++; | ||||
|     } | ||||
| @ -131,9 +130,9 @@ void updateAverage(sMeasurement *pMeasurement) | ||||
|  | ||||
|     // Average form the last 60sec | ||||
|     pMeasurement->average60s.samples[pMeasurement->average60s.bufferIndex] = pMeasurement->fCurrentValue; | ||||
|     pMeasurement->average60s.bufferIndex = (pMeasurement->average60s.bufferIndex + 1) % AVG60S_SAMPLE_SIZE; | ||||
|     pMeasurement->average60s.bufferIndex = (pMeasurement->average60s.bufferIndex + 1) % AVG60_SAMPLE_SIZE; | ||||
|  | ||||
|     if (pMeasurement->average60s.bufferCount < AVG60S_SAMPLE_SIZE) | ||||
|     if (pMeasurement->average60s.bufferCount < AVG60_SAMPLE_SIZE) | ||||
|     { | ||||
|         pMeasurement->average60s.bufferCount++; | ||||
|     } | ||||
| @ -145,24 +144,6 @@ void updateAverage(sMeasurement *pMeasurement) | ||||
|     } | ||||
|  | ||||
|     pMeasurement->average60s.fValue = sum / pMeasurement->average60s.bufferCount; | ||||
|  | ||||
|     // Damped current value | ||||
|     if (pMeasurement->fDampedValue == INITIALISATION_VALUE) | ||||
|     { | ||||
|         pMeasurement->fDampedValue = pMeasurement->fCurrentValue; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         if (pMeasurement->fCurrentValue > pMeasurement->fDampedValue) | ||||
|         { | ||||
|             pMeasurement->fDampedValue = pMeasurement->fDampedValue + (DAMPING_FACTOR_WARMER * (pMeasurement->fCurrentValue - pMeasurement->fDampedValue)); | ||||
|         } | ||||
|  | ||||
|         if (pMeasurement->fCurrentValue < pMeasurement->fDampedValue) | ||||
|         { | ||||
|             pMeasurement->fDampedValue = pMeasurement->fDampedValue - (DAMPING_FACTOR_COLDER * (pMeasurement->fDampedValue - pMeasurement->fCurrentValue)); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| void updatePrediction(sMeasurement *pMeasurement) | ||||
| @ -173,8 +154,8 @@ void updatePrediction(sMeasurement *pMeasurement) | ||||
|     // Update predict60s buffer | ||||
|     sPredict *predict60s = &pMeasurement->predict60s; | ||||
|     predict60s->samples[predict60s->bufferIndex] = pMeasurement->fCurrentValue; | ||||
|     predict60s->bufferIndex = (predict60s->bufferIndex + 1) % PRED60S_SAMPLE_SIZE; | ||||
|     if (predict60s->bufferCount < PRED60S_SAMPLE_SIZE) | ||||
|     predict60s->bufferIndex = (predict60s->bufferIndex + 1) % PRED60_SAMPLE_SIZE; | ||||
|     if (predict60s->bufferCount < PRED60_SAMPLE_SIZE) | ||||
|         predict60s->bufferCount++; | ||||
|  | ||||
|     // Predict 60s future value using linear regression | ||||
| @ -208,12 +189,12 @@ void taskInput(void *pvParameters) | ||||
|  | ||||
|             if (ds18x20_scan_devices(uDS18B20Pin, uOneWireAddresses, MAX_DN18B20_SENSORS, &sSensorCount) != ESP_OK) | ||||
|             { | ||||
|                 ESP_LOGE(TAG, "1-Wire device scan error!"); | ||||
|                // ESP_LOGE(TAG, "1-Wire device scan error!"); | ||||
|             } | ||||
|  | ||||
|             if (!sSensorCount) | ||||
|             { | ||||
|                 ESP_LOGW(TAG, "No 1-Wire devices detected!"); | ||||
|                // ESP_LOGW(TAG, "No 1-Wire devices detected!"); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
| @ -222,14 +203,14 @@ void taskInput(void *pvParameters) | ||||
|                 if (sSensorCount > MAX_DN18B20_SENSORS) | ||||
|                 { | ||||
|                     sSensorCount = MAX_DN18B20_SENSORS; | ||||
|                     ESP_LOGW(TAG, "More 1-Wire devices found than expected!"); | ||||
|                    // ESP_LOGW(TAG, "More 1-Wire devices found than expected!"); | ||||
|                 } | ||||
|  | ||||
|                 for (size_t iReadLoop = 0; iReadLoop < ONE_WIRE_LOOPS; iReadLoop++) | ||||
|                 { | ||||
|                     if (ds18x20_measure_and_read_multi(uDS18B20Pin, uOneWireAddresses, sSensorCount, fDS18B20Temps) != ESP_OK) | ||||
|                     { | ||||
|                         ESP_LOGE(TAG, "1-Wire devices read error"); | ||||
|                       //  ESP_LOGE(TAG, "1-Wire devices read error"); | ||||
|                         vTaskDelay(PERIODIC_INTERVAL * 100U / portTICK_PERIOD_MS); // Wait 100ms if bus error occurred | ||||
|                     } | ||||
|                     else | ||||
| @ -290,9 +271,9 @@ void taskInput(void *pvParameters) | ||||
| float linearRegressionPredict(const float *samples, size_t count, size_t bufferIndex, float futureIndex) | ||||
| { | ||||
|     if (count == 0) | ||||
|         return INITIALISATION_VALUE; // No prediction possible with no data | ||||
|         return 0.0f; // No prediction possible with no data | ||||
|  | ||||
|     float sumX = INITIALISATION_VALUE, sumY = INITIALISATION_VALUE, sumXY = INITIALISATION_VALUE, sumX2 = INITIALISATION_VALUE; | ||||
|     float sumX = 0.0f, sumY = 0.0f, sumXY = 0.0f, sumX2 = 0.0f; | ||||
|  | ||||
|     for (size_t i = 0; i < count; i++) | ||||
|     { | ||||
|  | ||||
| @ -1,13 +1,9 @@ | ||||
| #pragma once | ||||
|  | ||||
| #define MAX(a, b) ((a) > (b) ? (a) : (b)) | ||||
| #define INITIALISATION_VALUE 0.0f | ||||
| #define AVG10S_SAMPLE_SIZE 10U | ||||
| #define AVG60S_SAMPLE_SIZE 60U | ||||
| #define AVG24H_SAMPLE_SIZE 24U | ||||
| #define PRED60S_SAMPLE_SIZE 60U | ||||
| #define DAMPING_FACTOR_WARMER 0.00001f // 0.001% | ||||
| #define DAMPING_FACTOR_COLDER 0.00005f // 0.005% | ||||
| #define AVG10_SAMPLE_SIZE 10U | ||||
| #define AVG60_SAMPLE_SIZE 60U | ||||
| #define PRED60_SAMPLE_SIZE 60U | ||||
|  | ||||
| typedef enum _BurnerErrorState | ||||
| { | ||||
| @ -24,7 +20,7 @@ typedef enum _MeasurementErrorState | ||||
| typedef struct _Average | ||||
| { | ||||
|     float fValue; | ||||
|     float samples[MAX(AVG10S_SAMPLE_SIZE, MAX(AVG60S_SAMPLE_SIZE, AVG24H_SAMPLE_SIZE))]; | ||||
|     float samples[MAX(AVG10_SAMPLE_SIZE, AVG60_SAMPLE_SIZE)]; | ||||
|     size_t bufferIndex; | ||||
|     size_t bufferCount; | ||||
| } sAverage; | ||||
| @ -32,7 +28,7 @@ typedef struct _Average | ||||
| typedef struct _Predict | ||||
| { | ||||
|     float fValue; | ||||
|     float samples[PRED60S_SAMPLE_SIZE]; | ||||
|     float samples[PRED60_SAMPLE_SIZE]; | ||||
|     size_t bufferIndex; | ||||
|     size_t bufferCount; | ||||
| } sPredict; | ||||
| @ -40,7 +36,6 @@ typedef struct _Predict | ||||
| typedef struct _Measurement | ||||
| { | ||||
|     float fCurrentValue; | ||||
|     float fDampedValue; | ||||
|     sAverage average10s; | ||||
|     sAverage average60s; | ||||
|     sPredict predict60s; | ||||
|  | ||||
| @ -128,12 +128,6 @@ void taskMetrics(void *pvParameters) | ||||
|         aMetrics[u16MetricCounter].fMetricValue = getChamberTemperature().average60s.fValue; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Chamber Temperature Damped | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "chamber_temperature_damped"); | ||||
|         aMetrics[u16MetricCounter].type = FLOAT; | ||||
|         aMetrics[u16MetricCounter].fMetricValue = getChamberTemperature().fDampedValue; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Chamber Temperature Predict 60s | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "chamber_temperature_pred60"); | ||||
|         aMetrics[u16MetricCounter].type = FLOAT; | ||||
| @ -158,12 +152,6 @@ void taskMetrics(void *pvParameters) | ||||
|         aMetrics[u16MetricCounter].fMetricValue = getInletFlowTemperature().average60s.fValue; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Inlet Flow Temperature Damped | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "inlet_flow_temperature_damped"); | ||||
|         aMetrics[u16MetricCounter].type = FLOAT; | ||||
|         aMetrics[u16MetricCounter].fMetricValue = getInletFlowTemperature().fDampedValue; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Inlet Flow Temperature Predict 60s | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "inlet_flow_temperature_pred60"); | ||||
|         aMetrics[u16MetricCounter].type = FLOAT; | ||||
| @ -188,12 +176,6 @@ void taskMetrics(void *pvParameters) | ||||
|         aMetrics[u16MetricCounter].fMetricValue = getOutdoorTemperature().average60s.fValue; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Outdoor Temperature Average Damped | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "outdoor_temperature_damped"); | ||||
|         aMetrics[u16MetricCounter].type = FLOAT; | ||||
|         aMetrics[u16MetricCounter].fMetricValue = getOutdoorTemperature().fDampedValue; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Outdoor Temperature Predict 60s | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "outdoor_temperature_pred60"); | ||||
|         aMetrics[u16MetricCounter].type = FLOAT; | ||||
| @ -218,12 +200,6 @@ void taskMetrics(void *pvParameters) | ||||
|         aMetrics[u16MetricCounter].fMetricValue = getReturnFlowTemperature().average60s.fValue; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Return Flow Temperature Damped | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "return_flow_temperature_damped"); | ||||
|         aMetrics[u16MetricCounter].type = FLOAT; | ||||
|         aMetrics[u16MetricCounter].fMetricValue = getReturnFlowTemperature().fDampedValue; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Return Flow Temperature Predict 60s | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "return_flow_temperature_pred60"); | ||||
|         aMetrics[u16MetricCounter].type = FLOAT; | ||||
| @ -254,31 +230,6 @@ void taskMetrics(void *pvParameters) | ||||
|         aMetrics[u16MetricCounter].u8MetricValue = getControlState(); | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Control Current Weekday | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "control_current_weekday"); | ||||
|         aMetrics[u16MetricCounter].type = INTEGER_U8; | ||||
|         aMetrics[u16MetricCounter].u8MetricValue = getControlCurrentWeekday(); | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Control Current Entry Time | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "control_current_entry_time"); | ||||
|         aMetrics[u16MetricCounter].type = INTEGER_64; | ||||
|         int64_t i64SecondsSinceMidnight = (getControlCurrentTemperatureEntry().timestamp.hour * 60U * 60U) + (getControlCurrentTemperatureEntry().timestamp.minute * 60U); | ||||
|         aMetrics[u16MetricCounter].i64MetricValue = i64SecondsSinceMidnight; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Control Current Entry Chamber Temperature | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "control_current_entry_chamber_temperature"); | ||||
|         aMetrics[u16MetricCounter].type = FLOAT; | ||||
|         aMetrics[u16MetricCounter].fMetricValue = getControlCurrentTemperatureEntry().fChamberTemperature; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // Control Current Entry Return Flow Temperature | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "control_current_entry_return_flow_temperature"); | ||||
|         aMetrics[u16MetricCounter].type = FLOAT; | ||||
|         aMetrics[u16MetricCounter].fMetricValue = getControlCurrentTemperatureEntry().fReturnFlowTemperature; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         // SNTP State | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "sntp_state"); | ||||
|         aMetrics[u16MetricCounter].type = INTEGER_U8; | ||||
| @ -307,7 +258,6 @@ void taskMetrics(void *pvParameters) | ||||
|         aMetrics[u16MetricCounter].i64MetricValue = ap.rssi; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
|         ESP_ERROR_CHECK(u16MetricCounter > METRIC_MAX_COUNT); | ||||
|         vSetMetrics(aMetrics, u16MetricCounter); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -4,7 +4,7 @@ | ||||
|  | ||||
| #define HTML_RESPONSE_SIZE 4096U | ||||
| #define METRIC_NAME_MAX_SIZE 64U | ||||
| #define METRIC_MAX_COUNT 38U | ||||
| #define METRIC_MAX_COUNT 32U | ||||
|  | ||||
| typedef enum _MetricValueType | ||||
| { | ||||
|  | ||||
| @ -85,7 +85,7 @@ void checkSensorSanity(void) | ||||
|  | ||||
|         if (sCurrentMeasurement.state == MEASUREMENT_FAULT) | ||||
|         { | ||||
|             ESP_LOGE(TAG, "%s Sensor not found!", sanityChecks[i].name); | ||||
|             //ESP_LOGE(TAG, "%s Sensor not found!", sanityChecks[i].name); | ||||
|             sanityChecks[i].state = SENSOR_NOT_FOUND; | ||||
|             sSafetyState = SAFETY_SENSOR_ERROR; | ||||
|         } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user