PID Control 1.0
Discrete time implementation of P, PI, PD, PID. Including derivative filter, integral clamping, feed-forward, gain scheduling in standard and parallel form.
Loading...
Searching...
No Matches
control_system::pid::PID< T > Class Template Reference

Discrete-time proportional-integral-derivative controller. More...

#include <pid.h>

Public Member Functions

 PID ()
 Constructs a PID controller.
get_dt ()
 Gets the controller sampling time.
get_e_k_1 ()
 Gets the previous control error.
get_e_k_2 ()
 Gets the control error from two iterations ago.
get_Kd ()
 Gets the derivative gain.
get_Ki ()
 Gets the integral gain.
get_Kp ()
 Gets the proportional gain.
get_u_k_1 ()
 Gets the previous controller output.
get_u_max ()
 Gets the maximum controller output.
void init (T dt_, T Kp_, T Ki_, T Kd_, T u_k_1_, T u_max_)
 Initializes the PID controller.
void merge (T u_k_1_)
 Merges an external controller output into the PID state.
void reset ()
 Resets the PID controller state.
void set_dt (T dt_)
 Sets the controller sampling time.
void set_Kd (T Kd_)
 Sets the derivative gain.
void set_Ki (T Ki_)
 Sets the integral gain.
void set_Kp (T Kp_)
 Sets the proportional gain.
void set_param (T dt_, T Kp_, T Ki_, T Kd_, T u_k_1_, T u_max_)
 Sets the PID controller parameters.
void set_u_0 (T u_k_1_)
 Sets the previous controller output.
void set_u_max (T u_max_)
 Sets the maximum controller output.
update (T x_0, T x)
 Computes the PID control output.

Private Attributes

dt = 0.0
 Controller sampling time.
e_k_1 = 0.0
 Previous control error.
e_k_2 = 0.0
 Control error from two iterations ago.
Kd = 0.0
 Derivative gain.
Ki = 0.0
 Integral gain.
Kp = 0.0
 Proportional gain.
uint8_t start = 0
 Controller startup state.
u_k_1 = 0.0
 Previous controller output.
u_max = 9999999999
 Maximum controller output.

Detailed Description

template<typename T>
class control_system::pid::PID< T >

Discrete-time proportional-integral-derivative controller.

The PID class implements a discrete-time PID controller using configurable proportional, integral, and derivative gains.

The controller uses the sampling time to calculate the discrete-time integral and derivative contributions.

The previous control output and previous error values are maintained internally as controller state.

Template Parameters
TNumeric data type used for the controller.
Example
controller.init(
0.001,
Kp,
Ki,
Kd,
0.0,
100.0
);
double output = controller.update(reference, measurement);
Discrete-time proportional-integral-derivative controller.
Definition pid.h:83
void init(T dt_, T Kp_, T Ki_, T Kd_, T u_k_1_, T u_max_)
Initializes the PID controller.
Definition pid.h:18
T Kp
Proportional gain.
Definition pid.h:295
T Kd
Derivative gain.
Definition pid.h:305
T update(T x_0, T x)
Computes the PID control output.
Definition pid.h:36
T Ki
Integral gain.
Definition pid.h:300

Definition at line 82 of file pid.h.

Constructor & Destructor Documentation

◆ PID()

template<typename T>
control_system::pid::PID< T >::PID ( )

Constructs a PID controller.

Constructs the controller with its parameters and internal state initialized to their default values.

Definition at line 5 of file pid.h.

Member Function Documentation

◆ get_dt()

template<typename T>
T control_system::pid::PID< T >::get_dt ( )

Gets the controller sampling time.

Returns
Sampling time in seconds.

Definition at line 108 of file pid.h.

◆ get_e_k_1()

template<typename T>
T control_system::pid::PID< T >::get_e_k_1 ( )

Gets the previous control error.

Returns
Control error from the previous controller iteration.

Definition at line 138 of file pid.h.

◆ get_e_k_2()

template<typename T>
T control_system::pid::PID< T >::get_e_k_2 ( )

Gets the control error from two iterations ago.

Returns
Control error from two controller iterations ago.

Definition at line 144 of file pid.h.

◆ get_Kd()

template<typename T>
T control_system::pid::PID< T >::get_Kd ( )

Gets the derivative gain.

Returns
Derivative gain.

Definition at line 126 of file pid.h.

◆ get_Ki()

template<typename T>
T control_system::pid::PID< T >::get_Ki ( )

Gets the integral gain.

Returns
Integral gain.

Definition at line 120 of file pid.h.

◆ get_Kp()

template<typename T>
T control_system::pid::PID< T >::get_Kp ( )

Gets the proportional gain.

Returns
Proportional gain.

Definition at line 114 of file pid.h.

◆ get_u_k_1()

template<typename T>
T control_system::pid::PID< T >::get_u_k_1 ( )

Gets the previous controller output.

Returns
Previous controller output.

Definition at line 132 of file pid.h.

◆ get_u_max()

template<typename T>
T control_system::pid::PID< T >::get_u_max ( )

Gets the maximum controller output.

Returns
Maximum allowed controller output.

Definition at line 150 of file pid.h.

◆ init()

