Skip to content

Commit 0ca6f85

Browse files
authored
Merge pull request #159 from JuliaControl/bump
bump
2 parents 450e9c8 + 1e3072e commit 0ca6f85

9 files changed

+103
-46
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelPredictiveControl"
22
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
33
authors = ["Francis Gagnon"]
4-
version = "1.3.1"
4+
version = "1.3.2"
55

66
[deps]
77
ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e"

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

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
"""

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

test/test_sim_model.jl test/1_test_sim_model.jl

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@testitem "LinModel construction" setup=[SetupMPCtests] begin
2+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
23
linmodel1 = LinModel(sys, Ts, i_u=1:2)
34
@test linmodel1.nx == 2
45
@test linmodel1.nu == 2
@@ -97,6 +98,7 @@
9798
end
9899

99100
@testitem "LinModel sim methods" setup=[SetupMPCtests] begin
101+
using .SetupMPCtests, ControlSystemsBase
100102
linmodel1 = setop!(LinModel(Gss), uop=[10,50], yop=[50,30])
101103
@test updatestate!(linmodel1, [10, 50]) zeros(2)
102104
@test updatestate!(linmodel1, [10, 50], Float64[]) zeros(2)
@@ -117,25 +119,27 @@ end
117119
end
118120

119121
@testitem "LinModel real time simulations" setup=[SetupMPCtests] begin
120-
linmodel1 = LinModel(tf(2, [10, 1]), 0.1)
122+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
123+
linmodel1 = LinModel(tf(2, [10, 1]), 0.25)
121124
times1 = zeros(5)
122125
for i=1:5
123126
times1[i] = savetime!(linmodel1)
124127
updatestate!(linmodel1, [1])
125128
periodsleep(linmodel1)
126129
end
127-
@test all(isapprox.(diff(times1[2:end]), 0.1, atol=0.01))
128-
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)
129132
times2 = zeros(5)
130133
for i=1:5
131134
times2[i] = savetime!(linmodel2)
132135
updatestate!(linmodel2, [1])
133136
periodsleep(linmodel2, true)
134137
end
135-
@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))
136139
end
137140

138141
@testitem "NonLinModel construction" setup=[SetupMPCtests] begin
142+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
139143
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
140144
f1(x,u,_,model) = model.A*x + model.Bu*u
141145
h1(x,_,model) = model.C*x
@@ -243,6 +247,7 @@ end
243247
end
244248

245249
@testitem "NonLinModel sim methods" setup=[SetupMPCtests] begin
250+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
246251
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
247252
f1(x,u,_,model) = model.A*x + model.Bu*u
248253
h1(x,_,model) = model.C*x
@@ -262,6 +267,7 @@ end
262267
end
263268

264269
@testitem "NonLinModel linearization" setup=[SetupMPCtests] begin
270+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, ForwardDiff
265271
Ts = 1.0
266272
f1(x,u,d,_) = x.^5 + u.^4 + d.^3
267273
h1(x,d,_) = x.^2 + d
@@ -317,6 +323,7 @@ end
317323
end
318324

319325
@testitem "NonLinModel real time simulations" setup=[SetupMPCtests] begin
326+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
320327
linmodel1 = LinModel(tf(2, [10, 1]), 0.1)
321328
nonlinmodel1 = NonLinModel(
322329
(x,u,_,_)->linmodel1.A*x + linmodel1.Bu*u,

test/test_state_estim.jl test/2_test_state_estim.jl

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@testitem "SteadyKalmanFilter construction" setup=[SetupMPCtests] begin
2+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
23
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
34
skalmanfilter1 = SteadyKalmanFilter(linmodel1)
45
@test skalmanfilter1.nym == 2
@@ -61,6 +62,7 @@
6162
end
6263

6364
@testitem "SteadyKalmanFilter estimator methods" setup=[SetupMPCtests] begin
65+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
6466
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
6567
skalmanfilter1 = SteadyKalmanFilter(linmodel1, nint_ym=[1, 1])
6668
preparestate!(skalmanfilter1, [50, 30])
@@ -112,6 +114,7 @@ end
112114
end
113115

114116
@testitem "SteadyKalmanFilter set model" setup=[SetupMPCtests] begin
117+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
115118
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
116119
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
117120
skalmanfilter = SteadyKalmanFilter(linmodel, nint_ym=0)
@@ -122,19 +125,21 @@ end
122125
end
123126

124127
@testitem "SteadyKalmanFilter real-time simulations" setup=[SetupMPCtests] begin
125-
linmodel1 = LinModel(tf(2, [10, 1]), 0.1)
128+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
129+
linmodel1 = LinModel(tf(2, [10, 1]), 0.25)
126130
skalmanfilter1 = SteadyKalmanFilter(linmodel1)
127131
times1 = zeros(5)
128132
for i=1:5
129133
times1[i] = savetime!(skalmanfilter1)
130134
preparestate!(skalmanfilter1, [1])
131135
updatestate!(skalmanfilter1, [1], [1])
132-
periodsleep(skalmanfilter1)
136+
periodsleep(skalmanfilter1, true)
133137
end
134-
@test all(isapprox.(diff(times1[2:end]), 0.1, atol=0.01))
138+
@test all(isapprox.(diff(times1[2:end]), 0.25, atol=0.01))
135139
end
136140

137141
@testitem "KalmanFilter construction" setup=[SetupMPCtests] begin
142+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
138143
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
139144
kalmanfilter1 = KalmanFilter(linmodel1)
140145
@test kalmanfilter1.nym == 2
@@ -186,6 +191,7 @@ end
186191
end
187192

188193
@testitem "KalmanFilter estimator methods" setup=[SetupMPCtests] begin
194+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
189195
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
190196
kalmanfilter1 = KalmanFilter(linmodel1)
191197
preparestate!(kalmanfilter1, [50, 30])
@@ -232,6 +238,7 @@ end
232238
end
233239

234240
@testitem "KalmanFilter set model" setup=[SetupMPCtests] begin
241+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
235242
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
236243
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
237244
kalmanfilter = KalmanFilter(linmodel, nint_ym=0)
@@ -260,6 +267,7 @@ end
260267
end
261268

262269
@testitem "Luenberger construction" setup=[SetupMPCtests] begin
270+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
263271
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
264272
lo1 = Luenberger(linmodel1)
265273
@test lo1.nym == 2
@@ -300,6 +308,7 @@ end
300308
end
301309

302310
@testitem "Luenberger estimator methods" setup=[SetupMPCtests] begin
311+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
303312
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
304313
lo1 = Luenberger(linmodel1, nint_ym=[1, 1])
305314
preparestate!(lo1, [50, 30])
@@ -345,6 +354,7 @@ end
345354
end
346355

347356
@testitem "Luenberger set model" setup=[SetupMPCtests] begin
357+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
348358
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
349359
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
350360
lo = Luenberger(linmodel, nint_ym=0)
@@ -353,6 +363,7 @@ end
353363
end
354364

355365
@testitem "InternalModel construction" setup=[SetupMPCtests] begin
366+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
356367
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
357368
internalmodel1 = InternalModel(linmodel1)
358369
@test internalmodel1.nym == 2
@@ -422,6 +433,7 @@ end
422433
end
423434

424435
@testitem "InternalModel estimator methods" setup=[SetupMPCtests] begin
436+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
425437
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]) , uop=[10,50], yop=[50,30])
426438
internalmodel1 = InternalModel(linmodel1)
427439
preparestate!(internalmodel1, [50, 30] .+ 1)
@@ -459,6 +471,7 @@ end
459471
end
460472

461473
@testitem "InternalModel set model" setup=[SetupMPCtests] begin
474+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
462475
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
463476
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
464477
internalmodel = InternalModel(linmodel)
@@ -484,6 +497,7 @@ end
484497
end
485498

486499
@testitem "UnscentedKalmanFilter construction" setup=[SetupMPCtests] begin
500+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
487501
linmodel1 = LinModel(sys,Ts,i_d=[3])
488502
f(x,u,d,_) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
489503
h(x,d,_) = linmodel1.C*x + linmodel1.Du*d
@@ -541,6 +555,7 @@ end
541555
end
542556

543557
@testitem "UnscentedKalmanFilter estimator methods" setup=[SetupMPCtests] begin
558+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
544559
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
545560
f(x,u,_,_) = linmodel1.A*x + linmodel1.Bu*u
546561
h(x,_,_) = linmodel1.C*x
@@ -589,6 +604,7 @@ end
589604
end
590605

591606
@testitem "UnscentedKalmanFilter set model" setup=[SetupMPCtests] begin
607+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
592608
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
593609
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
594610
ukf1 = UnscentedKalmanFilter(linmodel, nint_ym=0)
@@ -625,6 +641,7 @@ end
625641
end
626642

627643
@testitem "ExtendedKalmanFilter construction" setup=[SetupMPCtests] begin
644+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
628645
linmodel1 = LinModel(sys,Ts,i_d=[3])
629646
f(x,u,d,_) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
630647
h(x,d,_) = linmodel1.C*x + linmodel1.Du*d
@@ -678,6 +695,7 @@ end
678695
end
679696

680697
@testitem "ExtendedKalmanFilter estimator methods" setup=[SetupMPCtests] begin
698+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
681699
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
682700
f(x,u,_,_) = linmodel1.A*x + linmodel1.Bu*u
683701
h(x,_,_) = linmodel1.C*x
@@ -726,6 +744,7 @@ end
726744
end
727745

728746
@testitem "ExtendedKalmanFilter set model" setup=[SetupMPCtests] begin
747+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
729748
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
730749
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
731750
ekf1 = ExtendedKalmanFilter(linmodel, nint_ym=0)
@@ -762,6 +781,7 @@ end
762781
end
763782

764783
@testitem "MovingHorizonEstimator construction" setup=[SetupMPCtests] begin
784+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt
765785
linmodel1 = LinModel(sys,Ts,i_d=[3])
766786
f(x,u,d,_) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
767787
h(x,d,_) = linmodel1.C*x + linmodel1.Du*d
@@ -843,6 +863,7 @@ end
843863
end
844864

845865
@testitem "MovingHorizonEstimator estimation and getinfo" setup=[SetupMPCtests] begin
866+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt, ForwardDiff
846867
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2], i_d=[3]), uop=[10,50], yop=[50,30], dop=[5])
847868
f(x,u,d,_) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
848869
h(x,d,_) = linmodel1.C*x + linmodel1.Dd*d
@@ -935,12 +956,12 @@ end
935956
preparestate!(mhe2, [50, 30], [5])
936957
updatestate!(mhe2, [11, 52], [50, 30], [5])
937958
end
938-
@test mhe2([5]) [50, 30] atol=1e-3
959+
@test mhe2([5]) [50, 30] atol=1e-2
939960
for i in 1:40
940961
preparestate!(mhe2, [51, 32], [5])
941962
updatestate!(mhe2, [10, 50], [51, 32], [5])
942963
end
943-
@test mhe2([5]) [51, 32] atol=1e-3
964+
@test mhe2([5]) [51, 32] atol=1e-2
944965
linmodel3 = LinModel{Float32}(0.5*ones(1,1), ones(1,1), ones(1,1), zeros(1,0), zeros(1,0), 1.0)
945966
mhe3 = MovingHorizonEstimator(linmodel3, He=1)
946967
preparestate!(mhe3, [0])
@@ -973,6 +994,7 @@ end
973994
end
974995

975996
@testitem "MovingHorizonEstimator fallbacks for arrival covariance estimation" setup=[SetupMPCtests] begin
997+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
976998
linmodel = setop!(LinModel(sys,Ts,i_u=[1,2], i_d=[3]), uop=[10,50], yop=[50,30], dop=[5])
977999
f(x,u,d,_) = linmodel.A*x + linmodel.Bu*u + linmodel.Bd*d
9781000
h(x,d,_) = linmodel.C*x + linmodel.Dd*d
@@ -1017,6 +1039,7 @@ end
10171039
end
10181040

10191041
@testitem "MovingHorizonEstimator set constraints" setup=[SetupMPCtests] begin
1042+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
10201043
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
10211044
mhe1 = MovingHorizonEstimator(linmodel1, He=1, nint_ym=0, Cwt=1e3)
10221045
setconstraint!(mhe1, x̂min=[-51,-52], x̂max=[53,54])
@@ -1098,6 +1121,7 @@ end
10981121
end
10991122

11001123
@testitem "MovingHorizonEstimator constraint violation" setup=[SetupMPCtests] begin
1124+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
11011125
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
11021126
mhe = MovingHorizonEstimator(linmodel1, He=1, nint_ym=0)
11031127

@@ -1196,6 +1220,7 @@ end
11961220
end
11971221

11981222
@testitem "MovingHorizonEstimator set model" setup=[SetupMPCtests] begin
1223+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
11991224
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
12001225
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
12011226
mhe = MovingHorizonEstimator(linmodel, He=1, nint_ym=0, direct=false)
@@ -1240,6 +1265,7 @@ end
12401265
end
12411266

12421267
@testitem "MovingHorizonEstimator v.s. Kalman filters" setup=[SetupMPCtests] begin
1268+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
12431269
linmodel1 = setop!(LinModel(sys,Ts,i_d=[3]), uop=[10,50], yop=[50,30], dop=[20])
12441270
mhe = MovingHorizonEstimator(linmodel1, He=3, nint_ym=0, direct=false)
12451271
kf = KalmanFilter(linmodel1, nint_ym=0, direct=false)
@@ -1316,6 +1342,7 @@ end
13161342
end
13171343

13181344
@testitem "MovingHorizonEstimator LinModel v.s. NonLinModel" setup=[SetupMPCtests] begin
1345+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt
13191346
linmodel = setop!(LinModel(sys,Ts,i_d=[3]), uop=[10,50], yop=[50,30], dop=[20])
13201347
f = (x,u,d,_) -> linmodel.A*x + linmodel.Bu*u + linmodel.Bd*d
13211348
h = (x,d,_) -> linmodel.C*x + linmodel.Dd*d

0 commit comments

Comments
 (0)