|
24 | 24 | """ |
25 | 25 | DiscretePID(; K = 1, Ti = false, Td = false, Tt = √(Ti*Td), N = 10, b = 1, umin = -Inf, umax = Inf, Ts, I = 0, D = 0, yold = 0) |
26 | 26 |
|
27 | | -A discrete-time PID controller with set-point weighting and integrator anti-windup. |
| 27 | +A discrete-time PID controller with set-point weighting and integrator anti-windup. |
28 | 28 | The controller is implemented on the standard form |
29 | 29 | ```math |
30 | 30 | u = K \\left( e + \\dfrac{1}{Ti} \\int e dt + T_d \\dfrac{de}{dt} \\right) |
@@ -74,13 +74,13 @@ function DiscretePID(; |
74 | 74 | N ≥ 0 || throw(ArgumentError("N must be positive")) |
75 | 75 | 0 ≤ b ≤ 1 || throw(ArgumentError("b must be ∈ [0, 1]")) |
76 | 76 | umax > umin || throw(ArgumentError("umax must be greater than umin")) |
77 | | - |
| 77 | + |
78 | 78 | ar = Ts / Tt |
79 | 79 | ad = Td / (Td + N * Ts) |
80 | 80 | bd = K * N * ad |
81 | | - |
| 81 | + |
82 | 82 | T = promote_type(typeof.((K, Ti, Td, Tt, N, b, umin, umax, Ts, bi, ar, bd, ad, I, D, yold))...) |
83 | | - |
| 83 | + |
84 | 84 | DiscretePID(T.((K, Ti, Td, Tt, N, b, umin, umax, Ts, bi, ar, bd, ad, I, D, yold))...) |
85 | 85 | end |
86 | 86 |
|
@@ -128,15 +128,15 @@ end |
128 | 128 |
|
129 | 129 |
|
130 | 130 | """ |
131 | | - u = calculate_control!(pid::DiscretePID, r, y) |
132 | | - (pid)(r, y) # Alternative syntax |
| 131 | + u = calculate_control!(pid::DiscretePID, r, y, uff=0) |
| 132 | + (pid)(r, y, uff=0) # Alternative syntax |
133 | 133 |
|
134 | | -Calculate the control output from the PID controller when `r` is the reference (set point) and `y` is the latest measurement. |
| 134 | +Calculate the control output from the PID controller when `r` is the reference (set point), `y` is the latest measurement and `uff` is the feed-forward contribution. |
135 | 135 | """ |
136 | | -function calculate_control!(pid::DiscretePID, r, y) |
| 136 | +function calculate_control!(pid::DiscretePID, r, y, uff=0) |
137 | 137 | P = pid.K * (pid.b * r - y) |
138 | 138 | pid.D = pid.ad * pid.D - pid.bd * (y - pid.yold) |
139 | | - v = P + pid.I + pid.D |
| 139 | + v = P + pid.I + pid.D + uff |
140 | 140 | u = clamp(v, pid.umin, pid.umax) |
141 | 141 | pid.I = pid.I + pid.bi * (r - y) + pid.ar * (u - v) |
142 | 142 | pid.yold = y |
|
0 commit comments