Skip to content

Commit 53395cc

Browse files
committed
added: ExtendedKalmanFilter with in-place jacobian!
1 parent 94fdf4f commit 53395cc

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/estimator/kalman.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,8 @@ struct ExtendedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
665665
::Hermitian{NT, Matrix{NT}}
666666
::Matrix{NT}
667667
::Matrix{NT}
668+
F̂_û::Matrix{NT}
669+
::Matrix{NT}
668670
function ExtendedKalmanFilter{NT, SM}(
669671
model::SM, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂
670672
) where {NT<:Real, SM<:SimModel}
@@ -681,14 +683,16 @@ struct ExtendedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
681683
= Hermitian(R̂, :L)
682684
= copy(P̂_0)
683685
K̂, M̂ = zeros(NT, nx̂, nym), zeros(NT, nx̂, nym)
686+
F̂_û, Ĥ = zeros(NT, nx̂+model.nu, nx̂), zeros(NT, model.ny, nx̂)
684687
return new{NT, SM}(
685688
model,
686689
lastu0, x̂op, f̂op, x̂0, P̂,
687690
i_ym, nx̂, nym, nyu, nxs,
688691
As, Cs_u, Cs_y, nint_u, nint_ym,
689692
Â, B̂u, Ĉ, B̂d, D̂d,
690693
P̂_0, Q̂, R̂,
691-
K̂, M̂
694+
K̂, M̂,
695+
F̂_û, Ĥ
692696
)
693697
end
694698
end
@@ -798,8 +802,10 @@ function update_estimate!(
798802
x̂nextû, ŷ = Vector{NT}(undef, nx̂ + nu), Vector{NT}(undef, ny)
799803
f̂AD! = (x̂nextû, x̂) -> @views f̂!(x̂nextû[1:nx̂], x̂nextû[nx̂+1:end], estim, model, x̂, u, d)
800804
ĥAD! = (ŷ, x̂) -> ĥ!(ŷ, estim, model, x̂, d)
801-
= ForwardDiff.jacobian(f̂AD!, x̂nextû, x̂)[1:nx̂, :]
802-
Ĥm = ForwardDiff.jacobian(ĥAD!, ŷ, x̂)[estim.i_ym, :]
805+
ForwardDiff.jacobian!(estim.F̂_û, f̂AD!, x̂nextû, x̂)
806+
ForwardDiff.jacobian!(estim.Ĥ, ĥAD!, ŷ, x̂)
807+
= @views estim.F̂_û[1:nx̂, :]
808+
Ĥm = @views estim.Ĥ[estim.i_ym, :]
803809
return update_estimate_kf!(estim, u, ym, d, F̂, Ĥm, P̂, x̂)
804810
end
805811

0 commit comments

Comments
 (0)