@@ -2,7 +2,7 @@ _clamp(u, u_min, u_max) = max(min(u, u_max), u_min)
2
2
_dead_zone (u, u_min, u_max) = ifelse (u > u_max, u - u_max, ifelse (u < u_min, u - u_min, 0 ))
3
3
4
4
"""
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)
6
6
7
7
Limit the range of a signal.
8
8
@@ -30,7 +30,7 @@ Limit the range of a signal.
30
30
end
31
31
32
32
"""
33
- DeadZone(; name, u_max, u_min= -u_max)
33
+ DeadZone(; name, u_max, u_min = -u_max)
34
34
35
35
The DeadZone block defines a region of zero output.
36
36
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
56
56
- `input`
57
57
- `output`
58
58
"""
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)
62
74
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)
72
75
end
73
76
74
77
"""
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 )
76
79
77
80
Limits the slew rate of a signal.
81
+ Initial value of state `Y` can be set with `int.y`
78
82
79
83
# Parameters:
80
84
81
85
- `rising`: Maximum rising slew rate
82
86
- `falling`: Maximum falling slew rate
83
87
- `Td`: [s] Derivative time constant
88
+ - `y_start`: Initial value of `y` state of SISO
84
89
85
90
# Connectors:
86
91
87
92
- `input`
88
93
- `output`
89
94
"""
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
105
112
end
0 commit comments