Skip to content

Commit 1151394

Browse files
committed
added JE call in NonLinMPC based on LinModel
1 parent e5b5f99 commit 1151394

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

docs/src/internals/predictive_control.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ ModelPredictiveControl.init_quadprog
1515
ModelPredictiveControl.init_stochpred
1616
ModelPredictiveControl.init_linconstraint
1717
ModelPredictiveControl.getestimates!
18-
ModelPredictiveControl.initprediction!
18+
ModelPredictiveControl.initpred!
1919
ModelPredictiveControl.linconstraint!
2020
```

src/controller/linmpc.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ function LinMPC(
196196
return LinMPC{S}(estim, Hp, Hc, Mwt, Nwt, Lwt, Cwt, ru, optim)
197197
end
198198

199-
function init_objective(mpc::LinMPC, ΔŨ)
199+
function init_objective!(mpc::LinMPC, ΔŨ)
200200
set_objective_function(mpc.optim, obj_quadprog(ΔŨ, mpc.P̃, mpc.q̃))
201+
return nothing
201202
end

src/controller/nonlinmpc.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,16 @@ function NonLinMPC(
185185
end
186186

187187

188-
function init_objective(mpc::NonLinMPC, _ )
189-
return nothing
190-
end
188+
init_objective!(mpc::NonLinMPC, _ ) = nothing
191189

192-
function obj_nonlinprog(mpc::NonLinMPC, ::LinModel, ΔŨ::NTuple{N, T}) where {T, N}
190+
function obj_nonlinprog(mpc::NonLinMPC, model::LinModel, ΔŨ::NTuple{N, T}) where {T, N}
193191
ΔŨ = collect(ΔŨ) # convert NTuple to Vector
194-
Jqp = obj_quadprog(ΔŨ, mpc.P̃, mpc.q̃)
195-
return Jqp
192+
U = mpc.S̃_Hp*ΔŨ + mpc.T_Hp*(mpc.estim.lastu0 + model.uop)
193+
UE = [U; U[end-model.nu+1:end]]
194+
ŶE = [mpc.ŷ; mpc.*ΔŨ + mpc.F]
195+
mpc.D̂0 + mpc.Dop
196+
D̂E = [mpc.d0 + model.dop; mpc.D̂0 + mpc.Dop]
197+
return obj_quadprog(ΔŨ, mpc.P̃, mpc.q̃) + mpc.JE(UE, ŶE, D̂E)
196198
end
197199

198200

src/predictive_control.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ function moveinput!(
246246
getestimates!(mpc, mpc.estim, ym, d)
247247
ŷs, Ŷs = predictstoch!(mpc, mpc.estim, d, ym)
248248
p = initpred!(mpc, mpc.estim.model, d, D̂, Ŷs, R̂y)
249-
linconstaint!(mpc, mpc.estim.model)
249+
linconstraint!(mpc, mpc.estim.model)
250250
ΔŨ, _ = optim_objective!(mpc, p)
251251
Δu = ΔŨ[1:mpc.estim.model.nu] # receding horizon principle: only Δu(k) is used (1st one)
252252
u = mpc.estim.lastu0 + mpc.estim.model.uop + Δu
@@ -359,8 +359,8 @@ end
359359
360360
Init `F`, `d0` and `D̂0` prediction matrices for [`NonLinModel`](@ref).
361361
362-
For [`NonLinModel`](@ref), the constant matrix `F` is ``\mathbf{F = Ŷ_s + Y_op}``, thus
363-
it incorporates the stochastic predictions and the output operating point ``\mathbf{y_op}``
362+
For [`NonLinModel`](@ref), the constant matrix `F` is ``\mathbf{F = Ŷ_s + Y_{op}}``, thus it
363+
incorporates the stochastic predictions and the output operating point ``\mathbf{y_{op}}``
364364
repeated over ``H_p``. `d0` and `D̂0` are the measured disturbances and the predictions
365365
without the operating points ``\mathbf{d_{op}}``.
366366
"""
@@ -378,7 +378,7 @@ end
378378
379379
Set `b` vector for the linear model inequality constraints (``\mathbf{A ΔŨ ≤ b}``).
380380
"""
381-
function linconstaint!(mpc::PredictiveController, model::LinModel)
381+
function linconstraint!(mpc::PredictiveController, model::LinModel)
382382
mpc.con.b[:] = [
383383
-mpc.con.Umin + mpc.T_Hc*(mpc.estim.lastu0 + model.uop)
384384
+mpc.con.Umax - mpc.T_Hc*(mpc.estim.lastu0 + model.uop)
@@ -395,7 +395,7 @@ end
395395
396396
Set `b` that excludes predicted output ``\mathbf{Ŷ}`` constraints for [`NonLinModel`](@ref).
397397
"""
398-
function linconstaint!(mpc::PredictiveController, model::NonLinModel)
398+
function linconstraint!(mpc::PredictiveController, model::NonLinModel)
399399
mpc.con.b[:] = [
400400
-mpc.con.Umin + mpc.T_Hc*(mpc.estim.lastu0 + model.uop)
401401
+mpc.con.Umax - mpc.T_Hc*(mpc.estim.lastu0 + model.uop)
@@ -420,7 +420,7 @@ function optim_objective!(mpc::PredictiveController, p)
420420
# if soft constraints, append the last slack value ϵ_{k-1}:
421421
!isinf(mpc.C) && (ΔŨ0 = [ΔŨ0; lastΔŨ[end]])
422422
set_start_value.(ΔŨ, ΔŨ0)
423-
init_objective(mpc, ΔŨ)
423+
init_objective!(mpc, ΔŨ)
424424
try
425425
optimize!(optim)
426426
catch err

0 commit comments

Comments
 (0)