2020-04-20 21:16:39 +02:00
|
|
|
/* Reflow Oven Controller
|
2020-08-31 22:50:39 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
|
|
|
|
#include <reflow-controller/pid-controller.h>
|
2020-06-13 23:23:59 +02:00
|
|
|
#include <string.h>
|
2020-04-20 21:16:39 +02:00
|
|
|
|
2020-08-31 22:50:39 +02:00
|
|
|
void pid_init(struct pid_controller *pid, float k_deriv, float k_int, float k_p, float output_sat_min,
|
2021-01-24 19:56:00 +01:00
|
|
|
float output_sat_max, float integral_max, float kd_tau, float sample_period)
|
2020-04-20 21:16:39 +02:00
|
|
|
{
|
|
|
|
if (!pid)
|
|
|
|
return;
|
|
|
|
|
2020-05-25 01:56:54 +02:00
|
|
|
pid->sample_period = sample_period;
|
2020-04-20 21:16:39 +02:00
|
|
|
pid->k_p = k_p;
|
|
|
|
pid->k_int = k_int;
|
|
|
|
pid->k_deriv = k_deriv;
|
2020-06-09 23:01:04 +02:00
|
|
|
pid->k_int_t = pid->k_int * pid->sample_period * 0.5f;
|
2021-01-09 23:44:15 +01:00
|
|
|
pid->k_deriv_t = pid->k_deriv * 2.0f / (pid->sample_period + 2.0f * kd_tau);
|
|
|
|
pid->k_inv_deriv_t = (2.0f * kd_tau - sample_period) / (2.0f * kd_tau + sample_period);
|
2020-04-20 21:16:39 +02:00
|
|
|
pid->output_sat_max = output_sat_max;
|
|
|
|
pid->output_sat_min = output_sat_min;
|
|
|
|
pid->integral_max = integral_max;
|
2020-05-25 01:56:54 +02:00
|
|
|
|
|
|
|
pid_zero(pid);
|
2020-04-20 21:16:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 23:23:59 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:16:39 +02:00
|
|
|
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)
|
|
|
|
{
|
2020-05-25 01:56:54 +02:00
|
|
|
pid->integral = pid->integral + pid->k_int_t * (deviation + pid->last_in);
|
2020-04-20 21:16:39 +02:00
|
|
|
|
2020-11-30 21:40:28 +01:00
|
|
|
/* Saturate integral term to specified maximum */
|
2020-08-31 22:50:39 +02:00
|
|
|
if (pid->integral > pid->integral_max)
|
2020-04-20 21:16:39 +02:00
|
|
|
pid->integral = pid->integral_max;
|
2021-03-21 19:18:08 +01:00
|
|
|
else if (pid->integral < 0.0f)
|
|
|
|
pid->integral = 0.0f;
|
2020-04-20 21:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float pid_sample(struct pid_controller *pid, float deviation)
|
|
|
|
{
|
|
|
|
float output;
|
|
|
|
|
|
|
|
if (!pid)
|
2020-10-31 01:11:17 +01:00
|
|
|
return 0.0f;
|
2020-04-20 21:16:39 +02:00
|
|
|
|
|
|
|
output = deviation * pid->k_p;
|
|
|
|
|
2020-09-27 22:54:06 +02:00
|
|
|
/* PID runaway compensation */
|
2020-11-30 21:40:28 +01:00
|
|
|
if (!(deviation > 0.0f && pid->control_output > pid->output_sat_max - 0.5f && pid->integral > 0) &&
|
|
|
|
!(deviation < 0.0f && pid->control_output < pid->output_sat_min + 0.5f && pid->integral < 0)) {
|
2020-04-20 21:16:39 +02:00
|
|
|
calculate_integral(pid, deviation);
|
|
|
|
}
|
|
|
|
|
2020-05-25 01:56:54 +02:00
|
|
|
/* Calculate derivative part */
|
2020-11-30 21:40:28 +01:00
|
|
|
pid->derivate = pid->k_deriv_t * (deviation - pid->last_in) - pid->k_inv_deriv_t * pid->derivate;
|
2020-04-20 21:16:39 +02:00
|
|
|
|
2020-05-25 01:56:54 +02:00
|
|
|
output += pid->derivate;
|
|
|
|
output += pid->integral;
|
2020-04-20 21:16:39 +02:00
|
|
|
|
2020-05-25 01:56:54 +02:00
|
|
|
/* Saturate output */
|
2020-04-20 21:16:39 +02:00
|
|
|
if (output < pid->output_sat_min)
|
|
|
|
output = pid->output_sat_min;
|
2020-05-25 01:56:54 +02:00
|
|
|
else if (output > pid->output_sat_max)
|
|
|
|
output = pid->output_sat_max;
|
2020-04-20 21:16:39 +02:00
|
|
|
|
|
|
|
pid->control_output = output;
|
2020-05-25 01:56:54 +02:00
|
|
|
pid->last_in = deviation;
|
2020-04-20 21:16:39 +02:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
float pid_get_control_output(const struct pid_controller *pid)
|
|
|
|
{
|
|
|
|
if (!pid)
|
|
|
|
return 0.0f;
|
|
|
|
|
|
|
|
return pid->control_output;
|
|
|
|
}
|