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
pd_lpf_1.tpp
Go to the documentation of this file.
2
3template <typename T>
5{
6 dt = 0.0;
7 e_k_1 = 0.0;
8 Kd = 0.0;
9 start = true;
10
11 u_k_1 = 0.0;
12 tau = 0.0;
13}
14
15template <typename T>
16void control_system::pd::PD_LPF_1<T>::init(T dt_, T Kp_, T Kd_, T fc_, T u_max_)
17{
18 set_param(dt_, Kp_, Kd_, fc_, u_max_);
19 start = true;
20}
21
22template <typename T>
23void control_system::pd::PD_LPF_1<T>::set_param(T dt_, T Kp_, T Kd_, T fc_, T u_max_)
24{
25 dt = dt_;
26 Kp = Kp_;
27 Kd = Kd_;
28 tau = 1.0 / (6.28318530717958647692528676655900576 * fc_);
29 u_max = u_max_;
30}
31
32template <typename T>
34{
35 T e_k = x_0 - x;
36 T u_k = 0.0;
37 if (start == true)
38 {
39 start = false;
40 u_k = Kp * e_k;
41 }
42 else
43 {
44 u_k = (tau * u_k_1 + (Kp * (dt + tau) + Kd) * e_k - (Kp * tau + Kd) * e_k_1) / (tau + dt);
45 }
46 u_k = saturate(u_k, -u_max, u_max);
47 e_k_1 = e_k;
48 u_k_1 = u_k;
49 return u_k;
50}
51
52template <typename T>
54{
55 e_k_1 = 0.0;
56 start = true;
57 u_k_1 = 0.0;
58}
59
60template <typename T>
62{
63}
64
65template <typename T>
67{
68 dt = dt_;
69}
70
71template <typename T>
73{
74 Kp = Kp_;
75}
76
77template <typename T>
79{
80 Kd = Kd_;
81}
82
83template <typename T>
85{
86 tau = 1.0 / (6.28318530717958647692528676655900576 * fc_);
87}
88
89template <typename T>
91{
92 u_max = u_max_;
93}
94
95template <typename T>
97{
98 return dt;
99}
100
101template <typename T>
103{
104 return Kp;
105}
106
107template <typename T>
109{
110 return Kd;
111}
112
113template <typename T>
115{
116 return 1.0 / (6.28318530717958647692528676655900576 * tau);
117}
118
119template <typename T>
121{
122 return e_k_1;
123}
124
125template <typename T>
127{
128 return u_k_1;
129}
130
131template <typename T>
133{
134 return u_max;
135}
T get_fc()
Gets the derivative filter cutoff frequency.
Definition pd_lpf_1.h:115
PD_LPF_1()
Constructs a PD controller with filtered derivative.
Definition pd_lpf_1.h:5
void set_fc(T fc_)
Sets the derivative filter cutoff frequency.
Definition pd_lpf_1.h:85
void merge(T u_k_1_)
Merges an external controller output into the PD state.
Definition pd_lpf_1.h:62
void set_param(T dt_, T Kp_, T Kd_, T fc_, T u_max_)
Sets the PD controller parameters.
Definition pd_lpf_1.h:24
T get_Kp()
Gets the proportional gain.
Definition pd_lpf_1.h:103
T get_u_k_1()
Gets the previous controller output.
Definition pd_lpf_1.h:127
T get_e_k_1()
Gets the previous control error.
Definition pd_lpf_1.h:121
void set_u_max(T u_max_)
Sets the maximum controller output.
Definition pd_lpf_1.h:91
T get_u_max()
Gets the maximum controller output.
Definition pd_lpf_1.h:133
void set_Kd(T Kd_)
Sets the derivative gain.
Definition pd_lpf_1.h:79
T get_Kd()
Gets the derivative gain.
Definition pd_lpf_1.h:109
void set_dt(T dt_)
Sets the controller sampling time.
Definition pd_lpf_1.h:67
void init(T dt_, T Kp_, T Kd_, T fc_, T u_max_)
Initializes the PD controller.
Definition pd_lpf_1.h:17
T update(T x_0, T x)
Computes the PD control output.
Definition pd_lpf_1.h:34
void reset()
Resets the PD controller state.
Definition pd_lpf_1.h:54
void set_Kp(T Kp_)
Sets the proportional gain.
Definition pd_lpf_1.h:73
T get_dt()
Gets the controller sampling time.
Definition pd_lpf_1.h:97
Proportional-Derivative (PD) controller with first-order low-pass filtering.
constexpr T saturate(T x, T x_min, T x_max)
Saturates a value within a specified range.
Definition utility.h:51