Skip to content

Commit 1e3072e

Browse files
committed
debug: add busywait arg to periodsleep for StateEstimator and PredictiveController
Also : restructure test files
1 parent 6a66709 commit 1e3072e

8 files changed

+27
-32
lines changed

Diff for: src/controller/execute.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,11 @@ Call `savetime!(mpc.estim.model)` and return the time `t`.
585585
savetime!(mpc::PredictiveController) = savetime!(mpc.estim.model)
586586

587587
"""
588-
periodsleep(mpc::PredictiveController) -> nothing
588+
periodsleep(mpc::PredictiveController, busywait=false) -> nothing
589589
590590
Call `periodsleep(mpc.estim.model)`.
591591
"""
592-
periodsleep(mpc::PredictiveController) = periodsleep(mpc.estim.model)
592+
periodsleep(mpc::PredictiveController, busywait=false) = periodsleep(mpc.estim.model, busywait)
593593

594594
"""
595595
setstate!(mpc::PredictiveController, x̂) -> mpc

Diff for: src/estimator/execute.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ Call `savetime!(estim.model)` and return the time `t`.
291291
savetime!(estim::StateEstimator) = savetime!(estim.model)
292292

293293
"""
294-
periodsleep(estim::StateEstimator) -> nothing
294+
periodsleep(estim::StateEstimator, busywait=false) -> nothing
295295
296296
Call `periodsleep(estim.model)`.
297297
"""
298-
periodsleep(estim::StateEstimator) = periodsleep(estim.model)
298+
periodsleep(estim::StateEstimator, busywait=false) = periodsleep(estim.model, busywait)
299299

300300

301301
"""

Diff for: test/0_test_module.jl

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@testmodule SetupMPCtests begin
2+
using ControlSystemsBase
3+
Ts = 400.0
4+
sys = [ tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]);
5+
tf(-0.74,[800.0,1]) tf(0.74,[800.0,1]) tf(-0.74,[800.0,1]) ]
6+
sys_ss = minreal(ss(sys))
7+
Gss = c2d(sys_ss[:,1:2], Ts, :zoh)
8+
Gss2 = c2d(sys_ss[:,1:2], 0.5Ts, :zoh)
9+
export Ts, sys, sys_ss, Gss, Gss2
10+
end

Diff for: test/test_sim_model.jl renamed to test/1_test_sim_model.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,22 @@ end
120120

121121
@testitem "LinModel real time simulations" setup=[SetupMPCtests] begin
122122
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
123-
linmodel1 = LinModel(tf(2, [10, 1]), 0.1)
123+
linmodel1 = LinModel(tf(2, [10, 1]), 0.25)
124124
times1 = zeros(5)
125125
for i=1:5
126126
times1[i] = savetime!(linmodel1)
127127
updatestate!(linmodel1, [1])
128128
periodsleep(linmodel1)
129129
end
130-
@test all(isapprox.(diff(times1[2:end]), 0.1, atol=0.01))
131-
linmodel2 = LinModel(tf(2, [0.1, 1]), 0.001)
130+
@test all(isapprox.(diff(times1[2:end]), 0.25, atol=0.01))
131+
linmodel2 = LinModel(tf(2, [0.1, 1]), 0.25)
132132
times2 = zeros(5)
133133
for i=1:5
134134
times2[i] = savetime!(linmodel2)
135135
updatestate!(linmodel2, [1])
136136
periodsleep(linmodel2, true)
137137
end
138-
@test all(isapprox.(diff(times2[2:end]), 0.001, atol=0.0001))
138+
@test all(isapprox.(diff(times2[2:end]), 0.25, atol=0.0001))
139139
end
140140

141141
@testitem "NonLinModel construction" setup=[SetupMPCtests] begin

Diff for: test/test_state_estim.jl renamed to test/2_test_state_estim.jl

+2-3
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ end
133133
times1[i] = savetime!(skalmanfilter1)
134134
preparestate!(skalmanfilter1, [1])
135135
updatestate!(skalmanfilter1, [1], [1])
136-
periodsleep(skalmanfilter1)
136+
periodsleep(skalmanfilter1, true)
137137
end
138-
println(diff(times1))
139138
@test all(isapprox.(diff(times1[2:end]), 0.25, atol=0.01))
140139
end
141140

@@ -864,7 +863,7 @@ end
864863
end
865864

866865
@testitem "MovingHorizonEstimator estimation and getinfo" setup=[SetupMPCtests] begin
867-
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt
866+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt, ForwardDiff
868867
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2], i_d=[3]), uop=[10,50], yop=[50,30], dop=[5])
869868
f(x,u,d,_) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
870869
h(x,d,_) = linmodel1.C*x + linmodel1.Dd*d

Diff for: test/test_predictive_control.jl renamed to test/3_test_predictive_control.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ end
356356
times1[i] = savetime!(mpc1)
357357
preparestate!(mpc1, [1])
358358
updatestate!(mpc1, [1], [1])
359-
periodsleep(mpc1)
359+
periodsleep(mpc1, true)
360360
end
361361
@test all(isapprox.(diff(times1[2:end]), 0.1, atol=0.01))
362362
end
@@ -580,7 +580,7 @@ end
580580
end
581581

582582
@testitem "NonLinMPC moves and getinfo" setup=[SetupMPCtests] begin
583-
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
583+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, ForwardDiff
584584
linmodel = setop!(LinModel(tf(5, [2000, 1]), 3000.0), yop=[10])
585585
Hp = 1000
586586
nmpc_lin = NonLinMPC(linmodel, Nwt=[0], Hp=Hp, Hc=1)
File renamed without changes.

Diff for: test/runtests.jl

+5-19
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,11 @@ using Test, TestItemRunner
1111

1212
@run_package_tests
1313

14-
@testset "ModelPredictiveControl.jl" begin
15-
16-
@testmodule SetupMPCtests begin
17-
using ControlSystemsBase
18-
Ts = 400.0
19-
sys = [ tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]);
20-
tf(-0.74,[800.0,1]) tf(0.74,[800.0,1]) tf(-0.74,[800.0,1]) ]
21-
sys_ss = minreal(ss(sys))
22-
Gss = c2d(sys_ss[:,1:2], Ts, :zoh)
23-
Gss2 = c2d(sys_ss[:,1:2], 0.5Ts, :zoh)
24-
export Ts, sys, sys_ss, Gss, Gss2
25-
end
26-
27-
include("test_sim_model.jl")
28-
include("test_state_estim.jl")
29-
include("test_predictive_control.jl")
30-
include("test_plot_sim.jl")
14+
include("0_test_module.jl")
15+
include("1_test_sim_model.jl")
16+
include("2_test_state_estim.jl")
17+
include("3_test_predictive_control.jl")
18+
include("4_test_plot_sim.jl")
3119

3220
old_debug_level = get(ENV, "JULIA_DEBUG", "")
3321
DocMeta.setdocmeta!(
@@ -43,6 +31,4 @@ DocMeta.setdocmeta!(
4331
doctest(ModelPredictiveControl, testset="DocTest")
4432
ENV["JULIA_DEBUG"] = old_debug_level
4533

46-
end;
47-
4834
nothing

0 commit comments

Comments
 (0)