@@ -2,7 +2,7 @@ using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks
22using OrdinaryDiffEq, LinearAlgebra
33using Test
44using ModelingToolkit: t_nounits as t, D_nounits as D, AnalysisPoint, get_sensitivity,
5- get_comp_sensitivity, get_looptransfer
5+ get_comp_sensitivity, get_looptransfer, open_loop, AbstractSystem
66using Symbolics: NAMESPACE_SEPARATOR
77
88@testset " AnalysisPoint is lowered to `connect`" begin
@@ -13,14 +13,14 @@ using Symbolics: NAMESPACE_SEPARATOR
1313 eqs = [connect (P. output, C. input)
1414 connect (C. output, ap, P. input)]
1515 sys_ap = ODESystem (eqs, t, systems = [P, C], name = :hej )
16- sys_ap2 = @test_nowarn expand_connections (sys )
16+ sys_ap2 = @test_nowarn expand_connections (sys_ap )
1717
1818 @test all (eq -> ! (eq. lhs isa AnalysisPoint), equations (sys_ap2))
1919
2020 eqs = [connect (P. output, C. input)
2121 connect (C. output, P. input)]
2222 sys_normal = ODESystem (eqs, t, systems = [P, C], name = :hej )
23- sys_normal2 = @test_nowarn expand_connections (sys )
23+ sys_normal2 = @test_nowarn expand_connections (sys_normal )
2424
2525 @test isequal (sys_ap2, sys_normal2)
2626end
191191 @test matrices. B[] * matrices. C[] == 1 # both positive
192192 @test matrices. D[] == 0
193193end
194-
195- using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra
196- using ModelingToolkitStandardLibrary. Mechanical. Rotational
197- using ModelingToolkitStandardLibrary. Blocks: Sine, PID, SecondOrder, Step, RealOutput
198- using ModelingToolkit: connect
199-
200- @testset " Complicated model" begin
201- # Parameters
202- m1 = 1
203- m2 = 1
204- k = 1000 # Spring stiffness
205- c = 10 # Damping coefficient
206- @named inertia1 = Inertia (; J = m1)
207- @named inertia2 = Inertia (; J = m2)
208- @named spring = Spring (; c = k)
209- @named damper = Damper (; d = c)
210- @named torque = Torque ()
211-
212- function SystemModel (u = nothing ; name = :model )
213- eqs = [connect (torque. flange, inertia1. flange_a)
214- connect (inertia1. flange_b, spring. flange_a, damper. flange_a)
215- connect (inertia2. flange_a, spring. flange_b, damper. flange_b)]
216- if u != = nothing
217- push! (eqs, connect (torque. tau, u. output))
218- return ODESystem (eqs, t;
219- systems = [
220- torque,
221- inertia1,
222- inertia2,
223- spring,
224- damper,
225- u
226- ],
227- name)
228- end
229- ODESystem (eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name)
230- end
231-
232- @named r = Step (start_time = 0 )
233- model = SystemModel ()
234- @named pid = PID (k = 100 , Ti = 0.5 , Td = 1 )
235- @named filt = SecondOrder (d = 0.9 , w = 10 )
236- @named sensor = AngleSensor ()
237- @named er = Add (k2 = - 1 )
238-
239- connections = [connect (r. output, :r , filt. input)
240- connect (filt. output, er. input1)
241- connect (pid. ctr_output, :u , model. torque. tau)
242- connect (model. inertia2. flange_b, sensor. flange)
243- connect (sensor. phi, :y , er. input2)
244- connect (er. output, :e , pid. err_input)]
245-
246- closed_loop = ODESystem (connections, t, systems = [model, pid, filt, sensor, r, er],
247- name = :closed_loop , defaults = [
248- model. inertia1. phi => 0.0 ,
249- model. inertia2. phi => 0.0 ,
250- model. inertia1. w => 0.0 ,
251- model. inertia2. w => 0.0 ,
252- filt. x => 0.0 ,
253- filt. xd => 0.0
254- ])
255-
256- sys = structural_simplify (closed_loop)
257- prob = ODEProblem (sys, unknowns (sys) .=> 0.0 , (0.0 , 4.0 ))
258- sol = solve (prob, Rodas5P (), reltol = 1e-6 , abstol = 1e-9 )
259- end
0 commit comments