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_lpf_1_ff.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 u_k_2 = 0.0;
12 e_k_1 = 0.0;
13 e_k_2 = 0.0;
14 u_ff = 0.0;
15 start = 0;
16}
17
18template <typename T>
19void control_system::pid::PID_LPF_1_FF<T>::init(T dt_, T Kp_, T Ki_, T Kd_, T fc_, T u_k_1_, T u_k_2_, T u_max_)
20{
21 set_param(dt_, Kp_, Ki_, Kd_, fc_, u_k_1_, u_k_2_, u_max_);
22 start = 0;
23}
24
25template <typename T>
26void control_system::pid::PID_LPF_1_FF<T>::set_param(T dt_, T Kp_, T Ki_, T Kd_, T fc_, T u_k_1_, T u_k_2_, T u_max_)
27{
28 dt = dt_;
29 Kp = Kp_;
30 Ki = Ki_;
31 Kd = Kd_;
32 tau = 1.0 / (6.28318530717958647692528676655900576 * fc_);
33 u_k_1 = u_k_1_;
34 u_k_2 = u_k_2_;
35 u_max = u_max_;
36}
37
38template <typename T>
40{
41 T e_k = x_0 - x;
42 u_ff = u_ff_;
43 T u_k = 0.0;
44 if ((start == 0) || (start == 1))
45 {
46 start++;
47 u_k = u_ff + u_k_1 + Kp * e_k;
48 }
49 else
50 {
51 u_k = u_ff + (-tau * u_k_2 + (2.0 * tau + dt) * u_k_1 + ((Kp + Ki * dt) * (dt + tau) + Kd) * e_k - (Kp * (2.0 * tau + dt) + Ki * dt * tau + 2.0 * Kd) * e_k_1 + (tau + Kd) * e_k_2) / (dt + tau);
52 }
53 u_k = saturate(u_k, -u_max, u_max);
54 u_k_2 = u_k_1;
55 u_k_1 = u_k;
56 e_k_2 = e_k_1;
57 e_k_1 = e_k;
58 return u_k;
59}
60
61template <typename T>
63{
64 u_k_1 = 0.0;
65 u_k_2 = 0.0;
66 e_k_1 = 0.0;
67 e_k_2 = 0.0;
68 u_ff = 0.0;
69 start = 0;
70}
71
72template <typename T>
74{
75 u_k_2 = u_k_2_;
76}
77
78template <typename T>
80{
81 dt = dt_;
82}
83
84template <typename T>
86{
87 Kp = Kp_;
88}
89
90template <typename T>
92{
93 Ki = Ki_;
94}
95
96template <typename T>
98{
99 Kd = Kd_;
100}
101
102template <typename T>
104{
105 tau = 1.0 / (6.28318530717958647692528676655900576 * fc_);
106}
107
108template <typename T>
110{
111 u_ff = u_ff_;
112}
113
114template <typename T>
116{
117 u_k_1 = u_k_1_;
118}
119
120template <typename T>
122{
123 u_max = u_max_;
124}
125
126template <typename T>
128{
129 return dt;
130}
131
132template <typename T>
134{
135 return Kp;
136}
137
138template <typename T>
140{
141 return Ki;
142}
143
144template <typename T>
146{
147 return Kd;
148}
149
150template <typename T>
152{
153 return 1.0 / (6.28318530717958647692528676655900576 * tau);
154}
155
156template <typename T>
158{
159 return u_ff;
160}
161
162template <typename T>
164{
165 return u_k_1;
166}
167
168template <typename T>
170{
171 return e_k_1;
172}
173
174template <typename T>
176{
177 return e_k_2;
178}
179
180template <typename T>
182{
183 return u_max;
184}
void set_u_0(T u_k_1_)
Sets the previous controller output.
T get_fc()
Gets the derivative filter cutoff frequency.
T get_u_max()
Gets the maximum controller output.
PID_LPF_1_FF()
Constructs a PID controller with filtered derivative and feed-forward support.
Definition pid_lpf_1_ff.h:5
void set_Kd(T Kd_)
Sets the derivative gain.
T get_u_k_1()
Gets the previous controller output.
void set_dt(T dt_)
Sets the controller sampling time.
void set_ff(T u_ff_)
Sets the feed-forward control input.
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.
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
T get_e_k_1()
Gets the previous control error.
void reset()
Resets the PID controller state.
void set_Ki(T Ki_)
Sets the integral gain.
T get_e_k_2()
Gets the control error from two iterations ago.
void set_u_max(T u_max_)
Sets the maximum controller output.
T get_Kd()
Gets the derivative gain.
T get_Ki()
Gets the integral gain.
T get_dt()
Gets the controller sampling time.
void merge(T u_k_1_)
Merges an external controller output into the PID state.
void set_Kp(T Kp_)
Sets the proportional gain.
T update(T x_0, T x, T u_ff_=0.0)
Computes the PID control output with feed-forward.
T get_ff()
Gets the feed-forward control input.
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.
T get_Kp()
Gets the proportional gain.
PID controller with first-order low-pass filtered derivative 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