@@ -338,25 +338,27 @@ the [`LinMPC`](@ref) instance based on repeated online linearization.
338
338
The [ ` setmodel! ` ] ( @ref ) method allows online adaptation of a linear plant model. Combined
339
339
with the automatic linearization of [ ` linearize ` ] ( @ref ) , a successive linearization MPC can
340
340
be designed with minimal efforts. The [ ` SteadyKalmanFilter ` ] ( @ref ) does not support
341
- [ ` setmodel! ` ] ( @ref ) , so we need to use the time-varying [ ` KalmanFilter ` ] ( @ref ) instead:
341
+ [ ` setmodel! ` ] ( @ref ) so we need to use the time-varying [ ` KalmanFilter ` ] ( @ref ) , and we
342
+ initialize it with a linearization at `` θ = ω = τ = 0 `` :
342
343
343
344
``` @example 1
344
- kf = KalmanFilter(linmodel; σQ, σR, nint_u, σQint_u)
345
+ linmodel = linearize(model, x=[0, 0], u=[0])
346
+ kf = KalmanFilter(linmodel; σQ, σR, nint_u, σQint_u)
345
347
mpc3 = LinMPC(kf; Hp, Hc, Mwt, Nwt, Cwt=Inf, optim=daqp)
346
348
mpc3 = setconstraint!(mpc3; umin, umax)
347
349
```
348
350
349
351
We create a function that simulates the plant and the adaptive controller:
350
352
351
353
``` @example 1
352
- function test_slmpc(nonlinmodel, mpc, ry, plant; x_0=plant.xop, y_step=0)
353
- N = 35
354
+ function sim_adapt!(mpc, nonlinmodel, N, ry, plant, x_0, x̂_0, y_step=[0])
354
355
U_data, Y_data, Ry_data = zeros(plant.nu, N), zeros(plant.ny, N), zeros(plant.ny, N)
355
356
setstate!(plant, x_0)
356
- u, y = [0.0], plant()
357
- x̂ = initstate!(mpc, u, y)
357
+ initstate!(mpc, [0], plant())
358
+ setstate!(mpc, x̂_0)
359
+ x̂ = x̂_0
358
360
for i = 1:N
359
- y = plant() . + y_step
361
+ y = plant() + y_step
360
362
u = moveinput!(mpc, ry)
361
363
linmodel = linearize(nonlinmodel; u, x=x̂[1:2])
362
364
setmodel!(mpc, linmodel)
@@ -376,7 +378,8 @@ The [`SimResult`](@ref) object is for plotting purposes only. The adaptive [`Lin
376
378
performances are similar to the nonlinear MPC, both for the 180° setpoint:
377
379
378
380
``` @example 1
379
- res_slin = test_slmpc(model, mpc3, [180], plant, x_0=[0, 0])
381
+ x_0 = [0, 0]; x̂_0 = [0, 0, 0]; ry = [180]
382
+ res_slin = sim_adapt!(mpc3, model, N, ry, plant, x_0, x̂_0)
380
383
plot(res_slin)
381
384
savefig("plot10_NonLinMPC.svg"); nothing # hide
382
385
```
@@ -386,7 +389,8 @@ savefig("plot10_NonLinMPC.svg"); nothing # hide
386
389
and the 10° step disturbance:
387
390
388
391
``` @example 1
389
- res_slin = test_slmpc(model, mpc3, [180], plant, x_0=[π, 0], y_step=[10])
392
+ x_0 = [π, 0]; x̂_0 = [π, 0, 0]; y_step = [10]
393
+ res_slin = sim_adapt!(mpc3, model, N, ry, plant, x_0, x̂_0, y_step)
390
394
plot(res_slin)
391
395
savefig("plot11_NonLinMPC.svg"); nothing # hide
392
396
```
0 commit comments