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.h
Go to the documentation of this file.
1
5
* @details
6
* This file defines the @ref control_system::pid::PID class, which
7
* implements a discrete-time PID controller.
8
*
9
* The controller combines proportional, integral, and derivative
10
* components to generate a control output based on the error between
11
* a reference value and the measured value.
12
*
13
* The controller maintains previous error and output values as internal
14
* state variables and applies a configurable maximum output limit.
15
*
16
* The class is templated to support different numeric types such as
17
* @c float and @c double.
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_H
30
#define CONTROL_SYSTEM_PID_PID_H
31
32
#include <cstdint>
33
#include "
../utility/utility.h
"
34
35
namespace
control_system
36
{
41
42
namespace
pid
43
{
48
57
* The controller uses the sampling time to calculate the
58
* discrete-time integral and derivative contributions.
59
*
60
* The previous control output and previous error values are
61
* maintained internally as controller state.
62
*
63
* @tparam T Numeric data type used for the controller.
64
*
65
* @par Example
66
* @code{.cpp}
67
* control_system::pid::PID<double> controller;
68
*
69
* controller.init(
70
* 0.001,
71
* Kp,
72
* Ki,
73
* Kd,
74
* 0.0,
75
* 100.0
76
* );
77
*
78
* double output = controller.update(reference, measurement);
79
* @endcode
80
*/
81
template
<
typename
T>
82
class
PID
83
{
84
public
:
88
90
* state initialized to their default values.
91
*/
92
PID
();
93
94
96
*
97
* @param[in] dt_ Controller sampling time in seconds.
98
* @param[in] Kp_ Proportional gain.
99
* @param[in] Ki_ Integral gain.
100
* @param[in] Kd_ Derivative gain.
101
* @param[in] u_k_1 Previous controller output.
102
* @param[in] u_max_ Maximum allowed controller output.
103
*
104
* @details
105
* Initializes the controller parameters and previous output
106
* state.
107
*/
108
void
init
(T dt_, T Kp_, T Ki_, T Kd_, T u_k_1_, T u_max_);
109
112
114
* @param[in] Kp_ Proportional gain.
115
* @param[in] Ki_ Integral gain.
116
* @param[in] Kd_ Derivative gain.
117
* @param[in] u_k_1 Previous controller output.
118
* @param[in] u_max_ Maximum allowed controller output.
119
*
120
* @details
121
* Updates the controller gains, sampling time, previous
122
* output, and output limit.
123
*/
124
void
set_param
(T dt_, T Kp_, T Ki_, T Kd_, T u_k_1_, T u_max_);
125
126
/**
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
*
132
* @return PID controller output limited by the configured
133
* maximum output.
134
*
135
* @details
136
* Calculates the control output from the proportional,
137
* integral, and derivative contributions using the current
138
* control error and the stored controller state.
139
*/
140
T
update
(T x_0, T x);
141
142
144
*
145
* @details
146
* Resets the stored previous errors, previous output, and
147
* startup state of the controller.
148
*/
149
void
reset
();
150
154
161
void
merge
(T u_k_1_);
162
168
void
set_dt
(T dt_);
169
175
void
set_Kp
(T Kp_);
176
182
void
set_Ki
(T Ki_);
183
189
void
set_Kd
(T Kd_);
190
196
void
set_u_0
(T u_k_1_);
197
203
void
set_u_max
(T u_max_);
204
210
T
get_dt
();
211
217
T
get_Kp
();
218
224
T
get_Ki
();
225
231
T
get_Kd
();
232
238
T
get_u_k_1
();
239
245
T
get_e_k_1
();
246
252
T
get_e_k_2
();
253
259
T
get_u_max
();
260
261
private
:
269
T
dt
= 0.0;
270
277
T
u_k_1
= 0.0;
278
285
T
e_k_1
= 0.0;
286
290
T
e_k_2
= 0.0;
291
295
T
Kp
= 0.0;
296
300
T
Ki
= 0.0;
301
305
T
Kd
= 0.0;
306
314
T
u_max
= 9999999999;
315
323
uint8_t
start
= 0;
324
};
325
326
#include "
../../src/pid/pid.tpp
"
327
}
328
}
329
330
#endif
control_system::pid::PID::merge
void merge(T u_k_1_)
Merges an external controller output into the PID state.
Definition
pid.h:66
control_system::pid::PID::u_k_1
T u_k_1
Previous controller output.
Definition
pid.h:277
control_system::pid::PID::e_k_1
T e_k_1
Previous control error.
Definition
pid.h:285
control_system::pid::PID::set_dt
void set_dt(T dt_)
Sets the controller sampling time.
Definition
pid.h:72
control_system::pid::PID::u_max
T u_max
Maximum controller output.
Definition
pid.h:314
control_system::pid::PID::get_e_k_1
T get_e_k_1()
Gets the previous control error.
Definition
pid.h:138
control_system::pid::PID::get_Kd
T get_Kd()
Gets the derivative gain.
Definition
pid.h:126
control_system::pid::PID::e_k_2
T e_k_2
Control error from two iterations ago.
Definition
pid.h:290
control_system::pid::PID::set_param
void set_param(T dt_, T Kp_, T Ki_, T Kd_, T u_k_1_, T u_max_)
Sets the PID controller parameters.
Definition
pid.h:25
control_system::pid::PID::set_u_max
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition
pid.h:102
control_system::pid::PID::PID
PID()
Constructs a PID controller.
Definition
pid.h:5
control_system::pid::PID::get_u_max
T get_u_max()
Gets the maximum controller output.
Definition
pid.h:150
control_system::pid::PID::reset
void reset()
Resets the PID controller state.
Definition
pid.h:57
control_system::pid::PID::init
void init(T dt_, T Kp_, T Ki_, T Kd_, T u_k_1_, T u_max_)
Initializes the PID controller.
Definition
pid.h:18
control_system::pid::PID::dt
T dt
Controller sampling time.
Definition
pid.h:269
control_system::pid::PID::set_Ki
void set_Ki(T Ki_)
Sets the integral gain.
Definition
pid.h:84
control_system::pid::PID::Kp
T Kp
Proportional gain.
Definition
pid.h:295
control_system::pid::PID::Kd
T Kd
Derivative gain.
Definition
pid.h:305
control_system::pid::PID::get_Kp
T get_Kp()
Gets the proportional gain.
Definition
pid.h:114
control_system::pid::PID::set_u_0
void set_u_0(T u_k_1_)
Sets the previous controller output.
Definition
pid.h:96
control_system::pid::PID::get_dt
T get_dt()
Gets the controller sampling time.
Definition
pid.h:108
control_system::pid::PID::start
uint8_t start
Controller startup state.
Definition
pid.h:323
control_system::pid::PID::get_e_k_2
T get_e_k_2()
Gets the control error from two iterations ago.
Definition
pid.h:144
control_system::pid::PID::get_u_k_1
T get_u_k_1()
Gets the previous controller output.
Definition
pid.h:132
control_system::pid::PID::update
T update(T x_0, T x)
Computes the PID control output.
Definition
pid.h:36
control_system::pid::PID::Ki
T Ki
Integral gain.
Definition
pid.h:300
control_system::pid::PID::get_Ki
T get_Ki()
Gets the integral gain.
Definition
pid.h:120
control_system::pid::PID::set_Kp
void set_Kp(T Kp_)
Sets the proportional gain.
Definition
pid.h:78
control_system::pid::PID::set_Kd
void set_Kd(T Kd_)
Sets the derivative gain.
Definition
pid.h:90
control_system
Contains control-system algorithms and controllers.
Definition
d.h:32
pid
Contains proportional-integral-derivative controller implementations.
pid.tpp
utility.h
Utility functions for the Control System library.
include
pid
pid.h
Generated by
1.17.0