Skip to content

Commit 9875e7f

Browse files
committed
refactor: Blocks/nonlinear.jl
1 parent 2505ad3 commit 9875e7f

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

src/Blocks/nonlinear.jl

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ _clamp(u, u_min, u_max) = max(min(u, u_max), u_min)
22
_dead_zone(u, u_min, u_max) = ifelse(u > u_max, u - u_max, ifelse(u < u_min, u - u_min, 0))
33

44
"""
5-
Limiter(;name, y_max, y_min=y_max > 0 ? -y_max : -Inf)
5+
Limiter(;name, y_max, y_min = y_max > 0 ? -y_max : -Inf)
66
77
Limit the range of a signal.
88
@@ -30,7 +30,7 @@ Limit the range of a signal.
3030
end
3131

3232
"""
33-
DeadZone(; name, u_max, u_min=-u_max)
33+
DeadZone(; name, u_max, u_min = -u_max)
3434
3535
The DeadZone block defines a region of zero output.
3636
If the input is within `u_min` ... `u_max`, the output is zero. Outside of this zone, the output is a linear function of the input with a slope of 1.
@@ -56,50 +56,57 @@ 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
"""
75-
SlewRateLimiter(;name, rising=1, falling=-rising, Td=0.001, y_start=0.0)
78+
SlewRateLimiter(; name, y_start, rising = 1.0, falling = -rising, Td = 0.001)
7679
7780
Limits the slew rate of a signal.
81+
Initial value of state `Y` can be set with `int.y`
7882
7983
# Parameters:
8084
8185
- `rising`: Maximum rising slew rate
8286
- `falling`: Maximum falling slew rate
8387
- `Td`: [s] Derivative time constant
88+
- `y_start`: Initial value of `y` state of SISO
8489
8590
# Connectors:
8691
8792
- `input`
8893
- `output`
8994
"""
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)
95+
@mtkmodel SlewRateLimiter begin
96+
@parameters begin
97+
rising = 1.0, [description = "Maximum rising slew rate of SlewRateLimiter"]
98+
falling = -rising, [description = "Derivative time constant of SlewRateLimiter"]
99+
Td = 0.001, [description = "Derivative time constant"]
100+
y_start
101+
end
102+
begin
103+
getdefault(rising) getdefault(falling) ||
104+
throw(ArgumentError("`rising` must be smaller than `falling`"))
105+
getdefault(Td) > 0 ||
106+
throw(ArgumentError("Time constant `Td` must be strictly positive"))
107+
end
108+
@extend u, y = siso = SISO(y_start = y_start)
109+
@equations begin
110+
D(y) ~ max(min((u - y) / Td, rising), falling)
111+
end
105112
end

0 commit comments

Comments
 (0)