Skip to content

Commit ed2dd51

Browse files
committed
added: error throw if direct==true and preparestate! is not called
1 parent 67d924d commit ed2dd51

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

src/estimator/execute.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ function preparestate!(estim::StateEstimator, ym, d=estim.model.dop)
227227
if estim.direct
228228
validate_args(estim, ym, d)
229229
y0m, d0 = remove_op!(estim, ym, d)
230-
correct_estimate!(estim, y0m, d0) # compute x̂0corr
230+
correct_estimate!(estim, y0m, d0)
231+
estim.corrected[] = true
231232
end
232233
= estim.buffer.
233234
x̂ .= estim.x̂0 .+ estim.x̂op
@@ -258,9 +259,13 @@ julia> x̂ = updatestate!(kf, u, ym) # x̂[2] is the integrator state (nint_ym a
258259
```
259260
"""
260261
function updatestate!(estim::StateEstimator, u, ym, d=estim.buffer.empty)
262+
if estim.direct && !estim.corrected[]
263+
error("preparestate! must be called before updatestate! with direct=true option")
264+
end
261265
validate_args(estim, ym, d, u)
262266
y0m, d0, u0 = remove_op!(estim, ym, d, u)
263267
update_estimate!(estim, y0m, d0, u0)
268+
estim.corrected[] = false
264269
x̂next = estim.buffer.
265270
x̂next .= estim.x̂0 .+ estim.x̂op
266271
return x̂next

src/estimator/internal_model.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct InternalModel{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
2525
Âs::Matrix{NT}
2626
B̂s::Matrix{NT}
2727
direct::Bool
28+
corrected::Vector{Bool}
2829
buffer::StateEstimatorBuffer{NT}
2930
function InternalModel{NT, SM}(
3031
model::SM, i_ym, Asm, Bsm, Csm, Dsm
@@ -43,6 +44,7 @@ struct InternalModel{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
4344
x̂s = zeros(NT, nxs)
4445
y0m, d0 = zeros(NT, nym), zeros(NT, model.nd)
4546
direct = true # InternalModel always uses direct transmission from ym
47+
corrected = [false]
4648
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
4749
return new{NT, SM}(
4850
model,
@@ -52,7 +54,7 @@ struct InternalModel{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
5254
As, Bs, Cs, Ds,
5355
Â, B̂u, Ĉ, B̂d, D̂d,
5456
Âs, B̂s,
55-
direct,
57+
direct, corrected,
5658
buffer
5759
)
5860
end

src/estimator/kalman.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct SteadyKalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
2323
::Hermitian{NT, Matrix{NT}}
2424
::Matrix{NT}
2525
direct::Bool
26+
corrected::Vector{Bool}
2627
buffer::StateEstimatorBuffer{NT}
2728
function SteadyKalmanFilter{NT, SM}(
2829
model::SM, i_ym, nint_u, nint_ym, Q̂, R̂; direct=true
@@ -51,6 +52,7 @@ struct SteadyKalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
5152
lastu0 = zeros(NT, nu)
5253
x̂0 = [zeros(NT, model.nx); zeros(NT, nxs)]
5354
Q̂, R̂ = Hermitian(Q̂, :L), Hermitian(R̂, :L)
55+
corrected = [false]
5456
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
5557
return new{NT, SM}(
5658
model,
@@ -60,7 +62,7 @@ struct SteadyKalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
6062
Â, B̂u, Ĉ, B̂d, D̂d,
6163
Q̂, R̂,
6264
K̂,
63-
direct,
65+
direct, corrected,
6466
buffer
6567
)
6668
end
@@ -271,6 +273,7 @@ struct KalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
271273
::Matrix{NT}
272274
::Matrix{NT}
273275
direct::Bool
276+
corrected::Vector{Bool}
274277
buffer::StateEstimatorBuffer{NT}
275278
function KalmanFilter{NT, SM}(
276279
model::SM, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂; direct=true
@@ -288,6 +291,7 @@ struct KalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
288291
P̂_0 = Hermitian(P̂_0, :L)
289292
= copy(P̂_0)
290293
K̂, M̂ = zeros(NT, nx̂, nym), zeros(NT, nx̂, nym)
294+
corrected = [false]
291295
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
292296
return new{NT, SM}(
293297
model,
@@ -297,7 +301,7 @@ struct KalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
297301
Â, B̂u, Ĉ, B̂d, D̂d,
298302
P̂_0, Q̂, R̂,
299303
K̂, M̂,
300-
direct,
304+
direct, corrected,
301305
buffer
302306
)
303307
end
@@ -459,6 +463,7 @@ struct UnscentedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
459463
::Vector{NT}
460464
::Diagonal{NT, Vector{NT}}
461465
direct::Bool
466+
corrected::Vector{Bool}
462467
buffer::StateEstimatorBuffer{NT}
463468
function UnscentedKalmanFilter{NT, SM}(
464469
model::SM, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂, α, β, κ; direct=true
@@ -480,6 +485,7 @@ struct UnscentedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
480485
= Hermitian(zeros(NT, nym, nym), :L)
481486
X̂0, Ŷ0m = zeros(NT, nx̂, nσ), zeros(NT, nym, nσ)
482487
sqrtP̂ = LowerTriangular(zeros(NT, nx̂, nx̂))
488+
corrected = [false]
483489
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
484490
return new{NT, SM}(
485491
model,
@@ -490,7 +496,7 @@ struct UnscentedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
490496
P̂_0, Q̂, R̂,
491497
K̂, M̂, X̂0, Ŷ0m, sqrtP̂,
492498
nσ, γ, m̂, Ŝ,
493-
direct,
499+
direct, corrected,
494500
buffer
495501
)
496502
end
@@ -777,6 +783,7 @@ struct ExtendedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
777783
F̂_û::Matrix{NT}
778784
::Matrix{NT}
779785
direct::Bool
786+
corrected::Vector{Bool}
780787
buffer::StateEstimatorBuffer{NT}
781788
function ExtendedKalmanFilter{NT, SM}(
782789
model::SM, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂; direct=true
@@ -796,6 +803,7 @@ struct ExtendedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
796803
= copy(P̂_0)
797804
K̂, M̂ = zeros(NT, nx̂, nym), zeros(NT, nx̂, nym)
798805
F̂_û, Ĥ = zeros(NT, nx̂+nu, nx̂), zeros(NT, ny, nx̂)
806+
corrected = [false]
799807
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
800808
return new{NT, SM}(
801809
model,
@@ -806,7 +814,7 @@ struct ExtendedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
806814
P̂_0, Q̂, R̂,
807815
K̂, M̂,
808816
F̂_û, Ĥ,
809-
direct,
817+
direct, corrected,
810818
buffer
811819
)
812820
end
@@ -1007,7 +1015,7 @@ end
10071015
"""
10081016
update_estimate_kf!(estim::StateEstimator, y0m, d0, u0, Ĉm, Â)
10091017
1010-
Update time-varying/extended Kalman Filter estimates with augmented `` and `Ĉm` matrices.
1018+
Update time-varying/extended Kalman Filter estimates with augmented `Ĉm` and `` matrices.
10111019
10121020
Allows code reuse for [`KalmanFilter`](@ref), [`ExtendedKalmanFilterKalmanFilter`](@ref).
10131021
They update the state `x̂` and covariance `P̂` with the same equations. The extended filter

src/estimator/luenberger.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct Luenberger{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
2121
D̂d ::Matrix{NT}
2222
::Matrix{NT}
2323
direct::Bool
24+
corrected::Vector{Bool}
2425
buffer::StateEstimatorBuffer{NT}
2526
function Luenberger{NT, SM}(
2627
model, i_ym, nint_u, nint_ym, poles; direct=true
@@ -39,6 +40,7 @@ struct Luenberger{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
3940
end
4041
lastu0 = zeros(NT, nu)
4142
x̂0 = [zeros(NT, model.nx); zeros(NT, nxs)]
43+
corrected = [false]
4244
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
4345
return new{NT, SM}(
4446
model,
@@ -47,7 +49,7 @@ struct Luenberger{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
4749
As, Cs_u, Cs_y, nint_u, nint_ym,
4850
Â, B̂u, Ĉ, B̂d, D̂d,
4951
K̂,
50-
direct,
52+
direct, corrected,
5153
buffer
5254
)
5355
end

src/estimator/mhe/construct.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ struct MovingHorizonEstimator{
103103
P̂arr_old ::Hermitian{NT, Matrix{NT}}
104104
Nk::Vector{Int}
105105
direct::Bool
106+
corrected::Vector{Bool}
106107
buffer::StateEstimatorBuffer{NT}
107108
function MovingHorizonEstimator{NT, SM, JM, CE}(
108109
model::SM, He, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂, Cwt, optim::JM, covestim::CE;
@@ -144,6 +145,7 @@ struct MovingHorizonEstimator{
144145
P̂arr_old = copy(P̂_0)
145146
Nk = [0]
146147
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
148+
corrected = [false]
147149
estim = new{NT, SM, JM, CE}(
148150
model, optim, con, covestim,
149151
Z̃, lastu0, x̂op, f̂op, x̂0,
@@ -156,7 +158,7 @@ struct MovingHorizonEstimator{
156158
P̂_0, Q̂, R̂, invP̄, invQ̂_He, invR̂_He, Cwt,
157159
X̂op, X̂0, Y0m, U0, D0, Ŵ,
158160
x̂0arr_old, P̂arr_old, Nk,
159-
direct,
161+
direct, corrected,
160162
buffer
161163
)
162164
init_optimization!(estim, model, optim)

0 commit comments

Comments
 (0)