Skip to content

Commit 112b2e9

Browse files
committed
cleanup in ExtendedKalmanFilter code
1 parent 5f3a969 commit 112b2e9

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/estimator/kalman.jl

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,8 +1057,8 @@ Return the `linfunc!` function that computes the Jacobians of the augmented mode
10571057
10581058
The function has the two following methods:
10591059
```
1060-
linfunc!(x̂0next , ::Nothing, F̂ , ::Nothing, backend, x̂0, cst_u0, cst_d0) -> nothing
1061-
linfunc!(::Nothing, ŷ0 , ::Nothing, Ĥ , backend, x̂0, _ , cst_d0) -> nothing
1060+
linfunc!(x̂0next , ::Nothing, F̂ , ::Nothing, backend, x̂0, cst_u0 , cst_d0) -> nothing
1061+
linfunc!(::Nothing, ŷ0 , ::Nothing, Ĥ , backend, x̂0, ::Nothing , cst_d0) -> nothing
10621062
```
10631063
To respectively compute only `F̂` or `Ĥ` Jacobian. The methods mutate all the arguments
10641064
before `backend` argument. The `backend` argument is an `AbstractADType` object from
@@ -1067,34 +1067,33 @@ objects with the linearization points.
10671067
"""
10681068
function get_ekf_linfunc(NT, model, i_ym, nint_u, nint_ym, jacobian)
10691069
As, Cs_u, Cs_y = init_estimstoch(model, i_ym, nint_u, nint_ym)
1070-
nxs = size(As, 1)
1071-
x̂op, f̂op = [model.xop; zeros(nxs)], [model.fop; zeros(nxs)]
1070+
nu, ny, nd, nk = model.nu, model.ny, model.nd, model.nk
1071+
nx̂ = model.nx + size(As, 1)
1072+
x̂op = f̂op = zeros(nx̂) # not important for Jacobians
10721073
f̂_ekf!(x̂0next, x̂0, û0, k0, u0, d0) = f̂!(
10731074
x̂0next, û0, k0, model, As, Cs_u, f̂op, x̂op, x̂0, u0, d0
10741075
)
10751076
ĥ_ekf!(ŷ0, x̂0, d0) = ĥ!(
10761077
ŷ0, model, Cs_y, x̂0, d0
10771078
)
10781079
strict = Val(true)
1079-
nu, ny, nd, nk = model.nu, model.ny, model.nd, model.nk
1080-
nx̂ = model.nx + nxs
10811080
x̂0next = zeros(NT, nx̂)
10821081
ŷ0 = zeros(NT, ny)
10831082
x̂0 = zeros(NT, nx̂)
1084-
tmp_û0 = Cache(zeros(NT, nu))
1085-
tmp_x0i = Cache(zeros(NT, nk))
1083+
tmp_û0 = Cache(zeros(NT, nu))
1084+
tmp_k0 = Cache(zeros(NT, nk))
10861085
cst_u0 = Constant(zeros(NT, nu))
10871086
cst_d0 = Constant(zeros(NT, nd))
10881087
F̂_prep = prepare_jacobian(
1089-
f̂_ekf!, x̂0next, jacobian, x̂0, tmp_û0, tmp_x0i, cst_u0, cst_d0; strict
1088+
f̂_ekf!, x̂0next, jacobian, x̂0, tmp_û0, tmp_k0, cst_u0, cst_d0; strict
10901089
)
10911090
Ĥ_prep = prepare_jacobian(ĥ_ekf!, ŷ0, jacobian, x̂0, cst_d0; strict)
1092-
function linfunc!(x̂0next, ŷ0::Nothing, F̂, ::Nothing, backend, x̂0, cst_u0, cst_d0)
1091+
function linfunc!(x̂0next, ::Nothing, F̂, ::Nothing, backend, x̂0, cst_u0, cst_d0)
10931092
return jacobian!(
1094-
f̂_ekf!, x̂0next, F̂, F̂_prep, backend, x̂0, tmp_û0, tmp_x0i, cst_u0, cst_d0
1093+
f̂_ekf!, x̂0next, F̂, F̂_prep, backend, x̂0, tmp_û0, tmp_k0, cst_u0, cst_d0
10951094
)
10961095
end
1097-
function linfunc!(x̂0next::Nothing, ŷ0, ::Nothing, Ĥ, backend, x̂0, _ , cst_d0)
1096+
function linfunc!(::Nothing, ŷ0, ::Nothing, Ĥ, backend, x̂0, ::Nothing , cst_d0)
10981097
return jacobian!(ĥ_ekf!, ŷ0, Ĥ, Ĥ_prep, backend, x̂0, cst_d0)
10991098
end
11001099
return linfunc!
@@ -1106,12 +1105,12 @@ end
11061105
Do the same but for the [`ExtendedKalmanFilter`](@ref).
11071106
"""
11081107
function correct_estimate!(estim::ExtendedKalmanFilter, y0m, d0)
1109-
model, x̂0 = estim.model, estim.x̂0
1108+
x̂0 = estim.x̂0
11101109
cst_d0 = Constant(d0)
1111-
ŷ0, Ĥ = estim.buffer.ŷ, estim.
1110+
ŷ0, Ĥ, Ĥm = estim.buffer.ŷ, estim., estim.Ĥm
11121111
estim.linfunc!(nothing, ŷ0, nothing, Ĥ, estim.jacobian, x̂0, nothing, cst_d0)
1113-
estim.Ĥm .= @views estim.Ĥ[estim.i_ym, :]
1114-
return correct_estimate_kf!(estim, y0m, d0, estim.Ĥm)
1112+
Ĥm .= @views Ĥ[estim.i_ym, :]
1113+
return correct_estimate_kf!(estim, y0m, d0, Ĥm)
11151114
end
11161115

11171116

@@ -1154,19 +1153,14 @@ and prediction step equations are provided below. The correction step is skipped
11541153
```
11551154
"""
11561155
function update_estimate!(estim::ExtendedKalmanFilter{NT}, y0m, d0, u0) where NT<:Real
1157-
model, x̂0 = estim.model, estim.x̂0
1158-
nx̂, nu = estim.nx̂, model.nu
1159-
cst_u0, cst_d0 = Constant(u0), Constant(d0)
11601156
if !estim.direct
1161-
ŷ0, Ĥ = estim.buffer.ŷ, estim.
1162-
estim.linfunc!(nothing, ŷ0, nothing, Ĥ, estim.jacobian, x̂0, nothing, cst_d0)
1163-
estim.Ĥm .= @views estim.Ĥ[estim.i_ym, :]
1164-
correct_estimate_kf!(estim, y0m, d0, estim.Ĥm)
1157+
correct_estimate!(estim, y0m, d0)
11651158
end
1159+
cst_u0, cst_d0 = Constant(u0), Constant(d0)
11661160
x̂0corr = estim.x̂0
11671161
x̂0next, F̂ = estim.buffer.x̂, estim.
11681162
estim.linfunc!(x̂0next, nothing, F̂, nothing, estim.jacobian, x̂0corr, cst_u0, cst_d0)
1169-
return predict_estimate_kf!(estim, u0, d0, estim.F̂)
1163+
return predict_estimate_kf!(estim, u0, d0, F̂)
11701164
end
11711165

11721166
"Set `estim.cov.P̂` to `estim.cov.P̂_0` for the time-varying Kalman Filters."

0 commit comments

Comments
 (0)