Basic_Frame_TypeC_2023_Omni
Loading...
Searching...
No Matches
pid.cpp File Reference

Implementation of all control PID systems. More...

#include "pid.h"
#include "uarm_lib.hpp"
#include "uarm_math.hpp"

Functions

void pid_param_init (PID_t *pid, int32_t max_out, float max_i_out, float max_err, float kp, float ki, float kd)
 Initializes a PID_t structure with provided parameters.
float pid_calculate (PID_t *pid, float cur_val, float target_val, float dt)
 Computes a single PID iteration for one time-step.
float pid_incremental_calculate (PID_t *pid, float cur_val, float target_val)
 Calculates incremental PID output for one time-step.
float pid_single_loop_control (float target_val, PID_t *pid, float cur_val, float dt)
 Calculates iteration of single-loop PID controller.
float pid_dual_loop_control (float f_tar_val, PID_t *f_pid, PID_t *s_pid, float f_cur_val, float s_cur_val, float dt)
 Calculate an iteration of dual-loop cascade PID controller.
void pid2_init (PID2_t &pid, float k_p, float k_i, float k_d, float beta, float yeta, float min_out, float max_out)
 Initializes a PID2_t controller.
void pid2_set_limits (PID2_t &pid, float new_min_out, float new_max_out)
 Sets new output limits for PID2_t controller.
float pid2_calculate (PID2_t &pid, float sp, float pv, float dt)
 Calculates PID2 output for one time-step.
float pid2_single_loop_control (PID2_t &pid, float sp, float pv, float dt)
 Caculate an iteration of a single-loop PID2 controller.
float pid2_dual_loop_control (PID2_t &f_pid, PID2_t &s_pid, float sp, float f_pv, float s_pv, float f_dt, float s_dt)
 Calculates an iteration of a dual-loop PID2 controller.
float pid2_triple_loop_control (PID2_t &outer_pid, PID2_t &middle_pid, PID2_t &inner_pid, float sp, float outer_pv, float middle_pv, float inner_pv, float outer_dt, float middle_dt, float inner_dt)
 Calculates an iteration for a triple-loop PID2 controller.
void prescaled_pid2_init (Prescaled_PID2_t *prescaled, uint32_t prescalar, float k_p, float k_i, float k_d, float beta, float yeta, float min_out, float max_out)
 Initializes a Prescaled_PID2_t structure with provided parameters.
void prescaled_pid2_single_loop_control (Prescaled_PID2_t *prescaled, float sp, float pv, float dt)
 Calculate an iteration for prescaled single-loop PID2 controller.

Detailed Description

Implementation of all control PID systems.

All implementations for control systems that utilizes PID as the primary controller are included in this file. This includes single and cascade control loops as well as different implementations of PID with their own solutions to problems known about PID controllers in real systems (e.g. integator windup, derivative kick, etc.).

Author
James Fu
Date
2025-07-17

Function Documentation

◆ pid2_calculate()

float pid2_calculate ( PID2_t & pid,
float sp,
float pv,
float dt )

Calculates PID2 output for one time-step.

Parameters
[in,out]pidReference to PID2_t struct.
[in]spSetpoint value.
[in]pvPlant value.
[in]dtTime step. Must be positive.
Returns
PID2 calculated output.
Warning
This PID implementation has a rudimentary anti-integrator windup. Back-calculation is not implemented.

◆ pid2_dual_loop_control()

float pid2_dual_loop_control ( PID2_t & f_pid,
PID2_t & s_pid,
float sp,
float f_pv,
float s_pv,
float f_dt,
float s_dt )

Calculates an iteration of a dual-loop PID2 controller.

Parameters
[in,out]f_pidReference to outer loop PID2_t struct.
[in,out]s_pidReference to inner loop PID2_t struct.
[in]spSetpoint value for outer loop.
[in]f_pvPlant value for outer loop.
[in]s_pvPlant value for inner loop.
[in]f_dtTime step for outer loop.
[in]s_dtTime step for inner loop.
Returns
PID2 calculated output from inner loop.
Note
Calculations are done with pid2_calculate.

◆ pid2_init()

void pid2_init ( PID2_t & pid,
float k_p,
float k_i,
float k_d,
float beta,
float yeta,
float min_out,
float max_out )

Initializes a PID2_t controller.

Parameters
[in,out]pidReference to PID2_t controller struct to initialize.
[in]k_pProportional gain.
[in]k_iIntegral gain.
[in]k_dDerivative gain.
[in]betaProportional error scaling factor.
[in]yetaDerivative error scaling factor.
Note
beta and yeta are parameters for setpoint weighting, which is primarily used for negating effects of derivative kick.
Parameters
[in]min_outMinimum output value.
[in]max_outMaximum output value. Must be greater or equal to min_out.

◆ pid2_set_limits()

void pid2_set_limits ( PID2_t & pid,
float new_min_out,
float new_max_out )

Sets new output limits for PID2_t controller.

Parameters
[in,out]pidReference to PID2_t controller struct.
[in]new_min_outNew minimum output value.
[in]new_max_outNew maximum output value. Must be greater or equal to new_min_out.
Warning
Anti-integrator windup is not yet implemented.
Todo
Implement anti-integrator windup with new_min_out and new_max_out.

◆ pid2_single_loop_control()

float pid2_single_loop_control ( PID2_t & pid,
float sp,
float pv,
float dt )

Caculate an iteration of a single-loop PID2 controller.

