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_gs.h
Go to the documentation of this file.
1
5 * @details
6 * This file defines the @ref control_system::pid::PID_GS class, which
7 * implements a discrete-time proportional-integral-derivative (PID)
8 * controller with gain scheduling.
9 *
10 * The controller supports configurable proportional, integral, and
11 * derivative gains, integral and output limits, optional derivative
12 * filtering, and feed-forward control.
13 *
14 * The PID gains can be scheduled according to an operating condition
15 * using user-provided lookup tables. The controller interpolates the
16 * gain values between lookup-table points.
17 *
18 * @tparam T Numeric data type used for the controller calculations.
19 *
20 * @author Abhinay Kumar
21 * @version 1.0.0
22 * @date 2026-08-11
23 *
24 * @copyright
25 * Copyright (c) 2026 Abhinay Kumar
26 */
27
28#ifndef CONTROL_SYSTEM_PID_PID_GS_H
29#define CONTROL_SYSTEM_PID_PID_GS_H
30
31#include <cstdint>
32#include "filters.h"
33#include "../utility/utility.h"
34
35namespace control_system
36{
41
42 namespace pid
43 {
44
46 * @brief Contains proportional-integral-derivative controller implementations.
47 */
48
78
80 * Kd,
81 * I_max,
82 * u_max
83 * );
84 *
85 * controller.set_gain_lookup_table(
86 * op_cond_arr,
87 * Kp_arr,
88 * Ki_arr,
89 * Kd_arr,
90 * len
91 * );
92 *
93 * double output = controller.update(
94 * reference,
95 * measurement,
96 * feed_forward,
97 * operating_condition
98 * );
99 * @endcode
100 */
101 template <typename T>
102 class PID_GS
103 {
104 public:
108 * @details
109 * Constructs the controller with its parameters and internal
110 * state initialized to their default values.
111 */
112 PID_GS();
113
130 void init(T dt_, T Kp_, T Ki_, T Kd_, T I_max_, T u_max_, bool d_filter_ = false, T fc_ = 10.0);
131
143
145 * Updates the controller parameters and derivative filter
146 * configuration.
147 */
148 void set_param(T dt_, T Kp_, T Ki_, T Kd_, T I_max_, T u_max_, bool d_filter_ = false, T fc_ = 10.0);
149
150
153 * @param[in] x_0 Reference or desired value.
154 * @param[in] x Current or measured value.
155 * @param[in] u_ff_ Feed-forward control input.
156 * @param[in] op_cond_ Current operating condition used for
157 * gain scheduling.
159 * @return PID controller output.
160 *
161 * @details
162 * Updates the PID gains according to the supplied operating
163 * condition, calculates the proportional, integral, derivative,
164 * and feed-forward contributions, and produces the controller
165 * output.
166 *
167 * The integral contribution and final controller output are
168 * subject to their configured limits.
169 */
170 T update(T x_0, T x, T u_ff_ = 0.0, T op_cond_ = 0.0);
171
174
176 * Resets the stored control error, proportional, integral,
177 * derivative, controller output, and startup state.
178 */
179 void reset();
180
182 * @brief Merges an external controller output into the PID state.
183 *
184 * @param[in] u_k_1_ Previous or externally provided controller
185 * output.
186 *
187 * @details
188 * Updates the internal controller state using the specified
189 * previous controller output.
190 */
191 void merge(T u_k_1_);
192
193
196 * @param[in] op_cond_arr_ Array containing operating-condition
197 * values.
198 * @param[in] Kp_arr_ Array containing proportional gain values.
199 * @param[in] Ki_arr_ Array containing integral gain values.
200 * @param[in] Kd_arr_ Array containing derivative gain values.
201 * @param[in] len_arr_ Number of elements in each lookup table.
202 *
203 * @details
204 * Configures the lookup tables used to determine the
205 * proportional, integral, and derivative gains as a function
206 * of the operating condition.
207 */
208 void set_gain_lookup_table(T *op_cond_arr_, T *Kp_arr_, T *Ki_arr_, T *Kd_arr_, uint16_t len_arr_);
209
211
213 * @param[in] dt_ Sampling time in seconds.
214 */
215 void set_dt(T dt_);
216
217
220 * @param[in] Kp_ Proportional gain.
221 */
222 void set_Kp(T Kp_);
223
225 * @brief Sets the integral gain.
226 *
227 * @param[in] Ki_ Integral gain.
228 */
229 void set_Ki(T Ki_);
230
231 /**
232 * @brief Sets the derivative gain.
233 *
234 * @param[in] Kd_ Derivative gain.
235 */
236 void set_Kd(T Kd_);
241
243 void set_I_max(T I_max_);
244
247
249 */
250 void set_u_max(T u_max_);
251
253
255 * @param[in] d_filter_ Derivative filter enable state.
256 */
257 void set_d_filter(bool d_filter_);
258
259
262 * @param[in] fc_ Cutoff frequency in Hz.
263 */
264 void set_fc(T fc_);
265
267 * @brief Sets the feed-forward control input.
268 *
269 * @param[in] u_ff_ Feed-forward control input.
270 */
271 void set_ff(T u_ff_);
272
273 /**
274 * @brief Gets the controller sampling time.
275 *
276 * @return Sampling time in seconds.
277 */
278 T get_dt();
283
286
289
292 T get_Ki();
293
299 T get_Kd();
300
306 T get_I_max();
307
313 T get_u_max();
314
321 bool get_d_filter();
322
328 T get_fc();
329
335 T get_ff();
336
342 T get_P();
343
349 T get_I();
350
356 T get_D();
357
363 T get_u();
364
370 T get_e_k_1();
371
372 private:
376 LPF_1<T> lpf;
377
388 void update_gain(T op_cond_);
389
401 T interpolate(T x, T x1, T y1, T x2, T y2);
402
410 T dt = 0.0;
411
415 T Kp = 0.0;
416
420 T Ki = 0.0;
421
425 T Kd = 0.0;
426
430 T I_max = 0.0;
431
435 T u_max = 0.0;
436
440 bool d_filter = false;
441
448 T fc = 0.0;
449
453 T e_k_1 = 0.0;
454
458 T P = 0.0;
459
463 T I = 0.0;
464
468 T D = 0.0;
469
473 T u = 0.0;
474
478 T u_ff = 0.0;
479
488
493
498
503
507 uint16_t len_arr;
508
515 bool start = true;
516 };
517
519 }
520}
521
522#endif
T u
PID controller output.
Definition pid_gs.h:473
void set_ff(T u_ff_)
Sets the feed-forward control input.
Definition pid_gs.h:195
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
Definition pid_gs.h:188
void set_Ki(T Ki_)
Sets the integral gain.
Definition pid_gs.h:158
T get_ff()
Gets the feed-forward control input.
Definition pid_gs.h:249
void reset()
Resets the PID controller state.
Definition pid_gs.h:80
LPF_1< T > lpf
First-order low-pass filter used for derivative filtering.
Definition pid_gs.h:376
T * Ki_arr
Integral-gain lookup table.
Definition pid_gs.h:497
void set_gain_lookup_table(T *op_cond_arr_, T *Kp_arr_, T *Ki_arr_, T *Kd_arr_, uint16_t len_arr_)
Configures the gain-scheduling lookup tables.
Definition pid_gs.h:98
T get_P()
Gets the proportional contribution.
Definition pid_gs.h:255
PID_GS()
Constructs a gain-scheduled PID controller.
Definition pid_gs.h:5
T get_u()
Gets the controller output.
Definition pid_gs.h:273
void update_gain(T op_cond_)
Updates the PID gains according to the operating condition.
Definition pid_gs.h:108
T fc
Cutoff frequency of the derivative filter.
Definition pid_gs.h:448
T I
Integral contribution.
Definition pid_gs.h:463
T interpolate(T x, T x1, T y1, T x2, T y2)
Linearly interpolates a value between two lookup-table points.
Definition pid_gs.h:285
T get_I_max()
Gets the maximum integral contribution.
Definition pid_gs.h:225
T get_e_k_1()
Gets the previous control error.
Definition pid_gs.h:279
void set_d_filter(bool d_filter_)
Enables or disables derivative filtering.
Definition pid_gs.h:182
T P
Proportional contribution.
Definition pid_gs.h:458
T D
Derivative contribution.
Definition pid_gs.h:468
bool get_d_filter()
Gets the derivative filter enable state.
Definition pid_gs.h:237
T u_max
Maximum allowed controller output.
Definition pid_gs.h:435
void set_param(T dt_, T Kp_, T Ki_, T Kd_, T I_max_, T u_max_, bool d_filter_=false, T fc_=10.0)
Sets the PID controller parameters.
Definition pid_gs.h:32
T get_D()
Gets the derivative contribution.
Definition pid_gs.h:267
T Ki
Integral gain.
Definition pid_gs.h:420
uint16_t len_arr
Number of entries in the gain lookup tables.
Definition pid_gs.h:507
T * Kp_arr
Proportional-gain lookup table.
Definition pid_gs.h:492
T get_u_max()
Gets the maximum controller output.
Definition pid_gs.h:231
T update(T x_0, T x, T u_ff_=0.0, T op_cond_=0.0)
Computes the gain-scheduled PID control output.
Definition pid_gs.h:46
T dt
Controller sampling time.
Definition pid_gs.h:410
void set_Kd(T Kd_)
Sets the derivative gain.
Definition pid_gs.h:164
T Kd
Derivative gain.
Definition pid_gs.h:425
T get_Kp()
Gets the proportional gain.
Definition pid_gs.h:207
T e_k_1
Previous control error.
Definition pid_gs.h:453
T get_Ki()
Gets the integral gain.
Definition pid_gs.h:213
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition pid_gs.h:176
bool d_filter
Derivative filter enable state.
Definition pid_gs.h:440
T get_dt()
Gets the controller sampling time.
Definition pid_gs.h:201
T * op_cond_arr
Operating-condition lookup table.
Definition pid_gs.h:487
T u_ff
Feed-forward control input.
Definition pid_gs.h:478
void set_I_max(T I_max_)
Sets the maximum integral contribution.
Definition pid_gs.h:170
void set_dt(T dt_)
Sets the controller sampling time.
Definition pid_gs.h:145
T Kp
Proportional gain.
Definition pid_gs.h:415
void set_Kp(T Kp_)
Sets the proportional gain.
Definition pid_gs.h:152
void merge(T u_k_1_)
Merges an external controller output into the PID state.
Definition pid_gs.h:93
T get_fc()
Gets the derivative filter cutoff frequency.
Definition pid_gs.h:243
T get_Kd()
Gets the derivative gain.
Definition pid_gs.h:219
T get_I()
Gets the integral contribution.
Definition pid_gs.h:261
T * Kd_arr
Derivative-gain lookup table.
Definition pid_gs.h:502
bool start
Controller startup state.
Definition pid_gs.h:515
void init(T dt_, T Kp_, T Ki_, T Kd_, T I_max_, T u_max_, bool d_filter_=false, T fc_=10.0)
Initializes the PID controller.
Definition pid_gs.h:25
T I_max
Maximum allowed integral contribution.
Definition pid_gs.h:430
Contains control-system algorithms and controllers.
Definition d.h:32
Contains proportional-integral-derivative controller implementations.
Utility functions for the Control System library.