Skip to content

Commit 9952577

Browse files
committed
doc : sim function for PredictiveController
1 parent 1401206 commit 9952577

File tree

6 files changed

+61
-24
lines changed

6 files changed

+61
-24
lines changed

docs/src/public/generic_func.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ initstate!
2727
```@docs
2828
setstate!
2929
```
30+
31+
## Quick Simulation
32+
33+
```@docs
34+
sim
35+
```

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/controller/nonlinmpc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ end
301301
setnontlincon!(::NonLinMPC, ::LinModel) = nothing
302302

303303
"Set the nonlinear constraints on the output predictions `Ŷ`."
304-
function setnonlincon!(mpc::NonLinMPC, model::NonLinModel)
304+
function setnonlincon!(mpc::NonLinMPC, ::NonLinModel)
305305
optim = mpc.optim
306306
ΔŨvar = mpc.optim[:ΔŨvar]
307307
con = mpc.con

src/predictive_control.jl

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

318-
function sim!(
318+
@doc raw"""
319+
sim(
320+
mpc::PredictiveController,
321+
N = mpc.Hp + 10,
322+
ry = mpc.estim.model.yop .+ 1,
323+
d = mpc.estim.model.dop;
324+
<keyword arguments>
325+
)
326+
327+
Closed-loop simulation of `mpc` controller for `N` time steps, default to setpoint bumps.
328+
329+
# Arguments
330+
331+
- `mpc::PredictiveController` : predictive controller to simulate
332+
- `N = mpc.Hp + 10` : simulation length in time steps
333+
- `ry = mpc.estim.model.yop .+ 1` : plant output setpoint ``\mathbf{r_y}`` value
334+
- `d = mpc.estim.model.dop` : plant measured disturbance ``\mathbf{d}`` value
335+
- `plant::SimModel = mpc.estim.model` : simulated plant model
336+
- `u_step = zeros(plant.nu)` : step disturbance on manipulated input ``\mathbf{u}``
337+
- `u_noise = zeros(plant.nu)` : additive gaussian noise on manipulated input ``\mathbf{u}``
338+
- `y_step = zeros(plant.ny)` : step disturbance on plant outputs ``\mathbf{y}``
339+
- `y_noise = zeros(plant.ny)` : additive gaussian noise on plant outputs ``\mathbf{y}``
340+
- `d_step = zeros(plant.nd)` : step disturbance on measured dist. ``\mathbf{d}``
341+
- `d_noise = zeros(plant.nd)` : additive gaussian noise on measured dist. ``\mathbf{d}``
342+
- `x0 = zeros(plant.nx)` : plant initial state ``\mathbf{x}(0)``
343+
- `x̂0 = nothing` : `mpc.estim` state estimator initial state ``\mathbf{x̂}(0)``, if `nothing`
344+
[`initstate!(::PredictiveController)`](@ref) is used to init ``\mathbf{x̂}``.
345+
- `lastu = plant.uop` : last manipulated input ``\mathbf{u}`` for ``\mathbf{x̂}`` initialization
346+
347+
"""
348+
function sim(
319349
mpc::PredictiveController,
320350
N::Int = mpc.Hp + 10,
321351
ry::Vector{<:Real} = mpc.estim.model.yop .+ 1,
322352
d ::Vector{<:Real} = mpc.estim.model.dop;
323-
u_step ::Vector{<:Real} = zeros(mpc.estim.model.nu),
324-
u_noise::Vector{<:Real} = zeros(mpc.estim.model.nu),
325-
y_step ::Vector{<:Real} = zeros(mpc.estim.model.ny),
326-
y_noise::Vector{<:Real} = zeros(mpc.estim.model.ny),
327-
d_step ::Vector{<:Real} = zeros(mpc.estim.model.nd),
328-
d_noise::Vector{<:Real} = zeros(mpc.estim.model.nd),
329353
plant::SimModel = mpc.estim.model,
354+
u_step ::Vector{<:Real} = zeros(plant.nu),
355+
u_noise::Vector{<:Real} = zeros(plant.nu),
356+
y_step ::Vector{<:Real} = zeros(plant.ny),
357+
y_noise::Vector{<:Real} = zeros(plant.ny),
358+
d_step ::Vector{<:Real} = zeros(plant.nd),
359+
d_noise::Vector{<:Real} = zeros(plant.nd),
360+
x0 = zeros(plant.nx),
361+
x̂0 = nothing,
330362
lastu = plant.uop,
331-
x0 = plant.x,
332-
x̂0 = nothing,
333363
)
334364
model, i_ym = mpc.estim.model, mpc.estim.i_ym
335365
model.Ts plant.Ts || error("Sampling time Ts of mpc and plant must be equal")
@@ -355,18 +385,19 @@ function sim!(
355385
for i=1:N
356386
d = lastd + d_step + d_noise.*randn(plant.nd)
357387
y = evaloutput(plant, d) + y_step + y_noise.*randn(plant.ny)
358-
u = moveinput!(mpc, ry, d; ym=y[i_ym])
388+
ym = y[i_ym]
389+
u = moveinput!(mpc, ry, d; ym)
359390
up = u + u_step + u_noise.*randn(plant.nu)
360391
Y_data[:, i] = y
361-
Ŷ_data[:, i] = mpc.
392+
Ŷ_data[:, i] = evalŷ(mpc.estim, ym, d)
362393
Ry_data[:, i] = ry
363394
U_data[:, i] = u
364395
Ru_data[:, i] = ru
365396
D_data[:, i] = d
366397
X_data[:, i] = plant.x
367398
X̂_data[:, i] = mpc.estim.
368399
updatestate!(plant, up, d)
369-
updatestate!(mpc, u, y[i_ym], d)
400+
updatestate!(mpc, u, ym, d)
370401
end
371402
res = SimResult(
372403
mpc, T_data, Y_data, Ry_data, Ŷ_data, U_data, Ru_data, D_data, X_data, X̂_data
@@ -415,11 +446,11 @@ end
415446
416447
Get estimator output and split `x̂` into the deterministic `x̂d` and stochastic `x̂s` states.
417448
"""
418-
function getestimates!(mpc::PredictiveController, estim::StateEstimator, _ , d)
449+
function getestimates!(mpc::PredictiveController, estim::StateEstimator, ym , d)
419450
nx = estim.model.nx
420451
mpc.x̂d[:] = estim.x̂[1:nx]
421452
mpc.x̂s[:] = estim.x̂[nx+1:end]
422-
mpc.ŷ[:] = evaloutput(estim, d)
453+
mpc.ŷ[:] = evalŷ(estim, ym, d)
423454
return mpc.x̂d, mpc.x̂s, mpc.
424455
end
425456

@@ -433,7 +464,7 @@ function getestimates!(mpc::PredictiveController, estim::InternalModel, ym, d)
433464
"outputs ym in keyword argument to compute control actions u")
434465
mpc.x̂d[:] = estim.x̂d
435466
mpc.x̂s[:] = estim.x̂s
436-
mpc.ŷ[:] = evaloutput(estim, ym, d)
467+
mpc.ŷ[:] = evalŷ(estim, ym, d)
437468
return mpc.x̂d, mpc.x̂s, mpc.
438469
end
439470

@@ -605,11 +636,6 @@ end
605636
"By default, no change to the objective function."
606637
set_objective!(::PredictiveController, _ ) = nothing
607638

608-
"Evaluate current output of `InternalModel` estimator."
609-
evalŷ!(estim::InternalModel, ym, d) = (mpc.ŷ[:] = estim(ym, d))
610-
"Evaluate current output of the other `StateEstimator`s."
611-
evalŷ!(estim::StateEstimator, _, d) = (mpc.ŷ[:] = estim(d))
612-
613639
@doc raw"""
614640
init_ΔUtoU(nu, Hp, Hc, C, c_Umin, c_Umax)
615641

src/state_estim.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,9 @@ end
266266
(estim::StateEstimator)(d=Float64[]) = evaloutput(estim, d)
267267

268268
include("estimator/kalman.jl")
269-
include("estimator/internal_model.jl")
269+
include("estimator/internal_model.jl")
270+
271+
"Get [`InternalModel`](@ref) output `ŷ` from current measured outputs `ym` and dist. `d`."
272+
evalŷ(estim::InternalModel, ym, d) = evaloutput(estim,ym, d)
273+
"Other [`StateEstimator`](@ref) ignores `ym` to evaluate `ŷ`."
274+
evalŷ(estim::StateEstimator, _, d) = evaloutput(estim, d)

0 commit comments

Comments
 (0)