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_s.h
Go to the documentation of this file.
1
5 * @details
6 * This file defines the @ref control_system::pid::PID_S class, which
7 * implements a proportional-integral-derivative (PID) controller with
8 * configurable derivative filtering and feed-forward control.
9 *
10 * The controller provides access to the individual proportional,
11 * integral, and derivative contributions as well as the final control
12 * output.
13 *
14 * @tparam T Numeric data type used for the controller calculations.
15 *
16 * @author Abhinay Kumar
17 * @version 1.0.0
18 * @date 2026-08-11
19 *
20 * @copyright
21 * Copyright (c) 2026 Abhinay Kumar
22 */
23
24#ifndef CONTROL_SYSTEM_PID_PID_S_H
25#define CONTROL_SYSTEM_PID_PID_S_H
26
27#include "filters.h"
28#include "../utility/utility.h"
29
30namespace control_system
31{
32 /**
33 * @namespace control_system
34 * @brief Contains control-system algorithms and controllers.
35 */
36
37 namespace pid
38 {
43
44
46 * @brief PID controller with configurable derivative filtering.
47 *
48 * @details
49 * The PID_S class implements a PID controller with configurable
50 * proportional, integral, and derivative time constants.
51 *
52 * The derivative term can optionally be filtered using a first-order
53 * low-pass filter. A feed-forward term can also be included in the
54 * controller output.
55 *
56 * The controller provides configurable integral and output limits
57 * and maintains its control state between successive updates.
58 *
59 * @tparam T Numeric data type used for the controller.
60 *
61 * @par Example
62 * @code{.cpp}
63 * control_system::pid::PID_S<double> controller;
64 *
65 * controller.init(
66 * 0.001,
67 * Kp,
68 * Ti,
69 * Td,
70 * I_max,
71 * u_max
72 * );
73 *
74 * double output = controller.update(reference, measurement);
75 * @endcode
76 */
77 template <typename T>
78 class PID_S
79 {
80 public:
88 PID_S();
89
91 * @brief Initializes the PID controller.
92 *
93 * @param[in] dt_ Controller sampling time in seconds.
94 * @param[in] Kp_ Proportional gain.
95 * @param[in] T_i_ Integral time constant.
96 * @param[in] T_d_ Derivative time constant.
97 * @param[in] I_max_ Maximum allowed integral contribution.
98 * @param[in] u_max_ Maximum allowed controller output.
99 * @param[in] d_filter_ Enables or disables derivative filtering.
100 * @param[in] fc_ Cutoff frequency of the derivative filter in Hz.
101 *
102 * @details
103 * Initializes the PID controller parameters and optionally
104 * configures the derivative low-pass filter.
105 */
106 void init(T dt_, T Kp_, T T_i_, T T_d_, T I_max_, T u_max_, bool d_filter_ = false, T fc_ = 10.0);
107
109 * @brief Sets the PID controller parameters.
110 *
111 * @param[in] dt_ Controller sampling time in seconds.
112 * @param[in] Kp_ Proportional gain.
113 * @param[in] T_i_ Integral time constant.
114 * @param[in] T_d_ Derivative time constant.
115 * @param[in] I_max_ Maximum allowed integral contribution.
116 * @param[in] u_max_ Maximum allowed controller output.
117 * @param[in] d_filter_ Enables or disables derivative filtering.
118 * @param[in] fc_ Cutoff frequency of the derivative filter in Hz.
119 *
120 * @details
121 * Updates the PID controller parameters and derivative filter
122 * configuration.
123 */
124 void set_param(T dt_, T Kp_, T T_i_, T T_d_, T I_max_, T u_max_, bool d_filter_ = false, T fc_ = 10.0);
125
127 * @brief Computes the PID control output.
128 *
129 * @param[in] x_0 Reference or desired value.
130 * @param[in] x Current or measured value.
131 * @param[in] u_ff_ Feed-forward control input.
132 *
133 * @return PID controller output.
134 *
135 * @details
136 * Calculates the proportional, integral, derivative, and
137 * feed-forward contributions and combines them to produce
138 * the controller output.
140 * The integral contribution and controller output are subject
141 * to their configured limits.
142 */
143 T update(T x_0, T x, T u_ff_ = 0.0);
144
146 * @brief Resets the PID controller state.
147 *
148 * @details
149 * Resets the stored control error, proportional, integral,
150 * derivative, and controller output states.
151 */
152 void reset();
153
156
158 * output.
159 *
160 * @details
161 * Updates the controller state using an externally provided
162 * previous controller output.
163 */
164 void merge(T u_k_1_);
165
168
170 */
171 void set_dt(T dt_);
172
174
176 * @param[in] Kp_ Proportional gain.
177 */
178 void set_Kp(T Kp_);
179
180
183 * @param[in] T_i_ Integral time constant.
184 */
185 void set_Ti(T T_i_);
186
188 * @brief Sets the derivative time constant.
189 *
190 * @param[in] T_d_ Derivative time constant.
191 */
192 void set_Td(T T_d_);
193
194 /**
195 * @brief Sets the maximum integral contribution.
196 *
197 * @param[in] I_max_ Maximum allowed integral contribution.
198 */
199 void set_I_max(T I_max_);
204
206 void set_u_max(T u_max_);
207
210
212 */
213 void set_d_filter(bool d_filter_);
214
216
218 * @param[in] fc_ Cutoff frequency in Hz.
219 */
220 void set_fc(T fc_);
221
222
225 * @param[in] u_ff_ Feed-forward control input.
226 */
227 void set_ff(T u_ff_);
228
230 * @brief Gets the controller sampling time.
231 *
232 * @return Sampling time in seconds.
233 */
234 T get_dt();
235
241 T get_Kp();
242
248 T get_T_i();
249
255 T get_T_d();
256
262 T get_I_max();
263
269 T get_u_max();
270
277 bool get_d_filter();
278
284 T get_fc();
285
291 T get_ff();
292
298 T get_P();
299
305 T get_I();
306
312 T get_D();
313
319 T get_u();
320
326 T get_e_k_1();
327
328 private:
332 LPF_1<T> lpf;
333
340 T dt = 0.0;
341
345 T Kp = 0.0;
346
350 T T_i = 0.0;
351
355 T T_d = 0.0;
356
360 T I_max = 0.0;
361
365 T u_max = 0.0;
366
370 bool d_filter = false;
371
378 T fc = 0.0;
379
383 T e_k_1 = 0.0;
384
388 T P = 0.0;
389
393 T I = 0.0;
394
398 T D = 0.0;
399
403 T u = 0.0;
404
408 T u_ff = 0.0;
409
416 bool start = true;
417 };
418
420 }
421}
422
423#endif
T get_u()
Gets the controller output.
Definition pid_s.h:224
void set_param(T dt_, T Kp_, T T_i_, T T_d_, T I_max_, T u_max_, bool d_filter_=false, T fc_=10.0)
Sets the PID controller parameters.
Definition pid_s.h:32
void merge(T u_k_1_)
Merges an external controller output into the PID state.
Definition pid_s.h:91
T u_ff
Feed-forward control input.
Definition pid_s.h:408
T get_D()
Gets the derivative contribution.
Definition pid_s.h:218
T get_T_i()
Gets the integral time constant.
Definition pid_s.h:164
T e_k_1
Previous control error.
Definition pid_s.h:383
T T_i
Integral time constant.
Definition pid_s.h:350
T Kp
Proportional gain.
Definition pid_s.h:345
T get_ff()
Gets the feed-forward control input.
Definition pid_s.h:200
bool get_d_filter()
Gets the derivative filter enable state.
Definition pid_s.h:188
T u
PID controller output.
Definition pid_s.h:403
void set_dt(T dt_)
Sets the controller sampling time.
Definition pid_s.h:96
bool d_filter
Derivative filter enable state.
Definition pid_s.h:370
void set_Ti(T T_i_)
Sets the integral time constant.
Definition pid_s.h:109
T get_P()
Gets the proportional contribution.
Definition pid_s.h:206
bool start
Controller startup state.
Definition pid_s.h:416
void set_I_max(T I_max_)
Sets the maximum integral contribution.
Definition pid_s.h:121
T I_max
Maximum allowed integral contribution.
Definition pid_s.h:360
T update(T x_0, T x, T u_ff_=0.0)
Computes the PID control output.
Definition pid_s.h:46
T get_u_max()
Gets the maximum controller output.
Definition pid_s.h:182
T P
Proportional contribution.
Definition pid_s.h:388
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
Definition pid_s.h:139
PID_S()
Constructs a PID controller.
Definition pid_s.h:5
T fc
Cutoff frequency of the derivative filter.
Definition pid_s.h:378
T get_fc()
Gets the derivative filter cutoff frequency.
Definition pid_s.h:194
T get_T_d()
Gets the derivative time constant.
Definition pid_s.h:170
T get_I()
Gets the integral contribution.
Definition pid_s.h:212
LPF_1< T > lpf
First-order low-pass filter used for derivative filtering.
Definition pid_s.h:332
T get_I_max()
Gets the maximum integral contribution.
Definition pid_s.h:176
void set_Td(T T_d_)
Sets the derivative time constant.
Definition pid_s.h:115
T get_Kp()
Gets the proportional gain.
Definition pid_s.h:158
T D
Derivative contribution.
Definition pid_s.h:398
void set_d_filter(bool d_filter_)
Enables or disables derivative filtering.
Definition pid_s.h:133
T u_max
Maximum allowed controller output.
Definition pid_s.h:365
void set_Kp(T Kp_)
Sets the proportional gain.
Definition pid_s.h:103
T get_e_k_1()
Gets the previous control error.
Definition pid_s.h:230
T T_d
Derivative time constant.
Definition pid_s.h:355
void reset()
Resets the PID controller state.
Definition pid_s.h:78
T dt
Controller sampling time.
Definition pid_s.h:340
void init(T dt_, T Kp_, T T_i_, T T_d_, T I_max_, T u_max_, bool d_filter_=false, T fc_=10.0)
Initializes the PID controller.
Definition pid_s.h:25
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition pid_s.h:127
void set_ff(T u_ff_)
Sets the feed-forward control input.
Definition pid_s.h:146
T get_dt()
Gets the controller sampling time.
Definition pid_s.h:152
T I
Integral contribution.
Definition pid_s.h:393
Contains control-system algorithms and controllers.
Definition d.h:32
Contains proportional-integral-derivative controller implementations.
Utility functions for the Control System library.