@@ -30,7 +30,7 @@ We first construct and instantiate the pendulum model:
3030
3131``` @example 1
3232using ModelPredictiveControl, ModelingToolkit
33- using ModelingToolkit: D_nounits as D, t_nounits as t, varmap_to_vars
33+ using ModelingToolkit: D_nounits as D, t_nounits as t
3434@parameters g=9.8 L=0.4 K=1.2 m=0.3
3535@variables θ(t)=0 ω(t)=0 τ(t)=0 y(t)
3636eqs = [
@@ -85,35 +85,41 @@ function generate_f_h(model, inputs, outputs)
8585 end
8686 return nothing
8787 end
88- println(bindings(io_sys))
89- p = varmap_to_vars(bindings(io_sys), p_sym)
90- return f!, h!, p, x_sym, nu, nx, ny
88+ ic = initial_conditions(io_sys)
89+ p_map = Dict(sym => ic[sym] for sym in p_sym if haskey(ic, sym))
90+ p = ModelingToolkit.varmap_to_vars(p_map, p_sym)
91+ return f!, h!, p, x_sym, p_sym, nu, nx, ny
9192end
9293inputs, outputs = [τ], [y]
93- f!, h!, p, x_sym, nu, nx, ny = generate_f_h(mtk_model, inputs, outputs)
94+ f!, h!, p, x_sym, p_sym, nu, nx, ny = generate_f_h(mtk_model, inputs, outputs)
9495x_sym
9596```
9697
9798Since MTK is an acausal modeling framework, we do not have the control on the state
9899realization chosen by the package. The content of ` x_sym ` above shows it settled for the
99100state vector `` \mathbf{x}(t) = [\begin{smallmatrix}ω(t) && θ(t)\end{smallmatrix}]' `` ,
100- that is, the states of the [ last section] (@ref man_nonlin) in the reverse order. We can now
101- construct a [ ` NonLinModel ` ] ( @ref ) with this specific state realization:
101+ that is, the states of the [ last section] (@ref man_nonlin) in the reverse order. As the same
102+ also applies for the parameters, the ` p_sym ` object informs on how the ` p ` vector is sorted:
103+
104+ ``` @example 1
105+ [p_sym p]
106+ ```
107+
108+ We can now construct a [ ` NonLinModel ` ] ( @ref ) with this specific state realization:
102109
103110``` @example 1
104111vu, vx, vy = ["\$τ\$ (Nm)"], ["\$ω\$ (rad/s)", "\$θ\$ (rad)"], ["\$θ\$ (°)"]
105112Ts = 0.1
106113model = setname!(NonLinModel(f!, h!, Ts, nu, nx, ny; p); u=vu, x=vx, y=vy)
107114```
108115
109- We also instantiate a plant model with a 25 % larger friction coefficient `` K `` :
116+ We also instantiate a plant model with a 25 % larger friction coefficient `` K `` , which is
117+ the third element of ` p ` , as shown above:
110118
111119``` @example 1
112- plant_bindings = merge(bindings(mtk_model), Dict(K => 1.25 * bindings(mtk_model)[K]))
113- @named mtk_plant = System(eqs, t, [θ, ω, τ, y], [g, L, K, m]; bindings=plant_bindings)
114- inputs, outputs = [mtk_plant.τ], [mtk_plant.y]
115- f2!, h2!, p2 = generate_f_h(mtk_plant, inputs, outputs)
116- plant = setname!(NonLinModel(f2!, h2!, Ts, nu, nx, ny; p=p2), u=vu, x=vx, y=vy)
120+ p2 = copy(p)
121+ p2[3] = 1.25*p[3]
122+ plant = setname!(NonLinModel(f!, h!, Ts, nu, nx, ny; p=p2), u=vu, x=vx, y=vy)
117123```
118124
119125## Controller Design
0 commit comments