Skip to content

Commit c69e7f8

Browse files
committed
debug: extended input Ue calculated correctly
1 parent 1a3c727 commit c69e7f8

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/controller/execute.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,13 @@ function extended_predictions!(Ue, Ŷe, Ū, mpc, model, Ŷ0, ΔŨ)
373373
# --- extended manipulated inputs Ue = [U; u(k+Hp-1)] ---
374374
U =
375375
U .= mul!(U, mpc.S̃, ΔŨ) .+ mpc.T_lastu0 .+ mpc.Uop
376+
Ue[1:end-nu] .= U
376377
# u(k + Hp) = u(k + Hp - 1) since Δu(k+Hp) = 0 (because Hc ≤ Hp):
377378
Ue[end-nu+1:end] .= @views U[end-nu+1:end]
378379
# --- extended output predictions Ŷe = [ŷ(k); Ŷ] ---
379380
Ŷe[1:ny] .= mpc.
380381
Ŷe[ny+1:end] .= Ŷ0 .+ mpc.Yop
381-
return Ue, Ŷe
382+
return Ue, Ŷe
382383
end
383384

384385
"""

src/controller/nonlinmpc.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct NonLinMPC{
4848
buffer::PredictiveControllerBuffer{NT}
4949
function NonLinMPC{NT, SE, JM, JEfunc, GCfunc, P}(
5050
estim::SE,
51-
Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt, JE::JEfunc, gc::GCfunc, nc, p::P, optim::JM
51+
Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt, JE::JEfunc, gc!::GCfunc, nc, p::P, optim::JM
5252
) where {
5353
NT<:Real,
5454
SE<:StateEstimator,
@@ -60,8 +60,6 @@ struct NonLinMPC{
6060
model = estim.model
6161
nu, ny, nd, nx̂ = model.nu, model.ny, model.nd, estim.nx̂
6262
= copy(model.yop) # dummy vals (updated just before optimization)
63-
validate_JE(NT, JE)
64-
gc! = get_mutating_gc(NT, gc)
6563
weights = ControllerWeights{NT}(model, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt)
6664
# dummy vals (updated just before optimization):
6765
R̂y, R̂u, T_lastu0 = zeros(NT, ny*Hp), zeros(NT, nu*Hp), zeros(NT, nu*Hp)
@@ -243,7 +241,7 @@ function NonLinMPC(
243241
L_Hp = diagm(repeat(Lwt, Hp)),
244242
Cwt = DEFAULT_CWT,
245243
Ewt = DEFAULT_EWT,
246-
JE::Function = (_,_,_,_) -> 0.0,
244+
JE ::Function = (_,_,_,_) -> 0.0,
247245
gc!::Function = (_,_,_,_,_,_) -> nothing,
248246
gc ::Function = gc!,
249247
nc::Int = 0,
@@ -323,7 +321,7 @@ function NonLinMPC(
323321
Ewt = DEFAULT_EWT,
324322
JE ::JEfunc = (_,_,_,_) -> 0.0,
325323
gc!::Function = (_,_,_,_,_,_) -> nothing,
326-
gc ::GCfunc = gc!,
324+
gc ::Function = gc!,
327325
nc = 0,
328326
p::P = estim.model.p,
329327
optim::JM = JuMP.Model(DEFAULT_NONLINMPC_OPTIMIZER, add_bridges=false),
@@ -332,16 +330,18 @@ function NonLinMPC(
332330
SE<:StateEstimator{NT},
333331
JM<:JuMP.GenericModel,
334332
JEfunc<:Function,
335-
GCfunc<:Function,
336333
P<:Any
337334
}
338335
nk = estimate_delays(estim.model)
339336
if Hp nk
340337
@warn("prediction horizon Hp ($Hp) ≤ estimated number of delays in model "*
341338
"($nk), the closed-loop system may be unstable or zero-gain (unresponsive)")
342339
end
340+
validate_JE(NT, JE)
341+
gc! = get_mutating_gc(NT, gc)
342+
GCfunc = get_type_mutating_gc(gc!)
343343
return NonLinMPC{NT, SE, JM, JEfunc, GCfunc, P}(
344-
estim, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt, JE, gc, nc, p, optim
344+
estim, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt, JE, gc!, nc, p, optim
345345
)
346346
end
347347

@@ -398,6 +398,9 @@ function get_mutating_gc(NT, gc)
398398
return gc!
399399
end
400400

401+
"Get the type of the mutating version of the custom constrain function `gc!`."
402+
get_type_mutating_gc(::GCfunc) where {GCfunc<:Function} = GCfunc
403+
401404
"""
402405
test_custom_functions(NT, model::SimModel, JE, gc!, nc, Uop, Yop, Dop, p)
403406

0 commit comments

Comments
 (0)