@@ -338,25 +338,27 @@ the [`LinMPC`](@ref) instance based on repeated online linearization.
338338The [ ` setmodel! ` ] ( @ref ) method allows online adaptation of a linear plant model. Combined
339339with the automatic linearization of [ ` linearize ` ] ( @ref ) , a successive linearization MPC can
340340be 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 `` :
342343
343344``` @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)
345347mpc3 = LinMPC(kf; Hp, Hc, Mwt, Nwt, Cwt=Inf, optim=daqp)
346348mpc3 = setconstraint!(mpc3; umin, umax)
347349```
348350
349351We create a function that simulates the plant and the adaptive controller:
350352
351353``` @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])
354355 U_data, Y_data, Ry_data = zeros(plant.nu, N), zeros(plant.ny, N), zeros(plant.ny, N)
355356 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
358360 for i = 1:N
359- y = plant() . + y_step
361+ y = plant() + y_step
360362 u = moveinput!(mpc, ry)
361363 linmodel = linearize(nonlinmodel; u, x=x̂[1:2])
362364 setmodel!(mpc, linmodel)
@@ -376,7 +378,8 @@ The [`SimResult`](@ref) object is for plotting purposes only. The adaptive [`Lin
376378performances are similar to the nonlinear MPC, both for the 180° setpoint:
377379
378380``` @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)
380383plot(res_slin)
381384savefig("plot10_NonLinMPC.svg"); nothing # hide
382385```
@@ -386,7 +389,8 @@ savefig("plot10_NonLinMPC.svg"); nothing # hide
386389and the 10° step disturbance:
387390
388391``` @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)
390394plot(res_slin)
391395savefig("plot11_NonLinMPC.svg"); nothing # hide
392396```
0 commit comments