2020-04-20 21:16:39 +02:00
|
|
|
/* 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/>.
|
|
|
|
*/
|
|
|
|
|
2021-07-15 00:20:03 +02:00
|
|
|
/** @addtogroup pid-controller
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2020-04-20 21:16:39 +02:00
|
|
|
#ifndef __PID_CONTROLLER_H__
|
|
|
|
#define __PID_CONTROLLER_H__
|
|
|
|
|
2021-07-15 00:20:03 +02:00
|
|
|
/**
|
|
|
|
* @brief Representation of a PID controller
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
struct pid_controller {
|
2021-07-15 00:20:03 +02:00
|
|
|
/**
|
|
|
|
* @brief Derivate term @f$k_d@f$
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
float k_deriv;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Integral term @f$k_i@f$
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
float k_int;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Proportional term @f$k_p@f$
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
float k_p;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Internally precalculated auxiliary value. @f$k_{i_t} = \frac{1}{2}k_{i} \cdot T_s@f$
|
|
|
|
*
|
|
|
|
* This value is caluclated during the @ref pid_init function to reduce calculations during the continous
|
|
|
|
* PID sampling.
|
|
|
|
*/
|
2020-05-25 01:56:54 +02:00
|
|
|
float k_int_t;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Internally precalculated auxiliary value. @f$k_{d_t} = 2\frac{k_{d}}{T_s + 2 k_{d\tau}}@f$
|
|
|
|
*
|
|
|
|
* This value is caluclated during the @ref pid_init function to reduce calculations during the continous
|
|
|
|
* PID sampling.
|
|
|
|
*/
|
2020-05-25 01:56:54 +02:00
|
|
|
float k_deriv_t;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Internally precalculated auxiliary value. @f$\overline{k_{d_t}} = \frac{2 k_{d\tau} - T_s}{ 2k_{d\tau} + T_s}@f$
|
|
|
|
*
|
|
|
|
* This value is caluclated during the @ref pid_init function to reduce calculations during the continous
|
|
|
|
* PID sampling.
|
|
|
|
*/
|
2020-11-30 21:43:38 +01:00
|
|
|
float k_inv_deriv_t;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Saturation of output value
|
|
|
|
* @note This value is set to 100 (corresponding to 100%) for the temperature controller
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
float output_sat_max;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Minumum saturation of PID output value.
|
|
|
|
* @note This value is set to 0 for the temperature controller as the oven driver is not able to cool
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
float output_sat_min;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Maximum value @f$i_{max}@f$ the inegrator in the PID controller can reach.
|
|
|
|
*
|
|
|
|
* The output of the integral term of the PID controller is limited to @f$\pm i_{max}@f$
|
|
|
|
*
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
float integral_max;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sampling period @f$T_s@f$ of the PID controller in seconds
|
|
|
|
*/
|
2020-05-25 01:56:54 +02:00
|
|
|
float sample_period;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Output value of the PID controller
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
volatile float control_output;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Internal state variable holding the last input value.
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
volatile float last_in;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief integral term's value
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
volatile float integral;
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief derivate term's value
|
|
|
|
*/
|
2020-05-25 01:56:54 +02:00
|
|
|
volatile float derivate;
|
2020-04-20 21:16:39 +02:00
|
|
|
};
|
|
|
|
|
2021-07-15 00:20:03 +02:00
|
|
|
/**
|
|
|
|
* @brief Initialize a @ref pid_controller struct
|
|
|
|
* @param pid Struct to initilaize
|
|
|
|
* @param k_deriv Derivate term
|
|
|
|
* @param k_int integral term
|
|
|
|
* @param k_p Proportional term
|
|
|
|
* @param output_sat_min Minimum output saturation
|
|
|
|
* @param output_sat_max Maximum output saturation
|
|
|
|
* @param integral_max Maximum integral term
|
|
|
|
* @param kd_tau Time constant of derivate term low pass filter
|
|
|
|
* @param sample_period Sample time in seconds
|
|
|
|
*/
|
2021-01-24 19:56:00 +01:00
|
|
|
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 kd_tau, float sample_period);
|
2020-04-20 21:16:39 +02:00
|
|
|
|
2021-07-15 00:20:03 +02:00
|
|
|
/**
|
|
|
|
* @brief Reset a PID controller.
|
|
|
|
*
|
|
|
|
* This sets all internal states to 0.
|
|
|
|
*
|
|
|
|
* @param pid Controller
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
void pid_zero(struct pid_controller *pid);
|
|
|
|
|
2021-07-15 00:20:03 +02:00
|
|
|
/**
|
|
|
|
* @brief Execute a tiome step of the PID controller. This function must be periodically called in an pid_controller::sample_period interval
|
|
|
|
* @note This function does not check its calling interval. It must be ensured by the caller.
|
|
|
|
* @param pid Pid controller to execute
|
|
|
|
* @param deviation Input deviation
|
|
|
|
* @return Current controller output after the calculated time step
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
float pid_sample(struct pid_controller *pid, float deviation);
|
|
|
|
|
2021-07-15 00:20:03 +02:00
|
|
|
/**
|
|
|
|
* @brief Retrieve the controller's current output
|
|
|
|
* @param pid Pid controller
|
|
|
|
* @return Output
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
float pid_get_control_output(const struct pid_controller *pid);
|
|
|
|
|
2021-07-15 00:20:03 +02:00
|
|
|
/**
|
|
|
|
* @brief Duplicate a PID controller
|
|
|
|
* @param dest destination controller
|
|
|
|
* @param src Source controller
|
|
|
|
* @return 0 if successful
|
|
|
|
*/
|
2020-06-13 23:23:59 +02:00
|
|
|
int pid_copy(struct pid_controller *dest, const struct pid_controller *src);
|
|
|
|
|
2020-04-20 21:16:39 +02:00
|
|
|
#endif /* __PID_CONTROLLER_H__ */
|
2021-07-15 00:20:03 +02:00
|
|
|
|
|
|
|
/** @} */
|