Skip to content

Commit 57d789a

Browse files
committed
debug: intput tracking term now works
added: MPC terminal cost test with LQR tuning
1 parent 442443c commit 57d789a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/controller/execute.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function initpred!(mpc::PredictiveController, model::LinModel, d, ym, D̂, R̂y,
180180
mpc.R̂u .= R̂u
181181
C_u = mpc.T_lastu .- mpc.R̂u
182182
mpc..+= lmul!(2, (mpc.L_Hp*mpc.S̃)'*C_u)
183-
mpc.p .+= dot(C_y, mpc.L_Hp, C_u)
183+
mpc.p .+= dot(C_u, mpc.L_Hp, C_u)
184184
end
185185
return nothing
186186
end

test/test_predictive_control.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,32 @@ end
232232
@test info[:x̂end][1] 0 atol=1e-1
233233
end
234234

235+
@testset "LinMPC terminal cost" begin
236+
model = LinModel(ss([0.5 -0.4;0.6 0.5], [1 0;0 1], [1 0; 0 1], 0, 1))
237+
K = lqr(Discrete, model.A, model.Bu, I, 0.5I)
238+
M_end, _ = ControlSystemsBase.ared(model.A, model.Bu, I, 0.5I)
239+
M_Hp = [I(4) zeros(4,2); zeros(2,4) M_end]
240+
mpc = LinMPC(model; Hp=3, Hc=3, M_Hp, Nwt=[0; 0], Lwt=[0.5, 0.5], nint_ym=0)
241+
X_mpc = zeros(2,20)
242+
setstate!(mpc,[1,1])
243+
setstate!(model, [1,1])
244+
for i=1:20
245+
y = model()
246+
u = moveinput!(mpc, [0, 0])
247+
X_mpc[:,i] = model.x
248+
updatestate!(mpc, u, y)
249+
updatestate!(model, u)
250+
end
251+
X_lqr = zeros(2,20)
252+
x=[1,1]
253+
for i=1:20
254+
u = -K*x
255+
X_lqr[:,i] = x
256+
x = model.A*x + model.Bu*u
257+
end
258+
@test all(isapprox.(X_mpc, X_lqr, atol=1e-3))
259+
end
260+
235261
@testset "ExplicitMPC construction" begin
236262
model = LinModel(sys, Ts, i_d=[3])
237263
mpc1 = ExplicitMPC(model, Hp=15)

0 commit comments

Comments
 (0)