Skip to content

Commit b5912f7

Browse files
committed
wip: trying to update to MTK v11
1 parent 20f8634 commit b5912f7

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

docs/src/manual/mtk.md

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,30 @@ the last section.
2323
will work for all corner cases.
2424

2525
!!! compat
26-
The example works on `ModelingToolkit.jl` v10 (corresponding to the following `[compat]`
27-
entry: `ModelingToolkit = "10"`).
26+
The example works on `ModelingToolkit.jl` v11 (corresponding to the following `[compat]`
27+
entry: `ModelingToolkit = "11"`).
2828

2929
We first construct and instantiate the pendulum model:
3030

3131
```@example 1
3232
using ModelPredictiveControl, ModelingToolkit
3333
using ModelingToolkit: D_nounits as D, t_nounits as t, varmap_to_vars
34-
@mtkmodel Pendulum begin
35-
@parameters begin
36-
g = 9.8
37-
L = 0.4
38-
K = 1.2
39-
m = 0.3
40-
end
41-
@variables begin
42-
θ(t) # state
43-
ω(t) # state
44-
τ(t) # input
45-
y(t) # output
46-
end
47-
@equations begin
48-
D(θ) ~ ω
49-
D(ω) ~ -g/L*sin(θ) - K/m*ω + τ/m/L^2
50-
y ~ θ * 180 / π
51-
end
52-
end
53-
@named mtk_model = Pendulum()
54-
mtk_model = complete(mtk_model)
34+
@parameters g=9.8 L=0.4 K=1.2 m=0.3
35+
@variables θ(t)=0 ω(t)=0 τ(t)=0 y(t)
36+
eqs = [
37+
D(θ) ~ ω
38+
D(ω) ~ -g/L*sin(θ) - K/m*ω + τ/m/L^2
39+
y ~ θ * 180 / π
40+
]
41+
@named mtk_model = System(eqs, t)
5542
```
5643

5744
We than convert the MTK model to an [input-output system](https://docs.sciml.ai/ModelingToolkit/stable/basics/InputOutput/):
5845

5946
```@example 1
6047
function generate_f_h(model, inputs, outputs)
6148
(_, f_ip), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function(
62-
model, inputs, split=false; outputs
49+
model, inputs, split=false, simplify=true
6350
)
6451
if any(ModelingToolkit.is_alg_equation, equations(io_sys))
6552
error("Systems with algebraic equations are not supported")
@@ -98,10 +85,11 @@ function generate_f_h(model, inputs, outputs)
9885
end
9986
return nothing
10087
end
101-
p = varmap_to_vars(defaults(io_sys), p_sym)
88+
println(bindings(io_sys))
89+
p = varmap_to_vars(bindings(io_sys), p_sym)
10290
return f!, h!, p, x_sym, nu, nx, ny
10391
end
104-
inputs, outputs = [mtk_model.τ], [mtk_model.y]
92+
inputs, outputs = [τ], [y]
10593
f!, h!, p, x_sym, nu, nx, ny = generate_f_h(mtk_model, inputs, outputs)
10694
x_sym
10795
```
@@ -121,8 +109,8 @@ model = setname!(NonLinModel(f!, h!, Ts, nu, nx, ny; p); u=vu, x=vx, y=vy)
121109
We also instantiate a plant model with a 25 % larger friction coefficient ``K``:
122110

123111
```@example 1
124-
@named mtk_plant = Pendulum(K=1.25*defaults(mtk_model)[mtk_model.K])
125-
mtk_plant = complete(mtk_plant)
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)
126114
inputs, outputs = [mtk_plant.τ], [mtk_plant.y]
127115
f2!, h2!, p2 = generate_f_h(mtk_plant, inputs, outputs)
128116
plant = setname!(NonLinModel(f2!, h2!, Ts, nu, nx, ny; p=p2), u=vu, x=vx, y=vy)

0 commit comments

Comments
 (0)