diff --git a/stm-firmware/include/reflow-controller/oven-driver.h b/stm-firmware/include/reflow-controller/oven-driver.h index 4690419..7df463c 100644 --- a/stm-firmware/include/reflow-controller/oven-driver.h +++ b/stm-firmware/include/reflow-controller/oven-driver.h @@ -22,6 +22,16 @@ #define __OVEN_DRIVER_H__ #include +#include +#include + +struct oven_pid_status { + bool active; + bool aborted; + float target_temp; + float current_temp; + uint64_t timestamp_last_run; +}; void oven_driver_init(void); @@ -29,4 +39,14 @@ void oven_driver_set_power(uint8_t power); void oven_driver_disable(void); +void oven_pid_init(struct pid_controller *controller_to_copy); + +void oven_pid_handle(float target_temp, float current_temp); + +void oven_pid_stop(); + +void oven_pid_report_error(void); + +const struct oven_pid_status *oven_pid_get_status(void); + #endif /* __OVEN_DRIVER_H__ */ diff --git a/stm-firmware/include/reflow-controller/pid-controller.h b/stm-firmware/include/reflow-controller/pid-controller.h index c2914cc..87abd5a 100644 --- a/stm-firmware/include/reflow-controller/pid-controller.h +++ b/stm-firmware/include/reflow-controller/pid-controller.h @@ -45,4 +45,6 @@ float pid_sample(struct pid_controller *pid, float deviation); float pid_get_control_output(const struct pid_controller *pid); +int pid_copy(struct pid_controller *dest, const struct pid_controller *src); + #endif /* __PID_CONTROLLER_H__ */ diff --git a/stm-firmware/main.c b/stm-firmware/main.c index 14db170..fec8370 100644 --- a/stm-firmware/main.c +++ b/stm-firmware/main.c @@ -37,7 +37,6 @@ #include #include "fatfs/shimatta_sdio_driver/shimatta_sdio.h" #include -#include #include #include #include @@ -61,7 +60,6 @@ static void setup_nvic_priorities() /* Process parameters are defined static globally to be watched in debugger from any context */ static float target_temperature; -static struct pid_controller pid; FATFS fs; FATFS *fs_ptr = &fs; @@ -219,9 +217,6 @@ int main() shell_handle = shell_init(write_shell_callback); shell_print_motd(shell_handle); - pid_init(&pid, 0.1, 0.1, 4.0, 0.0, 100.0, 40.0, 0.25); - pid_zero(&pid); - while (1) { sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted); diff --git a/stm-firmware/oven-driver.c b/stm-firmware/oven-driver.c index 7a01a9f..4084b59 100644 --- a/stm-firmware/oven-driver.c +++ b/stm-firmware/oven-driver.c @@ -21,6 +21,17 @@ #include #include #include +#include + +static struct pid_controller oven_pid; + +static struct oven_pid_status oven_pid_current_status = { + .active = false, + .aborted = false, + .target_temp = 0.0f, + .current_temp = 0.0f, + .timestamp_last_run = 0ULL +}; void oven_driver_init() { @@ -57,3 +68,47 @@ void oven_driver_disable() rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(OVEN_CONTROLLER_PORT_RCC_MASK)); rcc_manager_disable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(OVEN_CONTROLLER_TIM_RCC_MASK)); } + +void oven_pid_init(struct pid_controller *controller_to_copy) +{ + pid_copy(&oven_pid, controller_to_copy); + oven_pid.output_sat_min = 0.0f; + oven_pid.output_sat_max = 100.0f; + oven_pid_current_status.timestamp_last_run = 0ULL; + oven_pid_current_status.active = true; +} + +void oven_pid_handle(float target_temp, float current_temp) +{ + float pid_out; + + if (oven_pid_current_status.active) { + if (systick_ticks_have_passed(oven_pid_current_status.timestamp_last_run, + (uint64_t)(oven_pid.sample_period * 1000))) { + pid_out = pid_sample(&oven_pid, target_temp - current_temp); + oven_driver_set_power((uint8_t)pid_out); + oven_pid_current_status.timestamp_last_run = systick_get_global_tick(); + oven_pid_current_status.active = true; + oven_pid_current_status.target_temp = target_temp; + oven_pid_current_status.current_temp = current_temp; + } + } +} + +void oven_pid_report_error() +{ + oven_pid_current_status.active = false; + oven_pid_current_status.aborted = true; +} + +const struct oven_pid_status *oven_pid_get_status() +{ + return &oven_pid_current_status; +} + +void oven_pid_stop() +{ + oven_pid_current_status.active = false; + oven_pid_current_status.target_temp = 0.0f; + oven_pid_current_status.current_temp = 0.0f; +} diff --git a/stm-firmware/pid-controller.c b/stm-firmware/pid-controller.c index 583fdae..04eecc3 100644 --- a/stm-firmware/pid-controller.c +++ b/stm-firmware/pid-controller.c @@ -19,6 +19,7 @@ */ #include +#include void pid_init(struct pid_controller *pid, float k_deriv, float k_int, float k_p, float output_sat_min, float output_sat_max, float integral_max, float sample_period) { @@ -38,6 +39,16 @@ void pid_init(struct pid_controller *pid, float k_deriv, float k_int, float k_p, pid_zero(pid); } +int pid_copy(struct pid_controller *dest, const struct pid_controller *src) +{ + if (!dest || !src) + return -1; + + memcpy(dest, src, sizeof(struct pid_controller)); + + return 0; +} + void pid_zero(struct pid_controller *pid) { pid->control_output = 0.0f;