Skip to content

Commit 50ea44b

Browse files
committed
add parameters to PID controllers
1 parent 37b5157 commit 50ea44b

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

src/Blocks/continuous.jl

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,21 @@ See also [`LimPID`](@ref)
258258
with_D = !isequal(Td, false)
259259
@named err_input = RealInput() # control error
260260
@named ctr_output = RealOutput() # control signal
261-
!isequal(Ti, false) &&
262-
(Ti 0 || throw(ArgumentError("Ti out of bounds, got $(Ti) but expected Ti ≥ 0")))
263-
!isequal(Td, false) &&
264-
(Td 0 || throw(ArgumentError("Td out of bounds, got $(Td) but expected Td ≥ 0")))
265-
Nd > 0 || throw(ArgumentError("Nd out of bounds, got $(Nd) but expected Nd > 0"))
261+
@symcheck Ti 0 ||
262+
throw(ArgumentError("Ti out of bounds, got $(Ti) but expected Ti ≥ 0"))
263+
@symcheck Td 0 ||
264+
throw(ArgumentError("Td out of bounds, got $(Td) but expected Td ≥ 0"))
265+
@symcheck Nd > 0 ||
266+
throw(ArgumentError("Nd out of bounds, got $(Nd) but expected Nd > 0"))
267+
268+
pars = @parameters begin
269+
k = k, [description = "Proportional gain"]
270+
Ti = Ti, [description = "Integrator time constant"]
271+
Td = Td, [description = "Derivative time constant"]
272+
Nd = Nd, [description = "Derivative limit"]
273+
end
266274

267-
@named gainPID = Gain(k)
275+
@named gainPID = Gain(; k)
268276
@named addPID = Add3()
269277
if with_I
270278
@named int = Integrator(k = 1 / Ti, x = int__x)
@@ -304,7 +312,7 @@ See also [`LimPID`](@ref)
304312
else
305313
push!(eqs, connect(Dzero.output, addPID.input3))
306314
end
307-
ODESystem(eqs, t, [], []; name = name, systems = sys)
315+
ODESystem(eqs, t, [], pars; name = name, systems = sys)
308316
end
309317

310318
"""
@@ -334,15 +342,22 @@ The simplified expression above is given without the anti-windup protection.
334342
throw(ArgumentError("Time constant `Ta` has to be strictly positive"))
335343
@symcheck T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
336344
@symcheck u_max u_min || throw(ArgumentError("u_min must be smaller than u_max"))
345+
pars = @parameters begin
346+
k = k, [description = "Proportional gain"]
347+
T = T, [description = "Integrator time constant"]
348+
Ta = Ta, [description = "Tracking time constant"]
349+
u_max = u_max, [description = "Upper saturation limit"]
350+
u_min = u_min, [description = "Lower saturation limit"]
351+
end
337352
@named err_input = RealInput() # control error
338353
@named ctr_output = RealOutput() # control signal
339-
@named gainPI = Gain(k)
354+
@named gainPI = Gain(; k)
340355
@named addPI = Add()
341356
@named addTrack = Add()
342357
@named int = Integrator(k = 1 / T, x = int__x)
343358
@named limiter = Limiter(y_max = u_max, y_min = u_min)
344359
@named addSat = Add(k1 = 1, k2 = -1)
345-
@named gainTrack = Gain(1 / Ta)
360+
@named gainTrack = Gain(k = 1 / Ta)
346361
sys = [err_input, ctr_output, gainPI, addPI, int, addTrack, limiter, addSat, gainTrack]
347362
eqs = [
348363
connect(err_input, addPI.input1),
@@ -357,7 +372,7 @@ The simplified expression above is given without the anti-windup protection.
357372
connect(addTrack.output, int.input),
358373
connect(int.output, addPI.input2)
359374
]
360-
ODESystem(eqs, t, [], []; name = name, systems = sys)
375+
ODESystem(eqs, t, [], pars; name = name, systems = sys)
361376
end
362377

363378
"""
@@ -408,18 +423,25 @@ where the transfer function for the derivative includes additional filtering, se
408423
Ti = k / Ti
409424
Td = Td / k
410425
end
411-
0 wp 1 ||
412-
throw(ArgumentError("wp out of bounds, got $(wp) but expected wp ∈ [0, 1]"))
413-
0 wd 1 ||
414-
throw(ArgumentError("wd out of bounds, got $(wd) but expected wd ∈ [0, 1]"))
415-
!isequal(Ti, false) &&
416-
(Ti 0 || throw(ArgumentError("Ti out of bounds, got $(Ti) but expected Ti ≥ 0")))
417-
!isequal(Td, false) &&
418-
(Td 0 || throw(ArgumentError("Td out of bounds, got $(Td) but expected Td ≥ 0")))
426+
@symcheck Ti 0 ||
427+
throw(ArgumentError("Ti out of bounds, got $(Ti) but expected Ti ≥ 0"))
428+
@symcheck Td 0 ||
429+
throw(ArgumentError("Td out of bounds, got $(Td) but expected Td ≥ 0"))
419430
@symcheck u_max u_min || throw(ArgumentError("u_min must be smaller than u_max"))
420431
@symcheck Nd > 0 ||
421432
throw(ArgumentError("Nd out of bounds, got $(Nd) but expected Nd > 0"))
422433

434+
pars = @parameters begin
435+
k = k, [description = "Proportional gain"]
436+
Ti = Ti, [description = "Integrator time constant"]
437+
Td = Td, [description = "Derivative time constant"]
438+
wp = wp, [description = "Set-point weighting in the proportional part"]
439+
wd = wd, [description = "Set-point weighting in the derivative part"]
440+
Ni = Ni, [description = "Anti-windup tracking gain"]
441+
Nd = Nd, [description = "Derivative limit"]
442+
u_max = u_max, [description = "Upper saturation limit"]
443+
u_min = u_min, [description = "Lower saturation limit"]
444+
end
423445
@named reference = RealInput()
424446
@named measurement = RealInput()
425447
@named ctr_output = RealOutput() # control signal
@@ -431,7 +453,7 @@ where the transfer function for the derivative includes additional filtering, se
431453
if with_AWM
432454
@named addI = Add3(k1 = 1, k2 = -1, k3 = 1)
433455
@named addSat = Add(k1 = 1, k2 = -1)
434-
@named gainTrack = Gain(1 / (k * Ni))
456+
@named gainTrack = Gain(k = 1 / (k * Ni))
435457
else
436458
@named addI = Add(k1 = 1, k2 = -1)
437459
end
@@ -492,7 +514,7 @@ where the transfer function for the derivative includes additional filtering, se
492514
push!(eqs, connect(Dzero.output, addPID.input2))
493515
end
494516

495-
ODESystem(eqs, t, [], []; name = name, systems = sys)
517+
ODESystem(eqs, t, [], pars; name = name, systems = sys)
496518
end
497519

498520
"""

0 commit comments

Comments
 (0)