Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug: fallback if nonfinite arr. cov. after correction/update #156

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/estimator/mhe/execute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,14 @@ function correct_cov!(estim::MovingHorizonEstimator)
estim.covestim.P̂ .= estim.P̂arr_old
try
correct_estimate!(estim.covestim, y0marr, d0arr)
all(isfinite, estim.covestim.P̂) || error("Arrival covariance P̄ is not finite")
estim.P̂arr_old .= estim.covestim.P̂
invert_cov!(estim, estim.P̂arr_old)
catch err
if err isa PosDefException
@error("Arrival covariance P̄ is not positive definite: keeping the old one")
elseif err isa ErrorException
@error("Arrival covariance P̄ is not finite: keeping the old one")
else
rethrow()
end
Expand All @@ -461,11 +464,14 @@ function update_cov!(estim::MovingHorizonEstimator)
estim.covestim.P̂ .= estim.P̂arr_old
try
update_estimate!(estim.covestim, y0marr, d0arr, u0arr)
all(isfinite, estim.covestim.P̂) || error("Arrival covariance P̄ is not finite")
estim.P̂arr_old .= estim.covestim.P̂
invert_cov!(estim, estim.P̂arr_old)
catch err
if err isa PosDefException
@error("Arrival covariance P̄ is not positive definite: keeping the old one")
elseif err isa ErrorException
@error("Arrival covariance P̄ is not finite: keeping the old one")
else
rethrow()
end
Expand Down
15 changes: 15 additions & 0 deletions test/test_state_estim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,21 @@ end
(:error, "Arrival covariance P̄ is not invertible: keeping the old one"),
ModelPredictiveControl.invert_cov!(mhe, Hermitian(zeros(mhe.nx̂, mhe.nx̂),:L))
)
mhe.P̂arr_old[1, 1] = Inf # Inf to trigger fallback
P̂arr_old_copy = deepcopy(mhe.P̂arr_old)
invP̄_copy = deepcopy(mhe.invP̄)
@test_logs(
(:error, "Arrival covariance P̄ is not finite: keeping the old one"),
preparestate!(mhe, [50, 30], [5])
)
@test mhe.P̂arr_old ≈ P̂arr_old_copy
@test mhe.invP̄ ≈ invP̄_copy
@test_logs(
(:error, "Arrival covariance P̄ is not finite: keeping the old one"),
updatestate!(mhe, [10, 50], [50, 30], [5])
)
@test mhe.P̂arr_old ≈ P̂arr_old_copy
@test mhe.invP̄ ≈ invP̄_copy
end

@testset "MovingHorizonEstimator set constraints" begin
Expand Down
Loading