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_gs.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 fc = 0.0;
11 I_max = 0.0;
12 u_max = 0.0;
13 d_filter = false;
14 e_k_1 = 0.0;
15 P = 0.0;
16 I = 0.0;
17 D = 0.0;
18 u = 0.0;
19 u_ff = 0.0;
20 start = true;
21}
22
23template <typename T>
24void control_system::pid::PID_GS<T>::init(T dt_, T Kp_, T Ki_, T Kd_, T I_max_, T u_max_, bool d_filter_, T fc_)
25{
26 set_param(dt_, Kp_, Ki_, Kd_, I_max_, u_max_, d_filter_, fc_);
27 start = true;
28}
29
30template <typename T>
31void control_system::pid::PID_GS<T>::set_param(T dt_, T Kp_, T Ki_, T Kd_, T I_max_, T u_max_, bool d_filter_, T fc_)
32{
33 dt = dt_;
34 Kp = Kp_;
35 Ki = Ki_;
36 Kd = Kd_;
37 I_max = I_max_;
38 u_max = u_max_;
39 d_filter = d_filter_;
40 fc = fc_;
41 lpf.set_param(fc, dt);
42}
43
44template <typename T>
45T control_system::pid::PID_GS<T>::update(T x_0, T x, T u_ff_, T op_cond_)
46{
47 update_gain(op_cond_);
48
49 T e_k = x_0 - x;
50 u_ff = u_ff_;
51
52 P = Kp * e_k;
53
54 I = I + Ki * e_k * dt;
55 I = saturate(I, -I_max, I_max);
56
57 if (start == true)
58 {
59 D = 0.0;
60 start = false;
61 }
62 else
63 {
64 D = Kd * (e_k - e_k_1) / dt;
65 }
66
67 if (d_filter == true)
68 {
69 lpf.cal_y(D);
70 D = lpf.get_y();
71 }
72 e_k_1 = e_k;
73 u = u_ff + P + I + D;
74 u = saturate(u, -u_max, u_max);
75 return u;
76}
77
78template <typename T>
80{
81 e_k_1 = 0.0;
82 P = 0.0;
83 I = 0.0;
84 D = 0.0;
85 u = 0.0;
86 u_ff = 0.0;
87 start = true;
88 lpf.reset();
89}
90
91template <typename T>
93{
94}
95
96template <typename T>
97void control_system::pid::PID_GS<T>::set_gain_lookup_table(T *op_cond_arr_, T *Kp_arr_, T *Ki_arr_, T *Kd_arr_, uint16_t len_arr_)
98{
99 op_cond_arr = op_cond_arr_;
100 Kp_arr = Kp_arr_;
101 Ki_arr = Ki_arr_;
102 Kd_arr = Kd_arr_;
103 len_arr = len_arr_;
104}
105
106template <typename T>
108{
109 if (op_cond_ <= op_cond_arr[0])
110 {
111 Kp = Kp_arr[0];
112 Ki = Ki_arr[0];
113 Kd = Kd_arr[0];
114 }
115 else if (op_cond_ >= op_cond_arr[len_arr - 1])
116 {
117 Kp = Kp_arr[len_arr - 1];
118 Ki = Ki_arr[len_arr - 1];
119 Kd = Kd_arr[len_arr - 1];
120 }
121 else
122 {
123 uint16_t index_i = 0;
124
125 for (uint16_t i = 0; i < len_arr; i++)
126 {
127 if (op_cond_ > op_cond_arr[i])
128 {
129 index_i++;
130 }
131 else
132 {
133 break;
134 }
135 }
136
137 Kp = interpolate(op_cond_, op_cond_arr[index_i], Kp_arr[index_i], op_cond_arr[index_i + 1], Kp_arr[index_i + 1]);
138 Ki = interpolate(op_cond_, op_cond_arr[index_i], Ki_arr[index_i], op_cond_arr[index_i + 1], Ki_arr[index_i + 1]);
139 Kd = interpolate(op_cond_, op_cond_arr[index_i], Kd_arr[index_i], op_cond_arr[index_i + 1], Kd_arr[index_i + 1]);
140 }
141}
142
143template <typename T>
145{
146 dt = dt_;
147 lpf.set_param(fc, dt);
148}
149
150template <typename T>
152{
153 Kp = Kp_;
154}
155
156template <typename T>
158{
159 Ki = Ki_;
160}
161
162template <typename T>
164{
165 Kd = Kd_;
166}
167
168template <typename T>
170{
171 I_max = I_max_;
172}
173
174template <typename T>
176{
177 u_max = u_max_;
178}
179
180template <typename T>
182{
183 d_filter = d_filter_;
184}
185
186template <typename T>
188{
189 fc = fc_;
190 lpf.set_param(fc, dt);
191}
192
193template <typename T>
195{
196 u_ff = u_ff_;
197}
198
199template <typename T>
201{
202 return dt;
203}
204
205template <typename T>
207{
208 return Kp;
209}
210
211template <typename T>
213{
214 return Ki;
215}
216
217template <typename T>
219{
220 return Kd;
221}
222
223template <typename T>
225{
226 return I_max;
227}
228
229template <typename T>
231{
232 return u_max;
233}
234
235template <typename T>
237{
238 return d_filter;
239}
240
241template <typename T>
243{
244 return fc;
245}
246
247template <typename T>
249{
250 return u_ff;
251}
252
253template <typename T>
255{
256 return P;
257}
258
259template <typename T>
261{
262 return I;
263}
264
265template <typename T>
267{
268 return D;
269}
270
271template <typename T>
273{
274 return u;
275}
276
277template <typename T>
279{
280 return e_k_1;
281}
282
283template <typename T>
284T control_system::pid::PID_GS<T>::interpolate(T x, T x1, T y1, T x2, T y2)
285{
286 return y1 + (x - x1) * (y2 - y1) / (x2 - x1);
287}
void set_ff(T u_ff_)
Sets the feed-forward control input.
Definition pid_gs.h:195
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
Definition pid_gs.h:188
void set_Ki(T Ki_)
Sets the integral gain.
Definition pid_gs.h:158
T get_ff()
Gets the feed-forward control input.
Definition pid_gs.h:249
void reset()
Resets the PID controller state.
Definition pid_gs.h:80
void set_gain_lookup_table(T *op_cond_arr_, T *Kp_arr_, T *Ki_arr_, T *Kd_arr_, uint16_t len_arr_)
Configures the gain-scheduling lookup tables.
Definition pid_gs.h:98
T get_P()
Gets the proportional contribution.
Definition pid_gs.h:255
PID_GS()
Constructs a gain-scheduled PID controller.
Definition pid_gs.h:5
T get_u()
Gets the controller output.
Definition pid_gs.h:273
void update_gain(T op_cond_)
Updates the PID gains according to the operating condition.
Definition pid_gs.h:108
T interpolate(T x, T x1, T y1, T x2, T y2)
Linearly interpolates a value between two lookup-table points.
Definition pid_gs.h:285
T get_I_max()
Gets the maximum integral contribution.
Definition pid_gs.h:225
T get_e_k_1()
Gets the previous control error.
Definition pid_gs.h:279
void set_d_filter(bool d_filter_)
Enables or disables derivative filtering.
Definition pid_gs.h:182
bool get_d_filter()
Gets the derivative filter enable state.
Definition pid_gs.h:237
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_gs.h:32
T get_D()
Gets the derivative contribution.
Definition pid_gs.h:267
T get_u_max()
Gets the maximum controller output.
Definition pid_gs.h:231
T update(T x_0, T x, T u_ff_=0.0, T op_cond_=0.0)
Computes the gain-scheduled PID control output.
Definition pid_gs.h:46
void set_Kd(T Kd_)
Sets the derivative gain.
Definition pid_gs.h:164
T get_Kp()
Gets the proportional gain.
Definition pid_gs.h:207
T get_Ki()
Gets the integral gain.
Definition pid_gs.h:213
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition pid_gs.h:176
T get_dt()
Gets the controller sampling time.
Definition pid_gs.h:201
void set_I_max(T I_max_)
Sets the maximum integral contribution.
Definition pid_gs.h:170
void set_dt(T dt_)
Sets the controller sampling time.
Definition pid_gs.h:145
void set_Kp(T Kp_)
Sets the proportional gain.
Definition pid_gs.h:152
void merge(T u_k_1_)
Merges an external controller output into the PID state.
Definition pid_gs.h:93
T get_fc()
Gets the derivative filter cutoff frequency.
Definition pid_gs.h:243
T get_Kd()
Gets the derivative gain.
Definition pid_gs.h:219
T get_I()
Gets the integral contribution.
Definition pid_gs.h:261
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_gs.h:25
PID controller with gain scheduling.
constexpr T saturate(T x, T x_min, T x_max)
Saturates a value within a specified range.
Definition utility.h:51