11using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra, ControlSystemsBase
22using ModelingToolkitStandardLibrary. Mechanical. Rotational
33using ModelingToolkitStandardLibrary. Blocks
4- using ModelingToolkit: connect, AnalysisPoint, t_nounits as t, D_nounits as D
4+ using ModelingToolkit: connect, AnalysisPoint, t_nounits as t, D_nounits as D,
5+ get_sensitivity, get_comp_sensitivity, get_looptransfer, open_loop
56import ControlSystemsBase as CS
67
78@testset " Complicated model" begin
@@ -154,10 +155,10 @@ end
154155 t,
155156 systems = [P_outer, sys_inner])
156157
157- Souter = sminreal (ss (get_sensitivity (sys_outer, :sys_inner_u )[1 ]. .. ))
158+ Souter = sminreal (ss (get_sensitivity (sys_outer, sys_inner . u )[1 ]. .. ))
158159
159160 Sinner2 = sminreal (ss (get_sensitivity (
160- sys_outer, :sys_inner_u , loop_openings = [:y2 ])[1 ]. .. ))
161+ sys_outer, sys_inner . u , loop_openings = [:y2 ])[1 ]. .. ))
161162
162163 @test Sinner. nx == 1
163164 @test Sinner == Sinner2
@@ -183,23 +184,23 @@ end
183184 connect (K. output, :plant_input , P. input)]
184185 sys = ODESystem (eqs, t, systems = [P, K], name = :hej )
185186
186- matrices, _ = Blocks . get_sensitivity (sys, :plant_input )
187+ matrices, _ = get_sensitivity (sys, :plant_input )
187188 S = CS. feedback (I (2 ), Kss * Pss, pos_feedback = true )
188189
189190 @test CS. tf (CS. ss (matrices... )) ≈ CS. tf (S)
190191
191- matrices, _ = Blocks . get_comp_sensitivity (sys, :plant_input )
192+ matrices, _ = get_comp_sensitivity (sys, :plant_input )
192193 T = - CS. feedback (Kss * Pss, I (2 ), pos_feedback = true )
193194
194195 # bodeplot([ss(matrices...), T])
195196 @test CS. tf (CS. ss (matrices... )) ≈ CS. tf (T)
196197
197- matrices, _ = Blocks . get_looptransfer (
198+ matrices, _ = get_looptransfer (
198199 sys, :plant_input )
199200 L = Kss * Pss
200201 @test CS. tf (CS. ss (matrices... )) ≈ CS. tf (L)
201202
202- matrices, _ = linearize (sys, :plant_input , :plant_output )
203+ matrices, _ = linearize (sys, AnalysisPoint ( :plant_input ) , :plant_output )
203204 G = CS. feedback (Pss, Kss, pos_feedback = true )
204205 @test CS. tf (CS. ss (matrices... )) ≈ CS. tf (G)
205206end
@@ -222,12 +223,53 @@ end
222223 connect (F. output, sys_inner. add. input1)]
223224 sys_outer = ODESystem (eqs, t, systems = [F, sys_inner, r], name = :outer )
224225
225- matrices, _ = get_sensitivity (sys_outer, [:, :inner_plant_output ])
226+ matrices, _ = get_sensitivity (
227+ sys_outer, [sys_outer. inner. plant_input, sys_outer. inner. plant_output])
226228
227229 Ps = tf (1 , [1 , 1 ]) |> ss
228230 Cs = tf (1 ) |> ss
229231
230232 G = CS. ss (matrices... ) |> sminreal
231233 Si = CS. feedback (1 , Cs * Ps)
232234 @test tf (G[1 , 1 ]) ≈ tf (Si)
235+
236+ So = CS. feedback (1 , Ps * Cs)
237+ @test tf (G[2 , 2 ]) ≈ tf (So)
238+ @test tf (G[1 , 2 ]) ≈ tf (- CS. feedback (Cs, Ps))
239+ @test tf (G[2 , 1 ]) ≈ tf (CS. feedback (Ps, Cs))
240+
241+ matrices, _ = get_comp_sensitivity (
242+ sys_outer, [sys_outer. inner. plant_input, sys_outer. inner. plant_output])
243+
244+ G = CS. ss (matrices... ) |> sminreal
245+ Ti = CS. feedback (Cs * Ps)
246+ @test tf (G[1 , 1 ]) ≈ tf (Ti)
247+
248+ To = CS. feedback (Ps * Cs)
249+ @test tf (G[2 , 2 ]) ≈ tf (To)
250+ @test tf (G[1 , 2 ]) ≈ tf (CS. feedback (Cs, Ps)) # The negative sign appears in a confusing place due to negative feedback not happening through Ps
251+ @test tf (G[2 , 1 ]) ≈ tf (- CS. feedback (Ps, Cs))
252+
253+ # matrices, _ = get_looptransfer(sys_outer, [:inner_plant_input, :inner_plant_output])
254+ matrices, _ = get_looptransfer (sys_outer, sys_outer. inner. plant_input)
255+ L = CS. ss (matrices... ) |> sminreal
256+ @test tf (L) ≈ - tf (Cs * Ps)
257+
258+ matrices, _ = get_looptransfer (sys_outer, sys_outer. inner. plant_output)
259+ L = CS. ss (matrices... ) |> sminreal
260+ @test tf (L[1 , 1 ]) ≈ - tf (Ps * Cs)
261+
262+ # Calling looptransfer like below is not the intended way, but we can work out what it should return if we did so it remains a valid test
263+ matrices, _ = get_looptransfer (
264+ sys_outer, [sys_outer. inner. plant_input, sys_outer. inner. plant_output])
265+ L = CS. ss (matrices... ) |> sminreal
266+ @test tf (L[1 , 1 ]) ≈ tf (0 )
267+ @test tf (L[2 , 2 ]) ≈ tf (0 )
268+ @test sminreal (L[1 , 2 ]) ≈ ss (- 1 )
269+ @test tf (L[2 , 1 ]) ≈ tf (Ps)
270+
271+ matrices, _ = linearize (
272+ sys_outer, [sys_outer. inner. plant_input], [sys_inner. plant_output])
273+ G = CS. ss (matrices... ) |> sminreal
274+ @test tf (G) ≈ tf (CS. feedback (Ps, Cs))
233275end
0 commit comments