@@ -27,14 +27,14 @@ friction coefficient at the pivot point, and ``m``, the mass attached at the end
2727pendulum. Here, the explicit Euler method discretizes the system to construct a
2828[ ` NonLinModel ` ] ( @ref ) :
2929
30- ``` @example 2
30+ ``` @example 1
3131using ModelPredictiveControl
3232function pendulum(par, x, u)
3333 g, L, K, m = par # [m/s], [m], [kg/s], [kg]
3434 θ, ω = x[1], x[2] # [rad], [rad/s]
3535 τ = u[1] # [N m]
3636 dθ = ω
37- dω = -g/L*sin(θ) - k /m*ω + τ/m/L^2
37+ dω = -g/L*sin(θ) - K /m*ω + τ/m/L^2
3838 return [dθ, dω]
3939end
4040Ts = 0.1 # [s]
@@ -48,42 +48,42 @@ model = NonLinModel(f, h, Ts, nu, nx, ny)
4848The output function `` \mathbf{h} `` converts the angular position `` θ `` to degrees. It
4949is good practice to first simulate ` model ` using [ ` sim! ` ] ( @ref ) as a quick sanity check:
5050
51- ``` @example 2
51+ ``` @example 1
5252using Plots
5353u = [0.5] # τ = 0.5 N m
5454plot(sim!(model, 60, u), plotu=false)
5555```
5656
5757An [ ` UnscentedKalmanFilter ` ] ( @ref ) estimates the plant state :
5858
59- ``` @example 2
59+ ``` @example 1
6060estim = UnscentedKalmanFilter(model, σQ=[0.5, 2.5], σQ_int=[0.5])
6161```
6262
6363The standard deviation of the angular velocity `` ω `` is higher here (` σQ ` second value)
6464since `` \dot{ω}(t) `` equation includes an uncertain parameter: the friction coefficient
6565`` K `` . The estimator tuning is tested on a plant simulated with a different `` K `` :
6666
67- ``` @example 2
67+ ``` @example 1
6868par_plant = (par[1], par[2], par[3] + 0.25, par[4])
6969f_plant(x, u, _) = x + Ts*pendulum(par_plant, x, u)
7070plant = NonLinModel(f_plant, h, Ts, nu, nx, ny)
7171res = sim!(estim, 30, [0.5], plant=plant, y_noise=[0.5]) # τ = 0.5 N m
72- p2 = plot(res, plotu=false, plotx=true, plotx̂=true)
72+ plot(res, plotu=false, plotx=true, plotx̂=true)
7373```
7474
7575The Kalman filter performance seems sufficient for control. As the motor torque is limited
7676to -1.5 to 1.5 N m, we incorporate the input constraints in a [ ` NonLinMPC ` ] ( @ref ) :
7777
78- ``` @example 2
78+ ``` @example 1
7979mpc = NonLinMPC(estim, Hp=20, Hc=2, Mwt=[0.1], Nwt=[1.0], Cwt=Inf)
8080mpc = setconstraint!(mpc, umin=[-1.5], umax=[+1.5])
8181```
8282
8383We test ` mpc ` performance on ` plant ` by imposing an angular setpoint of 180° (inverted
8484position):
8585
86- ``` @example 2
86+ ``` @example 1
8787res = sim!(mpc, 30, [180.0], x̂0=zeros(mpc.estim.nx̂), plant=plant, x0=zeros(plant.nx))
8888plot(res, plotŷ=true)
8989```
0 commit comments