Skip to content

Redefine models with @component #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[compat]
IfElse = "0.1"
ModelingToolkit = "8.26"
ModelingToolkit = "8.48"
Symbolics = "4.9, 5"
julia = "1.6"

Expand Down
35 changes: 18 additions & 17 deletions src/Blocks/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Outputs `y = ∫k*u dt`, corresponding to the transfer function `1/s`.
- `k`: Gain of integrator
- `x_start`: Initial value of integrator
"""
function Integrator(; name, k = 1, x_start = 0.0)
@component function Integrator(; name, k = 1, x_start = 0.0)
@named siso = SISO()
@unpack u, y = siso
sts = @variables x(t)=x_start [description = "State of Integrator $name"]
Expand Down Expand Up @@ -51,7 +51,7 @@ A smaller `T` leads to a more ideal approximation of the derivative.
- `input`
- `output`
"""
function Derivative(; name, k = 1, T, x_start = 0.0)
@component function Derivative(; name, k = 1, T, x_start = 0.0)
T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
@named siso = SISO()
@unpack u, y = siso
Expand Down Expand Up @@ -97,7 +97,7 @@ sT + 1 - k

See also [`SecondOrder`](@ref)
"""
function FirstOrder(; name, k = 1, T, x_start = 0.0, lowpass = true)
@component function FirstOrder(; name, k = 1, T, x_start = 0.0, lowpass = true)
T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
@named siso = SISO()
@unpack u, y = siso
Expand Down Expand Up @@ -138,7 +138,7 @@ Critical damping corresponds to `d=1`, which yields the fastest step response wi
- `input`
- `output`
"""
function SecondOrder(; name, k = 1, w, d, x_start = 0.0, xd_start = 0.0)
@component function SecondOrder(; name, k = 1, w, d, x_start = 0.0, xd_start = 0.0)
@named siso = SISO()
@unpack u, y = siso
@variables x(t)=x_start [description = "State of SecondOrder filter $name"]
Expand Down Expand Up @@ -170,7 +170,7 @@ Textbook version of a PI-controller without actuator saturation and anti-windup

See also [`LimPI`](@ref)
"""
function PI(; name, k = 1, T, x_start = 0.0)
@component function PI(; name, k = 1, T, x_start = 0.0)
T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
@named err_input = RealInput() # control error
@named ctr_output = RealOutput() # control signal
Expand Down Expand Up @@ -209,7 +209,8 @@ Text-book version of a PID-controller without actuator saturation and anti-windu

See also [`LimPID`](@ref)
"""
function PID(; name, k = 1, Ti = false, Td = false, Nd = 10, xi_start = 0, xd_start = 0)
@component function PID(; name, k = 1, Ti = false, Td = false, Nd = 10, xi_start = 0,
xd_start = 0)
with_I = !isequal(Ti, false)
with_D = !isequal(Td, false)
@named err_input = RealInput() # control error
Expand Down Expand Up @@ -280,7 +281,7 @@ Text-book version of a PI-controller with actuator saturation and anti-windup me
- `err_input`
- `ctr_output`
"""
function LimPI(; name, k = 1, T, u_max, u_min = -u_max, Ta, x_start = 0.0)
@component function LimPI(; name, k = 1, T, u_max, u_min = -u_max, Ta, x_start = 0.0)
Ta > 0 || throw(ArgumentError("Time constant `Ta` has to be strictly positive"))
T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
u_max ≥ u_min || throw(ArgumentError("u_min must be smaller than u_max"))
Expand Down Expand Up @@ -343,14 +344,14 @@ where the transfer function for the derivative includes additional filtering, se
- `measurement`
- `ctr_output`
"""
function LimPID(; name, k = 1, Ti = false, Td = false, wp = 1, wd = 1,
Ni = Ti == 0 ? Inf : √(max(Td / Ti, 1e-6)),
Nd = 10,
u_max = Inf,
u_min = u_max > 0 ? -u_max : -Inf,
gains = false,
xi_start = 0.0,
xd_start = 0.0)
@component function LimPID(; name, k = 1, Ti = false, Td = false, wp = 1, wd = 1,
Ni = Ti == 0 ? Inf : √(max(Td / Ti, 1e-6)),
Nd = 10,
u_max = Inf,
u_min = u_max > 0 ? -u_max : -Inf,
gains = false,
xi_start = 0.0,
xd_start = 0.0)
with_I = !isequal(Ti, false)
with_D = !isequal(Td, false)
with_AWM = Ni != Inf
Expand Down Expand Up @@ -478,8 +479,8 @@ y &= h(x, u)

