Skip to content

Commit f317a13

Browse files
committed
debug MHE
1 parent 5f86a30 commit f317a13

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

src/estimator/internal_model.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,22 @@ function init_estimate!(estim::InternalModel, model::LinModel{NT}, u, ym, d) whe
261261
return nothing
262262
end
263263

264+
@doc raw"""
265+
evalŷ(estim::InternalModel, ym, d) -> ŷ
266+
267+
Get [`InternalModel`](@ref) output `ŷ` from current measured outputs `ym` and dist. `d`.
268+
269+
[`InternalModel`](@ref) estimator needs current measured outputs ``\mathbf{y^m}(k)`` to
270+
estimate its outputs ``\mathbf{ŷ}(k)``, since the strategy imposes that
271+
``\mathbf{ŷ^m}(k) = \mathbf{y^m}(k)`` is always true.
272+
"""
273+
function evalŷ(estim::InternalModel{NT}, ym, d) where NT<:Real
274+
= Vector{NT}(undef, estim.model.ny)
275+
= h!(ŷ, estim.model, estim.x̂d, d - estim.model.dop) .+ estim.model.yop
276+
ŷ[estim.i_ym] = ym
277+
return
278+
end
279+
264280
"Print InternalModel information without i/o integrators."
265281
function print_estim_dim(io::IO, estim::InternalModel, n)
266282
nu, nd = estim.model.nu, estim.model.nd

src/estimator/mhe/execute.jl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ function getinfo(estim::MovingHorizonEstimator{NT}) where NT<:Real
137137
= Vector{NT}(undef, ny*Nk)
138138
for i=1:Nk
139139
d0 = @views D[(1 + nd*(i-1)):(nd*i)] # operating point already removed in estim.D
140-
= @views X̂[(1 + nx̂*(i-1)):(nx̂*i)]
141-
Ŷ[(1 + ny*(i-1)):(ny*i)] = (estim, model, x̂, d0) + model.yop
140+
= @views X̂[(1 + nx̂*(i-1)):(nx̂*i)]
141+
ĥ!(Ŷ[(1 + ny*(i-1)):(ny*i)], estim, model, x̂, d0)
142+
Ŷ[(1 + ny*(i-1)):(ny*i)] .+= model.yop
142143
end
143144
Ŷm = Ym -
144145
info[:Ŵ] = estim.Ŵ[1:Nk*nŵ]
@@ -326,10 +327,11 @@ function update_cov!(P̂, estim::MovingHorizonEstimator, ::LinModel, u, ym, d)
326327
return update_estimate_kf!(estim, u, ym, d, estim.Â, estim.Ĉ[estim.i_ym, :], P̂)
327328
end
328329
"Update it with the `ExtendedKalmanFilter` if model is not a `LinModel`."
329-
function update_cov!(P̂, estim::MovingHorizonEstimator, ::SimModel, u, ym, d)
330+
function update_cov!(P̂, estim::MovingHorizonEstimator, model::SimModel, u, ym, d)
330331
# TODO: also support UnscentedKalmanFilter
331-
= ForwardDiff.jacobian(x̂ -> (estim, estim.model, x̂, u, d), estim.x̂)
332-
= ForwardDiff.jacobian(x̂ -> (estim, estim.model, x̂, d), estim.x̂)
332+
x̂next, ŷ = similar(estim.x̂), similar(model.yop)
333+
= ForwardDiff.jacobian((x̂next, x̂) -> f̂!(x̂next, estim, estim.model, x̂, u, d), x̂next, estim.x̂)
334+
= ForwardDiff.jacobian((ŷ, x̂) -> ĥ!(ŷ, estim, estim.model, x̂, d), ŷ, estim.x̂)
333335
return update_estimate_kf!(estim, u, ym, d, F̂, Ĥ[estim.i_ym, :], P̂)
334336
end
335337

@@ -394,16 +396,22 @@ function predict!(
394396
V̂, X̂, estim::MovingHorizonEstimator, model::SimModel, Z̃::Vector{T}
395397
) where {T<:Real}
396398
Nk = estim.Nk[]
397-
nu, nd, nx̂, nŵ, nym = model.nu, model.nd, estim.nx̂, estim.nx̂, estim.nym
399+
nu, nd, ny, nx̂, nŵ, nym = model.nu, model.nd, model.ny, estim.nx̂, estim.nx̂, estim.nym
398400
nx̃ = !isinf(estim.C) + nx̂
401+
# TODO: avoid these two allocations if possible:
402+
ŷ, x̂next = Vector{T}(undef, ny), Vector{T}(undef, nx̂)
399403
= @views Z̃[nx̃-nx̂+1:nx̃]
400404
for j=1:Nk
401405
u = @views estim.U[ (1 + nu * (j-1)):(nu*j)]
402406
ym = @views estim.Ym[(1 + nym * (j-1)):(nym*j)]
403407
d = @views estim.D[ (1 + nd * (j-1)):(nd*j)]
404408
= @views Z̃[(1 + nx̃ + nŵ*(j-1)):(nx̃ + nŵ*j)]
405-
V̂[(1 + nym*(j-1)):(nym*j)] = ym - (estim, model, x̂, d)[estim.i_ym]
406-
X̂[(1 + nx̂ *(j-1)):(nx̂ *j)] = (estim, model, x̂, u, d) +
409+
ĥ!(ŷ, estim, model, x̂, d)
410+
ŷm = @views ŷ[estim.i_ym]
411+
V̂[(1 + nym*(j-1)):(nym*j)] .= ym .- ŷm
412+
f̂!(x̂next, estim, model, x̂, u, d)
413+
x̂next .+=
414+
X̂[(1 + nx̂ *(j-1)):(nx̂ *j)] = x̂next
407415
= @views X̂[(1 + nx̂*(j-1)):(nx̂*j)]
408416
end
409417
return V̂, X̂

src/state_estim.jl

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,4 @@ Evaluate [`StateEstimator`](@ref) output `ŷ` from measured disturbance `d` and
6666
Second argument is ignored, except for [`InternalModel`](@ref).
6767
"""
6868
evalŷ(estim::StateEstimator, _ , d) = evaloutput(estim, d)
69-
70-
@doc raw"""
71-
evalŷ(estim::InternalModel, ym, d) -> ŷ
72-
73-
Get [`InternalModel`](@ref) output `ŷ` from current measured outputs `ym` and dist. `d`.
74-
75-
[`InternalModel`](@ref) estimator needs current measured outputs ``\mathbf{y^m}(k)`` to
76-
estimate its outputs ``\mathbf{ŷ}(k)``, since the strategy imposes that
77-
``\mathbf{ŷ^m}(k) = \mathbf{y^m}(k)`` is always true.
78-
"""
79-
function evalŷ(estim::InternalModel, ym, d)
80-
= h(estim.model, estim.x̂d, d - estim.model.dop) + estim.model.yop
81-
ŷ[estim.i_ym] = ym
82-
return
83-
end
8469

0 commit comments

Comments
 (0)