5959
6060@doc raw """
6161 sim(
62- estim::StateEstimator ,
62+ mpc::PredictiveController ,
6363 N::Int,
64- u = mpc.estim.model.yop .+ 1,
65- d = mpc.estim.model.dop;
64+ ry = mpc.estim.model.yop .+ 1,
65+ d = mpc.estim.model.dop;
6666 <keyword arguments>
6767 )
6868
6969Closed-loop simulation of `mpc` controller for `N` time steps, default to setpoint bumps.
7070
71- See Arguments for the option list. The noise arguments are in standard deviations σ. The
72- sensor and process noises of the simulated plant are specified by `y_noise` and `x_noise`
73- arguments, respectively.
74-
71+ `ry` is the output setpoint value applied at ``t = 0`` second. The keyword arguments are
72+ identical to [`sim(::StateEstimator)`](@ref).
7573
7674"""
7775function sim (
@@ -85,9 +83,7 @@ function sim(
8583end
8684
8785
88-
89-
90-
86+ " Quick simulation function for `SimModel`, `StateEstimator` and `PredictiveController`."
9187function sim_all (
9288 estim_mpc:: Union{StateEstimator, PredictiveController} ,
9389 estim:: StateEstimator ,
@@ -159,142 +155,185 @@ sim_getu!(mpc::PredictiveController, ry, d, ym) = moveinput!(mpc, ry, d; ym)
159155 plotRy = true ,
160156 plotŶminŶmax = true ,
161157 plotŶ = false ,
158+ plotU = true ,
162159 plotRu = true ,
163160 plotUminUmax = true ,
164161 plotD = true ,
165162 plotX = false ,
166163 plotX̂ = false
167164)
168-
169165 mpc = res. obj
170- t = res. T_data
171- Ns = length (t)
172166
167+ t = res. T_data
173168 ny = size (res. Y_data, 1 )
174169 nu = size (res. U_data, 1 )
175170 nd = size (res. D_data, 1 )
171+ nx = size (res. X_data, 1 )
172+ nx̂ = size (res. X̂_data, 1 )
173+ layout_mat = [(ny, 1 )]
174+ plotU && (layout_mat = [layout_mat (nu, 1 )])
175+ (plotD && nd ≠ 0 ) && (layout_mat = [layout_mat (nd, 1 )])
176+ plotX && (layout_mat = [layout_mat (nx, 1 )])
177+ plotX̂ && (layout_mat = [layout_mat (nx̂, 1 )])
178+
179+ layout := layout_mat
180+ xguide --> " Time (s)"
176181
177- layout := @layout (nd ≠ 0 && plotD) ? [(ny,1 ) (nu,1 ) (nd, 1 )] : [(ny,1 ) (nu,1 )]
178-
182+ # --- outputs y ---
179183 subplot_base = 0
180184 for i in 1 : ny
181185 @series begin
182- xguide --> " Time (s)"
183186 yguide --> " \$ y_$i \$ "
184187 color --> 1
185188 subplot --> subplot_base + i
186189 label --> " \$\\ mathbf{y}\$ "
190+ legend --> false
187191 t, res. Y_data[i, :]
188192 end
189- if plotRy && ! iszero (mpc . M_Hp)
193+ if plotŶ
190194 @series begin
191- xguide --> " Time (s)"
192195 yguide --> " \$ y_$i \$ "
193196 color --> 2
194197 subplot --> subplot_base + i
198+ linestyle --> :dashdot
199+ linewidth --> 0.75
200+ label --> " \$\\ mathbf{\\ hat{y}}\$ "
201+ legend --> true
202+ t, res. Ŷ_data[i, :]
203+ end
204+ end
205+ if plotRy && ! iszero (mpc. M_Hp)
206+ @series begin
207+ yguide --> " \$ y_$i \$ "
208+ color --> 3
209+ subplot --> subplot_base + i
195210 linestyle --> :dash
211+ linewidth --> 0.75
196212 label --> " \$\\ mathbf{r_y}\$ "
213+ legend --> true
197214 t, res. Ry_data[i, :]
198215 end
199216 end
200217 if plotŶminŶmax && ! isinf (mpc. con. Ŷmin[i])
201218 @series begin
202- xguide --> " Time (s)"
203219 yguide --> " \$ y_$i \$ "
204- color --> 3
220+ color --> 4
205221 subplot --> subplot_base + i
206222 linestyle --> :dot
207223 linewidth --> 2.0
208224 label --> " \$\\ mathbf{\\ hat{y}_{min}}\$ "
209- t, fill (mpc. con. Ŷmin[i], Ns)
225+ legend --> true
226+ t, fill (mpc. con. Ŷmin[i], length (t))
210227 end
211228 end
212229 if plotŶminŶmax && ! isinf (mpc. con. Ŷmax[i])
213230 @series begin
214- xguide --> " Time (s)"
215231 yguide --> " \$ y_$i \$ "
216- color --> 4
232+ color --> 5
217233 subplot --> subplot_base + i
218234 linestyle --> :dot
219235 linewidth --> 2.0
220236 label --> " \$\\ mathbf{\\ hat{y}_{max}}\$ "
221- t, fill (mpc. con. Ŷmax[i], Ns)
222- end
223- end
224- if plotŶ
225- @series begin
226- xguide --> " Time (s)"
227- yguide --> " \$ y_$i \$ "
228- color --> 5
229- subplot --> subplot_base + i
230- linestyle --> :dashdot
231- label --> " \$\\ mathbf{\\ hat{y}}\$ "
232- t, res. Ŷ_data[i, :]
237+ legend --> true
238+ t, fill (mpc. con. Ŷmax[i], length (t))
233239 end
234240 end
235241 end
236242 subplot_base += ny
237- for i in 1 : nu
238- @series begin
239- xguide --> " Time (s)"
240- yguide --> " \$ u_$i \$ "
241- color --> 1
242- subplot --> subplot_base + i
243- seriestype --> :steppost
244- label --> " \$\\ mathbf{u}\$ "
245- t, res. U_data[i, :]
246- end
247- if plotRu && ! iszero (mpc. L_Hp) # TODO :
248- #=
243+ # --- manipulated inputs u ---
244+ if plotU
245+ for i in 1 : nu
249246 @series begin
250- xguide --> "Time (s) "
251- yguide --> "\$u_$i\$"
252- color --> 2
253- subplot --> subplot_base + i
254- linestyle --> :dash
255- label --> "\$\\mathbf{r_{u}}\$"
256- t, res.Ry_data [i, :]
247+ yguide --> " \$ u_ $i \$ "
248+ color --> 1
249+ subplot --> subplot_base + i
250+ seriestype --> :steppost
251+ label --> " \$\\ mathbf{u} \$ "
252+ legend --> false
253+ t, res. U_data [i, :]
257254 end
258- =#
259- end
260- if plotUminUmax && ! isinf (mpc. con. Umin[i])
261- @series begin
262- xguide --> " Time (s)"
263- yguide --> " \$ u_$i \$ "
264- color --> 3
265- subplot --> subplot_base + i
266- linestyle --> :dot
267- linewidth --> 2.0
268- label --> " \$\\ mathbf{u_{min}}\$ "
269- t, fill (mpc. con. Umin[i], Ns)
255+ if plotRu && ! iszero (mpc. L_Hp)
256+ @series begin
257+ yguide --> " \$ u_$i \$ "
258+ color --> 3
259+ subplot --> subplot_base + i
260+ linestyle --> :dash
261+ label --> " \$\\ mathbf{r_{u}}\$ "
262+ legend --> true
263+ t, res. Ry_data[i, :]
264+ end
270265 end
271- end
272- if plotUminUmax && ! isinf (mpc. con. Umax[i])
273- @series begin
274- xguide --> " Time (s)"
275- yguide --> " \$ u_$i \$ "
276- color --> 4
277- subplot --> subplot_base + i
278- linestyle --> :dot
279- linewidth --> 2.0
280- label --> " \$\\ mathbf{u_{max}}\$ "
281- t, fill (mpc. con. Umax[i], Ns)
266+ if plotUminUmax && ! isinf (mpc. con. Umin[i])
267+ @series begin
268+ yguide --> " \$ u_$i \$ "
269+ color --> 4
270+ subplot --> subplot_base + i
271+ linestyle --> :dot
272+ linewidth --> 2.0
273+ label --> " \$\\ mathbf{u_{min}}\$ "
274+ legend --> true
275+ t, fill (mpc. con. Umin[i], length (t))
276+ end
277+ end
278+ if plotUminUmax && ! isinf (mpc. con. Umax[i])
279+ @series begin
280+ yguide --> " \$ u_$i \$ "
281+ color --> 5
282+ subplot --> subplot_base + i
283+ linestyle --> :dot
284+ linewidth --> 2.0
285+ label --> " \$\\ mathbf{u_{max}}\$ "
286+ legend --> true
287+ t, fill (mpc. con. Umax[i], length (t))
288+ end
282289 end
283290 end
291+ subplot_base += nu
284292 end
285- subplot_base += nu
293+ # --- measured disturbances d ---
286294 if plotD
287295 for i in 1 : nd
288296 @series begin
289297 xguide --> " Time (s)"
290298 yguide --> " \$ d_$i \$ "
291299 color --> 1
292300 subplot --> subplot_base + i
293- label --> " "
301+ label --> " \$\\ mathbf{d}\$ "
302+ legend --> false
294303 t, res. D_data[i, :]
295304 end
296305 end
297306 end
307+ # --- plant states x ---
308+ if plotX
309+ for i in 1 : nx
310+ @series begin
311+ yguide --> " \$ x_$i \$ "
312+ color --> 1
313+ subplot --> subplot_base + i
314+ label --> " \$\\ mathbf{x}\$ "
315+ legend --> false
316+ t, res. X_data[i, :]
317+ end
318+ end
319+ subplot_base += nx
320+ end
321+ # --- estimated states x̂ ---
322+ if plotX̂
323+ for i in 1 : nx̂
324+ @series begin
325+ yguide --> " \$\\ hat{x}_$i \$ "
326+ color --> 2
327+ subplot --> subplot_base + i
328+ linestyle --> :dashdot
329+ linewidth --> 0.75
330+ label --> " \$\\ mathbf{\\ hat{x}}\$ "
331+ legend --> false
332+ t, res. X̂_data[i, :]
333+ end
334+ end
335+ end
336+
298337end
299338
300339
@@ -307,25 +346,20 @@ end
307346 plotX = false ,
308347 plotX̂ = false
309348)
310-
311349 t = res. T_data
312-
313350 ny = size (res. Y_data, 1 )
314351 nu = size (res. U_data, 1 )
315352 nd = size (res. D_data, 1 )
316353 nx = size (res. X_data, 1 )
317354 nx̂ = size (res. X̂_data, 1 )
318-
319355 layout_mat = [(ny, 1 )]
320356 plotU && (layout_mat = [layout_mat (nu, 1 )])
321357 (plotD && nd ≠ 0 ) && (layout_mat = [layout_mat (nd, 1 )])
322358 plotX && (layout_mat = [layout_mat (nx, 1 )])
323359 plotX̂ && (layout_mat = [layout_mat (nx̂, 1 )])
324360
325361 layout := layout_mat
326-
327362 xguide --> " Time (s)"
328-
329363 # --- outputs y ---
330364 subplot_base = 0
331365 for i in 1 : ny
340374 if plotŶ
341375 @series begin
342376 yguide --> " \$ y_$i \$ "
343- color --> 5
377+ color --> 2
344378 subplot --> subplot_base + i
345379 linestyle --> :dashdot
380+ linewidth --> 0.75
346381 label --> " \$\\ mathbf{\\ hat{y}}\$ "
347382 legend --> true
348383 t, res. Ŷ_data[i, :]
398433 for i in 1 : nx̂
399434 @series begin
400435 yguide --> " \$\\ hat{x}_$i \$ "
401- color --> 5
436+ color --> 2
402437 subplot --> subplot_base + i
403438 linestyle --> :dashdot
439+ linewidth --> 0.75
404440 label --> " \$\\ mathbf{\\ hat{x}}\$ "
405441 legend --> false
406442 t, res. X̂_data[i, :]
0 commit comments