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_ff.h
Go to the documentation of this file.
1
5 * @details
6 * This file defines the @ref control_system::pid::PID_LPF_1_FF class,
7 * which implements a discrete-time proportional-integral-derivative
8 * (PID) controller with a first-order low-pass filtered derivative
9 * contribution and feed-forward control.
10 *
11 * The controller uses configurable proportional, integral, and
12 * derivative gains together with a derivative filter cutoff frequency.
13 * A feed-forward contribution can be added to the PID controller output.
14 *
15 * Previous controller outputs and control errors are maintained as
16 * internal state variables.
17 *
18 * The controller output is constrained by a configurable maximum
19 * output limit.
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_LPF_1_FF_H
32#define CONTROL_SYSTEM_PID_PID_LPF_1_FF_H
33
34#include <cstdint>
35#include "../utility/utility.h"
36
37namespace control_system
38{
40 * @namespace control_system
41 * @brief Contains control-system algorithms and controllers.
42 */
43
44 namespace pid
45 {
50
61
63 * The controller is configured using the sampling time,
64 * proportional gain, integral gain, derivative gain, derivative
65 * filter cutoff frequency, previous controller outputs, and
66 * maximum output limit.
67 *
68 * @tparam T Numeric data type used for the controller.
69 *
70 * @par Example
71 * @code{.cpp}
72 * control_system::pid::PID_LPF_1_FF<double> controller;
73 *
74 * controller.init(
75 * 0.001,
76 * Kp,
77 * Ki,
78 * Kd,
79 * 10.0,
80 * 0.0,
81 * 0.0,
82 * 100.0
83 * );
84 *
85 * double output = controller.update(reference, measurement, feed_forward);
86 * @endcode
87 */
88 template <typename T>
90 {
91 public:
92 /**
93 * @brief Constructs a PID controller with filtered derivative
94 * and feed-forward support.
95 *
96 * @details
97 * Constructs the controller with its parameters and internal
98 * state initialized to their default values.
99 */
100 PID_LPF_1_FF();
101
102
105 * @param[in] dt_ Controller sampling time in seconds.
106 * @param[in] Kp_ Proportional gain.
107 * @param[in] Ki_ Integral gain.
108 * @param[in] Kd_ Derivative gain.
109 * @param[in] fc_ Derivative filter cutoff frequency in Hz.
110 * @param[in] u_k_1_ Previous controller output.
111 * @param[in] u_k_2_ Controller output from two iterations ago.
112 * @param[in] u_max_ Maximum allowed controller output.
113 *
114 * @details
115 * Initializes the PID controller parameters, derivative filter
116 * parameters, previous controller outputs, and output limit.
117 */
118 void init(T dt_, T Kp_, T Ki_, T Kd_, T fc_, T u_k_1_, T u_k_2_, T u_max_);
119
120
123 * @param[in] dt_ Controller sampling time in seconds.
124 * @param[in] Kp_ Proportional gain.
125 * @param[in] Ki_ Integral gain.
126 * @param[in] Kd_ Derivative gain.
127 * @param[in] fc_ Derivative filter cutoff frequency in Hz.
128 * @param[in] u_k_1_ Previous controller output.
129 * @param[in] u_k_2_ Controller output from two iterations ago.
130 * @param[in] u_max_ Maximum allowed controller output.
131 *
132 * @details
133 * Updates the PID gains, sampling time, derivative filter
134 * cutoff frequency, previous controller outputs, and output
135 * limit.
136 */
137 void set_param(T dt_, T Kp_, T Ki_, T Kd_, T fc_, T u_k_1_, T u_k_2_, T u_max_);
138
140 * @brief Computes the PID control output with feed-forward.
141 *
142 * @param[in] x_0 Reference or desired value.
143 * @param[in] x Current or measured value.
144 * @param[in] u_ff_ Feed-forward control input.
145 *
146 * @return PID controller output including the feed-forward
147 * contribution and limited by the configured maximum output.
148 *
149 * @details
150 * Calculates the proportional, integral, and derivative
151 * contributions. The derivative contribution is filtered
152 * using the configured first-order low-pass filter, and the
153 * feed-forward contribution is incorporated into the final
154 * controller output.
155 */
156 T update(T x_0, T x, T u_ff_ = 0.0);
157
158 /**
159 * @brief Resets the PID controller state.
160 *
161 * @details
162 * Resets the stored controller outputs, control errors,
163 * feed-forward value, and startup state.
164 */
165 void reset();
166
168
170 * @param[in] u_k_1_ Previous or externally provided controller
171 * output.
172 *
173 * @details
174 * Updates the internal previous-output state using the
175 * specified controller output.
176 */
177 void merge(T u_k_1_);
178
180
182 * @param[in] dt_ Sampling time in seconds.
183 */
184 void set_dt(T dt_);
185
186
191 void set_Kp(T Kp_);
192
198 void set_Ki(T Ki_);
199
205 void set_Kd(T Kd_);
206
212 void set_fc(T fc_);
213
219 void set_ff(T u_ff_);
220
226 void set_u_0(T u_k_1_);
227
233 void set_u_max(T u_max_);
234
240 T get_dt();
241
247 T get_Kp();
248
254 T get_Ki();
255
261 T get_Kd();
262
268 T get_fc();
269
275 T get_ff();
276
282 T get_u_k_1();
283
289 T get_e_k_1();
290
296 T get_e_k_2();
297
303 T get_u_max();
304
305 private:
313 T dt = 0.0;
314
321 T u_k_1 = 0.0;
322
326 T u_k_2 = 0.0;
327
334 T e_k_1 = 0.0;
335
339 T e_k_2 = 0.0;
340
344 T Kp = 0.0;
345
349 T Ki = 0.0;
350
354 T Kd = 0.0;
355
363 T tau = 0.0;
364
368 T u_ff = 0.0;
369
377 T u_max = 9999999999;
378
386 uint8_t start = 0;
387 };
388
390 }
391}
392
393#endif
T u_ff
Feed-forward control input.
void set_u_0(T u_k_1_)
Sets the previous controller output.
T e_k_1
Previous control error.
T get_fc()
Gets the derivative filter cutoff frequency.
T dt
Controller sampling time.
T get_u_max()
Gets the maximum controller output.
PID_LPF_1_FF()
Constructs a PID controller with filtered derivative and feed-forward support.
Definition pid_lpf_1_ff.h:5
T e_k_2
Control error from two iterations ago.
void set_Kd(T Kd_)
Sets the derivative gain.
T get_u_k_1()
Gets the previous controller output.
T u_k_1
Previous controller output.
T u_max
Maximum controller output.
void set_dt(T dt_)
Sets the controller sampling time.
void set_ff(T u_ff_)
Sets the feed-forward control input.
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.
T u_k_2
Controller output from two iterations ago.
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
T tau
Time constant of the derivative low-pass filter.
T get_e_k_1()
Gets the previous control error.
void reset()
Resets the PID controller state.
void set_Ki(T Ki_)
Sets the integral gain.
T get_e_k_2()
Gets the control error from two iterations ago.
uint8_t start
Controller startup state.
void set_u_max(T u_max_)
Sets the maximum controller output.
T get_Kd()
Gets the derivative gain.
T get_Ki()
Gets the integral gain.
T get_dt()
Gets the controller sampling time.
void merge(T u_k_1_)
Merges an external controller output into the PID state.
void set_Kp(T Kp_)
Sets the proportional gain.
T update(T x_0, T x, T u_ff_=0.0)
Computes the PID control output with feed-forward.
T get_ff()
Gets the feed-forward control input.
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.
T get_Kp()
Gets the proportional gain.
Contains control-system algorithms and controllers.
Definition d.h:32
Contains proportional-integral-derivative controller implementations.
Utility functions for the Control System library.