Skip to content

Commit 3250589

Browse files
committed
refactor: Blocks/nonlinear.jl
1 parent 418188a commit 3250589

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

src/Blocks/nonlinear.jl

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,22 @@ If the input is within `u_min` ... `u_max`, the output is zero. Outside of this
5656
- `input`
5757
- `output`
5858
"""
59-
@component function DeadZone(; name, u_max, u_min = -u_max)
60-
if !ModelingToolkit.isvariable(u_max)
61-
u_max u_min || throw(ArgumentError("`u_min` must be smaller than `u_max`"))
59+
@mtkmodel DeadZone begin
60+
@parameters begin
61+
u_max, [description = "Upper limit of dead zone of DeadZone"]
62+
u_min = -u_max, [description = "Lower limit of dead zone of DeadZone"]
63+
end
64+
begin
65+
if !ModelingToolkit.isvariable(u_max)
66+
u_max u_min || throw(ArgumentError("`u_min` must be smaller than `u_max`"))
67+
end
68+
end
69+
70+
@extend u, y = siso = SISO()
71+
72+
@equations begin
73+
y ~ _dead_zone(u, u_min, u_max)
6274
end
63-
@named siso = SISO()
64-
@unpack u, y = siso
65-
pars = @parameters u_max=u_max [
66-
description = "Upper limit of dead zone of DeadZone $name",
67-
] u_min=u_min [description = "Lower limit of dead zone of DeadZone $name"]
68-
eqs = [
69-
y ~ _dead_zone(u, u_min, u_max),
70-
]
71-
extend(ODESystem(eqs, t, [], pars; name = name), siso)
7275
end
7376

7477
"""
@@ -87,19 +90,19 @@ Limits the slew rate of a signal.
8790
- `input`
8891
- `output`
8992
"""
90-
@component function SlewRateLimiter(; name, rising = 1, falling = -rising, Td = 0.001,
91-
y_start = 0.0)
92-
rising falling || throw(ArgumentError("`rising` must be smaller than `falling`"))
93-
Td > 0 || throw(ArgumentError("Time constant `Td` must be strictly positive"))
94-
@named siso = SISO(y_start = y_start)
95-
@unpack u, y = siso
96-
pars = @parameters rising=rising [
97-
description = "Maximum rising slew rate of SlewRateLimiter $name",
98-
] falling=falling [description = "Maximum falling slew rate of SlewRateLimiter $name"] Td=Td [
99-
description = "Derivative time constant of SlewRateLimiter $name",
100-
]
101-
eqs = [
102-
D(y) ~ max(min((u - y) / Td, rising), falling),
103-
]
104-
extend(ODESystem(eqs, t, [], pars; name = name), siso)
93+
@mtkmodel SlewRateLimiter begin
94+
95+
@parameters begin
96+
rising = 1, [description = "Maximum rising slew rate of SlewRateLimiter"]
97+
falling = -rising, [description = "Derivative time constant of SlewRateLimiter"]
98+
Td = 0.001
99+
end
100+
begin
101+
getdefault(rising) getdefault(falling) || throw(ArgumentError("`rising` must be smaller than `falling`"))
102+
getdefault(Td) > 0 || throw(ArgumentError("Time constant `Td` must be strictly positive"))
103+
end
104+
@extend u, y = siso = SISO(y = 0.0)
105+
@equations begin
106+
D(y) ~ max(min((u - y) / Td, rising), falling)
107+
end
105108
end

test/Blocks/nonlinear.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ end
100100

101101
@testset "SlewRateLimiter" begin
102102
@named source = Sine(; frequency = 1 / 2)
103-
@named rl = SlewRateLimiter(; rising = 1, falling = -1, Td = 0.001, y_start = -1 / 3)
103+
@named rl = SlewRateLimiter(; rising = 1, falling = -1, Td = 0.001, siso.y = -1 / 3)
104104
@named iosys = ODESystem([
105105
connect(source.output, rl.input),
106106
],

0 commit comments

Comments
 (0)