template<typename T>
void control_system::pid::PID< T >::init ( T dt_,
T Kp_,
T Ki_,
T Kd_,
T u_k_1_,
T u_max_ )

Initializes the PID controller.

Parameters
[in]dt_Controller sampling time in seconds.
[in]Kp_Proportional gain.
[in]Ki_Integral gain.
[in]Kd_Derivative gain.
[in]u_k_1Previous controller output.
[in]u_max_Maximum allowed controller output.

Initializes the controller parameters and previous output state.

Definition at line 18 of file pid.h.

◆ merge()

template<typename T>
void control_system::pid::PID< T >::merge ( T u_k_1_)

Merges an external controller output into the PID state.

Parameters
[in]u_k_1_Previous or externally provided controller output.

Updates the internal previous-output state with the specified value.

Definition at line 66 of file pid.h.

◆ reset()

template<typename T>
void control_system::pid::PID< T >::reset ( )

Resets the PID controller state.

Resets the stored previous errors, previous output, and startup state of the controller.

Definition at line 57 of file pid.h.

◆ set_dt()

template<typename T>
void control_system::pid::PID< T >::set_dt ( T dt_)

Sets the controller sampling time.

Parameters
[in]dt_Sampling time in seconds.

Definition at line 72 of file pid.h.

◆ set_Kd()

template<typename T>
void control_system::pid::PID< T >::set_Kd ( T Kd_)

Sets the derivative gain.

Parameters
[in]Kd_Derivative gain.

Definition at line 90 of file pid.h.

◆ set_Ki()

template<typename T>
void control_system::pid::PID< T >::set_Ki ( T Ki_)

Sets the integral gain.

Parameters
[in]Ki_Integral gain.

Definition at line 84 of file pid.h.

◆ set_Kp()

template<typename T>
void control_system::pid::PID< T >::set_Kp ( T Kp_)

Sets the proportional gain.

Parameters
[in]Kp_Proportional gain.

Definition at line 78 of file pid.h.

◆ set_param()

template<typename T>
void control_system::pid::PID< T >::set_param ( T dt_,
T Kp_,
T Ki_,
T Kd_,
T u_k_1_,
T u_max_ )

Sets the PID controller parameters.

Parameters
[in]dt_Controller sampling time in seconds.
[in]Kp_Proportional gain.
[in]Ki_Integral gain.
[in]Kd_Derivative gain.
[in]u_k_1Previous controller output.
[in]u_max_Maximum allowed controller output.

Updates the controller gains, sampling time, previous output, and output limit.

Definition at line 25 of file pid.h.

◆ set_u_0()

template<typename T>
void control_system::pid::PID< T >::set_u_0 ( T u_k_1_)

Sets the previous controller output.

Parameters
[in]u_k_1_Previous controller output.

Definition at line 96 of file pid.h.

◆ set_u_max()

template<typename T>
void control_system::pid::PID< T >::set_u_max ( T u_max_)

Sets the maximum controller output.

Parameters
[in]u_max_Maximum allowed controller output.

Definition at line 102 of file pid.h.

◆ update()

template<typename T>
T control_system::pid::PID< T >::update ( T x_0,
T x )

Computes the PID control output.

Parameters
[in]x_0Reference or desired value.
[in]xCurrent or measured value.
Returns
PID controller output limited by the configured maximum output.

Calculates the control output from the proportional, integral, and derivative contributions using the current control error and the stored controller state.

Definition at line 36 of file pid.h.

Member Data Documentation

◆ dt

template<typename T>
T control_system::pid::PID< T >::dt = 0.0
private

Controller sampling time.

Sampling period of the discrete-time PID controller in seconds.

Definition at line 269 of file pid.h.

◆ e_k_1

template<typename T>
T control_system::pid::PID< T >::e_k_1 = 0.0
private

Previous control error.

Stores the control error from the previous iteration.

Definition at line 285 of file pid.h.

◆ e_k_2

template<typename T>
T control_system::pid::PID< T >::e_k_2 = 0.0
private

Control error from two iterations ago.

Definition at line 290 of file pid.h.

◆ Kd

template<typename T>
T control_system::pid::PID< T >::Kd = 0.0
private

Derivative gain.

Definition at line 305 of file pid.h.

◆ Ki

template<typename T>
T control_system::pid::PID< T >::Ki = 0.0
private

Integral gain.

Definition at line 300 of file pid.h.

◆ Kp

template<typename T>
T control_system::pid::PID< T >::Kp = 0.0
private

Proportional gain.

Definition at line 295 of file pid.h.

◆ start

template<typename T>
uint8_t control_system::pid::PID< T >::start = 0
private

Controller startup state.

Indicates the startup state of the discrete-time PID controller.

Definition at line 323 of file pid.h.

◆ u_k_1

template<typename T>
T control_system::pid::PID< T >::u_k_1 = 0.0
private

Previous controller output.

Stores the controller output from the previous iteration.

Definition at line 277 of file pid.h.

◆ u_max

template<typename T>
T control_system::pid::PID< T >::u_max = 9999999999
private

Maximum controller output.

Defines the maximum allowed magnitude of the controller output.

Definition at line 314 of file pid.h.


The documentation for this class was generated from the following files: