Skip to content

Commit 03fa801

Browse files
committed
sim function for PredictiveController is working
1 parent 88118ec commit 03fa801

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1111
OSQP = "ab2f91bb-94b4-55e3-9ba0-7f65df51de79"
1212
PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
1313
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
14+
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1415

1516
[compat]
1617
ControlSystemsBase = "1"

example/juMPC.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ end
149149
test_mpc(linModel4, mpc)
150150
@time u_data, y_data, r_data, d_data = test_mpc(linModel4, mpc)
151151

152+
res = sim(mpc)
153+
152154
test_mpc(linModel4, nmpc)
153155
@time u_data, y_data, r_data, d_data = test_mpc(linModel4, nmpc)
154156

src/ModelPredictiveControl.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ export SimModel, LinModel, NonLinModel, setop!, setstate!, updatestate!, evalout
1313
export StateEstimator, InternalModel
1414
export SteadyKalmanFilter, KalmanFilter, UnscentedKalmanFilter
1515
export initstate!
16-
export PredictiveController, LinMPC, NonLinMPC, setconstraint!, moveinput!, getinfo
16+
export PredictiveController, LinMPC, NonLinMPC, setconstraint!, moveinput!, getinfo, sim
1717

18+
include("plots.jl")
1819
include("sim_model.jl")
1920
include("state_estim.jl")
2021
include("predictive_control.jl")

src/plots.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct SimResult
2+
T_data ::Vector{Float64}
3+
Y_data ::Matrix{Float64}
4+
Ry_data::Matrix{Float64}
5+
Ŷ_data ::Matrix{Float64}
6+
U_data ::Matrix{Float64}
7+
Ru_data::Matrix{Float64}
8+
D_data ::Matrix{Float64}
9+
X_data ::Matrix{Float64}
10+
X̂_data ::Matrix{Float64}
11+
end
12+
13+
#=
14+
@recipe function f(res::SimResult)
15+
y = map(xi -> pdf(dist,xi), x)
16+
seriestype --> :path # there is always an attribute dictionary `d` available...
17+
# If the user didn't specify a seriestype, we choose :path
18+
return x, y
19+
end
20+
=#

src/predictive_control.jl

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,46 @@ function sim(
333333
)
334334
model = mpc.estim.model
335335
model.Ts plant.Ts || error("Sampling time Ts of mpc and plant must be equal")
336+
T_data = collect(plant.Ts*(0:(N-1)))
337+
Y_data = Matrix{Float64}(undef, plant.ny, N)
338+
Ŷ_data = Matrix{Float64}(undef, model.ny, N)
339+
Ry_data = Matrix{Float64}(undef, plant.ny, N)
340+
U_data = Matrix{Float64}(undef, plant.nu, N)
341+
Ru_data = Matrix{Float64}(undef, plant.nu, N)
342+
D_data = Matrix{Float64}(undef, plant.nd, N)
343+
X_data = Matrix{Float64}(undef, plant.nx, N)
344+
X̂_data = Matrix{Float64}(undef, mpc.estim.nx̂, N)
336345
setstate!(plant, x0)
337346
if isnothing(x̂0)
338-
initstate!(mpc, lastu, plant(), d)
347+
initstate!(mpc, lastu, plant(d), d)
348+
else
349+
setstate!(mpc, x̂0)
339350
end
340-
ry_data = Matrix{Float64}(undef, plant.ny, N)
341-
u_data = Matrix{Float64}(undef, plant.nu, N)
342-
y_data = Matrix{Float64}(undef, plant.ny, N)
343-
d_data = Matrix{Float64}(undef, plant.nd, N)
344-
t_data = plant.Ts*(0:(N-1))
351+
lastd = d
352+
ru = !isempty(mpc.R̂u) ? mpc.R̂u[:, begin] : fill(NaN, plant.nu)
353+
x = plant.x
354+
= mpc.estim.
345355
for i=1:N
356+
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)
360+
up = u + u_step + u_noise.*randn(plant.nu)
361+
Y_data[:, i] = y
362+
Ŷ_data[:, i] = mpc.
363+
Ry_data[:, i] = ry
364+
U_data[:, i] = u
365+
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)
346371
end
347-
return nothing
372+
res = SimResult(
373+
T_data, Y_data, Ry_data, Ŷ_data, U_data, Ru_data, D_data, X_data, X̂_data
374+
)
375+
return res
348376
end
349377

350378

0 commit comments

Comments
 (0)