@@ -58,15 +58,14 @@ We than convert the MTK model to an [input-output system](https://docs.sciml.ai/
5858
5959``` @example 1
6060function generate_f_h(model, inputs, outputs)
61- (_, f_ip), dvs, psym , io_sys = ModelingToolkit.generate_control_function(
61+ (_, f_ip), x_sym, p_sym , io_sys = ModelingToolkit.generate_control_function(
6262 model, inputs, split=false; outputs
6363 )
6464 if any(ModelingToolkit.is_alg_equation, equations(io_sys))
6565 error("Systems with algebraic equations are not supported")
6666 end
67- nu, nx, ny = length(inputs), length(dvs), length(outputs)
68- vx = string.(dvs)
69- p = varmap_to_vars(defaults(io_sys), psym)
67+ nu, nx, ny = length(inputs), length(x_sym), length(outputs)
68+ vx = string.(x_sym)
7069 function f!(ẋ, x, u, _ , p)
7170 try
7271 f_ip(ẋ, x, u, p, nothing)
@@ -100,10 +99,11 @@ function generate_f_h(model, inputs, outputs)
10099 end
101100 return nothing
102101 end
103- return f!, h!, p, nu, nx, ny, vx
102+ p = varmap_to_vars(defaults(io_sys), p_sym)
103+ return f!, h!, p, vx, nu, nx, ny
104104end
105105inputs, outputs = [mtk_model.τ], [mtk_model.y]
106- f!, h!, p, nu, nx, ny, vx = generate_f_h(mtk_model, inputs, outputs)
106+ f!, h!, p, vx, nu, nx, ny = generate_f_h(mtk_model, inputs, outputs)
107107Ts = 0.1
108108vu, vy = ["\$τ\$ (Nm)"], ["\$θ\$ (°)"]
109109nothing # hide
@@ -118,9 +118,11 @@ model = setname!(NonLinModel(f!, h!, Ts, nu, nx, ny; p); u=vu, x=vx, y=vy)
118118We also instantiate a plant model with a 25 % larger friction coefficient `` K `` :
119119
120120``` @example 1
121- mtk_model.K = defaults(mtk_model)[mtk_model.K] * 1.25
122- f_plant, h_plant, p = generate_f_h(mtk_model, inputs, outputs)
123- plant = setname!(NonLinModel(f_plant, h_plant, Ts, nu, nx, ny; p); u=vu, x=vx, y=vy)
121+ @named mtk_model_plant = Pendulum(K=1.2*1.25)
122+ mtk_model_plant = complete(mtk_model_plant)
123+ f2!, h2!, p2, vx2 = generate_f_h(mtk_model_plant, inputs, outputs)
124+ plant = NonLinModel(f2!, h2!, Ts, nu, nx, ny; p=p2)
125+ plant = setname!(plant, u=vu, x=vx2, y=vy)
124126```
125127
126128## Controller Design
0 commit comments