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.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 start = 0;
15}
16
17template <typename T>
18void control_system::pid::PID_LPF_1<T>::init(T dt_, T Kp_, T Ki_, T Kd_, T fc_, T u_k_1_, T u_k_2_, T u_max_)
19{
20 set_param(dt_, Kp_, Ki_, Kd_, fc_, u_k_1_, u_k_2_, u_max_);
21 start = 0;
22}
23
24template <typename T>
25void control_system::pid::PID_LPF_1<T>::set_param(T dt_, T Kp_, T Ki_, T Kd_, T fc_, T u_k_1_, T u_k_2_, T u_max_)
26{
27 dt = dt_;
28 Kp = Kp_;
29 Ki = Ki_;
30 Kd = Kd_;
31 tau = 1.0 / (6.28318530717958647692528676655900576 * fc_);
32 u_k_1 = u_k_1_;
33 u_k_2 = u_k_2_;
34 u_max = u_max_;
35}
36
37template <typename T>
39{
40 T e_k = x_0 - x;
41 T u_k = 0.0;
42 if ((start == 0) || (start == 1))
43 {
44 start++;
45 u_k = u_k_1 + Kp * e_k;
46 }
47 else
48 {
49 u_k = (-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);
50 }
51 u_k = saturate(u_k, -u_max, u_max);
52 u_k_2 = u_k_1;
53 u_k_1 = u_k;
54 e_k_2 = e_k_1;
55 e_k_1 = e_k;
56 return u_k;
57}
58
59template <typename T>
61{
62 u_k_1 = 0.0;
63 u_k_2 = 0.0;
64 e_k_1 = 0.0;
65 e_k_2 = 0.0;
66 start = 0;
67}
68
69template <typename T>
71{
72 u_k_2 = u_k_2_;
73}
74
75template <typename T>
77{
78 dt = dt_;
79}
80
81template <typename T>
83{
84 Kp = Kp_;
85}
86
87template <typename T>
89{
90 Ki = Ki_;
91}
92
93template <typename T>
95{
96 Kd = Kd_;
97}
98
99template <typename T>
101{
102 tau = 1.0 / (6.28318530717958647692528676655900576 * fc_);
103}
104
105template <typename T>
107{
108 u_k_1 = u_k_1_;
109}
110
111template <typename T>
113{
114 u_max = u_max_;
115}
116
117template <typename T>
119{
120 return dt;
121}
122
123template <typename T>
125{
126 return Kp;
127}
128
129template <typename T>
131{
132 return Ki;
133}
134
135template <typename T>
137{
138 return Kd;
139}
140
141template <typename T>
143{
144 return 1.0 / (6.28318530717958647692528676655900576 * tau);
145}
146
147template <typename T>
149{
150 return u_k_1;
151}
152
153template <typename T>
155{
156 return e_k_1;
157}
158
159template <typename T>
161{
162 return e_k_2;
163}
164
165template <typename T>
167{
168 return u_max;
169}
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.
Definition pid_lpf_1.h:19
void set_u_0(T u_k_1_)
Sets the previous controller output.
Definition pid_lpf_1.h:107
void set_Ki(T Ki_)
Sets the integral gain.
Definition pid_lpf_1.h:89
T get_Ki()
Gets the integral gain.
Definition pid_lpf_1.h:131
void merge(T u_k_1_)
Merges an external controller output into the PID state.
Definition pid_lpf_1.h:71
T get_e_k_2()
Gets the control error from two iterations ago.
Definition pid_lpf_1.h:161
void set_Kd(T Kd_)
Sets the derivative gain.
Definition pid_lpf_1.h:95
T get_fc()
Gets the derivative filter cutoff frequency.
Definition pid_lpf_1.h:143
void set_dt(T dt_)
Sets the controller sampling time.
Definition pid_lpf_1.h:77
T update(T x_0, T x)
Computes the PID control output.
Definition pid_lpf_1.h:39
void reset()
Resets the PID controller state.
Definition pid_lpf_1.h:61
T get_u_max()
Gets the maximum controller output.
Definition pid_lpf_1.h:167
T get_Kp()
Gets the proportional gain.
Definition pid_lpf_1.h:125
T get_u_k_1()
Gets the previous controller output.
Definition pid_lpf_1.h:149
PID_LPF_1()
Constructs a PID controller with filtered derivative.
Definition pid_lpf_1.h:5
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
Definition pid_lpf_1.h:101
void set_Kp(T Kp_)
Sets the proportional gain.
Definition pid_lpf_1.h:83
T get_dt()
Gets the controller sampling time.
Definition pid_lpf_1.h:119
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.
Definition pid_lpf_1.h:26
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition pid_lpf_1.h:113
T get_e_k_1()
Gets the previous control error.
Definition pid_lpf_1.h:155
T get_Kd()
Gets the derivative gain.
Definition pid_lpf_1.h:137
PID controller with first-order low-pass filtered derivative.
constexpr T saturate(T x, T x_min, T x_max)
Saturates a value within a specified range.
Definition utility.h:51