Skip to content

Commit 2fc285e

Browse files
committed
changed: keep lastu0 in MHE since needed when direct==true
`preparestate!` add the new data in the windows and solve the optimization problem in this case. Since the function do not receive `u0`, it needs to be stored at the last time step in `update_estimate!`
1 parent feaf0ef commit 2fc285e

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/controller/execute.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ function setmodel!(
556556
Ñ_Hc = Ntilde_Hc,
557557
kwargs...
558558
)
559+
uop_old = copy(mpc.estim.model.uop)
559560
x̂op_old = copy(mpc.estim.x̂op)
560561
nu, ny, Hp, Hc, nϵ = model.nu, model.ny, mpc.Hp, mpc.Hc, mpc.
561562
setmodel!(mpc.estim, model; kwargs...)
@@ -601,12 +602,12 @@ function setmodel!(
601602
mpc.weights.L_Hp .= L_Hp
602603
mpc.weights.iszero_L_Hp[] = iszero(mpc.weights.L_Hp)
603604
end
604-
setmodel_controller!(mpc, x̂op_old)
605+
setmodel_controller!(mpc, uop_old, x̂op_old)
605606
return mpc
606607
end
607608

608609
"Update the prediction matrices, linear constraints and JuMP optimization."
609-
function setmodel_controller!(mpc::PredictiveController, x̂op_old)
610+
function setmodel_controller!(mpc::PredictiveController, uop_old, x̂op_old)
610611
model, estim, transcription = mpc.estim.model, mpc.estim, mpc.transcription
611612
nu, ny, nd, Hp, Hc = model.nu, model.ny, model.nd, mpc.Hp, mpc.Hc
612613
optim, con = mpc.optim, mpc.con
@@ -636,6 +637,7 @@ function setmodel_controller!(mpc::PredictiveController, x̂op_old)
636637
con.x̂0min .+= x̂op_old # convert x̂0 to x̂ with the old operating point
637638
con.x̂0max .+= x̂op_old # convert x̂0 to x̂ with the old operating point
638639
# --- operating points ---
640+
mpc.lastu0 .+= uop_old .- model.uop
639641
for i in 0:Hp-1
640642
mpc.Uop[(1+nu*i):(nu+nu*i)] .= model.uop
641643
mpc.Yop[(1+ny*i):(ny+ny*i)] .= model.yop

src/estimator/execute.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ function setmodel!(
403403
yop_old = copy(estim.model.yop)
404404
dop_old = copy(estim.model.dop)
405405
setmodel_linmodel!(estim.model, model)
406-
estim.lastu0 .+= uop_old .- model.uop
407406
setmodel_estimator!(estim, model, uop_old, yop_old, dop_old, Q̂, R̂)
408407
return estim
409408
end

src/estimator/mhe/execute.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ end
4343
4444
Update [`MovingHorizonEstimator`](@ref) state `estim.x̂0`.
4545
46-
The optimization problem of [`MovingHorizonEstimator`](@ref) documentation is solved at
47-
each discrete time ``k``. The prediction matrices are provided at [`init_predmat_mhe`](@ref)
48-
documentation. Once solved, the optimal estimate ``\mathbf{x̂}_k(k+p)`` is computed by
49-
inserting the optimal values of ``\mathbf{x̂}_k(k-N_k+p)`` and ``\mathbf{Ŵ}`` in the
50-
augmented model from ``j = N_k-1`` to ``0`` inclusively. Afterward, if ``N_k = H_e``, the
51-
arrival covariance for the next time step ``\mathbf{P̂}_{k-N_k}(k-N_k+1)`` is estimated using
52-
`estim.covestim` object.
46+
The optimization problem of [`MovingHorizonEstimator`](@ref) documentation is solved if
47+
`estim.direct` is `false` (otherwise solved in [`correct_estimate!`](@ref)). The prediction
48+
matrices are provided at [`init_predmat_mhe`](@ref) documentation. Once solved, the optimal
49+
estimate ``\mathbf{x̂}_k(k+p)`` is computed by inserting the optimal values of
50+
``\mathbf{x̂}_k(k-N_k+p)`` and ``\mathbf{Ŵ}`` in the augmented model from ``j = N_k-1`` to
51+
``0`` inclusively. Afterward, if ``N_k = H_e``, the arrival covariance for the next time
52+
step ``\mathbf{P̂}_{k-N_k}(k-N_k+1)`` is estimated using `estim.covestim` object. It
53+
also stores `u0` at `estim.lastu0`, so it can be added to the data window at the next time
54+
step in [`correct_estimate!`](@ref).
5355
"""
5456
function update_estimate!(estim::MovingHorizonEstimator, y0m, d0, u0)
5557
if !estim.direct
@@ -59,6 +61,7 @@ function update_estimate!(estim::MovingHorizonEstimator, y0m, d0, u0)
5961
optim_objective!(estim)
6062
end
6163
(estim.Nk[] == estim.He) && update_cov!(estim)
64+
estim.lastu0 .= u0
6265
return nothing
6366
end
6467

@@ -779,8 +782,10 @@ function setmodel_estimator!(
779782
# convert d to d0 with the new operating point:
780783
estim.D0[(1+nd*(i-1)):(nd*i)] .-= model.dop
781784
end
785+
estim.lastu0 .+= uop_old
782786
estim.Z̃[nϵ+1:+nx̂] .+= x̂op_old
783787
estim.x̂0arr_old .+= x̂op_old
788+
estim.lastu0 .-= model.uop
784789
estim.Z̃[nϵ+1:+nx̂] .-= x̂op
785790
estim.x̂0arr_old .-= x̂op
786791
# --- covariance matrices ---

0 commit comments

Comments
 (0)