Add PID controller to oven driver module

This commit is contained in:
Mario Hüttel 2020-06-13 23:23:59 +02:00
parent a6dc4f9b46
commit 2547c134f2
5 changed files with 88 additions and 5 deletions

View File

@ -22,6 +22,16 @@
#define __OVEN_DRIVER_H__
#include <stdint.h>
#include <stdbool.h>
#include <reflow-controller/pid-controller.h>
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__ */

View File

@ -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__ */

View File

@ -37,7 +37,6 @@
#include <reflow-controller/digio.h>
#include "fatfs/shimatta_sdio_driver/shimatta_sdio.h"
#include <reflow-controller/temp-converter.h>
#include <reflow-controller/pid-controller.h>
#include <stm-periph/stm32-gpio-macros.h>
#include <stm-periph/clock-enable-manager.h>
#include <stm-periph/uart.h>
@ -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);

View File

@ -21,6 +21,17 @@
#include <reflow-controller/oven-driver.h>
#include <reflow-controller/periph-config/oven-driver-hwcfg.h>
#include <stm-periph/clock-enable-manager.h>
#include <reflow-controller/systick.h>
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;
}

View File

@ -19,6 +19,7 @@
*/
#include <reflow-controller/pid-controller.h>
#include <string.h>
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;