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
pid_lpf_1.h
Go to the documentation of this file.
1
5 * @details
6 * This file defines the @ref control_system::pid::PID_LPF_1 class,
7 * which implements a discrete-time proportional-integral-derivative
8 * (PID) controller with a first-order low-pass filter applied to the
9 * derivative contribution.
10 *
11 * The controller uses configurable proportional, integral, and
12 * derivative gains, together with a derivative filter cutoff frequency.
13 * Previous control errors and controller outputs are maintained as
14 * internal state variables.
15 *
16 * The controller output is constrained by a configurable maximum output
17 * limit.
18 *
19 * @tparam T Numeric data type used for the controller calculations.
20 *
21 * @author Abhinay Kumar
22 * @version 1.0.0
23 * @date 2026-08-11
24 *
25 * @copyright
26 * Copyright (c) 2026 Abhinay Kumar
27 */
28
29#ifndef CONTROL_SYSTEM_PID_PID_LPF_1_H
30#define CONTROL_SYSTEM_PID_PID_LPF_1_H
31
32#include <cstdint>
33#include "../utility/utility.h"
34
35namespace control_system
36{
37
39 * @brief Contains control-system algorithms and controllers.
40 */
41
42 namespace pid
43 {
48
59
61 *
62 * Previous controller outputs and control errors are retained
63 * internally to implement the discrete-time controller.
64 *
65 * @tparam T Numeric data type used for the controller.
66 *
67 * @par Example
68 * @code{.cpp}
69 * control_system::pid::PID_LPF_1<double> controller;
70 *
71 * controller.init(
72 * 0.001,
73 * Kp,
74 * Ki,
75 * Kd,
76 * 10.0,
77 * 0.0,
78 * 0.0,
79 * 100.0
80 * );
81 *
82 * double output = controller.update(reference, measurement);
83 * @endcode
84 */
85 template <typename T>
87 {
88 public:
89 /**
90 * @brief Constructs a PID controller with filtered derivative.
91 *
92 * @details
93 * Constructs the controller with its parameters and internal
94 * state initialized to their default values.
95 */
96 PID_LPF_1();
97
99
101 * @param[in] dt_ Controller sampling time in seconds.
102 * @param[in] Kp_ Proportional gain.
103 * @param[in] Ki_ Integral gain.
104 * @param[in] Kd_ Derivative gain.
105 * @param[in] fc_ Derivative filter cutoff frequency in Hz.
106 * @param[in] u_k_1_ Previous controller output.
107 * @param[in] u_k_2_ Controller output from two iterations ago.
108 * @param[in] u_max_ Maximum allowed controller output.
109 *
110 * @details
111 * Initializes the PID controller parameters, derivative
112 * filter parameters, previous controller outputs, and
113 * controller output limit.
114 */
115 void init(T dt_, T Kp_, T Ki_, T Kd_, T fc_, T u_k_1_, T u_k_2_, T u_max_);
116
117
120 * @param[in] dt_ Controller sampling time in seconds.
121 * @param[in] Kp_ Proportional gain.
122 * @param[in] Ki_ Integral gain.
123 * @param[in] Kd_ Derivative gain.
124 * @param[in] fc_ Derivative filter cutoff frequency in Hz.
125 * @param[in] u_k_1_ Previous controller output.
126 * @param[in] u_k_2_ Controller output from two iterations ago.
127 * @param[in] u_max_ Maximum allowed controller output.
128 *
129 * @details
130 * Updates the PID gains, sampling time, derivative filter
131 * cutoff frequency, previous controller outputs, and output
132 * limit.
133 */
134 void set_param(T dt_, T Kp_, T Ki_, T Kd_, T fc_, T u_k_1_, T u_k_2_, T u_max_);
135
137 * @brief Computes the PID control output.
138 *
139 * @param[in] x_0 Reference or desired value.
140 * @param[in] x Current or measured value.
141 *
142 * @return PID controller output limited by the configured
143 * maximum output.
144 *
145 * @details
146 * Calculates the proportional, integral, and derivative
147 * contributions using the current and previous control
148 * errors. The derivative contribution is filtered using
149 * the configured first-order low-pass filter.
150 */
151 T update(T x_0, T x);
152
153
156 * @details
157 * Resets the stored controller outputs, control errors,
158 * and startup state.
159 */
160 void reset();
165
168 * @details
169 * Updates the internal previous-output state using the
170 * specified controller output.
171 */
172 void merge(T u_k_1_);
173
179 void set_dt(T dt_);
180
186 void set_Kp(T Kp_);
187
193 void set_Ki(T Ki_);
194
200 void set_Kd(T Kd_);
201
207 void set_fc(T fc_);
208
214 void set_u_0(T u_k_1_);
215
221 void set_u_max(T u_max_);
222
228 T get_dt();
229
235 T get_Kp();
236
242 T get_Ki();
243
249 T get_Kd();
250
256 T get_fc();
257
263 T get_u_k_1();
264
270 T get_e_k_1();
271
277 T get_e_k_2();
278
284 T get_u_max();
285
286 private:
294 T dt = 0.0;
295
302 T u_k_1 = 0.0;
303
307 T u_k_2 = 0.0;
308
315 T e_k_1 = 0.0;
316
320 T e_k_2 = 0.0;
321
325 T Kp = 0.0;
326
330 T Ki = 0.0;
331
335 T Kd = 0.0;
336
344 T tau = 0.0;
345
353 T u_max = 9999999999;
354
362 uint8_t start = 0;
363 };
364
366 }
367}
368
369#endif
void init(T dt_, T Kp_, T Ki_, T Kd_, T fc_, T u_k_1_, T u_k_2_, T u_max_)
Initializes the PID controller.
Definition pid_lpf_1.h:19
void set_u_0(T u_k_1_)
Sets the previous controller output.
Definition pid_lpf_1.h:107
void set_Ki(T Ki_)
Sets the integral gain.
Definition pid_lpf_1.h:89
T Kp
Proportional gain.
Definition pid_lpf_1.h:325
T u_k_2
Controller output from two iterations ago.
Definition pid_lpf_1.h:307
T get_Ki()
Gets the integral gain.
Definition pid_lpf_1.h:131
void merge(T u_k_1_)
Merges an external controller output into the PID state.
Definition pid_lpf_1.h:71
T get_e_k_2()
Gets the control error from two iterations ago.
Definition pid_lpf_1.h:161
T e_k_2
Control error from two iterations ago.
Definition pid_lpf_1.h:320
uint8_t start
Controller startup state.
Definition pid_lpf_1.h:362
void set_Kd(T Kd_)
Sets the derivative gain.
Definition pid_lpf_1.h:95
T get_fc()
Gets the derivative filter cutoff frequency.
Definition pid_lpf_1.h:143
void set_dt(T dt_)
Sets the controller sampling time.
Definition pid_lpf_1.h:77
T update(T x_0, T x)
Computes the PID control output.
Definition pid_lpf_1.h:39
void reset()
Resets the PID controller state.
Definition pid_lpf_1.h:61
T get_u_max()
Gets the maximum controller output.
Definition pid_lpf_1.h:167
T get_Kp()
Gets the proportional gain.
Definition pid_lpf_1.h:125
T u_k_1
Previous controller output.
Definition pid_lpf_1.h:302
T dt
Controller sampling time.
Definition pid_lpf_1.h:294
T get_u_k_1()
Gets the previous controller output.
Definition pid_lpf_1.h:149
PID_LPF_1()
Constructs a PID controller with filtered derivative.
Definition pid_lpf_1.h:5
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
Definition pid_lpf_1.h:101
void set_Kp(T Kp_)
Sets the proportional gain.
Definition pid_lpf_1.h:83
T get_dt()
Gets the controller sampling time.
Definition pid_lpf_1.h:119
T u_max
Maximum controller output.
Definition pid_lpf_1.h:353
void set_param(T dt_, T Kp_, T Ki_, T Kd_, T fc_, T u_k_1_, T u_k_2_, T u_max_)
Sets the PID controller parameters.
Definition pid_lpf_1.h:26
T e_k_1
Previous control error.
Definition pid_lpf_1.h:315
T tau
Time constant of the derivative low-pass filter.
Definition pid_lpf_1.h:344
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition pid_lpf_1.h:113
T get_e_k_1()
Gets the previous control error.
Definition pid_lpf_1.h:155
T get_Kd()
Gets the derivative gain.
Definition pid_lpf_1.h:137
Contains control-system algorithms and controllers.
Definition d.h:32
Contains proportional-integral-derivative controller implementations.
Utility functions for the Control System library.