@@ -315,21 +315,51 @@ function getinfo(mpc::PredictiveController)
315315 return info, sol_summary
316316end
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 ``\m athbf{r_y}`` value
334+ - `d = mpc.estim.model.dop` : plant measured disturbance ``\m athbf{d}`` value
335+ - `plant::SimModel = mpc.estim.model` : simulated plant model
336+ - `u_step = zeros(plant.nu)` : step disturbance on manipulated input ``\m athbf{u}``
337+ - `u_noise = zeros(plant.nu)` : additive gaussian noise on manipulated input ``\m athbf{u}``
338+ - `y_step = zeros(plant.ny)` : step disturbance on plant outputs ``\m athbf{y}``
339+ - `y_noise = zeros(plant.ny)` : additive gaussian noise on plant outputs ``\m athbf{y}``
340+ - `d_step = zeros(plant.nd)` : step disturbance on measured dist. ``\m athbf{d}``
341+ - `d_noise = zeros(plant.nd)` : additive gaussian noise on measured dist. ``\m athbf{d}``
342+ - `x0 = zeros(plant.nx)` : plant initial state ``\m athbf{x}(0)``
343+ - `x̂0 = nothing` : `mpc.estim` state estimator initial state ``\m athbf{x̂}(0)``, if `nothing`
344+ [`initstate!(::PredictiveController)`](@ref) is used to init ``\m athbf{x̂}``.
345+ - `lastu = plant.uop` : last manipulated input ``\m athbf{u}`` for ``\m athbf{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. x̂
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
416447Get 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. ŷ
424455end
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. ŷ
438469end
439470
605636" By default, no change to the objective function."
606637set_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
0 commit comments