# HG changeset patch # User Ethan Zonca # Date 2023-07-17 17:16:01 # Node ID c1b2840961f0c69081210217ef8241e9c2826957 # Parent 9ae5559ab974a7468dd21f5730a338986dfe88d3 Hacking thermostatic mode into operation. This code needs some love. diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + @@ -16,7 +16,7 @@ - + diff --git a/inc/config.h b/inc/config.h --- a/inc/config.h +++ b/inc/config.h @@ -51,6 +51,9 @@ #define DEFAULT_SETPOINT_COUNT 1 #define DEFAULT_SETPOINT_AUX_SELECT_ENABLE 0 +#define DEFAULT_CONTROL_MODE 1 // thermostat +#define DEFAULT_PLANT_TYPE 1 + ////////////////////////////////////////////////////// // Watchdog Settings diff --git a/inc/pwmout.h b/inc/pwmout.h --- a/inc/pwmout.h +++ b/inc/pwmout.h @@ -6,9 +6,6 @@ #include "flash.h" #include "error.h" -#define SSR_PIN GPIO_PIN_7 -#define SSR_GPIO_Port GPIOB -#define SSR SSR_GPIO_Port, SSR_PIN diff --git a/inc/system/gpio.h b/inc/system/gpio.h --- a/inc/system/gpio.h +++ b/inc/system/gpio.h @@ -43,6 +43,10 @@ #define AUX_RETURN_GPIO_Port GPIOA #define AUX_RETURN AUX_RETURN_GPIO_Port , AUX_RETURN_Pin +#define SSR_PIN GPIO_PIN_7 +#define SSR_GPIO_Port GPIOB +#define SSR SSR_GPIO_Port, SSR_PIN + void user_input(uint16_t* to_modify); void user_input_min_max(uint16_t* to_modify, uint16_t min, uint16_t max); diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -80,7 +80,7 @@ int main(void) if(flash_getsettings()->val.control_mode == MODE_PID && (HAL_GetTick() - last_pid > PID_PERIOD)) { - duty = pid_process(); + //duty = pid_process(); last_pid = HAL_GetTick(); } @@ -91,7 +91,7 @@ int main(void) last_thermostat = HAL_GetTick(); } - pwmout_process((int16_t)duty); + //pwmout_process((int16_t)duty); display_process(); watchdog_feed(); diff --git a/src/pwmout.c b/src/pwmout.c --- a/src/pwmout.c +++ b/src/pwmout.c @@ -88,8 +88,8 @@ void pwmout_process(int16_t duty) { if(duty == 0) { - HAL_GPIO_WritePin(SSR, 0); - HAL_GPIO_WritePin(LED, 0); +// HAL_GPIO_WritePin(SSR, 0); +// HAL_GPIO_WritePin(LED, 0); } if(duty < 0) duty = 0; diff --git a/src/system/flash.c b/src/system/flash.c --- a/src/system/flash.c +++ b/src/system/flash.c @@ -77,6 +77,8 @@ void flash_restoresettings(void) settings.val.windup_guard = DEFAULT_WINDUP_GUARD; settings.val.sensor_type = 1; settings.val.setpoint_count = DEFAULT_SETPOINT_COUNT; + settings.val.control_mode = DEFAULT_CONTROL_MODE; + settings.val.plant_type = DEFAULT_PLANT_TYPE; } } diff --git a/src/system/interrupts.c b/src/system/interrupts.c --- a/src/system/interrupts.c +++ b/src/system/interrupts.c @@ -66,8 +66,8 @@ void HAL_TIM_PWM_PulseFinishedCallback(T { if(htim == pwmout_get_tim()) { - HAL_GPIO_WritePin(SSR, 0); - HAL_GPIO_WritePin(LED, 0); +// HAL_GPIO_WritePin(SSR, 0); +// HAL_GPIO_WritePin(LED, 0); } } @@ -77,13 +77,13 @@ void HAL_TIM_PeriodElapsedCallback(TIM_H { if(htim->Instance->CCR1 == 0) { - HAL_GPIO_WritePin(LED, 0); - HAL_GPIO_WritePin(SSR, 0); +// HAL_GPIO_WritePin(LED, 0); +// HAL_GPIO_WritePin(SSR, 0); } else { - HAL_GPIO_WritePin(LED, 1); - HAL_GPIO_WritePin(SSR, 1); +// HAL_GPIO_WritePin(LED, 1); +// HAL_GPIO_WritePin(SSR, 1); } } } diff --git a/src/thermostat.c b/src/thermostat.c --- a/src/thermostat.c +++ b/src/thermostat.c @@ -3,8 +3,10 @@ // #include "thermostat.h" +#include "gpio.h" -// PID implementation +// Thermostatic control implementation +static uint8_t thermostat_plant_on = 0; float thermostat_process(void) { @@ -15,23 +17,28 @@ float thermostat_process(void) // max31855_readtemp(spi_get(), &set, &status); // Read MAX31855 // #endif - float ssr_output = 0.0; - uint8_t thermostat_plant_on = 0; // TODO: Migrate this FxP conversion to the readtemp code or similar - float temp = runtime_status()->temp; - + float temp = runtime_status()->temp; // (scale to whole degrees) + uint32_t hyst = flash_getsettings()->val.hysteresis; // EMZ FIXME: This could be way simpler - if(flash_getsettings()->val.plant_type == PLANT_HEATER && runtime_status()->setpoint * 10 < temp - flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 1; - else if(flash_getsettings()->val.plant_type == PLANT_HEATER && runtime_status()->setpoint * 10 > temp + flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 0; - if(flash_getsettings()->val.plant_type == PLANT_COOLER && runtime_status()->setpoint * 10 > temp + flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 1; - else if(flash_getsettings()->val.plant_type == PLANT_COOLER && runtime_status()->setpoint * 10 < temp - flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 0; + if(flash_getsettings()->val.plant_type == PLANT_HEATER) + { + if(temp < runtime_status()->setpoint - hyst) + thermostat_plant_on = 1; + if(temp > runtime_status()->setpoint + hyst) + thermostat_plant_on = 0; + } + + if(flash_getsettings()->val.plant_type == PLANT_COOLER) + { + if(temp < runtime_status()->setpoint - hyst) + thermostat_plant_on = 0; + if(temp > runtime_status()->setpoint + hyst) + thermostat_plant_on = 1; + } // EMZ: TODO: Refactor to output_enabled or something if(runtime_status()->pid_enabled && thermostat_plant_on) @@ -40,21 +47,24 @@ float thermostat_process(void) // put ssr output on display ssd1306_drawstring(" ", 0, 90); //fixme: this is bad, but I can't get the old digits to clear otherwise char tempstr[6]; - itoa(ssr_output, tempstr, 10); + itoa(thermostat_plant_on, tempstr, 10); ssd1306_drawstring(tempstr, 0, 90); - // HAL_GPIO_WritePin(SSR_PIN, 1); + HAL_GPIO_WritePin(SSR, 1); HAL_GPIO_WritePin(LED, 1); } else { - // HAL_GPIO_WritePin(SSR_PIN, 0); - HAL_GPIO_WritePin(LED, 0); + HAL_GPIO_WritePin(SSR, 0); + HAL_GPIO_WritePin(LED, 0); } - return ssr_output; + if(thermostat_plant_on) + return 1000.0f; + else + return 0.0f; }