Skip to content

Commit 3081143

Browse files
committed
debug: use correct state estimate for MHE cov update
1 parent e53863f commit 3081143

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/estimator/kalman.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,21 @@ function update_estimate!(estim::UnscentedKalmanFilter{NT}, u, ym, d) where NT<:
568568
x̂, P̂, Q̂, R̂, K̂ = estim.x̂, estim.P̂, estim.Q̂, estim.R̂, estim.
569569
nym, nx̂, nσ = estim.nym, estim.nx̂, estim.
570570
γ, m̂, Ŝ = estim.γ, estim.m̂, estim.
571+
return update_estimate_ukf!(estim, u, ym, d, P̂, x̂)
572+
end
573+
574+
"""
575+
update_estimate_ukf!(estim::StateEstimator, u, ym, d, P̂, x̂=nothing)
576+
577+
Update Unscented Kalman Filter estimates and covariance matrices.
578+
579+
Allows code reuse for [`UnscentedKalmanFilter`](@ref) and [`MovingHorizonEstimator`](@ref).
580+
See [`update_estimate!(::UnscentedKalmanFilter, ::Any, ::Any, ::Any)`(@ref) docstring
581+
for the equations. If `isnothing(x̂)`, only the covariance `P̂` is updated.
582+
"""
583+
function update_estimate_ukf!(
584+
estim::StateEstimator{NT}, u, ym, d, P̂, x̂=nothing
585+
) where NT<:Real
571586
# --- initialize matrices ---
572587
X̂, X̂_next = Matrix{NT}(undef, nx̂, nσ), Matrix{NT}(undef, nx̂, nσ)
573588
ŷm = Vector{NT}(undef, nym)
@@ -802,7 +817,7 @@ end
802817
803818
Update time-varying/extended Kalman Filter estimates with augmented `Â` and `Ĉm` matrices.
804819
805-
Allows code reuse for [`KalmanFilter`](@ref) and [`ExtendedKalmanFilterKalmanFilter`](@ref).
820+
Allows code reuse for [`KalmanFilter`](@ref), [`ExtendedKalmanFilterKalmanFilter`](@ref).
806821
They update the state `x̂` and covariance `P̂` with the same equations. The extended filter
807822
substitutes the augmented model matrices with its Jacobians (`Â = F̂` and `Ĉm = Ĥm`).
808823
The implementation uses in-place operations and explicit factorization to reduce

src/estimator/mhe/execute.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function update_estimate!(estim::MovingHorizonEstimator{NT}, u, ym, d) where NT<
8080
x̂ .= X̂[end-nx̂+1:end]
8181
if Nk == estim.He
8282
uarr, ymarr, darr = estim.U[1:model.nu], estim.Ym[1:nym], estim.D[1:model.nd]
83-
update_cov!(estim.P̂arr_old, estim, model, uarr, ymarr, darr)
83+
update_cov!(estim, model, estim.x̂arr_old, estim.P̂arr_old, uarr, ymarr, darr)
8484
estim.invP̄.data .= Hermitian(inv(estim.P̂arr_old), :L)
8585
end
8686
return nothing
@@ -324,16 +324,16 @@ function trunc_bounds(estim::MovingHorizonEstimator{NT}, Bmin, Bmax, n) where NT
324324
end
325325

326326
"Update the covariance `P̂` with the `KalmanFilter` if `model` is a `LinModel`."
327-
function update_cov!(P̂, estim::MovingHorizonEstimator, ::LinModel, u, ym, d)
327+
function update_cov!(estim::MovingHorizonEstimator, ::LinModel, _ , P̂, u, ym, d)
328328
return update_estimate_kf!(estim, u, ym, d, estim.Â, estim.Ĉ[estim.i_ym, :], P̂)
329329
end
330330
"Update it with the `ExtendedKalmanFilter` if model is not a `LinModel`."
331-
function update_cov!(P̂, estim::MovingHorizonEstimator, model::SimModel, u, ym, d)
331+
function update_cov!(estim::MovingHorizonEstimator, model::SimModel, x̂, P̂, u, ym, d)
332332
# TODO: also support UnscentedKalmanFilter
333333
x̂next, ŷ = similar(estim.x̂), similar(model.yop)
334-
= ForwardDiff.jacobian((x̂next, x̂) -> f̂!(x̂next, estim, estim.model, x̂, u, d), x̂next, estim.x̂)
335-
= ForwardDiff.jacobian((ŷ, x̂) -> ĥ!(ŷ, estim, estim.model, x̂, d), ŷ, estim.x̂)
336-
return update_estimate_kf!(estim, u, ym, d, F̂, Ĥ[estim.i_ym, :], P̂)
334+
= ForwardDiff.jacobian((x̂next, x̂) -> f̂!(x̂next, estim, estim.model, x̂, u, d), x̂next, x̂)
335+
= ForwardDiff.jacobian((ŷ, x̂) -> ĥ!(ŷ, estim, estim.model, x̂, d), ŷ, x̂)
336+
return update_estimate_kf!(estim, u, ym, d, F̂, Ĥ[estim.i_ym, :], P̂)
337337
end
338338

339339

0 commit comments

Comments
 (0)