# HG changeset patch # User Ethan Zonca # Date 2017-07-27 13:12:04 # Node ID f153d4ca027cc4b85d12f9a87142ad86d5d7c3d3 # Parent a2c96432427bf7ed4d59fd1320404cbdd1805a19 PID control kinda working, still can't do 0percent duty diff --git a/inc/pwmout.h b/inc/pwmout.h --- a/inc/pwmout.h +++ b/inc/pwmout.h @@ -9,7 +9,7 @@ void pwmout_init(void); -void pwmout_process(uint16_t duty); +void pwmout_process(int16_t duty); TIM_HandleTypeDef* pwmout_get_tim(void); #endif diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -52,9 +52,10 @@ int main(void) uint32_t last_1hz = 0; uint32_t last_5hz = 0; + float duty = 0.0; + while (1) { - float duty = 0.0; if(HAL_GetTick() - last_1hz > 750) { @@ -84,7 +85,7 @@ int main(void) last_thermostat = HAL_GetTick(); } - pwmout_process(duty); + pwmout_process((int16_t)duty); display_process(); watchdog_feed(); diff --git a/src/pid.c b/src/pid.c --- a/src/pid.c +++ b/src/pid.c @@ -52,7 +52,7 @@ float pid_process(void) ssr_output = 0.0; } - return ssr_output; + return ssr_output; //ssr_output; } int16_t pid_update(void) @@ -64,7 +64,7 @@ int16_t pid_update(void) float temp = status->temp; // Calculate instantaneous error - int16_t error = status->setpoint * 10 - temp; // TODO: Use fixed point fraction + int16_t error = status->setpoint - temp; // TODO: Use fixed point fraction // Proportional component int32_t p_term = set->val.k_p * error; @@ -78,7 +78,7 @@ int16_t pid_update(void) // it cant help be cold despite its best efforts) // not necessary, but this makes windup guard values // relative to the current iGain - int32_t windup_guard_res = (set->val.windup_guard * 10) / set->val.k_i; + int32_t windup_guard_res = (set->val.windup_guard) / set->val.k_i; // Calculate integral term with windup guard if (state.i_state > windup_guard_res) diff --git a/src/pwmout.c b/src/pwmout.c --- a/src/pwmout.c +++ b/src/pwmout.c @@ -86,10 +86,19 @@ void pwmout_init(void) // functionality. this is a problem. // also duty cycling isn't working correctly... -void pwmout_process(uint16_t duty) +void pwmout_process(int16_t duty) { + if(duty == 0) + { + HAL_GPIO_WritePin(SSR, 0); + HAL_GPIO_WritePin(LED, 0); + // TODO: Disable timer + } + if(duty < 0) + duty = -duty; - htim17.Instance->CCR1 = duty; + + htim17.Instance->CCR1 = duty; //duty; // HAL_GPIO_TogglePin(SSR_GPIO_Port, SSR_PIN); // uint32_t ssr_output = duty; // meh