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.tpp
Go to the documentation of this file.
2
3template <typename T>
5{
6 dt = 0.0;
7 Kp = 0.0;
8 Ki = 0.0;
9 Kd = 0.0;
10 u_k_1 = 0.0;
11 e_k_1 = 0.0;
12 e_k_2 = 0.0;
13 start = 0;
14}
15
16template <typename T>
17void control_system::pid::PID<T>::init(T dt_, T Kp_, T Ki_, T Kd_, T u_k_1_, T u_max_)
18{
19 set_param(dt_, Kp_, Ki_, Kd_, u_k_1_, u_max_);
20 start = 0;
21}
22
23template <typename T>
24void control_system::pid::PID<T>::set_param(T dt_, T Kp_, T Ki_, T Kd_, T u_k_1_, T u_max_)
25{
26 dt = dt_;
27 Kp = Kp_;
28 Ki = Ki_;
29 Kd = Kd_;
30 u_k_1 = u_k_1_;
31 u_max = u_max_;
32}
33
34template <typename T>
36{
37 T e_k = x_0 - x;
38 T u_k = 0.0;
39 if ((start == 0) || (start == 1))
40 {
41 start++;
42 u_k = u_k_1 + Kp * e_k;
43 }
44 else
45 {
46 u_k = u_k_1 + (Kp + Ki * dt + Kd / dt) * e_k - (Kp + 2.0 * Kd / dt) * e_k_1 + (Kd / dt) * e_k_2;
47 }
48 u_k = saturate(u_k, -u_max, u_max);
49 u_k_1 = u_k;
50 e_k_2 = e_k_1;
51 e_k_1 = e_k;
52 return u_k;
53}
54
55template <typename T>
57{
58 u_k_1 = 0.0;
59 e_k_1 = 0.0;
60 e_k_2 = 0.0;
61 start = 0;
62}
63
64template <typename T>
66{
67 u_k_1 = u_k_1_;
68}
69
70template <typename T>
72{
73 dt = dt_;
74}
75
76template <typename T>
78{
79 Kp = Kp_;
80}
81
82template <typename T>
84{
85 Ki = Ki_;
86}
87
88template <typename T>
90{
91 Kd = Kd_;
92}
93
94template <typename T>
96{
97 u_k_1 = u_k_1_;
98}
99
100template <typename T>
102{
103 u_max = u_max_;
104}
105
106template <typename T>
108{
109 return dt;
110}
111
112template <typename T>
114{
115 return Kp;
116}
117
118template <typename T>
120{
121 return Ki;
122}
123
124template <typename T>
126{
127 return Kd;
128}
129
130template <typename T>
132{
133 return u_k_1;
134}
135
136template <typename T>
138{
139 return e_k_1;
140}
141
142template <typename T>
144{
145 return e_k_2;
146}
147
148template <typename T>
150{
151 return u_max;
152}
void merge(T u_k_1_)
Merges an external controller output into the PID state.
Definition pid.h:66
void set_dt(T dt_)
Sets the controller sampling time.
Definition pid.h:72
T get_e_k_1()
Gets the previous control error.
Definition pid.h:138
T get_Kd()
Gets the derivative gain.
Definition pid.h:126
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
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition pid.h:102
PID()
Constructs a PID controller.
Definition pid.h:5
T get_u_max()
Gets the maximum controller output.
Definition pid.h:150
void reset()
Resets the PID controller state.
Definition pid.h:57
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
void set_Ki(T Ki_)
Sets the integral gain.
Definition pid.h:84
T get_Kp()
Gets the proportional gain.
Definition pid.h:114
void set_u_0(T u_k_1_)
Sets the previous controller output.
Definition pid.h:96
T get_dt()
Gets the controller sampling time.
Definition pid.h:108
T get_e_k_2()
Gets the control error from two iterations ago.
Definition pid.h:144
T get_u_k_1()
Gets the previous controller output.
Definition pid.h:132
T update(T x_0, T x)
Computes the PID control output.
Definition pid.h:36
T get_Ki()
Gets the integral gain.
Definition pid.h:120
void set_Kp(T Kp_)
Sets the proportional gain.
Definition pid.h:78
void set_Kd(T Kd_)
Sets the derivative gain.
Definition pid.h:90
Proportional-Integral-Derivative (PID) controller.
constexpr T saturate(T x, T x_min, T x_max)
Saturates a value within a specified range.
Definition utility.h:51