Parameters
[in,out]pidReference to PID2_t controller struct.
[in]spSetpoint value.
[in]pvPlant value.
[in]dtTime step.
Returns
PID2 calculated output.
Note
This is a wrapper that relies on pid2_calculate to do calculations.

◆ pid2_triple_loop_control()

float pid2_triple_loop_control ( PID2_t & outer_pid,
PID2_t & middle_pid,
PID2_t & inner_pid,
float sp,
float outer_pv,
float middle_pv,
float inner_pv,
float outer_dt,
float middle_dt,
float inner_dt )

Calculates an iteration for a triple-loop PID2 controller.

Parameters
[in,out]outer_pidReference to outer loop PID2_t struct.
[in,out]middle_pidReference to middle loop PID2_t struct.
[in,out]inner_pidReference to inner loop PID2_t struct.
[in]spSetpoint value for outer loop.
[in]outer_pvPlant value for outer loop.
[in]middle_pvPlant value for middle loop.
[in]inner_pvPlant value for inner loop.
[in]outer_dtTime step for outer loop.
[in]middle_dtTime step for middle loop.
[in]inner_dtTime step for inner loop.
Returns
PID2 calculated output from inner loop.
Note
Uses pid2_calculate for each loop.

◆ pid_calculate()

float pid_calculate ( PID_t * pid,
float cur_val,
float target_val,
float dt )

Computes a single PID iteration for one time-step.

Parameters
[in,out]pidPointer to PID_t struct.
[in]cur_valPlant value.
[in]target_valTarget value.
[in]dtTime step (seconds).
Warning
dt parameter is ignored and the time-step is always assumed to be 1.0.
Returns
PID calculated output.
Note
If error exceeds max_err, the output is zero and PID iteration calculations are skipped.
Deprecated
This PID implementation has known problems and will be removed in the future.

◆ pid_dual_loop_control()

float pid_dual_loop_control ( float f_tar_val,
PID_t * f_pid,
PID_t * s_pid,
float f_cur_val,
float s_cur_val,
float dt )

Calculate an iteration of dual-loop cascade PID controller.

Parameters
[in]f_tar_valOuter loop target value.
[in,out]f_pidPointer to outer loop PID_t controller struct.
[in,out]s_pidPointer to inner loop PID_t controller struct.
[in]f_cur_valOuter loop current value.
[in]s_cur_valInner loop current value.
[in]dtTime step for both loops.
Returns
PID calculated output from inner loop.
Deprecated
This PID implementation has known problems and will be removed in the future.

◆ pid_incremental_calculate()

float pid_incremental_calculate ( PID_t * pid,
float cur_val,
float target_val )

Calculates incremental PID output for one time-step.

Parameters
[in,out]pidPointer to PID_t controller struct.
[in]cur_valPlant value.
[in]target_valTarget value.
Returns
PID calculated output.
Note
If error exceeds max_err, the output is zero and PID iteration is skipped.
Implements dead zone management: output below 10 is set to zero.
Deprecated
This PID implementation has known problems and will be removed in the future.

◆ pid_param_init()

void pid_param_init ( PID_t * pid,
int32_t max_out,
float max_i_out,
float max_err,
float kp,
float ki,
float kd )

Initializes a PID_t structure with provided parameters.

Parameters
[out]pidPointer to PID_t struct to initialize.
[in]max_outMaximum output magnitude. Must be non-negative.
[in]max_i_outMaximum integral output magnitude. Must be non-negative.
[in]max_errMaximum error value. Must be non-negative. Errors greater than this will skip a PID iteration.
Note
Errors greater than max_err will skip a PID iteration.
max_out, max_i_out, and max_err must be non-negative.
Parameters
[in]kpProportional gain.
[in]kiIntegral gain.
[in]kdDerivative gain.
Deprecated
This PID implementation has known problems and will be removed in the future.

◆ pid_single_loop_control()

float pid_single_loop_control ( float target_val,
PID_t * pid,
float cur_val,
float dt )

Calculates iteration of single-loop PID controller.

Parameters
[in]target_valTarget value.
[in,out]pidPointer to PID_t controller struct.
[in]cur_valPlant value.
[in]dtTime step.
Returns
PID calculated output.
Note
Calls pid_calculate for computation.
Deprecated
This PID implementation has known problems and will be removed in the future.

◆ prescaled_pid2_init()

void prescaled_pid2_init ( Prescaled_PID2_t * prescaled,
uint32_t prescalar,
float k_p,
float k_i,
float k_d,
float beta,
float yeta,
float min_out,
float max_out )

Initializes a Prescaled_PID2_t structure with provided parameters.

Parameters
prescaledPointer to Prescaled_PID2_t struct to initialize.
prescalarNumber of calls before PID2 calculation is performed.
k_pProportional gain.
k_iIntegral gain.
k_dDerivative gain.
betaProportional error scaling factor.
yetaDerivative error scaling factor.
Note
beta and yeta are parameters for setpoint weighting, which is primarily used for negating effects of derivative kick.
Parameters
min_outMinimum output value.
max_outMaximum output value.

◆ prescaled_pid2_single_loop_control()

void prescaled_pid2_single_loop_control ( Prescaled_PID2_t * prescaled,
float sp,
float pv,
float dt )

Calculate an iteration for prescaled single-loop PID2 controller.

Parameters
prescaledPointer to Prescaled_PID2_t struct.
spSetpoint value.
pvPlant (process) value.
dtTime step.
Note
PID2 calculation is performed only after prescalar calls.
Uses pid2_calculate for calculations.
pid2_calculate is called with an accumulated time step cumsum_dt.