Skip to content

Commit 894bdc4

Browse files
authored
Redefine models with @component (#153)
* chore(deps): bump MTK to 8.48 * refactor: redefine all components with `@component`
1 parent bbd25f9 commit 894bdc4

File tree

33 files changed

+200
-191
lines changed

33 files changed

+200
-191
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1111

1212
[compat]
1313
IfElse = "0.1"
14-
ModelingToolkit = "8.26"
14+
ModelingToolkit = "8.48"
1515
Symbolics = "4.9, 5"
1616
julia = "1.6"
1717

src/Blocks/continuous.jl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Outputs `y = ∫k*u dt`, corresponding to the transfer function `1/s`.
1313
- `k`: Gain of integrator
1414
- `x_start`: Initial value of integrator
1515
"""
16-
function Integrator(; name, k = 1, x_start = 0.0)
16+
@component function Integrator(; name, k = 1, x_start = 0.0)
1717
@named siso = SISO()
1818
@unpack u, y = siso
1919
sts = @variables x(t)=x_start [description = "State of Integrator $name"]
@@ -51,7 +51,7 @@ A smaller `T` leads to a more ideal approximation of the derivative.
5151
- `input`
5252
- `output`
5353
"""
54-
function Derivative(; name, k = 1, T, x_start = 0.0)
54+
@component function Derivative(; name, k = 1, T, x_start = 0.0)
5555
T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
5656
@named siso = SISO()
5757
@unpack u, y = siso
@@ -97,7 +97,7 @@ sT + 1 - k
9797
9898
See also [`SecondOrder`](@ref)
9999
"""
100-
function FirstOrder(; name, k = 1, T, x_start = 0.0, lowpass = true)
100+
@component function FirstOrder(; name, k = 1, T, x_start = 0.0, lowpass = true)
101101
T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
102102
@named siso = SISO()
103103
@unpack u, y = siso
@@ -138,7 +138,7 @@ Critical damping corresponds to `d=1`, which yields the fastest step response wi
138138
- `input`
139139
- `output`
140140
"""
141-
function SecondOrder(; name, k = 1, w, d, x_start = 0.0, xd_start = 0.0)
141+
@component function SecondOrder(; name, k = 1, w, d, x_start = 0.0, xd_start = 0.0)
142142
@named siso = SISO()
143143
@unpack u, y = siso
144144
@variables x(t)=x_start [description = "State of SecondOrder filter $name"]
@@ -170,7 +170,7 @@ Textbook version of a PI-controller without actuator saturation and anti-windup
170170
171171
See also [`LimPI`](@ref)
172172
"""
173-
function PI(; name, k = 1, T, x_start = 0.0)
173+
@component function PI(; name, k = 1, T, x_start = 0.0)
174174
T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
175175
@named err_input = RealInput() # control error
176176
@named ctr_output = RealOutput() # control signal
@@ -209,7 +209,8 @@ Text-book version of a PID-controller without actuator saturation and anti-windu
209209
210210
See also [`LimPID`](@ref)
211211
"""
212-
function PID(; name, k = 1, Ti = false, Td = false, Nd = 10, xi_start = 0, xd_start = 0)
212+
@component function PID(; name, k = 1, Ti = false, Td = false, Nd = 10, xi_start = 0,
213+
xd_start = 0)
213214
with_I = !isequal(Ti, false)
214215
with_D = !isequal(Td, false)
215216
@named err_input = RealInput() # control error
@@ -280,7 +281,7 @@ Text-book version of a PI-controller with actuator saturation and anti-windup me
280281
- `err_input`
281282
- `ctr_output`
282283
"""
283-
function LimPI(; name, k = 1, T, u_max, u_min = -u_max, Ta, x_start = 0.0)
284+
@component function LimPI(; name, k = 1, T, u_max, u_min = -u_max, Ta, x_start = 0.0)
284285
Ta > 0 || throw(ArgumentError("Time constant `Ta` has to be strictly positive"))
285286
T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
286287
u_max u_min || throw(ArgumentError("u_min must be smaller than u_max"))
@@ -343,14 +344,14 @@ where the transfer function for the derivative includes additional filtering, se
343344
- `measurement`
344345
- `ctr_output`
345346
"""
346-
function LimPID(; name, k = 1, Ti = false, Td = false, wp = 1, wd = 1,
347-
Ni = Ti == 0 ? Inf : (max(Td / Ti, 1e-6)),
348-
Nd = 10,
349-
u_max = Inf,
350-
u_min = u_max > 0 ? -u_max : -Inf,
351-
gains = false,
352-
xi_start = 0.0,
353-
xd_start = 0.0)
347+
@component function LimPID(; name, k = 1, Ti = false, Td = false, wp = 1, wd = 1,
348+
Ni = Ti == 0 ? Inf : (max(Td / Ti, 1e-6)),
349+
Nd = 10,
350+
u_max = Inf,
351+
u_min = u_max > 0 ? -u_max : -Inf,
352+
gains = false,
353+
xi_start = 0.0,
354+
xd_start = 0.0)
354355
with_I = !isequal(Ti, false)
355356
with_D = !isequal(Td, false)
356357
with_AWM = Ni != Inf
@@ -478,8 +479,8 @@ y &= h(x, u)
478479
479480
linearized around the operating point `x₀, u₀`, we have `y0, u0 = h(x₀, u₀), u₀`.
480481
"""
481-
function StateSpace(; A, B, C, D = nothing, x_start = zeros(size(A, 1)), name,
482-
u0 = zeros(size(B, 2)), y0 = zeros(size(C, 1)))
482+
@component function StateSpace(; A, B, C, D = nothing, x_start = zeros(size(A, 1)), name,
483+
u0 = zeros(size(B, 2)), y0 = zeros(size(C, 1)))
483484
nx, nu, ny = size(A, 1), size(B, 2), size(C, 1)
484485
size(A, 2) == nx || error("`A` has to be a square matrix.")
485486
size(B, 1) == nx || error("`B` has to be of dimension ($nx x $nu).")

src/Blocks/math.jl

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Output the product of a gain value with the input signal.
1212
- `input`
1313
- `output`
1414
"""
15-
function Gain(k; name)
15+
@component function Gain(k; name)
1616
@named siso = SISO()
1717
@unpack u, y = siso
1818
pars = @parameters k=k [description = "Gain of Gain $name"]
@@ -36,7 +36,7 @@ Output the product of a gain matrix with the input signal vector.
3636
- `input`
3737
- `output`
3838
"""
39-
function MatrixGain(K::AbstractArray; name)
39+
@component function MatrixGain(K::AbstractArray; name)
4040
nout, nin = size(K, 1), size(K, 2)
4141
@named input = RealInput(; nin = nin)
4242
@named output = RealOutput(; nout = nout)
@@ -58,7 +58,7 @@ Output the sum of the elements of the input port vector.
5858
- `input`
5959
- `output`
6060
"""
61-
function Sum(n::Int; name)
61+
@component function Sum(n::Int; name)
6262
@named input = RealInput(; nin = n)
6363
@named output = RealOutput()
6464
eqs = [
@@ -78,7 +78,7 @@ Output difference between reference input (input1) and feedback input (input2).
7878
- `input2`
7979
- `output`
8080
"""
81-
function Feedback(; name)
81+
@component function Feedback(; name)
8282
@named input1 = RealInput()
8383
@named input2 = RealInput()
8484
@named output = RealOutput()
@@ -104,7 +104,7 @@ Output the sum of the two scalar inputs.
104104
- `input2`
105105
- `output`
106106
"""
107-
function Add(; name, k1 = 1, k2 = 1)
107+
@component function Add(; name, k1 = 1, k2 = 1)
108108
@named input1 = RealInput()
109109
@named input2 = RealInput()
110110
@named output = RealOutput()
@@ -134,7 +134,7 @@ Output the sum of the three scalar inputs.
134134
- `input3`
135135
- `output`
136136
"""
137-
function Add3(; name, k1 = 1, k2 = 1, k3 = 1)
137+
@component function Add3(; name, k1 = 1, k2 = 1, k3 = 1)
138138
@named input1 = RealInput()
139139
@named input2 = RealInput()
140140
@named input3 = RealInput()
@@ -159,7 +159,7 @@ Output product of the two inputs.
159159
- `input2`
160160
- `output`
161161
"""
162-
function Product(; name)
162+
@component function Product(; name)
163163
@named input1 = RealInput()
164164
@named input2 = RealInput()
165165
@named output = RealOutput()
@@ -180,7 +180,7 @@ Output first input divided by second input.
180180
- `input2`
181181
- `output`
182182
"""
183-
function Division(; name)
183+
@component function Division(; name)
184184
@named input1 = RealInput()
185185
@named input2 = RealInput(u_start = 1.0) # denominator can not be zero
186186
@named output = RealOutput()
@@ -202,7 +202,7 @@ If the given function is not composed of simple core methods (e.g. sin, abs, ...
202202
- `input`
203203
- `output`
204204
"""
205-
function StaticNonLinearity(func; name)
205+
@component function StaticNonLinearity(func; name)
206206
@named siso = SISO()
207207
@unpack u, y = siso
208208
eqs = [y ~ func(u)]
@@ -218,7 +218,7 @@ Output the absolute value of the input.
218218
219219
See [`StaticNonLinearity`](@ref)
220220
"""
221-
Abs(; name) = StaticNonLinearity(abs; name)
221+
@component Abs(; name) = StaticNonLinearity(abs; name)
222222

223223
"""
224224
Sign(;name)
@@ -229,7 +229,7 @@ Output the sign of the input
229229
230230
See [`StaticNonLinearity`](@ref)
231231
"""
232-
Sign(; name) = StaticNonLinearity(sign; name)
232+
@component Sign(; name) = StaticNonLinearity(sign; name)
233233

234234
"""
235235
Sqrt(;name)
@@ -240,7 +240,7 @@ Output the square root of the input (input >= 0 required).
240240
241241
See [`StaticNonLinearity`](@ref)
242242
"""
243-
Sqrt(; name) = StaticNonLinearity(sqrt; name)
243+
@component Sqrt(; name) = StaticNonLinearity(sqrt; name)
244244

245245
"""
246246
Sin(;name)
@@ -251,7 +251,7 @@ Output the sine of the input.
251251
252252
See [`StaticNonLinearity`](@ref)
253253
"""
254-
Sin(; name) = StaticNonLinearity(sin; name)
254+
@component Sin(; name) = StaticNonLinearity(sin; name)
255255

256256
"""
257257
Cos(;name)
@@ -262,7 +262,7 @@ Output the cosine of the input.
262262
263263
See [`StaticNonLinearity`](@ref)
264264
"""
265-
Cos(; name) = StaticNonLinearity(cos; name)
265+
@component Cos(; name) = StaticNonLinearity(cos; name)
266266

267267
"""
268268
Tan(;name)
@@ -273,7 +273,7 @@ Output the tangent of the input.
273273
274274
See [`StaticNonLinearity`](@ref)
275275
"""
276-
Tan(; name) = StaticNonLinearity(tan; name)
276+
@component Tan(; name) = StaticNonLinearity(tan; name)
277277

278278
"""
279279
Asin(;name)
@@ -284,7 +284,7 @@ Output the arc sine of the input.
284284
285285
See [`StaticNonLinearity`](@ref)
286286
"""
287-
Asin(; name) = StaticNonLinearity(asin; name)
287+
@component Asin(; name) = StaticNonLinearity(asin; name)
288288

289289
"""
290290
Acos(;name)
@@ -295,7 +295,7 @@ Output the arc cosine of the input.
295295
296296
See [`StaticNonLinearity`](@ref)
297297
"""
298-
Acos(; name) = StaticNonLinearity(acos; name)
298+
@component Acos(; name) = StaticNonLinearity(acos; name)
299299

300300
"""
301301
Atan(;name)
@@ -306,7 +306,7 @@ Output the arc tangent of the input.
306306
307307
See [`StaticNonLinearity`](@ref)
308308
"""
309-
Atan(; name) = StaticNonLinearity(atan; name)
309+
@component Atan(; name) = StaticNonLinearity(atan; name)
310310

311311
"""
312312
Atan2(;name)
@@ -319,7 +319,7 @@ Output the arc tangent of the input.
319319
- `input2`
320320
- `output`
321321
"""
322-
function Atan2(; name)
322+
@component function Atan2(; name)
323323
@named input1 = RealInput()
324324
@named input2 = RealInput()
325325
@named output = RealOutput()
@@ -338,7 +338,7 @@ Output the hyperbolic sine of the input.
338338
339339
See [`StaticNonLinearity`](@ref)
340340
"""
341-
Sinh(; name) = StaticNonLinearity(sinh; name)
341+
@component Sinh(; name) = StaticNonLinearity(sinh; name)
342342

343343
"""
344344
Cosh(;name)
@@ -349,7 +349,7 @@ Output the hyperbolic cosine of the input.
349349
350350
See [`StaticNonLinearity`](@ref)
351351
"""
352-
Cosh(; name) = StaticNonLinearity(cosh; name)
352+
@component Cosh(; name) = StaticNonLinearity(cosh; name)
353353

354354
"""
355355
Tanh(;name)
@@ -360,7 +360,7 @@ Output the hyperbolic tangent of the input.
360360
361361
See [`StaticNonLinearity`](@ref)
362362
"""
363-
Tanh(; name) = StaticNonLinearity(tanh; name)
363+
@component Tanh(; name) = StaticNonLinearity(tanh; name)
364364

365365
"""
366366
Exp(;name)
@@ -371,7 +371,7 @@ Output the exponential (base e) of the input.
371371
372372
See [`StaticNonLinearity`](@ref)
373373
"""
374-
Exp(; name) = StaticNonLinearity(exp; name)
374+
@component Exp(; name) = StaticNonLinearity(exp; name)
375375

376376
"""
377377
Log(;name)
@@ -382,7 +382,7 @@ Output the natural (base e) logarithm of the input.
382382
383383
See [`StaticNonLinearity`](@ref)
384384
"""
385-
Log(; name) = StaticNonLinearity(log; name)
385+
@component Log(; name) = StaticNonLinearity(log; name)
386386

387387
"""
388388
Log10(;name)
@@ -393,4 +393,4 @@ Output the base 10 logarithm of the input.
393393
394394
See [`StaticNonLinearity`](@ref)
395395
"""
396-
Log10(; name) = StaticNonLinearity(log10; name)
396+
@component Log10(; name) = StaticNonLinearity(log10; name)

src/Blocks/nonlinear.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Limit the range of a signal.
1616
- `input`
1717
- `output`
1818
"""
19-
function Limiter(; name, y_max, y_min = y_max > 0 ? -y_max : -Inf)
19+
@component function Limiter(; name, y_max, y_min = y_max > 0 ? -y_max : -Inf)
2020
y_max y_min || throw(ArgumentError("`y_min` must be smaller than `y_max`"))
2121
@named siso = SISO()
2222
@unpack u, y = siso
@@ -56,7 +56,7 @@ If the input is within `u_min` ... `u_max`, the output is zero. Outside of this
5656
- `input`
5757
- `output`
5858
"""
59-
function DeadZone(; name, u_max, u_min = -u_max)
59+
@component function DeadZone(; name, u_max, u_min = -u_max)
6060
if !ModelingToolkit.isvariable(u_max)
6161
u_max u_min || throw(ArgumentError("`u_min` must be smaller than `u_max`"))
6262
end
@@ -87,7 +87,8 @@ Limits the slew rate of a signal.
8787
- `input`
8888
- `output`
8989
"""
90-
function SlewRateLimiter(; name, rising = 1, falling = -rising, Td = 0.001, y_start = 0.0)
90+
@component function SlewRateLimiter(; name, rising = 1, falling = -rising, Td = 0.001,
91+
y_start = 0.0)
9192
rising falling || throw(ArgumentError("`rising` must be smaller than `falling`"))
9293
Td > 0 || throw(ArgumentError("Time constant `Td` must be strictly positive"))
9394
@named siso = SISO(y_start = y_start)

0 commit comments

Comments
 (0)