@@ -34,7 +34,7 @@ Initial value of integrator state ``x`` can be set with `x`
3434
3535 equations = Equation[
3636 D(x) ~ k * u,
37- y ~ x
37+ y ~ x,
3838 ]
3939
4040 sys = System(equations, t, vars, pars; name, systems)
@@ -74,7 +74,7 @@ Initial value of the state ``x`` can be set with `x`.
7474"""
7575@component function Derivative(; name, k = 1 , T = nothing , x = 0.0 )
7676 @symcheck T > 0 ||
77- throw(ArgumentError(" Time constant `T` has to be strictly positive" ))
77+ throw(ArgumentError(" Time constant `T` has to be strictly positive" ))
7878
7979 @named siso = SISO()
8080 @unpack u, y = siso
@@ -93,7 +93,7 @@ Initial value of the state ``x`` can be set with `x`.
9393
9494 equations = Equation[
9595 D(x) ~ (u - x) / T,
96- y ~ (k / T) * (u - x)
96+ y ~ (k / T) * (u - x),
9797 ]
9898
9999 sys = System(equations, t, vars, pars; name, systems)
@@ -137,7 +137,7 @@ See also [`SecondOrder`](@ref)
137137"""
138138@component function FirstOrder(; name, lowpass = true , T = nothing , k = 1.0 , x = 0.0 )
139139 @symcheck T > 0 ||
140- throw(ArgumentError(" Time constant `T` has to be strictly positive" ))
140+ throw(ArgumentError(" Time constant `T` has to be strictly positive" ))
141141
142142 @named siso = SISO()
143143 @unpack u, y = siso
@@ -156,7 +156,7 @@ See also [`SecondOrder`](@ref)
156156
157157 equations = Equation[
158158 D(x) ~ (k * u - x) / T,
159- lowpass ? (y ~ x) : (y ~ k * u - x)
159+ lowpass ? (y ~ x) : (y ~ k * u - x),
160160 ]
161161
162162 sys = System(equations, t, vars, pars; name, systems)
@@ -211,7 +211,7 @@ Initial value of the state `x` can be set with `x`, and of derivative state `xd`
211211 equations = Equation[
212212 D(x) ~ xd,
213213 D(xd) ~ w * (w * (k * u - x) - 2 * d * xd),
214- y ~ x
214+ y ~ x,
215215 ]
216216
217217 sys = System(equations, t, vars, pars; name, systems)
@@ -243,7 +243,7 @@ See also [`LimPI`](@ref)
243243"""
244244@component function PI(; name, k = 1.0 , T = 1.0 , gainPI__k = nothing )
245245 @symcheck T > 0 ||
246- throw(ArgumentError(" Time constant `T` has to be strictly positive" ))
246+ throw(ArgumentError(" Time constant `T` has to be strictly positive" ))
247247
248248 pars = @parameters begin
249249 k = k, [description = " Proportional gain" ]
@@ -266,7 +266,7 @@ See also [`LimPI`](@ref)
266266 connect(addPI. output, gainPI. input),
267267 connect(gainPI. output, ctr_output),
268268 connect(err_input, int. input),
269- connect(int. output, addPI. input2)
269+ connect(int. output, addPI. input2),
270270 ]
271271
272272 return System(equations, t, vars, pars; name, systems)
@@ -293,18 +293,20 @@ Text-book version of a PID-controller without actuator saturation and anti-windu
293293
294294See also [`LimPID`](@ref)
295295"""
296- @component function PID(; name, k = 1 , Ti = false , Td = false , Nd = 10 , int__x = 0 ,
297- der__x = 0 )
296+ @component function PID(;
297+ name, k = 1 , Ti = false , Td = false , Nd = 10 , int__x = 0 ,
298+ der__x = 0
299+ )
298300 with_I = ! isequal(Ti, false )
299301 with_D = ! isequal(Td, false )
300302 @named err_input = RealInput() # control error
301303 @named ctr_output = RealOutput() # control signal
302304 @symcheck Ti ≥ 0 ||
303- throw(ArgumentError(" Ti out of bounds, got $(Ti) but expected Ti ≥ 0" ))
305+ throw(ArgumentError(" Ti out of bounds, got $(Ti) but expected Ti ≥ 0" ))
304306 @symcheck Td ≥ 0 ||
305- throw(ArgumentError(" Td out of bounds, got $(Td) but expected Td ≥ 0" ))
307+ throw(ArgumentError(" Td out of bounds, got $(Td) but expected Td ≥ 0" ))
306308 @symcheck Nd > 0 ||
307- throw(ArgumentError(" Nd out of bounds, got $(Nd) but expected Nd > 0" ))
309+ throw(ArgumentError(" Nd out of bounds, got $(Nd) but expected Nd > 0" ))
308310
309311 pars = @parameters begin
310312 k = k, [description = " Proportional gain" ]
@@ -339,7 +341,7 @@ See also [`LimPID`](@ref)
339341 eqs = [
340342 connect(err_input, addPID. input1),
341343 connect(addPID. output, gainPID. input),
342- connect(gainPID. output, ctr_output)
344+ connect(gainPID. output, ctr_output),
343345 ]
344346 if with_I
345347 push!(eqs, connect(err_input, int. input))
@@ -380,7 +382,7 @@ The simplified expression above is given without the anti-windup protection.
380382"""
381383@component function LimPI(; name, k = 1 , T = nothing , u_max = nothing , u_min = - u_max, Ta = nothing , int__x = 0.0 )
382384 @symcheck Ta > 0 ||
383- throw(ArgumentError(" Time constant `Ta` has to be strictly positive" ))
385+ throw(ArgumentError(" Time constant `Ta` has to be strictly positive" ))
384386 @symcheck T > 0 || throw(ArgumentError(" Time constant `T` has to be strictly positive" ))
385387 @symcheck u_max ≥ u_min || throw(ArgumentError(" u_min must be smaller than u_max" ))
386388 pars = @parameters begin
@@ -411,7 +413,7 @@ The simplified expression above is given without the anti-windup protection.
411413 connect(err_input, addTrack. input1),
412414 connect(gainTrack. output, addTrack. input2),
413415 connect(addTrack. output, int. input),
414- connect(int. output, addPI. input2)
416+ connect(int. output, addPI. input2),
415417 ]
416418 System(eqs, t, [], pars; name = name, systems = sys)
417419end
@@ -449,14 +451,16 @@ where the transfer function for the derivative includes additional filtering, se
449451 - `measurement`
450452 - `ctr_output`
451453"""
452- @component function LimPID(; name, k = 1 , Ti = false , Td = false , wp = 1 , wd = 1 ,
453- Ni = Ti == 0 ? Inf : √ (max(Td / Ti, 1e-6 )),
454+ @component function LimPID(;
455+ name, k = 1 , Ti = false , Td = false , wp = 1 , wd = 1 ,
456+ Ni = Ti == 0 ? Inf : √ (max(Td / Ti, 1.0e-6 )),
454457 Nd = 10 ,
455458 u_max = Inf ,
456459 u_min = u_max > 0 ? - u_max : - Inf ,
457460 gains = false ,
458461 int__x = 0.0 ,
459- der__x = 0.0 )
462+ der__x = 0.0
463+ )
460464 with_I = ! isequal(Ti, false )
461465 with_D = ! isequal(Td, false )
462466 with_AWM = Ni != Inf
@@ -465,12 +469,12 @@ where the transfer function for the derivative includes additional filtering, se
465469 Td = Td / k
466470 end
467471 @symcheck Ti ≥ 0 ||
468- throw(ArgumentError(" Ti out of bounds, got $(Ti) but expected Ti ≥ 0" ))
472+ throw(ArgumentError(" Ti out of bounds, got $(Ti) but expected Ti ≥ 0" ))
469473 @symcheck Td ≥ 0 ||
470- throw(ArgumentError(" Td out of bounds, got $(Td) but expected Td ≥ 0" ))
474+ throw(ArgumentError(" Td out of bounds, got $(Td) but expected Td ≥ 0" ))
471475 @symcheck u_max ≥ u_min || throw(ArgumentError(" u_min must be smaller than u_max" ))
472476 @symcheck Nd > 0 ||
473- throw(ArgumentError(" Nd out of bounds, got $(Nd) but expected Nd > 0" ))
477+ throw(ArgumentError(" Nd out of bounds, got $(Nd) but expected Nd > 0" ))
474478
475479 pars = @parameters begin
476480 k = k, [description = " Proportional gain" ]
@@ -530,7 +534,7 @@ where the transfer function for the derivative includes additional filtering, se
530534 connect(addP. output, addPID. input1),
531535 connect(addPID. output, gainPID. input),
532536 connect(gainPID. output, limiter. input),
533- connect(limiter. output, ctr_output)
537+ connect(limiter. output, ctr_output),
534538 ]
535539 if with_I
536540 push!(eqs, connect(reference, addI. input1))
@@ -592,8 +596,10 @@ y &= h(x, u)
592596
593597linearized around the operating point `x₀, u₀`, we have `y0, u0 = h(x₀, u₀), u₀`.
594598"""
595- @component function StateSpace(; A, B, C, D = nothing , x = zeros(size(A, 1 )), name,
596- u0 = zeros(size(B, 2 )), y0 = zeros(size(C, 1 )))
599+ @component function StateSpace(;
600+ A, B, C, D = nothing , x = zeros(size(A, 1 )), name,
601+ u0 = zeros(size(B, 2 )), y0 = zeros(size(C, 1 ))
602+ )
597603 nx, nu, ny = size(A, 1 ), size(B, 2 ), size(C, 1 )
598604 size(A, 2 ) == nx || error(" `A` has to be a square matrix." )
599605 size(B, 1 ) == nx || error(" `B` has to be of dimension ($nx x $nu )." )
@@ -608,19 +614,23 @@ linearized around the operating point `x₀, u₀`, we have `y0, u0 = h(x₀, u
608614 end
609615 @named input = RealInput(nin = nu)
610616 @named output = RealOutput(nout = ny)
611- @variables x(t)[1 : nx]= x [
612- description = " State variables of StateSpace system $name "
617+ @variables x(t)[1 : nx] = x [
618+ description = " State variables of StateSpace system $name " ,
613619 ]
614620 # pars = @parameters A=A B=B C=C D=D # This is buggy
615621 eqs = [ # FIXME : if array equations work
616- [Differential(t)(x[i]) ~
617- sum(A[i, k] * x[k] for k in 1 : nx) +
618- sum(B[i, j] * (input. u[j] - u0[j]) for j in 1 : nu)
619- for i in 1 : nx]. .. , # cannot use D here
620- [output. u[j] ~
621- sum(C[j, i] * x[i] for i in 1 : nx) +
622- sum(D[j, k] * (input. u[k] - u0[k]) for k in 1 : nu) + y0[j]
623- for j in 1 : ny]. ..
622+ [
623+ Differential(t)(x[i]) ~
624+ sum(A[i, k] * x[k] for k in 1 : nx) +
625+ sum(B[i, j] * (input. u[j] - u0[j]) for j in 1 : nu)
626+ for i in 1 : nx
627+ ]. .. , # cannot use D here
628+ [
629+ output. u[j] ~
630+ sum(C[j, i] * x[i] for i in 1 : nx) +
631+ sum(D[j, k] * (input. u[k] - u0[k]) for k in 1 : nu) + y0[j]
632+ for j in 1 : ny
633+ ]. .. ,
624634 ]
625635 compose(System(eqs, t, vcat(x... ), [], name = name), [input, output])
626636end
@@ -668,24 +678,24 @@ See also [`StateSpace`](@ref) which handles MIMO systems, as well as [ControlSys
668678
669679 @parameters begin
670680 b[1 : nb] = b,
671- [
672- description = " Numerator coefficients of transfer function (e.g., 2s + 3 is specified as [2,3])"
673- ]
681+ [
682+ description = " Numerator coefficients of transfer function (e.g., 2s + 3 is specified as [2,3])" ,
683+ ]
674684 a[1 : na] = a,
675- [
676- description = " Denominator coefficients of transfer function (e.g., `s² + 2ωs + ω^2` is specified as [1, 2ω, ω^2])"
677- ]
685+ [
686+ description = " Denominator coefficients of transfer function (e.g., `s² + 2ωs + ω^2` is specified as [1, 2ω, ω^2])" ,
687+ ]
678688 bb[1 : (nbb + nb)] = [zeros(nbb); b]
679689 end
680- d = bb[1 ] / a[1 ]# , [description = "Direct feedthrough gain"]
690+ d = bb[1 ] / a[1 ] # , [description = "Direct feedthrough gain"]
681691
682692 a = collect(a)
683693 a_end = ifelse(a[end ] > 100 * symbolic_eps(sqrt(a' * a)), a[end], 1.0)
684694
685695 pars = [collect(b); a; collect(bb)]
686696 @variables begin
687697 x(t)[1:nx] = zeros(nx),
688- [description = "State of transfer function on controller canonical form"]
698+ [description = "State of transfer function on controller canonical form"]
689699 x_scaled(t)[1:nx] = collect(x) * a_end, [description = "Scaled vector x"]
690700 u(t), [description = "Input of transfer function"]
691701 y(t), [description = "Output of transfer function"]
@@ -700,10 +710,12 @@ See also [`StateSpace`](@ref) which handles MIMO systems, as well as [ControlSys
700710 if nx == 0
701711 eqs = [y ~ d * u]
702712 else
703- eqs = Equation[D(x_scaled[1]) ~ (-a[2:na]' x_scaled + a_end * u) / a[1 ]
704- D.(x_scaled[2 : nx]) .~ x_scaled[1 : (nx - 1 )]
705- y ~ ((bb[2 : na] - d * a[2 : na])' x_scaled) / a_end + d * u
706- x .~ x_scaled ./ a_end]
713+ eqs = Equation[
714+ D(x_scaled[1]) ~ (-a[2:na]' x_scaled + a_end * u) / a[1 ]
715+ D.(x_scaled[2 : nx]) .~ x_scaled[1 : (nx - 1 )]
716+ y ~ ((bb[2 : na] - d * a[2 : na])' x_scaled) / a_end + d * u
717+ x .~ x_scaled ./ a_end
718+ ]
707719 end
708720 push!(eqs, input. u ~ u)
709721 push!(eqs, output. u ~ y)
0 commit comments