112
112
@test x̂ ≈ [0 , 0 ]
113
113
@test isa (x̂, Vector{Float32})
114
114
@test_throws ArgumentError updatestate! (kalmanfilter1, [10 , 50 ])
115
- @test_throws ErrorException setstate! (kalmanfilter1, [1 ,2 ,3 ,4 ], diagm (.1 : .1 : .4 ))
116
115
end
117
116
118
117
@testitem " SteadyKalmanFilter set model" setup= [SetupMPCtests] begin
@@ -904,17 +903,26 @@ end
904
903
@test_throws ArgumentError MovingHorizonEstimator (linmodel)
905
904
@test_throws ArgumentError MovingHorizonEstimator (linmodel, He= 0 )
906
905
@test_throws ArgumentError MovingHorizonEstimator (linmodel, Cwt= - 1 )
906
+ @test_throws ErrorException MovingHorizonEstimator (
907
+ nonlinmodel, 5 , 1 : 2 , 0 , [1 , 1 ], I_6, I_6, I_2, Inf ; optim,
908
+ covestim = InternalModel (nonlinmodel)
909
+ )
910
+ @test_throws ArgumentError MovingHorizonEstimator (
911
+ nonlinmodel, 5 , 1 : 2 , 0 , [1 , 1 ], I_6, I_6, I_2, Inf ; optim,
912
+ covestim = UnscentedKalmanFilter (nonlinmodel, nint_ym= [2 ,2 ])
913
+ )
907
914
end
908
915
909
916
@testitem " MovingHorizonEstimator estimation and getinfo" setup= [SetupMPCtests] begin
910
- using . SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt, ForwardDiff
917
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra, ForwardDiff
918
+ using JuMP, Ipopt, DAQP
911
919
linmodel = LinModel (sys,Ts,i_u= [1 ,2 ], i_d= [3 ])
912
920
linmodel = setop! (linmodel, uop= [10 ,50 ], yop= [50 ,30 ], dop= [5 ])
913
921
f (x,u,d,model) = model. A* x + model. Bu* u + model. Bd* d
914
922
h (x,d,model) = model. C* x + model. Dd* d
915
923
nonlinmodel = NonLinModel (f, h, Ts, 2 , 4 , 2 , 1 , solver= nothing , p= linmodel)
916
924
nonlinmodel = setop! (nonlinmodel, uop= [10 ,50 ], yop= [50 ,30 ], dop= [5 ])
917
-
925
+
918
926
mhe1 = MovingHorizonEstimator (nonlinmodel, He= 2 )
919
927
JuMP. set_attribute (mhe1. optim, " tol" , 1e-7 )
920
928
preparestate! (mhe1, [50 , 30 ], [5 ])
950
958
x̂ = updatestate! (mhe1, [10 , 50 ], [50 , 30 ], [5 ])
951
959
@test x̂ ≈ zeros (6 ) atol= 1e-9
952
960
@test mhe1. x̂0 ≈ zeros (6 ) atol= 1e-9
953
- @test evaloutput (mhe1, [5 ]) ≈ mhe1 ([5 ]) ≈ [50 , 30 ]
954
961
info = getinfo (mhe1)
955
962
@test info[:x̂ ] ≈ x̂ atol= 1e-9
956
963
@test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
976
983
@test x̂ ≈ zeros (6 ) atol= 1e-9
977
984
@test mhe2. x̂0 ≈ zeros (6 ) atol= 1e-9
978
985
preparestate! (mhe2, [50 , 30 ], [5 ])
979
- @test evaloutput (mhe2, [5 ]) ≈ mhe2 ([5 ]) ≈ [50 , 30 ]
980
986
info = getinfo (mhe2)
981
987
@test info[:x̂ ] ≈ x̂ atol= 1e-9
982
988
@test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
998
1004
x̂ = updatestate! (mhe2, [10 , 50 ], [50 , 30 ], [5 ])
999
1005
@test x̂ ≈ zeros (6 ) atol= 1e-9
1000
1006
@test mhe2. x̂0 ≈ zeros (6 ) atol= 1e-9
1001
- @test evaloutput (mhe2, [5 ]) ≈ mhe2 ([5 ]) ≈ [50 , 30 ]
1002
1007
info = getinfo (mhe2)
1003
1008
@test info[:x̂ ] ≈ x̂ atol= 1e-9
1004
1009
@test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
@@ -1012,12 +1017,29 @@ end
1012
1017
updatestate! (mhe2, [10 , 50 ], [51 , 32 ], [5 ])
1013
1018
end
1014
1019
@test mhe2 ([5 ]) ≈ [51 , 32 ] atol= 1e-2
1020
+
1021
+ Q̂ = diagm ([1 / 4 , 1 / 4 , 1 / 4 , 1 / 4 ]. ^ 2 )
1022
+ R̂ = diagm ([1 , 1 ]. ^ 2 )
1023
+ optim = Model (DAQP. Optimizer)
1024
+ covestim = SteadyKalmanFilter (linmodel, 1 : 2 , 0 , 0 , Q̂, R̂)
1025
+ P̂_0 = covestim. cov. P̂
1026
+ mhe3 = MovingHorizonEstimator (linmodel, 2 , 1 : 2 , 0 , 0 , P̂_0, Q̂, R̂; optim, covestim)
1027
+ preparestate! (mhe3, [50 , 30 ], [5 ])
1028
+ x̂ = updatestate! (mhe3, [10 , 50 ], [50 , 30 ], [5 ])
1029
+ @test x̂ ≈ zeros (4 ) atol= 1e-9
1030
+ @test mhe3. x̂0 ≈ zeros (4 ) atol= 1e-9
1031
+ preparestate! (mhe3, [50 , 30 ], [5 ])
1032
+ info = getinfo (mhe3)
1033
+ @test info[:x̂ ] ≈ x̂ atol= 1e-9
1034
+ @test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
1035
+
1015
1036
linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
1016
1037
mhe3 = MovingHorizonEstimator (linmodel3, He= 1 )
1017
1038
preparestate! (mhe3, [0 ])
1018
1039
x̂ = updatestate! (mhe3, [0 ], [0 ])
1019
1040
@test x̂ ≈ [0 , 0 ] atol= 1e-3
1020
1041
@test isa (x̂, Vector{Float32})
1042
+
1021
1043
mhe4 = setconstraint! (MovingHorizonEstimator (nonlinmodel, He= 1 , nint_ym= 0 ), v̂max= [50 ,50 ])
1022
1044
g_V̂max_end = mhe4. optim[:g_V̂max_2 ]. func
1023
1045
# execute update_predictions! branch in `gfunc_i` for coverage:
@@ -1027,7 +1049,7 @@ end
1027
1049
R̂ = diagm ([1 , 1 ]. ^ 2 )
1028
1050
optim = Model (Ipopt. Optimizer)
1029
1051
covestim = ExtendedKalmanFilter (nonlinmodel, 1 : 2 , 0 , 0 , Q̂, Q̂, R̂)
1030
- mhe5 = MovingHorizonEstimator (nonlinmodel, 1 , 1 : 2 , 0 , 0 , Q̂, Q̂, R̂, Inf ; optim, covestim)
1052
+ mhe5 = MovingHorizonEstimator (nonlinmodel, 1 , 1 : 2 , 0 , 0 , Q̂, Q̂, R̂; optim, covestim)
1031
1053
preparestate! (mhe5, [50 , 30 ], [5 ])
1032
1054
x̂ = updatestate! (mhe5, [10 , 50 ], [50 , 30 ], [5 ])
1033
1055
@test x̂ ≈ zeros (4 ) atol= 1e-9
0 commit comments