Add PID controller and oven driver module
This commit is contained in:
parent
985c29b281
commit
c419ca7bcb
@ -53,6 +53,8 @@ CFILES += stack-check.c
|
||||
|
||||
CFILES += fatfs/diskio.c fatfs/ff.c fatfs/ffsystem.c fatfs/ffunicode.c fatfs/shimatta_sdio_driver/shimatta_sdio.c
|
||||
|
||||
CFILES += pid-controller.c oven-driver.c
|
||||
|
||||
DEFINES += -DDEBUGBUILD
|
||||
|
||||
###################################################################################
|
||||
|
24
stm-firmware/include/reflow-controller/oven-driver.h
Normal file
24
stm-firmware/include/reflow-controller/oven-driver.h
Normal file
@ -0,0 +1,24 @@
|
||||
/* Reflow Oven Controller
|
||||
*
|
||||
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
|
||||
*
|
||||
* This file is part of the Reflow Oven Controller Project.
|
||||
*
|
||||
* The reflow oven controller is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the reflow oven controller project.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __OVEN_DRIVER_H__
|
||||
#define __OVEN_DRIVER_H__
|
||||
|
||||
#endif /* __OVEN_DRIVER_H__ */
|
44
stm-firmware/include/reflow-controller/pid-controller.h
Normal file
44
stm-firmware/include/reflow-controller/pid-controller.h
Normal file
@ -0,0 +1,44 @@
|
||||
/* Reflow Oven Controller
|
||||
*
|
||||
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
|
||||
*
|
||||
* This file is part of the Reflow Oven Controller Project.
|
||||
*
|
||||
* The reflow oven controller is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the reflow oven controller project.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __PID_CONTROLLER_H__
|
||||
#define __PID_CONTROLLER_H__
|
||||
|
||||
struct pid_controller {
|
||||
float k_deriv;
|
||||
float k_int;
|
||||
float k_p;
|
||||
float output_sat_max;
|
||||
float output_sat_min;
|
||||
float integral_max;
|
||||
volatile float control_output;
|
||||
volatile float last_in;
|
||||
volatile float integral;
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
void pid_zero(struct pid_controller *pid);
|
||||
|
||||
float pid_sample(struct pid_controller *pid, float deviation);
|
||||
|
||||
float pid_get_control_output(const struct pid_controller *pid);
|
||||
|
||||
#endif /* __PID_CONTROLLER_H__ */
|
21
stm-firmware/oven-driver.c
Normal file
21
stm-firmware/oven-driver.c
Normal file
@ -0,0 +1,21 @@
|
||||
/* Reflow Oven Controller
|
||||
*
|
||||
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
|
||||
*
|
||||
* This file is part of the Reflow Oven Controller Project.
|
||||
*
|
||||
* The reflow oven controller is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the reflow oven controller project.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <reflow-controller/oven-driver.h>
|
90
stm-firmware/pid-controller.c
Normal file
90
stm-firmware/pid-controller.c
Normal file
@ -0,0 +1,90 @@
|
||||
/* Reflow Oven Controller
|
||||
*
|
||||
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
|
||||
*
|
||||
* This file is part of the Reflow Oven Controller Project.
|
||||
*
|
||||
* The reflow oven controller is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the reflow oven controller project.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <reflow-controller/pid-controller.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)
|
||||
{
|
||||
if (!pid)
|
||||
return;
|
||||
|
||||
pid->k_p = k_p;
|
||||
pid->k_int = k_int;
|
||||
pid->k_deriv = k_deriv;
|
||||
pid->output_sat_max = output_sat_max;
|
||||
pid->output_sat_min = output_sat_min;
|
||||
pid->control_output = 0;
|
||||
pid->integral_max = integral_max;
|
||||
}
|
||||
|
||||
void pid_zero(struct pid_controller *pid)
|
||||
{
|
||||
pid->control_output = 0.0f;
|
||||
pid->last_in = 0.0f;
|
||||
pid->integral = 0.0f;
|
||||
}
|
||||
|
||||
static void calculate_integral(struct pid_controller *pid, float deviation)
|
||||
{
|
||||
pid->integral += deviation * pid->k_int;
|
||||
|
||||
if (pid->integral > pid->integral_max) {
|
||||
pid->integral = pid->integral_max;
|
||||
} else if (pid->integral < -pid->integral_max) {
|
||||
pid->integral = -pid->integral_max;
|
||||
}
|
||||
}
|
||||
|
||||
float pid_sample(struct pid_controller *pid, float deviation)
|
||||
{
|
||||
float output;
|
||||
|
||||
if (!pid)
|
||||
return 0.0;
|
||||
|
||||
output = deviation * pid->k_p;
|
||||
|
||||
if (!(deviation > 0.0f && pid->control_output > pid->output_sat_max - 0.5) &&
|
||||
!(deviation < 0.0f && pid->control_output < pid->output_sat_min + 0.5)) {
|
||||
calculate_integral(pid, deviation);
|
||||
}
|
||||
|
||||
output += pid->integral;
|
||||
output -= (deviation - pid->last_in) * pid->k_deriv;
|
||||
|
||||
pid->last_in = deviation;
|
||||
|
||||
if (output > pid->output_sat_max)
|
||||
output = pid->output_sat_max;
|
||||
if (output < pid->output_sat_min)
|
||||
output = pid->output_sat_min;
|
||||
|
||||
pid->control_output = output;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
float pid_get_control_output(const struct pid_controller *pid)
|
||||
{
|
||||
if (!pid)
|
||||
return 0.0f;
|
||||
|
||||
return pid->control_output;
|
||||
}
|
Loading…
Reference in New Issue
Block a user