@@ -17,18 +17,28 @@ Initial value of integrator state ``x`` can be set with `x`
1717
1818 - `x`: State of Integrator. Defaults to 0.0.
1919"""
20- @mtkmodel Integrator begin
21- @extend u, y = siso = SISO ()
22- @variables begin
23- x (t) = 0.0 , [description = " State of Integrator" ]
20+ @component function Integrator (; name, k = 1 , x = 0.0 )
21+ @named siso = SISO ()
22+ @unpack u, y = siso
23+
24+ pars = @parameters begin
25+ k = k, [description = " Gain" ]
2426 end
25- @parameters begin
26- k = 1 , [description = " Gain " ]
27+
28+ systems = @named begin
2729 end
28- @equations begin
29- D (x) ~ k * u
30- y ~ x
30+
31+ vars = @variables begin
32+ x (t) = x, [description = " State of Integrator " ]
3133 end
34+
35+ equations = Equation[
36+ D (x) ~ k * u,
37+ y ~ x
38+ ]
39+
40+ sys = System (equations, t, vars, pars; name, systems)
41+ return extend (sys, siso)
3242end
3343
3444"""
3747Outputs an approximate derivative of the input. The transfer function of this block is
3848
3949```
40- k k ks
41- ─ - ─────── = ──────
50+ k k ks
51+ ─ - ─────── = ──────
4252T sT² + T sT + 1
4353```
4454
@@ -62,23 +72,32 @@ Initial value of the state ``x`` can be set with `x`.
6272 - `input`
6373 - `output`
6474"""
65- @mtkmodel Derivative begin
66- @extend u, y = siso = SISO ()
67- @variables begin
68- x (t) = 0.0 , [description = " Derivative-filter state" ]
69- end
70- @parameters begin
75+ @component function Derivative (; name, k = 1 , T = nothing , x = 0.0 )
76+ @symcheck T > 0 ||
77+ throw (ArgumentError (" Time constant `T` has to be strictly positive" ))
78+
79+ @named siso = SISO ()
80+ @unpack u, y = siso
81+
82+ pars = @parameters begin
7183 T = T, [description = " Time constant" ]
72- k = 1 , [description = " Gain" ]
84+ k = k , [description = " Gain" ]
7385 end
74- begin
75- @symcheck T > 0 ||
76- throw (ArgumentError (" Time constant `T` has to be strictly positive" ))
86+
87+ systems = @named begin
7788 end
78- @equations begin
79- D (x) ~ (u - x) / T
80- y ~ (k / T) * (u - x)
89+
90+ vars = @variables begin
91+ x (t) = x, [description = " Derivative-filter state " ]
8192 end
93+
94+ equations = Equation[
95+ D (x) ~ (u - x) / T,
96+ y ~ (k / T) * (u - x)
97+ ]
98+
99+ sys = System (equations, t, vars, pars; name, systems)
100+ return extend (sys, siso)
82101end
83102
84103"""
@@ -116,26 +135,32 @@ Initial value of the state `x` can be set with `x`
116135
117136See also [`SecondOrder`](@ref)
118137"""
119- @mtkmodel FirstOrder begin
120- @extend u, y = siso = SISO ()
121- @structural_parameters begin
122- lowpass = true
123- end
124- @variables begin
125- x (t) = 0.0 , [description = " State of FirstOrder filter" ]
126- end
127- @parameters begin
138+ @component function FirstOrder (; name, lowpass = true , T = nothing , k = 1.0 , x = 0.0 )
139+ @symcheck T > 0 ||
140+ throw (ArgumentError (" Time constant `T` has to be strictly positive" ))
141+
142+ @named siso = SISO ()
143+ @unpack u, y = siso
144+
145+ pars = @parameters begin
128146 T = T, [description = " Time constant" ]
129- k = 1.0 , [description = " Gain" ]
147+ k = k , [description = " Gain" ]
130148 end
131- begin
132- @symcheck T > 0 ||
133- throw (ArgumentError (" Time constant `T` has to be strictly positive" ))
149+
150+ systems = @named begin
134151 end
135- @equations begin
136- D (x) ~ (k * u - x) / T
137- lowpass ? y ~ x : y ~ k * u - x
152+
153+ vars = @variables begin
154+ x (t) = x, [description = " State of FirstOrder filter " ]
138155 end
156+
157+ equations = Equation[
158+ D (x) ~ (k * u - x) / T,
159+ lowpass ? (y ~ x) : (y ~ k * u - x)
160+ ]
161+
162+ sys = System (equations, t, vars, pars; name, systems)
163+ return extend (sys, siso)
139164end
140165
141166"""
@@ -165,22 +190,32 @@ Initial value of the state `x` can be set with `x`, and of derivative state `xd`
165190 - `input`
166191 - `output`
167192"""
168- @mtkmodel SecondOrder begin
169- @extend u, y = siso = SISO ()
170- @variables begin
171- x (t), [description = " State of SecondOrder filter" , guess = 0.0 ]
172- xd (t), [description = " Derivative state of SecondOrder filter" , guess = 0.0 ]
193+ @component function SecondOrder (; name, k = 1.0 , w = 1.0 , d = 1.0 , x = nothing , xd = nothing )
194+ @named siso = SISO ()
195+ @unpack u, y = siso
196+
197+ pars = @parameters begin
198+ k = k, [description = " Gain" ]
199+ w = w, [description = " Bandwidth (angular frequency)" ]
200+ d = d, [description = " Relative damping" ]
173201 end
174- @parameters begin
175- k = 1.0 , [description = " Gain" ]
176- w = 1.0 , [description = " Bandwidth (angular frequency)" ]
177- d = 1.0 , [description = " Relative damping" ]
202+
203+ systems = @named begin
178204 end
179- @equations begin
180- D (x) ~ xd
181- D (xd) ~ w * (w * (k * u - x) - 2 * d * xd)
182- y ~ x
205+
206+ vars = @variables begin
207+ x (t) = x, [description = " State of SecondOrder filter " , guess = 0.0 ]
208+ xd (t) = xd, [description = " Derivative state of SecondOrder filter " , guess = 0.0 ]
183209 end
210+
211+ equations = Equation[
212+ D (x) ~ xd,
213+ D (xd) ~ w * (w * (k * u - x) - 2 * d * xd),
214+ y ~ x
215+ ]
216+
217+ sys = System (equations, t, vars, pars; name, systems)
218+ return extend (sys, siso)
184219end
185220
186221"""
@@ -206,29 +241,35 @@ U(s) = k (1 + \\dfrac{1}{sT}) E(S)
206241
207242See also [`LimPI`](@ref)
208243"""
209- @mtkmodel PI begin
210- @parameters begin
211- k = 1.0 , [description = " Proportional gain" ]
212- T = 1.0 , [description = " Integrator time constant" ]
213- end
214- begin
215- @symcheck T > 0 ||
216- throw (ArgumentError (" Time constant `T` has to be strictly positive" ))
244+ @component function PI (; name, k = 1.0 , T = 1.0 , gainPI__k = nothing )
245+ @symcheck T > 0 ||
246+ throw (ArgumentError (" Time constant `T` has to be strictly positive" ))
247+
248+ pars = @parameters begin
249+ k = k, [description = " Proportional gain" ]
250+ T = T, [description = " Integrator time constant" ]
217251 end
218- @components begin
252+
253+ systems = @named begin
219254 err_input = RealInput () # control error
220255 ctr_output = RealOutput () # control signal
221- gainPI = Gain (; k)
256+ gainPI = Gain (; k = gainPI__k )
222257 addPI = Add ()
223258 int = Integrator (k = 1 / T, x = 0.0 )
224259 end
225- @equations begin
226- connect (err_input, addPI. input1)
227- connect (addPI. output, gainPI. input)
228- connect (gainPI. output, ctr_output)
229- connect (err_input, int. input)
230- connect (int. output, addPI. input2)
260+
261+ vars = @variables begin
231262 end
263+
264+ equations = Equation[
265+ connect (err_input, addPI. input1),
266+ connect (addPI. output, gainPI. input),
267+ connect (gainPI. output, ctr_output),
268+ connect (err_input, int. input),
269+ connect (int. output, addPI. input2)
270+ ]
271+
272+ return System (equations, t, vars, pars; name, systems)
232273end
233274
234275"""
@@ -337,7 +378,7 @@ The simplified expression above is given without the anti-windup protection.
337378 - `err_input`
338379 - `ctr_output`
339380"""
340- @component function LimPI (; name, k = 1 , T, u_max, u_min = - u_max, Ta, int__x = 0.0 )
381+ @component function LimPI (; name, k = 1 , T = nothing , u_max = nothing , u_min = - u_max, Ta = nothing , int__x = 0.0 )
341382 @symcheck Ta > 0 ||
342383 throw (ArgumentError (" Time constant `Ta` has to be strictly positive" ))
343384 @symcheck T > 0 || throw (ArgumentError (" Time constant `T` has to be strictly positive" ))
@@ -610,7 +651,7 @@ To set the initial state, it's recommended to set the initial condition for `x`,
610651 - `input`
611652 - `output`
612653
613- See also [`StateSpace`](@ref) which handles MIMO systems, as well as [ControlSystemsMTK.jl](https://juliacontrol.github.io/ControlSystemsMTK.jl/stable/) for an interface between [ControlSystems.jl](https://juliacontrol.github.io/ControlSystems.jl/stable/) and ModelingToolkit .jl for advanced manipulation of transfer functions and linear statespace systems. For linearization, see [`linearize`](@ref) and [Linear Analysis](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/linear_analysis/).
654+ See also [`StateSpace`](@ref) which handles MIMO systems, as well as [ControlSystemsMTK.jl](https://juliacontrol.github.io/ControlSystemsMTK.jl/stable/) for an interface between [ControlSystems.jl](https://juliacontrol.github.io/ControlSystems.jl/stable/) and ModelingToolkitBase .jl for advanced manipulation of transfer functions and linear statespace systems. For linearization, see [`linearize`](@ref) and [Linear Analysis](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/linear_analysis/).
614655"""
615656@component function TransferFunction (; b = [1 ], a = [1 , 1 ], name)
616657 nb = length (b)
0 commit comments