Skip to content

Commit 4b02782

Browse files
authored
Merge pull request #375 from JuliaControl/mhe_skf_dispatch
added: dispatch on `covestim` type when inverting `P̄` in the `MovingHorizonEstimator`
2 parents 52804d8 + 54b3bd5 commit 4b02782

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/estimator/mhe/execute.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function init_estimate_cov!(estim::MovingHorizonEstimator, y0m, d0, u0)
3030
nd > 0 && (estim.D0[1:nd] .= d0) # add d0(-1) to the data window
3131
estim.lastu0 .= u0
3232
# estim.cov.P̂_0 is P̂(-1|-1) if estim.direct==false, else P̂(-1|0)
33-
invert_cov!(estim, estim.cov.P̂_0)
3433
estim.P̂arr_old .= estim.cov.P̂_0
34+
invert_cov!(estim, estim.covestim)
3535
estim.x̂0arr_old .= 0
3636
return nothing
3737
end
@@ -709,7 +709,7 @@ function correct_cov!(estim::MovingHorizonEstimator)
709709
correct_estimate!(estim.covestim, y0marr, d0arr)
710710
all(isfinite, estim.covestim.cov.P̂) || error("Arrival covariance P̄ is not finite")
711711
estim.P̂arr_old .= estim.covestim.cov.
712-
invert_cov!(estim, estim.P̂arr_old)
712+
invert_cov!(estim, estim.covestim)
713713
catch err
714714
if err isa PosDefException
715715
@error("Arrival covariance P̄ is not positive definite: keeping the old one")
@@ -736,7 +736,7 @@ function update_cov!(estim::MovingHorizonEstimator)
736736
update_estimate!(estim.covestim, y0marr, d0arr, u0arr)
737737
all(isfinite, estim.covestim.cov.P̂) || error("Arrival covariance P̄ is not finite")
738738
estim.P̂arr_old .= estim.covestim.cov.
739-
invert_cov!(estim, estim.P̂arr_old)
739+
invert_cov!(estim, estim.covestim)
740740
catch err
741741
if err isa PosDefException
742742
@error("Arrival covariance P̄ is not positive definite: keeping the old one")
@@ -749,8 +749,9 @@ function update_cov!(estim::MovingHorizonEstimator)
749749
return nothing
750750
end
751751

752-
"Invert the covariance estimate at arrival `P̄`."
753-
function invert_cov!(estim::MovingHorizonEstimator, P̄)
752+
"Invert the covariance estimate at arrival `P̄` and store it in `estim.cov.invP̄`."
753+
function invert_cov!(estim::MovingHorizonEstimator, covestim::StateEstimator)
754+
= estim.P̂arr_old
754755
estim.cov.invP̄ .=
755756
try
756757
inv!(estim.cov.invP̄)
@@ -763,6 +764,9 @@ function invert_cov!(estim::MovingHorizonEstimator, P̄)
763764
end
764765
return nothing
765766
end
767+
"Do nothing if `covestim` is a [`SteadyKalmanFilter`]."
768+
invert_cov!(::MovingHorizonEstimator, ::SteadyKalmanFilter) = nothing
769+
766770

767771
"""
768772
obj_nonlinprog(estim::MovingHorizonEstimator, ::LinModel, _ , _ , _ , Z̃)

test/2_test_state_estim.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,17 @@ end
10111011
info = getinfo(mhe3)
10121012
@test info[:x̂] x̂ atol=1e-9
10131013
@test info[:Ŷ][end-1:end] [50, 30] atol=1e-9
1014+
covestim = SteadyKalmanFilter(linmodel)
1015+
mhe3b = MovingHorizonEstimator(linmodel; He=1, σP_0=nothing, covestim)
1016+
preparestate!(mhe3b, [50, 30], [5])
1017+
= updatestate!(mhe3b, [10, 50], [50, 30], [5])
1018+
@test zeros(6) atol=1e-9
1019+
@test mhe3b.x̂0 zeros(6) atol=1e-9
1020+
@test mhe3b.cov.invP̄ inv(covestim.cov.P̂)
1021+
preparestate!(mhe3b, [50, 30], [5])
1022+
info = getinfo(mhe3b)
1023+
@test info[:x̂] x̂ atol=1e-9
1024+
@test info[:Ŷ][end-1:end] [50, 30] atol=1e-9
10141025

10151026
linmodel3 = LinModel{Float32}(0.5*ones(1,1), ones(1,1), ones(1,1), zeros(1,0), zeros(1,0), 1.0)
10161027
mhe3 = MovingHorizonEstimator(linmodel3, He=1)
@@ -1181,7 +1192,7 @@ end
11811192
@test mhe.cov.invP̄ invP̄_copy
11821193
@test_logs(
11831194
(:error, "Arrival covariance P̄ is not invertible: keeping the old one"),
1184-
ModelPredictiveControl.invert_cov!(mhe, Hermitian(zeros(mhe.nx̂, mhe.nx̂),:L))
1195+
ModelPredictiveControl.invert_cov!(mhe, mhe.covestim)
11851196
)
11861197
mhe.P̂arr_old[1, 1] = Inf # Inf to trigger fallback
11871198
P̂arr_old_copy = deepcopy(mhe.P̂arr_old)

0 commit comments

Comments
 (0)