Skip to content

Commit 1401206

Browse files
committed
sim function renamed to sim!
1 parent aa3df4c commit 1401206

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

example/juMPC.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ test_mpc(linModel4, mpc)
155155
@time u_data, y_data, r_data, d_data = test_mpc(linModel4, mpc)
156156

157157

158-
res = sim(mpc, x0=zeros(mpc.estim.model.nx))
158+
res = sim!(mpc, x0=zeros(mpc.estim.model.nx))
159159
ps = plot(res, plotD=false, plotŶminŶmax=false, plotUminUmax=false)
160160
display(ps)
161161

src/ModelPredictiveControl.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export SimModel, LinModel, NonLinModel, setop!, setstate!, updatestate!, evalout
1414
export StateEstimator, InternalModel
1515
export SteadyKalmanFilter, KalmanFilter, UnscentedKalmanFilter
1616
export initstate!
17-
export PredictiveController, LinMPC, NonLinMPC, setconstraint!, moveinput!, getinfo, sim
17+
export PredictiveController, LinMPC, NonLinMPC, setconstraint!, moveinput!, getinfo, sim!
1818

1919
include("sim_model.jl")
2020
include("state_estim.jl")

src/predictive_control.jl

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,11 @@ function getinfo(mpc::PredictiveController)
315315
return info, sol_summary
316316
end
317317

318-
function sim(
318+
function sim!(
319319
mpc::PredictiveController,
320320
N::Int = mpc.Hp + 10,
321321
ry::Vector{<:Real} = mpc.estim.model.yop .+ 1,
322-
d:: Vector{<:Real} = mpc.estim.model.dop;
322+
d ::Vector{<:Real} = mpc.estim.model.dop;
323323
u_step ::Vector{<:Real} = zeros(mpc.estim.model.nu),
324324
u_noise::Vector{<:Real} = zeros(mpc.estim.model.nu),
325325
y_step ::Vector{<:Real} = zeros(mpc.estim.model.ny),
@@ -331,8 +331,10 @@ function sim(
331331
x0 = plant.x,
332332
x̂0 = nothing,
333333
)
334-
model = mpc.estim.model
334+
model, i_ym = mpc.estim.model, mpc.estim.i_ym
335335
model.Ts plant.Ts || error("Sampling time Ts of mpc and plant must be equal")
336+
old_x0 = copy(plant.x)
337+
old_x̂0 = copy(mpc.estim.x̂)
336338
T_data = collect(plant.Ts*(0:(N-1)))
337339
Y_data = Matrix{Float64}(undef, plant.ny, N)
338340
Ŷ_data = Matrix{Float64}(undef, model.ny, N)
@@ -341,37 +343,36 @@ function sim(
341343
Ru_data = Matrix{Float64}(undef, plant.nu, N)
342344
D_data = Matrix{Float64}(undef, plant.nd, N)
343345
X_data = Matrix{Float64}(undef, plant.nx, N)
344-
X̂_data = Matrix{Float64}(undef, mpc.estim.nx̂, N)
346+
X̂_data = Matrix{Float64}(undef, mpc.estim.nx̂, N)
345347
setstate!(plant, x0)
348+
lastd, lasty = d, evaloutput(plant, d)
346349
if isnothing(x̂0)
347-
initstate!(mpc, lastu, plant(d), d)
350+
initstate!(mpc, lastu, lasty[i_ym], lastd)
348351
else
349352
setstate!(mpc, x̂0)
350353
end
351-
lastd = d
352354
ru = !isempty(mpc.R̂u) ? mpc.R̂u[:, begin] : fill(NaN, plant.nu)
353-
x = plant.x
354-
= mpc.estim.
355355
for i=1:N
356356
d = lastd + d_step + d_noise.*randn(plant.nd)
357-
y = plant(d) + y_step + y_noise.*randn(plant.ny)
358-
ym = y[mpc.estim.i_ym]
359-
u = moveinput!(mpc, ry, d; ym)
357+
y = evaloutput(plant, d) + y_step + y_noise.*randn(plant.ny)
358+
u = moveinput!(mpc, ry, d; ym=y[i_ym])
360359
up = u + u_step + u_noise.*randn(plant.nu)
361-
Y_data[:, i] = y
362-
Ŷ_data[:, i] = mpc.
360+
Y_data[:, i] = y
361+
Ŷ_data[:, i] = mpc.
363362
Ry_data[:, i] = ry
364-
U_data[:, i] = u
363+
U_data[:, i] = u
365364
Ru_data[:, i] = ru
366-
D_data[:, i] = d
367-
X_data[:, i] = x
368-
X̂_data[:, i] =
369-
x = updatestate!(plant, up, d)
370-
= updatestate!(mpc, u, ym, d)
365+
D_data[:, i] = d
366+
X_data[:, i] = plant.x
367+
X̂_data[:, i] = mpc.estim.
368+
updatestate!(plant, up, d)
369+
updatestate!(mpc, u, y[i_ym], d)
371370
end
372371
res = SimResult(
373372
mpc, T_data, Y_data, Ry_data, Ŷ_data, U_data, Ru_data, D_data, X_data, X̂_data
374373
)
374+
setstate!(plant, old_x0)
375+
setstate!(mpc, old_x̂0)
375376
return res
376377
end
377378

0 commit comments

Comments
 (0)