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.
Toggle main menu visibility
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
35
namespace
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>
86
class
PID_LPF_1
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
119
*
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
155
*
156
* @details
157
* Resets the stored controller outputs, control errors,
158
* and startup state.
159
*/
160
void
reset
();
161
165
167
*
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
365
#include "
../../src/pid/pid_lpf_1.tpp
"
366
}
367
}
368
369
#endif
control_system::pid::PID_LPF_1::init
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
control_system::pid::PID_LPF_1::set_u_0
void set_u_0(T u_k_1_)
Sets the previous controller output.
Definition
pid_lpf_1.h:107
control_system::pid::PID_LPF_1::set_Ki
void set_Ki(T Ki_)
Sets the integral gain.
Definition
pid_lpf_1.h:89
control_system::pid::PID_LPF_1::Kp
T Kp
Proportional gain.
Definition
pid_lpf_1.h:325
control_system::pid::PID_LPF_1::u_k_2
T u_k_2
Controller output from two iterations ago.
Definition
pid_lpf_1.h:307
control_system::pid::PID_LPF_1::get_Ki
T get_Ki()
Gets the integral gain.
Definition
pid_lpf_1.h:131
control_system::pid::PID_LPF_1::merge
void merge(T u_k_1_)
Merges an external controller output into the PID state.
Definition
pid_lpf_1.h:71
control_system::pid::PID_LPF_1::get_e_k_2
T get_e_k_2()
Gets the control error from two iterations ago.
Definition
pid_lpf_1.h:161
control_system::pid::PID_LPF_1::e_k_2
T e_k_2
Control error from two iterations ago.
Definition
pid_lpf_1.h:320
control_system::pid::PID_LPF_1::start
uint8_t start
Controller startup state.
Definition
pid_lpf_1.h:362
control_system::pid::PID_LPF_1::set_Kd
void set_Kd(T Kd_)
Sets the derivative gain.
Definition
pid_lpf_1.h:95
control_system::pid::PID_LPF_1::get_fc
T get_fc()
Gets the derivative filter cutoff frequency.
Definition
pid_lpf_1.h:143
control_system::pid::PID_LPF_1::set_dt
void set_dt(T dt_)
Sets the controller sampling time.
Definition
pid_lpf_1.h:77
control_system::pid::PID_LPF_1::update
T update(T x_0, T x)
Computes the PID control output.
Definition
pid_lpf_1.h:39
control_system::pid::PID_LPF_1::reset
void reset()
Resets the PID controller state.
Definition
pid_lpf_1.h:61
control_system::pid::PID_LPF_1::get_u_max
T get_u_max()
Gets the maximum controller output.
Definition
pid_lpf_1.h:167
control_system::pid::PID_LPF_1::Kd
T Kd
Derivative gain.
Definition
pid_lpf_1.h:335
control_system::pid::PID_LPF_1::get_Kp
T get_Kp()
Gets the proportional gain.
Definition
pid_lpf_1.h:125
control_system::pid::PID_LPF_1::u_k_1
T u_k_1
Previous controller output.
Definition
pid_lpf_1.h:302
control_system::pid::PID_LPF_1::dt
T dt
Controller sampling time.
Definition
pid_lpf_1.h:294
control_system::pid::PID_LPF_1::get_u_k_1
T get_u_k_1()
Gets the previous controller output.
Definition
pid_lpf_1.h:149
control_system::pid::PID_LPF_1::PID_LPF_1
PID_LPF_1()
Constructs a PID controller with filtered derivative.
Definition
pid_lpf_1.h:5
control_system::pid::PID_LPF_1::set_fc
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
Definition
pid_lpf_1.h:101
control_system::pid::PID_LPF_1::set_Kp
void set_Kp(T Kp_)
Sets the proportional gain.
Definition
pid_lpf_1.h:83
control_system::pid::PID_LPF_1::get_dt
T get_dt()
Gets the controller sampling time.
Definition
pid_lpf_1.h:119
control_system::pid::PID_LPF_1::u_max
T u_max
Maximum controller output.
Definition
pid_lpf_1.h:353
control_system::pid::PID_LPF_1::Ki
T Ki
Integral gain.
Definition
pid_lpf_1.h:330
control_system::pid::PID_LPF_1::set_param
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
control_system::pid::PID_LPF_1::e_k_1
T e_k_1
Previous control error.
Definition
pid_lpf_1.h:315
control_system::pid::PID_LPF_1::tau
T tau
Time constant of the derivative low-pass filter.
Definition
pid_lpf_1.h:344
control_system::pid::PID_LPF_1::set_u_max
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition
pid_lpf_1.h:113
control_system::pid::PID_LPF_1::get_e_k_1
T get_e_k_1()
Gets the previous control error.
Definition
pid_lpf_1.h:155
control_system::pid::PID_LPF_1::get_Kd
T get_Kd()
Gets the derivative gain.
Definition
pid_lpf_1.h:137
control_system
Contains control-system algorithms and controllers.
Definition
d.h:32
pid
Contains proportional-integral-derivative controller implementations.
pid_lpf_1.tpp
utility.h
Utility functions for the Control System library.
include
pid
pid_lpf_1.h
Generated by
1.17.0