Skip to content

Commit 0113c47

Browse files
committed
changed: two linearization functions for ExtendedKalmanFilter
1 parent 112b2e9 commit 0113c47

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

src/estimator/kalman.jl

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,8 @@ struct ExtendedKalmanFilter{
877877
SM<:SimModel,
878878
KC<:KalmanCovariances,
879879
JB<:AbstractADType,
880-
LF<:Function
880+
FF<:Function,
881+
HF<:Function
881882
} <: StateEstimator{NT}
882883
model::SM
883884
cov ::KC
@@ -907,13 +908,23 @@ struct ExtendedKalmanFilter{
907908
::Matrix{NT}
908909
Ĥm ::Matrix{NT}
909910
jacobian::JB
910-
linfunc!::LF
911+
linfuncF̂!::FF
912+
linfuncĤ!::HF
911913
direct::Bool
912914
corrected::Vector{Bool}
913915
buffer::StateEstimatorBuffer{NT}
914916
function ExtendedKalmanFilter{NT}(
915-
model::SM, i_ym, nint_u, nint_ym, cov::KC; jacobian::JB, linfunc!::LF, direct=true
916-
) where {NT<:Real, SM<:SimModel, KC<:KalmanCovariances, JB<:AbstractADType, LF<:Function}
917+
model::SM,
918+
i_ym, nint_u, nint_ym, cov::KC;
919+
jacobian::JB, linfuncF̂!::FF, linfuncĤ!::HF, direct=true
920+
) where {
921+
NT<:Real,
922+
SM<:SimModel,
923+
KC<:KalmanCovariances,
924+
JB<:AbstractADType,
925+
FF<:Function,
926+
HF<:Function
927+
}
917928
nu, ny, nd, nk = model.nu, model.ny, model.nd, model.nk
918929
nym, nyu = validate_ym(model, i_ym)
919930
As, Cs_u, Cs_y, nint_u, nint_ym = init_estimstoch(model, i_ym, nint_u, nint_ym)
@@ -927,7 +938,7 @@ struct ExtendedKalmanFilter{
927938
Ĥ, Ĥm = zeros(NT, ny, nx̂), zeros(NT, nym, nx̂)
928939
corrected = [false]
929940
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd, nk)
930-
return new{NT, SM, KC, JB, LF}(
941+
return new{NT, SM, KC, JB, FF, HF}(
931942
model,
932943
cov,
933944
x̂op, f̂op, x̂0,
@@ -936,7 +947,7 @@ struct ExtendedKalmanFilter{
936947
Â, B̂u, Ĉ, B̂d, D̂d, Ĉm, D̂dm,
937948
K̂,
938949
F̂_û, F̂, Ĥ, Ĥm,
939-
jacobian, linfunc!,
950+
jacobian, linfuncF̂!, linfuncĤ!,
940951
direct, corrected,
941952
buffer
942953
)
@@ -1044,59 +1055,54 @@ function ExtendedKalmanFilter(
10441055
) where {NT<:Real, SM<:SimModel{NT}}
10451056
P̂_0, Q̂, R̂ = to_mat(P̂_0), to_mat(Q̂), to_mat(R̂)
10461057
cov = KalmanCovariances(model, i_ym, nint_u, nint_ym, Q̂, R̂, P̂_0)
1047-
linfunc! = get_ekf_linfunc(NT, model, i_ym, nint_u, nint_ym, jacobian)
1058+
linfuncF̂!, linfuncĤ! = get_ekf_linfuncs(NT, model, i_ym, nint_u, nint_ym, jacobian)
10481059
return ExtendedKalmanFilter{NT}(
1049-
model, i_ym, nint_u, nint_ym, cov; jacobian, direct, linfunc!
1060+
model, i_ym, nint_u, nint_ym, cov; jacobian, linfuncF̂!, linfuncĤ!, direct
10501061
)
10511062
end
10521063

10531064
"""
1054-
get_ekf_linfunc(NT, model, i_ym, nint_u, nint_ym, jacobian) -> linfunc!
1065+
get_ekf_linfuncs(NT, model, i_ym, nint_u, nint_ym, jacobian) -> linfuncF̂!, linfuncĤ!
10551066
1056-
Return the `linfunc!` function that computes the Jacobians of the augmented model.
1067+
Return the functions that computes the `F̂` and `Ĥ` Jacobians of the augmented model.
10571068
1058-
The function has the two following methods:
1069+
The functions has the following signatures:
10591070
```
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
1071+
linfuncF̂!(F̂, x̂0next , backend, x̂0, cst_u0, cst_d0) -> nothing
1072+
linfuncĤ!(Ĥ, ŷ0 , backend, x̂0, cst_d0) -> nothing
10621073
```
1063-
To respectively compute only `F̂` or `Ĥ` Jacobian. The methods mutate all the arguments
1064-
before `backend` argument. The `backend` argument is an `AbstractADType` object from
1074+
They mutates all the arguments before `backend`, which is an `AbstractADType` object from
10651075
`DifferentiationInterface`. The `cst_u0` and `cst_d0` are `DifferentiationInterface.Constant`
10661076
objects with the linearization points.
10671077
"""
1068-
function get_ekf_linfunc(NT, model, i_ym, nint_u, nint_ym, jacobian)
1078+
function get_ekf_linfuncs(NT, model, i_ym, nint_u, nint_ym, jacobian)
10691079
As, Cs_u, Cs_y = init_estimstoch(model, i_ym, nint_u, nint_ym)
10701080
nu, ny, nd, nk = model.nu, model.ny, model.nd, model.nk
10711081
nx̂ = model.nx + size(As, 1)
1072-
x̂op = f̂op = zeros(nx̂) # not important for Jacobians
1073-
f̂_ekf!(x̂0next, x̂0, û0, k0, u0, d0) = f̂!(
1074-
x̂0next, û0, k0, model, As, Cs_u, f̂op, x̂op, x̂0, u0, d0
1075-
)
1076-
ĥ_ekf!(ŷ0, x̂0, d0) = ĥ!(
1077-
ŷ0, model, Cs_y, x̂0, d0
1078-
)
1082+
x̂op = f̂op = zeros(nx̂) # not important for Jacobian computations
1083+
function f̂_ekf!(x̂0next, x̂0, û0, k0, u0, d0)
1084+
return f̂!(x̂0next, û0, k0, model, As, Cs_u, f̂op, x̂op, x̂0, u0, d0)
1085+
end
1086+
ĥ_ekf!(ŷ0, x̂0, d0) = ĥ!(ŷ0, model, Cs_y, x̂0, d0)
10791087
strict = Val(true)
10801088
x̂0next = zeros(NT, nx̂)
10811089
ŷ0 = zeros(NT, ny)
10821090
x̂0 = zeros(NT, nx̂)
1083-
tmp_û0 = Cache(zeros(NT, nu))
1084-
tmp_k0 = Cache(zeros(NT, nk))
1091+
û0 = Cache(zeros(NT, nu))
1092+
k0 = Cache(zeros(NT, nk))
10851093
cst_u0 = Constant(zeros(NT, nu))
10861094
cst_d0 = Constant(zeros(NT, nd))
1087-
F̂_prep = prepare_jacobian(
1088-
f̂_ekf!, x̂0next, jacobian, x̂0, tmp_û0, tmp_k0, cst_u0, cst_d0; strict
1095+
F̂prep = prepare_jacobian(
1096+
f̂_ekf!, x̂0next, jacobian, x̂0, û0, k0, cst_u0, cst_d0; strict
10891097
)
1090-
Ĥ_prep = prepare_jacobian(ĥ_ekf!, ŷ0, jacobian, x̂0, cst_d0; strict)
1091-
function linfunc!(x̂0next, ::Nothing, F̂, ::Nothing, backend, x̂0, cst_u0, cst_d0)
1092-
return jacobian!(
1093-
f̂_ekf!, x̂0next, F̂, F̂_prep, backend, x̂0, tmp_û0, tmp_k0, cst_u0, cst_d0
1094-
)
1098+
Ĥprep = prepare_jacobian(ĥ_ekf!, ŷ0, jacobian, x̂0, cst_d0; strict)
1099+
function linfuncF̂!(F̂, x̂0next, backend, x̂0, cst_u0, cst_d0)
1100+
return jacobian!(f̂_ekf!, x̂0next, F̂, F̂prep, backend, x̂0, û0, k0, cst_u0, cst_d0)
10951101
end
1096-
function linfunc!(::Nothing, ŷ0, ::Nothing, Ĥ, backend, x̂0, ::Nothing , cst_d0)
1097-
return jacobian!(ĥ_ekf!, ŷ0, Ĥ, Ĥ_prep, backend, x̂0, cst_d0)
1102+
function linfuncĤ!(Ĥ, ŷ0, backend, x̂0, cst_d0)
1103+
return jacobian!(ĥ_ekf!, ŷ0, Ĥ, Ĥprep, backend, x̂0, cst_d0)
10981104
end
1099-
return linfunc!
1105+
return linfuncF̂!, linfuncĤ!
11001106
end
11011107

11021108
"""
@@ -1108,7 +1114,7 @@ function correct_estimate!(estim::ExtendedKalmanFilter, y0m, d0)
11081114
x̂0 = estim.x̂0
11091115
cst_d0 = Constant(d0)
11101116
ŷ0, Ĥ, Ĥm = estim.buffer.ŷ, estim.Ĥ, estim.Ĥm
1111-
estim.linfunc!(nothing, ŷ0, nothing, Ĥ, estim.jacobian, x̂0, nothing, cst_d0)
1117+
estim.linfuncĤ!(Ĥ, ŷ0, estim.jacobian, x̂0, cst_d0)
11121118
Ĥm .= @views Ĥ[estim.i_ym, :]
11131119
return correct_estimate_kf!(estim, y0m, d0, Ĥm)
11141120
end
@@ -1159,7 +1165,7 @@ function update_estimate!(estim::ExtendedKalmanFilter{NT}, y0m, d0, u0) where NT
11591165
cst_u0, cst_d0 = Constant(u0), Constant(d0)
11601166
x̂0corr = estim.x̂0
11611167
x̂0next, F̂ = estim.buffer.x̂, estim.
1162-
estim.linfunc!(x̂0next, nothing, F̂, nothing, estim.jacobian, x̂0corr, cst_u0, cst_d0)
1168+
estim.linfuncF̂!(F̂, x̂0next, estim.jacobian, x̂0corr, cst_u0, cst_d0)
11631169
return predict_estimate_kf!(estim, u0, d0, F̂)
11641170
end
11651171

0 commit comments

Comments
 (0)