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_p.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_P<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_P<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_P<T>::update(T x_0, T x, T u_ff_)
46{
47 T e_k = x_0 - x;
48 u_ff = u_ff_;
49
50 P = Kp * e_k;
51
52 I = I + Ki * e_k * dt;
53 I = saturate(I, -I_max, I_max);
54
55 if (start == true)
56 {
57 D = 0.0;
58 start = false;
59 }
60 else
61 {
62 D = Kd * (e_k - e_k_1) / dt;
63 }
64
65 if (d_filter == true)
66 {
67 lpf.cal_y(D);
68 D = lpf.get_y();
69 }
70 e_k_1 = e_k;
71 u = u_ff + P + I + D;
72 u = saturate(u, -u_max, u_max);
73 return u;
74}
75
76template <typename T>
78{
79 e_k_1 = 0.0;
80 P = 0.0;
81 I = 0.0;
82 D = 0.0;
83 u = 0.0;
84 u_ff = 0.0;
85 start = true;
86 lpf.reset();
87}
88
89template <typename T>
91{
92}
93
94template <typename T>
96{
97 dt = dt_;
98 lpf.set_param(fc, dt);
99}
100
101template <typename T>
103{
104 Kp = Kp_;
105}
106
107template <typename T>
109{
110 Ki = Ki_;
111}
112
113template <typename T>
115{
116 Kd = Kd_;
117}
118
119template <typename T>
121{
122 I_max = I_max_;
123}
124
125template <typename T>
127{
128 u_max = u_max_;
129}
130
131template <typename T>
133{
134 d_filter = d_filter_;
135}
136
137template <typename T>
139{
140 fc = fc_;
141 lpf.set_param(fc, dt);
142}
143
144template <typename T>
146{
147 u_ff = u_ff_;
148}
149
150template <typename T>
152{
153 return dt;
154}
155
156template <typename T>
158{
159 return Kp;
160}
161
162template <typename T>
164{
165 return Ki;
166}
167
168template <typename T>
170{
171 return Kd;
172}
173
174template <typename T>
176{
177 return I_max;
178}
179
180template <typename T>
182{
183 return u_max;
184}
185
186template <typename T>
188{
189 return d_filter;
190}
191
192template <typename T>
194{
195 return fc;
196}
197
198template <typename T>
200{
201 return u_ff;
202}
203
204template <typename T>
206{
207 return P;
208}
209
210template <typename T>
212{
213 return I;
214}
215
216template <typename T>
218{
219 return D;
220}
221
222template <typename T>
224{
225 return u;
226}
227
228template <typename T>
230{
231 return e_k_1;
232}
void set_d_filter(bool d_filter_)
Enables or disables derivative filtering.
Definition pid_p.h:133
T get_u_max()
Gets the maximum controller output.
Definition pid_p.h:182
T get_Kp()
Gets the proportional gain.
Definition pid_p.h:158
T get_P()
Gets the proportional contribution.
Definition pid_p.h:206
void set_dt(T dt_)
Sets the controller sampling time.
Definition pid_p.h:96
void set_Kd(T Kd_)
Sets the derivative gain.
Definition pid_p.h:115
void set_Ki(T Ki_)
Sets the integral gain.
Definition pid_p.h:109
void set_Kp(T Kp_)
Sets the proportional gain.
Definition pid_p.h:103
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition pid_p.h:127
T get_Ki()
Gets the integral gain.
Definition pid_p.h:164
void set_ff(T u_ff_)
Sets the feed-forward control input.
Definition pid_p.h:146
T update(T x_0, T x, T u_ff_=0.0)
Computes the PID control output.
Definition pid_p.h:46
T get_u()
Gets the controller output.
Definition pid_p.h:224
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_p.h:32
T get_ff()
Gets the feed-forward control input.
Definition pid_p.h:200
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_p.h:25
T get_I_max()
Gets the maximum integral contribution.
Definition pid_p.h:176
void merge(T u_k_1_)
Merges an external controller output into the PID state.
Definition pid_p.h:91
T get_D()
Gets the derivative contribution.
Definition pid_p.h:218
T get_Kd()
Gets the derivative gain.
Definition pid_p.h:170
T get_e_k_1()
Gets the previous control error.
Definition pid_p.h:230
T get_I()
Gets the integral contribution.
Definition pid_p.h:212
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
Definition pid_p.h:139
bool get_d_filter()
Gets the derivative filter enable state.
Definition pid_p.h:188
T get_fc()
Gets the derivative filter cutoff frequency.
Definition pid_p.h:194
void reset()
Resets the PID controller state.
Definition pid_p.h:78
T get_dt()
Gets the controller sampling time.
Definition pid_p.h:152
void set_I_max(T I_max_)
Sets the maximum integral contribution.
Definition pid_p.h:121
PID_P()
Constructs a PID controller.
Definition pid_p.h:5
PID controller with configurable derivative filtering and feed-forward.
constexpr T saturate(T x, T x_min, T x_max)
Saturates a value within a specified range.
Definition utility.h:51