Skip to content

Commit bbbca90

Browse files
committed
buffer StateEstimatorwith more attributes
1 parent 62006e4 commit bbbca90

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

src/estimator/internal_model.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ struct InternalModel{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
2626
function InternalModel{NT, SM}(
2727
model::SM, i_ym, Asm, Bsm, Csm, Dsm
2828
) where {NT<:Real, SM<:SimModel}
29+
nu, ny, nd = model.nu, model.ny, model.nd
2930
nym, nyu = validate_ym(model, i_ym)
3031
validate_internalmodel(model, nym, Csm, Dsm)
3132
As, Bs, Cs, Ds = stoch_ym2y(model, i_ym, Asm, Bsm, Csm, Dsm)
3233
nxs = size(As,1)
3334
nx̂ = model.nx
3435
Â, B̂u, Ĉ, B̂d, D̂d, x̂op, f̂op = matrices_internalmodel(model)
3536
Âs, B̂s = init_internalmodel(As, Bs, Cs, Ds)
36-
lastu0 = zeros(NT, model.nu)
37+
lastu0 = zeros(NT, nu)
3738
# x̂0 and x̂d are same object (updating x̂d will update x̂0):
3839
x̂d = x̂0 = zeros(NT, model.nx)
3940
x̂s = zeros(NT, nxs)
40-
buffer = StateEstimatorBuffer{NT}(nx̂, nym)
41+
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
4142
return new{NT, SM}(
4243
model,
4344
lastu0, x̂op, f̂op, x̂0, x̂d, x̂s,

src/estimator/kalman.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct SteadyKalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
2626
function SteadyKalmanFilter{NT, SM}(
2727
model::SM, i_ym, nint_u, nint_ym, Q̂, R̂
2828
) where {NT<:Real, SM<:LinModel}
29+
nu, ny, nd = model.nu, model.ny, model.nd
2930
nym, nyu = validate_ym(model, i_ym)
3031
As, Cs_u, Cs_y, nint_u, nint_ym = init_estimstoch(model, i_ym, nint_u, nint_ym)
3132
nxs = size(As, 1)
@@ -34,7 +35,7 @@ struct SteadyKalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
3435
validate_kfcov(nym, nx̂, Q̂, R̂)
3536
= try
3637
Q̂_kalman = Matrix(Q̂) # Matrix() required for Julia 1.6
37-
R̂_kalman = zeros(NT, model.ny, model.ny)
38+
R̂_kalman = zeros(NT, ny, ny)
3839
R̂_kalman[i_ym, i_ym] =
3940
ControlSystemsBase.kalman(Discrete, Â, Ĉ, Q̂_kalman, R̂_kalman)[:, i_ym]
4041
catch my_error
@@ -46,10 +47,10 @@ struct SteadyKalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
4647
rethrow()
4748
end
4849
end
49-
lastu0 = zeros(NT, model.nu)
50+
lastu0 = zeros(NT, nu)
5051
x̂0 = [zeros(NT, model.nx); zeros(NT, nxs)]
5152
Q̂, R̂ = Hermitian(Q̂, :L), Hermitian(R̂, :L)
52-
buffer = StateEstimatorBuffer{NT}(nx̂, nym)
53+
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
5354
return new{NT, SM}(
5455
model,
5556
lastu0, x̂op, f̂op, x̂0,
@@ -241,19 +242,20 @@ struct KalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
241242
function KalmanFilter{NT, SM}(
242243
model::SM, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂
243244
) where {NT<:Real, SM<:LinModel}
245+
nu, ny, nd = model.nu, model.ny, model.nd
244246
nym, nyu = validate_ym(model, i_ym)
245247
As, Cs_u, Cs_y, nint_u, nint_ym = init_estimstoch(model, i_ym, nint_u, nint_ym)
246248
nxs = size(As, 1)
247249
nx̂ = model.nx + nxs
248250
Â, B̂u, Ĉ, B̂d, D̂d, x̂op, f̂op = augment_model(model, As, Cs_u, Cs_y)
249251
validate_kfcov(nym, nx̂, Q̂, R̂, P̂_0)
250-
lastu0 = zeros(NT, model.nu)
252+
lastu0 = zeros(NT, nu)
251253
x̂0 = [zeros(NT, model.nx); zeros(NT, nxs)]
252254
Q̂, R̂ = Hermitian(Q̂, :L), Hermitian(R̂, :L)
253255
P̂_0 = Hermitian(P̂_0, :L)
254256
= copy(P̂_0)
255257
K̂, M̂ = zeros(NT, nx̂, nym), zeros(NT, nx̂, nym)
256-
buffer = StateEstimatorBuffer{NT}(nx̂, nym)
258+
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
257259
return new{NT, SM}(
258260
model,
259261
lastu0, x̂op, f̂op, x̂0, P̂,
@@ -412,14 +414,15 @@ struct UnscentedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
412414
function UnscentedKalmanFilter{NT, SM}(
413415
model::SM, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂, α, β, κ
414416
) where {NT<:Real, SM<:SimModel{NT}}
417+
nu, ny, nd = model.nu, model.ny, model.nd
415418
nym, nyu = validate_ym(model, i_ym)
416419
As, Cs_u, Cs_y, nint_u, nint_ym = init_estimstoch(model, i_ym, nint_u, nint_ym)
417420
nxs = size(As, 1)
418421
nx̂ = model.nx + nxs
419422
Â, B̂u, Ĉ, B̂d, D̂d, x̂op, f̂op = augment_model(model, As, Cs_u, Cs_y)
420423
validate_kfcov(nym, nx̂, Q̂, R̂, P̂_0)
421424
nσ, γ, m̂, Ŝ = init_ukf(model, nx̂, α, β, κ)
422-
lastu0 = zeros(NT, model.nu)
425+
lastu0 = zeros(NT, nu)
423426
x̂0 = [zeros(NT, model.nx); zeros(NT, nxs)]
424427
Q̂, R̂ = Hermitian(Q̂, :L), Hermitian(R̂, :L)
425428
P̂_0 = Hermitian(P̂_0, :L)
@@ -428,7 +431,7 @@ struct UnscentedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
428431
= Hermitian(zeros(NT, nym, nym), :L)
429432
X̂0, Ŷ0m = zeros(NT, nx̂, nσ), zeros(NT, nym, nσ)
430433
sqrtP̂ = LowerTriangular(zeros(NT, nx̂, nx̂))
431-
buffer = StateEstimatorBuffer{NT}(nx̂, nym)
434+
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
432435
return new{NT, SM}(
433436
model,
434437
lastu0, x̂op, f̂op, x̂0, P̂,
@@ -711,21 +714,22 @@ struct ExtendedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
711714
function ExtendedKalmanFilter{NT, SM}(
712715
model::SM, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂
713716
) where {NT<:Real, SM<:SimModel}
717+
nu, ny, nd = model.nu, model.ny, model.nd
714718
nym, nyu = validate_ym(model, i_ym)
715719
As, Cs_u, Cs_y, nint_u, nint_ym = init_estimstoch(model, i_ym, nint_u, nint_ym)
716720
nxs = size(As, 1)
717721
nx̂ = model.nx + nxs
718722
Â, B̂u, Ĉ, B̂d, D̂d, x̂op, f̂op = augment_model(model, As, Cs_u, Cs_y)
719723
validate_kfcov(nym, nx̂, Q̂, R̂, P̂_0)
720-
lastu0 = zeros(NT, model.nu)
724+
lastu0 = zeros(NT, nu)
721725
x̂0 = [zeros(NT, model.nx); zeros(NT, nxs)]
722726
P̂_0 = Hermitian(P̂_0, :L)
723727
= Hermitian(Q̂, :L)
724728
= Hermitian(R̂, :L)
725729
= copy(P̂_0)
726730
K̂, M̂ = zeros(NT, nx̂, nym), zeros(NT, nx̂, nym)
727-
F̂_û, Ĥ = zeros(NT, nx̂+model.nu, nx̂), zeros(NT, model.ny, nx̂)
728-
buffer = StateEstimatorBuffer{NT}(nx̂, nym)
731+
F̂_û, Ĥ = zeros(NT, nx̂+nu, nx̂), zeros(NT, ny, nx̂)
732+
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
729733
return new{NT, SM}(
730734
model,
731735
lastu0, x̂op, f̂op, x̂0, P̂,

src/estimator/luenberger.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct Luenberger{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
2424
function Luenberger{NT, SM}(
2525
model, i_ym, nint_u, nint_ym, poles
2626
) where {NT<:Real, SM<:LinModel}
27+
nu, ny, nd = model.nu, model.ny, model.nd
2728
nym, nyu = validate_ym(model, i_ym)
2829
validate_luenberger(model, nint_u, nint_ym, poles)
2930
As, Cs_u, Cs_y, nint_u, nint_ym = init_estimstoch(model, i_ym, nint_u, nint_ym)
@@ -35,9 +36,9 @@ struct Luenberger{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
3536
catch
3637
error("Cannot compute the Luenberger gain K̂ with specified poles.")
3738
end
38-
lastu0 = zeros(NT, model.nu)
39+
lastu0 = zeros(NT, nu)
3940
x̂0 = [zeros(NT, model.nx); zeros(NT, nxs)]
40-
buffer = StateEstimatorBuffer{NT}(nx̂, nym)
41+
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
4142
return new{NT, SM}(
4243
model,
4344
lastu0, x̂op, f̂op, x̂0,

src/estimator/mhe/construct.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ struct MovingHorizonEstimator{
106106
function MovingHorizonEstimator{NT, SM, JM, CE}(
107107
model::SM, He, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂, Cwt, optim::JM, covestim::CE
108108
) where {NT<:Real, SM<:SimModel{NT}, JM<:JuMP.GenericModel, CE<:StateEstimator{NT}}
109-
nu, nd = model.nu, model.nd
109+
nu, ny, nd = model.nu, model.ny, model.nd
110110
He < 1 && throw(ArgumentError("Estimation horizon He should be ≥ 1"))
111111
Cwt < 0 && throw(ArgumentError("Cwt weight should be ≥ 0"))
112112
nym, nyu = validate_ym(model, i_ym)
@@ -115,7 +115,7 @@ struct MovingHorizonEstimator{
115115
nx̂ = model.nx + nxs
116116
Â, B̂u, Ĉ, B̂d, D̂d, x̂op, f̂op = augment_model(model, As, Cs_u, Cs_y)
117117
validate_kfcov(nym, nx̂, Q̂, R̂, P̂_0)
118-
lastu0 = zeros(NT, model.nu)
118+
lastu0 = zeros(NT, nu)
119119
x̂0 = [zeros(NT, model.nx); zeros(NT, nxs)]
120120
P̂_0 = Hermitian(P̂_0, :L)
121121
Q̂, R̂ = Hermitian(Q̂, :L), Hermitian(R̂, :L)
@@ -141,7 +141,7 @@ struct MovingHorizonEstimator{
141141
x̂0arr_old = zeros(NT, nx̂)
142142
P̂arr_old = copy(P̂_0)
143143
Nk = [0]
144-
buffer = StateEstimatorBuffer{NT}(nx̂, nym)
144+
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
145145
estim = new{NT, SM, JM, CE}(
146146
model, optim, con, covestim,
147147
Z̃, lastu0, x̂op, f̂op, x̂0,

src/state_estim.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ julia> ŷ = kf()
1919
abstract type StateEstimator{NT<:Real} end
2020

2121
struct StateEstimatorBuffer{NT<:Real}
22+
u ::Vector{NT}
2223
::Vector{NT}
2324
ym::Vector{NT}
25+
::Vector{NT}
26+
d ::Vector{NT}
27+
empty::Vector{NT}
2428
end
2529

2630
@doc raw"""
@@ -30,10 +34,16 @@ Create a buffer for `StateEstimator` objects for estimated states and measured o
3034
3135
The buffer is used to store intermediate results during simulation without allocating.
3236
"""
33-
function StateEstimatorBuffer{NT}(nx̂::Int, nym::Int) where NT <: Real
37+
function StateEstimatorBuffer{NT}(
38+
nu::Int, nx̂::Int, nym::Int, ny::Int, nd::Int
39+
) where NT <: Real
40+
u = Vector{NT}(undef, nu)
3441
= Vector{NT}(undef, nx̂)
3542
ym = Vector{NT}(undef, nym)
36-
return StateEstimatorBuffer{NT}(x̂, ym)
43+
= Vector{NT}(undef, ny)
44+
d = Vector{NT}(undef, nd)
45+
empty = Vector{NT}(undef, 0)
46+
return StateEstimatorBuffer{NT}(u, x̂, ym, ŷ, d, empty)
3747
end
3848

3949
const IntVectorOrInt = Union{Int, Vector{Int}}

0 commit comments

Comments
 (0)