Skip to content

Commit 21d69cb

Browse files
committed
code cleanup: improve clarity for F=Ŷs initialization
1 parent 2c9c0be commit 21d69cb

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/controller/execute.jl

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -189,36 +189,26 @@ They are computed with these equations using in-place operations:
189189
```
190190
"""
191191
function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, R̂y, R̂u)
192-
lastu = mpc.buffer.u
193-
lastu .= mpc.estim.lastu0 .+ model.uop
194-
mul!(mpc.T_lastu, mpc.T, lastu)
195-
ŷ, F, q̃, r = mpc.ŷ, mpc.F, mpc.q̃, mpc.r
196-
Cy, Cu, M_Hp_Ẽ, L_Hp_S̃ = mpc.buffer.Ŷ, mpc.buffer.U, mpc.buffer.Ẽ, mpc.buffer.
197-
ŷ .= evaloutput(mpc.estim, d)
198-
predictstoch!(mpc, mpc.estim) # init F with Ŷs for InternalModel
192+
F = initpred_common!(mpc, model, d, D̂, R̂y, R̂u)
199193
F .+= mpc.B # F = F + B
200194
mul!(F, mpc.K, mpc.estim.x̂0, 1, 1) # F = F + K*x̂0
201195
mul!(F, mpc.V, mpc.estim.lastu0, 1, 1) # F = F + V*lastu0
202196
if model.nd 0
203-
mpc.d0 .= d .- model.dop
204-
mpc.D̂0 .=.- mpc.Dop
205-
mpc.D̂e[1:model.nd] .= d
206-
mpc.D̂e[model.nd+1:end] .=
207197
mul!(F, mpc.G, mpc.d0, 1, 1) # F = F + G*d0
208198
mul!(F, mpc.J, mpc.D̂0, 1, 1) # F = F + J*D̂0
209199
end
200+
Cy, Cu, M_Hp_Ẽ, L_Hp_S̃ = mpc.buffer.Ŷ, mpc.buffer.U, mpc.buffer.Ẽ, mpc.buffer.
201+
q̃, r = mpc.q̃, mpc.r
210202
q̃ .= 0
211203
r .= 0
212204
# --- output setpoint tracking term ---
213-
mpc.R̂y .= R̂y
214205
if !mpc.weights.iszero_M_Hp[]
215206
Cy .= F .+ mpc.Yop .- R̂y
216207
mul!(M_Hp_Ẽ, mpc.weights.M_Hp, mpc.Ẽ)
217208
mul!(q̃, M_Hp_Ẽ', Cy, 1, 1) # q̃ = q̃ + M_Hp*Ẽ'*Cy
218209
r .+= dot(Cy, mpc.weights.M_Hp, Cy) # r = r + Cy'*M_Hp*Cy
219210
end
220211
# --- input setpoint tracking term ---
221-
mpc.R̂u .= R̂u
222212
if !mpc.weights.iszero_L_Hp[]
223213
Cu .= mpc.T_lastu .- R̂u
224214
mul!(L_Hp_S̃, mpc.weights.L_Hp, mpc.S̃)
@@ -236,11 +226,23 @@ end
236226
Init `ŷ, F, d0, D̂0, D̂e, R̂y, R̂u` vectors when model is not a [`LinModel`](@ref).
237227
"""
238228
function initpred!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂u)
229+
F = initpred_common!(mpc, model, d, D̂, R̂y, R̂u)
230+
return nothing
231+
end
232+
233+
"""
234+
initpred_common!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂u) -> F
235+
236+
Common computations of `initpred!` for all types of [`SimModel`](@ref).
237+
238+
Will init `mpc.F` with 0 values, or with the stochastic predictions `Ŷs` if `mpc.estim` is
239+
an [`InternalModel`](@ref). The function returns `mpc.F`.
240+
"""
241+
function initpred_common!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂u)
239242
lastu = mpc.buffer.u
240243
lastu .= mpc.estim.lastu0 .+ model.uop
241244
mul!(mpc.T_lastu, mpc.T, lastu)
242245
mpc.ŷ .= evaloutput(mpc.estim, d)
243-
predictstoch!(mpc, mpc.estim) # init F with Ŷs for InternalModel
244246
if model.nd 0
245247
mpc.d0 .= d .- model.dop
246248
mpc.D̂0 .=.- mpc.Dop
@@ -249,22 +251,23 @@ function initpred!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂
249251
end
250252
mpc.R̂y .= R̂y
251253
mpc.R̂u .= R̂u
252-
return nothing
254+
F = predictstoch!(mpc, mpc.estim) # init mpc.F with Ŷs for InternalModel
255+
return F
253256
end
254257

255258
@doc raw"""
256-
predictstoch!(mpc::PredictiveController, estim::InternalModel)
259+
predictstoch!(mpc::PredictiveController, estim::InternalModel) -> F
257260
258261
Init `mpc.F` vector with ``\mathbf{F = Ŷ_s}`` when `estim` is an [`InternalModel`](@ref).
259262
"""
260-
function predictstoch!(mpc::PredictiveController{NT}, estim::InternalModel) where {NT<:Real}
263+
function predictstoch!(mpc::PredictiveController, estim::InternalModel)
261264
Ŷs = mpc.F
262265
mul!(Ŷs, mpc.Ks, estim.x̂s)
263266
mul!(Ŷs, mpc.Ps, estim.ŷs, 1, 1)
264-
return nothing
267+
return mpc.F
265268
end
266269
"Separate stochastic predictions are not needed if `estim` is not [`InternalModel`](@ref)."
267-
predictstoch!(mpc::PredictiveController, ::StateEstimator) = (mpc.F .= 0; nothing)
270+
predictstoch!(mpc::PredictiveController, ::StateEstimator) = (mpc.F .= 0)
268271

269272
@doc raw"""
270273
linconstraint!(mpc::PredictiveController, model::LinModel)

0 commit comments

Comments
 (0)