linearized around the operating point `x₀, u₀`, we have `y0, u0 = h(x₀, u₀), u₀`.
"""
function StateSpace(; A, B, C, D = nothing, x_start = zeros(size(A, 1)), name,
u0 = zeros(size(B, 2)), y0 = zeros(size(C, 1)))
@component function StateSpace(; A, B, C, D = nothing, x_start = zeros(size(A, 1)), name,
u0 = zeros(size(B, 2)), y0 = zeros(size(C, 1)))
nx, nu, ny = size(A, 1), size(B, 2), size(C, 1)
size(A, 2) == nx || error("`A` has to be a square matrix.")
size(B, 1) == nx || error("`B` has to be of dimension ($nx x $nu).")
Expand Down
50 changes: 25 additions & 25 deletions src/Blocks/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Output the product of a gain value with the input signal.
- `input`
- `output`
"""
function Gain(k; name)
@component function Gain(k; name)
@named siso = SISO()
@unpack u, y = siso
pars = @parameters k=k [description = "Gain of Gain $name"]
Expand All @@ -36,7 +36,7 @@ Output the product of a gain matrix with the input signal vector.
- `input`
- `output`
"""
function MatrixGain(K::AbstractArray; name)
@component function MatrixGain(K::AbstractArray; name)
nout, nin = size(K, 1), size(K, 2)
@named input = RealInput(; nin = nin)
@named output = RealOutput(; nout = nout)
Expand All @@ -58,7 +58,7 @@ Output the sum of the elements of the input port vector.
- `input`
- `output`
"""
function Sum(n::Int; name)
@component function Sum(n::Int; name)
@named input = RealInput(; nin = n)
@named output = RealOutput()
eqs = [
Expand All @@ -78,7 +78,7 @@ Output difference between reference input (input1) and feedback input (input2).
- `input2`
- `output`
"""
function Feedback(; name)
@component function Feedback(; name)
@named input1 = RealInput()
@named input2 = RealInput()
@named output = RealOutput()
Expand All @@ -104,7 +104,7 @@ Output the sum of the two scalar inputs.
- `input2`
- `output`
"""
function Add(; name, k1 = 1, k2 = 1)
@component function Add(; name, k1 = 1, k2 = 1)
@named input1 = RealInput()
@named input2 = RealInput()
@named output = RealOutput()
Expand Down Expand Up @@ -134,7 +134,7 @@ Output the sum of the three scalar inputs.
- `input3`
- `output`
"""
function Add3(; name, k1 = 1, k2 = 1, k3 = 1)
@component function Add3(; name, k1 = 1, k2 = 1, k3 = 1)
@named input1 = RealInput()
@named input2 = RealInput()
@named input3 = RealInput()
Expand All @@ -159,7 +159,7 @@ Output product of the two inputs.
- `input2`
- `output`
"""
function Product(; name)
@component function Product(; name)
@named input1 = RealInput()
@named input2 = RealInput()
@named output = RealOutput()
Expand All @@ -180,7 +180,7 @@ Output first input divided by second input.
- `input2`
- `output`
"""
function Division(; name)
@component function Division(; name)
@named input1 = RealInput()
@named input2 = RealInput(u_start = 1.0) # denominator can not be zero
@named output = RealOutput()
Expand All @@ -202,7 +202,7 @@ If the given function is not composed of simple core methods (e.g. sin, abs, ...
- `input`
- `output`
"""
function StaticNonLinearity(func; name)
@component function StaticNonLinearity(func; name)
@named siso = SISO()
@unpack u, y = siso
eqs = [y ~ func(u)]
Expand All @@ -218,7 +218,7 @@ Output the absolute value of the input.

See [`StaticNonLinearity`](@ref)
"""
Abs(; name) = StaticNonLinearity(abs; name)
@component Abs(; name) = StaticNonLinearity(abs; name)

"""
Sign(;name)
Expand All @@ -229,7 +229,7 @@ Output the sign of the input

See [`StaticNonLinearity`](@ref)
"""
Sign(; name) = StaticNonLinearity(sign; name)
@component Sign(; name) = StaticNonLinearity(sign; name)

"""
Sqrt(;name)
Expand All @@ -240,7 +240,7 @@ Output the square root of the input (input >= 0 required).

See [`StaticNonLinearity`](@ref)
"""
Sqrt(; name) = StaticNonLinearity(sqrt; name)
@component Sqrt(; name) = StaticNonLinearity(sqrt; name)

"""
Sin(;name)
Expand All @@ -251,7 +251,7 @@ Output the sine of the input.

See [`StaticNonLinearity`](@ref)
"""
Sin(; name) = StaticNonLinearity(sin; name)
@component Sin(; name) = StaticNonLinearity(sin; name)

"""
Cos(;name)
Expand All @@ -262,7 +262,7 @@ Output the cosine of the input.

See [`StaticNonLinearity`](@ref)
"""
Cos(; name) = StaticNonLinearity(cos; name)
@component Cos(; name) = StaticNonLinearity(cos; name)

"""
Tan(;name)
Expand All @@ -273,7 +273,7 @@ Output the tangent of the input.

See [`StaticNonLinearity`](@ref)
"""
Tan(; name) = StaticNonLinearity(tan; name)
@component Tan(; name) = StaticNonLinearity(tan; name)

"""
Asin(;name)
Expand All @@ -284,7 +284,7 @@ Output the arc sine of the input.

See [`StaticNonLinearity`](@ref)
"""
Asin(; name) = StaticNonLinearity(asin; name)
@component Asin(; name) = StaticNonLinearity(asin; name)

"""
Acos(;name)
Expand All @@ -295,7 +295,7 @@ Output the arc cosine of the input.

See [`StaticNonLinearity`](@ref)
"""
Acos(; name) = StaticNonLinearity(acos; name)
@component Acos(; name) = StaticNonLinearity(acos; name)

"""
Atan(;name)
Expand All @@ -306,7 +306,7 @@ Output the arc tangent of the input.

See [`StaticNonLinearity`](@ref)
"""
Atan(; name) = StaticNonLinearity(atan; name)
@component Atan(; name) = StaticNonLinearity(atan; name)

"""
Atan2(;name)
Expand All @@ -319,7 +319,7 @@ Output the arc tangent of the input.
- `input2`
- `output`
"""
function Atan2(; name)
@component function Atan2(; name)
@named input1 = RealInput()
@named input2 = RealInput()
@named output = RealOutput()
Expand All @@ -338,7 +338,7 @@ Output the hyperbolic sine of the input.

See [`StaticNonLinearity`](@ref)
"""
Sinh(; name) = StaticNonLinearity(sinh; name)
@component Sinh(; name) = StaticNonLinearity(sinh; name)

"""
Cosh(;name)
Expand All @@ -349,7 +349,7 @@ Output the hyperbolic cosine of the input.

See [`StaticNonLinearity`](@ref)
"""
Cosh(; name) = StaticNonLinearity(cosh; name)
@component Cosh(; name) = StaticNonLinearity(cosh; name)

"""
Tanh(;name)
Expand All @@ -360,7 +360,7 @@ Output the hyperbolic tangent of the input.

See [`StaticNonLinearity`](@ref)
"""
Tanh(; name) = StaticNonLinearity(tanh; name)
@component Tanh(; name) = StaticNonLinearity(tanh; name)

"""
Exp(;name)
Expand All @@ -371,7 +371,7 @@ Output the exponential (base e) of the input.

See [`StaticNonLinearity`](@ref)
"""
Exp(; name) = StaticNonLinearity(exp; name)
@component Exp(; name) = StaticNonLinearity(exp; name)

"""
Log(;name)
Expand All @@ -382,7 +382,7 @@ Output the natural (base e) logarithm of the input.

See [`StaticNonLinearity`](@ref)
"""
Log(; name) = StaticNonLinearity(log; name)
@component Log(; name) = StaticNonLinearity(log; name)

"""
Log10(;name)
Expand All @@ -393,4 +393,4 @@ Output the base 10 logarithm of the input.

See [`StaticNonLinearity`](@ref)
"""
Log10(; name) = StaticNonLinearity(log10; name)
@component Log10(; name) = StaticNonLinearity(log10; name)
7 changes: 4 additions & 3 deletions src/Blocks/nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Limit the range of a signal.
- `input`
- `output`
"""
function Limiter(; name, y_max, y_min = y_max > 0 ? -y_max : -Inf)
@component function Limiter(; name, y_max, y_min = y_max > 0 ? -y_max : -Inf)
y_max ≥ y_min || throw(ArgumentError("`y_min` must be smaller than `y_max`"))
@named siso = SISO()
@unpack u, y = siso
Expand Down Expand Up @@ -56,7 +56,7 @@ If the input is within `u_min` ... `u_max`, the output is zero. Outside of this
- `input`
- `output`
"""
function DeadZone(; name, u_max, u_min = -u_max)
@component function DeadZone(; name, u_max, u_min = -u_max)
if !ModelingToolkit.isvariable(u_max)
u_max ≥ u_min || throw(ArgumentError("`u_min` must be smaller than `u_max`"))
end
Expand Down Expand Up @@ -87,7 +87,8 @@ Limits the slew rate of a signal.
- `input`
- `output`
"""
function SlewRateLimiter(; name, rising = 1, falling = -rising, Td = 0.001, y_start = 0.0)
@component function SlewRateLimiter(; name, rising = 1, falling = -rising, Td = 0.001,
y_start = 0.0)
rising ≥ falling || throw(ArgumentError("`rising` must be smaller than `falling`"))
Td > 0 || throw(ArgumentError("Time constant `Td` must be strictly positive"))
@named siso = SISO(y_start = y_start)
Expand Down
Loading