22 initstate!(mpc::PredictiveController, u, ym, d=[]) -> x̂
33
44Init the states of `mpc.estim` [`StateEstimator`](@ref) and warm start `mpc.Z̃` at zero.
5+
6+ It also stores `u - mpc.estim.model.uop` at `mpc.lastu0` for converting the input increments
7+ ``\m athbf{ΔU}`` to inputs ``\m athbf{U}``.
58"""
69function initstate! (mpc:: PredictiveController , u, ym, d= mpc. estim. buffer. empty)
710 mpc. Z̃ .= 0
11+ mpc. lastu0 .= u .- mpc. estim. model. uop
812 return initstate! (mpc. estim, u, ym, d)
913end
1014
@@ -16,10 +20,11 @@ Compute the optimal manipulated input value `u` for the current control period.
1620Solve the optimization problem of `mpc` [`PredictiveController`](@ref) and return the
1721results ``\m athbf{u}(k)``. Following the receding horizon principle, the algorithm discards
1822the optimal future manipulated inputs ``\m athbf{u}(k+1), \m athbf{u}(k+2), ...`` Note that
19- the method mutates `mpc` internal data but it does not modifies `mpc.estim` states. Call
20- [`preparestate!(mpc, ym, d)`](@ref) before `moveinput!`, and [`updatestate!(mpc, u, ym, d)`](@ref)
21- after, to update `mpc` state estimates. Setpoint and measured disturbance previews can
22- be implemented with the `R̂y`, `R̂u` and `D̂` keyword arguments.
23+ the method mutates `mpc` internal data (it stores `u - mpc.estim.model.uop` at `mpc.lastu0`
24+ for instance) but it does not modifies `mpc.estim` states. Call [`preparestate!(mpc, ym, d)`](@ref)
25+ before `moveinput!`, and [`updatestate!(mpc, u, ym, d)`](@ref) after, to update `mpc` state
26+ estimates. Setpoint and measured disturbance previews can be implemented with the `R̂y`, `R̂u`
27+ and `D̂` keyword arguments.
2328
2429Calling a [`PredictiveController`](@ref) object calls this method.
2530
@@ -69,7 +74,7 @@ function moveinput!(
6974 linconstraint! (mpc, mpc. estim. model, mpc. transcription)
7075 linconstrainteq! (mpc, mpc. estim. model, mpc. transcription)
7176 Z̃ = optim_objective! (mpc)
72- return getinput (mpc, Z̃)
77+ return getinput! (mpc, Z̃)
7378end
7479
7580@doc raw """
@@ -207,7 +212,7 @@ function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, R̂y, R̂
207212 F = initpred_common! (mpc, model, d, D̂, R̂y, R̂u)
208213 F .+ = mpc. B # F = F + B
209214 mul! (F, mpc. K, mpc. estim. x̂0, 1 , 1 ) # F = F + K*x̂0
210- mul! (F, mpc. V, mpc. estim . lastu0, 1 , 1 ) # F = F + V*lastu0
215+ mul! (F, mpc. V, mpc. lastu0, 1 , 1 ) # F = F + V*lastu0
211216 if model. nd > 0
212217 mul! (F, mpc. G, mpc. d0, 1 , 1 ) # F = F + G*d0
213218 mul! (F, mpc. J, mpc. D̂0, 1 , 1 ) # F = F + J*D̂0
@@ -254,7 +259,7 @@ Will also init `mpc.F` with 0 values, or with the stochastic predictions `Ŷs`
254259is an [`InternalModel`](@ref). The function returns `mpc.F`.
255260"""
256261function initpred_common! (mpc:: PredictiveController , model:: SimModel , d, D̂, R̂y, R̂u)
257- mul! (mpc. Tu_lastu0, mpc. Tu, mpc. estim . lastu0)
262+ mul! (mpc. Tu_lastu0, mpc. Tu, mpc. lastu0)
258263 mpc. ŷ .= evaloutput (mpc. estim, d)
259264 if model. nd > 0
260265 mpc. d0 .= d .- model. dop
@@ -446,20 +451,23 @@ function preparestate!(mpc::PredictiveController, ym, d=mpc.estim.buffer.empty)
446451end
447452
448453@doc raw """
449- getinput(mpc::PredictiveController, Z̃) -> u
454+ getinput! (mpc::PredictiveController, Z̃) -> u
450455
451- Get current manipulated input `u` from a [`PredictiveController`](@ref) solution `Z̃`.
456+ Get current manipulated input `u` from the solution `Z̃`, store it and return it .
452457
453458The first manipulated input ``\m athbf{u}(k)`` is extracted from the decision vector
454- ``\m athbf{Z̃}`` and applied on the plant (from the receding horizon principle).
459+ ``\m athbf{Z̃}`` and applied on the plant (from the receding horizon principle). It also
460+ stores `u - mpc.estim.model.uop` at `mpc.lastu0`.
455461"""
456- function getinput (mpc, Z̃)
462+ function getinput! (mpc, Z̃)
463+ model = mpc. estim. model
457464 Δu = mpc. buffer. u
458- for i in 1 : mpc . estim . model. nu
465+ for i in 1 : model. nu
459466 Δu[i] = Z̃[i]
460467 end
461468 u = Δu
462- u .+ = mpc. estim. lastu0 .+ mpc. estim. model. uop
469+ u .+ = mpc. lastu0 .+ model. uop
470+ mpc. lastu0 .= u .- model. uop
463471 return u
464472end
465473
@@ -548,6 +556,7 @@ function setmodel!(
548556 Ñ_Hc = Ntilde_Hc,
549557 kwargs...
550558 )
559+ uop_old = copy (mpc. estim. model. uop)
551560 x̂op_old = copy (mpc. estim. x̂op)
552561 nu, ny, Hp, Hc, nϵ = model. nu, model. ny, mpc. Hp, mpc. Hc, mpc. nϵ
553562 setmodel! (mpc. estim, model; kwargs... )
@@ -593,12 +602,12 @@ function setmodel!(
593602 mpc. weights. L_Hp .= L_Hp
594603 mpc. weights. iszero_L_Hp[] = iszero (mpc. weights. L_Hp)
595604 end
596- setmodel_controller! (mpc, x̂op_old)
605+ setmodel_controller! (mpc, uop_old, x̂op_old)
597606 return mpc
598607end
599608
600609" Update the prediction matrices, linear constraints and JuMP optimization."
601- function setmodel_controller! (mpc:: PredictiveController , x̂op_old)
610+ function setmodel_controller! (mpc:: PredictiveController , uop_old, x̂op_old)
602611 model, estim, transcription = mpc. estim. model, mpc. estim, mpc. transcription
603612 nu, ny, nd, Hp, Hc = model. nu, model. ny, model. nd, mpc. Hp, mpc. Hc
604613 optim, con = mpc. optim, mpc. con
@@ -628,6 +637,7 @@ function setmodel_controller!(mpc::PredictiveController, x̂op_old)
628637 con. x̂0min .+ = x̂op_old # convert x̂0 to x̂ with the old operating point
629638 con. x̂0max .+ = x̂op_old # convert x̂0 to x̂ with the old operating point
630639 # --- operating points ---
640+ mpc. lastu0 .+ = uop_old .- model. uop
631641 for i in 0 : Hp- 1
632642 mpc. Uop[(1 + nu* i): (nu+ nu* i)] .= model. uop
633643 mpc. Yop[(1 + ny* i): (ny+ ny* i)] .= model. yop
0 commit comments