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_p.h
Go to the documentation of this file.
1
5 * @details
6 * This file defines the @ref control_system::pid::PID_P class, which
7 * implements a proportional-integral-derivative (PID) controller with
8 * configurable derivative filtering and feed-forward control.
9 *
10 * The controller provides configurable proportional, integral, and
11 * derivative gains. The integral contribution and total controller
12 * output can be limited independently.
13 *
14 * The derivative term can optionally be filtered using a first-order
15 * low-pass filter. A feed-forward term can also be added to the
16 * controller output.
17 *
18 * The class provides access to the individual proportional, integral,
19 * and derivative contributions as well as the final controller output.
20 *
21 * @tparam T Numeric data type used for the controller calculations.
22 *
23 * @author Abhinay Kumar
24 * @version 1.0.0
25 * @date 2026-08-11
26 *
27 * @copyright
28 * Copyright (c) 2026 Abhinay Kumar
29 */
30
31#ifndef CONTROL_SYSTEM_PID_PID_P_H
32#define CONTROL_SYSTEM_PID_PID_P_H
33
34#include "filters.h"
35#include "../utility/utility.h"
36
37namespace control_system
38{
43
44 namespace pid
45 {
46 /**
47 * @namespace pid
48 * @brief Contains proportional-integral-derivative controller implementations.
49 */
50
76
78 * I_max,
79 * u_max
80 * );
81 *
82 * double output = controller.update(reference, measurement);
83 * @endcode
84 */
85 template <typename T>
86 class PID_P
87 {
88 public:
91 *
92 * @details
93 * Constructs the controller with its parameters and internal
94 * state initialized to their default values.
95 */
97
101
103 * @param[in] Ki_ Integral gain.
104 * @param[in] Kd_ Derivative gain.
105 * @param[in] I_max_ Maximum allowed integral contribution.
106 * @param[in] u_max_ Maximum allowed controller output.
107 * @param[in] d_filter_ Enables or disables derivative filtering.
108 * @param[in] fc_ Cutoff frequency of the derivative filter in Hz.
110 * @details
111 * Initializes the controller parameters and optionally
112 * configures the derivative low-pass filter.
113 */
114 void init(T dt_, T Kp_, T Ki_, T Kd_, T I_max_, T u_max_, bool d_filter_ = false, T fc_ = 10.0);
119
121 * @param[in] Ki_ Integral gain.
122 * @param[in] Kd_ Derivative gain.
123 * @param[in] I_max_ Maximum allowed integral contribution.
124 * @param[in] u_max_ Maximum allowed controller output.
125 * @param[in] d_filter_ Enables or disables derivative filtering.
126 * @param[in] fc_ Cutoff frequency of the derivative filter in Hz.
128 * @details
129 * Updates the PID gains, sampling time, output limits, and
130 * derivative filter configuration.
131 */
132 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);
137
139 * @param[in] u_ff_ Feed-forward control input.
140 *
141 * @return PID controller output.
142 *
143 * @details
144 * Calculates the proportional, integral, derivative, and
145 * feed-forward contributions and combines them to produce
146 * the controller output.
147 *
148 * The integral contribution and final controller output are
149 * subject to their configured limits.
150 */
151 T update(T x_0, T x, T u_ff_ = 0.0);
156
158 * derivative, controller output, feed-forward value, and
159 * startup state.
160 */
161 void reset();
162
164 * @brief Merges an external controller output into the PID state.
165 *
166 * @param[in] u_k_1_ Previous or externally provided controller
167 * output.
168 *
169 * @details
170 * Updates the internal controller state using the specified
171 * previous controller output.
172 */
173 void merge(T u_k_1_);
174
176 * @brief Sets the controller sampling time.
177 *
178 * @param[in] dt_ Sampling time in seconds.
179 */
180 void set_dt(T dt_);
181
182 /**
183 * @brief Sets the proportional gain.
184 *
185 * @param[in] Kp_ Proportional gain.
186 */
187 void set_Kp(T Kp_);
192
194 void set_Ki(T Ki_);
195
198
200 */
201 void set_Kd(T Kd_);
202
204
206 * @param[in] I_max_ Maximum allowed integral contribution.
207 */
208 void set_I_max(T I_max_);
209
210
213 * @param[in] u_max_ Maximum allowed controller output.
214 */
215 void set_u_max(T u_max_);
216
218 * @brief Enables or disables derivative filtering.
219 *
220 * @param[in] d_filter_ Derivative filter enable state.
221 */
222 void set_d_filter(bool d_filter_);
223
224 /**
225 * @brief Sets the derivative filter cutoff frequency.
226 *
227 * @param[in] fc_ Cutoff frequency in Hz.
228 */
229 void set_fc(T fc_);
234
236 void set_ff(T u_ff_);
237
243 T get_dt();
244
250 T get_Kp();
251
257 T get_Ki();
258
264 T get_Kd();
265
271 T get_I_max();
272
278 T get_u_max();
279
286 bool get_d_filter();
287
293 T get_fc();
294
300 T get_ff();
301
307 T get_P();
308
314 T get_I();
315
321 T get_D();
322
328 T get_u();
329
335 T get_e_k_1();
336
337 private:
341 LPF_1<T> lpf;
342
349 T dt = 0.0;
350
354 T Kp = 0.0;
355
359 T Ki = 0.0;
360
364 T Kd = 0.0;
365
369 T I_max = 0.0;
370
374 T u_max = 0.0;
375
379 bool d_filter = false;
380
387 T fc = 0.0;
388
392 T e_k_1 = 0.0;
393
397 T P = 0.0;
398
402 T I = 0.0;
403
407 T D = 0.0;
408
412 T u = 0.0;
413
417 T u_ff = 0.0;
418
425 bool start = true;
426 };
427
429 }
430}
431
432#endif
void set_d_filter(bool d_filter_)
Enables or disables derivative filtering.
Definition pid_p.h:133
T get_u_max()
Gets the maximum controller output.
Definition pid_p.h:182
T get_Kp()
Gets the proportional gain.
Definition pid_p.h:158
T fc
Cutoff frequency of the derivative filter.
Definition pid_p.h:387
T get_P()
Gets the proportional contribution.
Definition pid_p.h:206
void set_dt(T dt_)
Sets the controller sampling time.
Definition pid_p.h:96
T dt
Controller sampling time.
Definition pid_p.h:349
void set_Kd(T Kd_)
Sets the derivative gain.
Definition pid_p.h:115
void set_Ki(T Ki_)
Sets the integral gain.
Definition pid_p.h:109
T I
Integral contribution.
Definition pid_p.h:402
void set_Kp(T Kp_)
Sets the proportional gain.
Definition pid_p.h:103
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition pid_p.h:127
T Kp
Proportional gain.
Definition pid_p.h:354
T get_Ki()
Gets the integral gain.
Definition pid_p.h:164
void set_ff(T u_ff_)
Sets the feed-forward control input.
Definition pid_p.h:146
T update(T x_0, T x, T u_ff_=0.0)
Computes the PID control output.
Definition pid_p.h:46
T Kd
Derivative gain.
Definition pid_p.h:364
T get_u()
Gets the controller output.
Definition pid_p.h:224
T D
Derivative contribution.
Definition pid_p.h:407
T e_k_1
Previous control error.
Definition pid_p.h:392
T I_max
Maximum allowed integral contribution.
Definition pid_p.h:369
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_p.h:32
T get_ff()
Gets the feed-forward control input.
Definition pid_p.h:200
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_p.h:25
T get_I_max()
Gets the maximum integral contribution.
Definition pid_p.h:176
void merge(T u_k_1_)
Merges an external controller output into the PID state.
Definition pid_p.h:91
T get_D()
Gets the derivative contribution.
Definition pid_p.h:218
T get_Kd()
Gets the derivative gain.
Definition pid_p.h:170
T P
Proportional contribution.
Definition pid_p.h:397
T get_e_k_1()
Gets the previous control error.
Definition pid_p.h:230
LPF_1< T > lpf
First-order low-pass filter used for derivative filtering.
Definition pid_p.h:341
T u_max
Maximum allowed controller output.
Definition pid_p.h:374
T u_ff
Feed-forward control input.
Definition pid_p.h:417
T get_I()
Gets the integral contribution.
Definition pid_p.h:212
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
Definition pid_p.h:139
bool get_d_filter()
Gets the derivative filter enable state.
Definition pid_p.h:188
T Ki
Integral gain.
Definition pid_p.h:359
bool d_filter
Derivative filter enable state.
Definition pid_p.h:379
T get_fc()
Gets the derivative filter cutoff frequency.
Definition pid_p.h:194
void reset()
Resets the PID controller state.
Definition pid_p.h:78
T get_dt()
Gets the controller sampling time.
Definition pid_p.h:152
bool start
Controller startup state.
Definition pid_p.h:425
void set_I_max(T I_max_)
Sets the maximum integral contribution.
Definition pid_p.h:121
PID_P()
Constructs a PID controller.
Definition pid_p.h:5
T u
PID controller output.
Definition pid_p.h:412
Contains control-system algorithms and controllers.
Definition d.h:32
Contains proportional-integral-derivative controller implementations.
Utility functions for the Control System library.