@@ -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- x̂ = @views X̂[(1 + nx̂* (i- 1 )): (nx̂* i)]
141- Ŷ[(1 + ny* (i- 1 )): (ny* i)] = ĥ (estim, model, x̂, d0) + model. yop
140+ x̂ = @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 - V̂
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̂)
327328end
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- F̂ = ForwardDiff. jacobian (x̂ -> f̂ (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+ F̂ = 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̂)
334336end
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 x̂ = @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)] = f̂ (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 x̂ = @views X̂[(1 + nx̂* (j- 1 )): (nx̂* j)]
408416 end
409417 return V̂, X̂
0 commit comments