|
10 | 10 | ] |
11 | 11 | @named mtk_model = System(eqs, t)</code></pre><p class="math-container">\[ \begin{align} |
12 | 12 | \frac{\mathrm{d} ~ \theta\left( t \right)}{\mathrm{d}t} &= \omega\left( t \right) \\ |
13 | | -\frac{\mathrm{d} ~ \omega\left( t \right)}{\mathrm{d}t} &= \frac{ - K ~ \omega\left( t \right)}{m} + \frac{ - g ~ \sin\left( \theta\left( t \right) \right)}{L} + \frac{\tau\left( t \right)}{L^{2} ~ m} \\ |
| 13 | +\frac{\mathrm{d} ~ \omega\left( t \right)}{\mathrm{d}t} &= \frac{ - K ~ \omega\left( t \right)}{m} + \frac{\tau\left( t \right)}{L^{2} ~ m} + \frac{ - g ~ \sin\left( \theta\left( t \right) \right)}{L} \\ |
14 | 14 | y\left( t \right) &= 57.296 ~ \theta\left( t \right) |
15 | 15 | \end{align} |
16 | 16 | \]</p><p>We than convert the MTK model to an <a href="https://docs.sciml.ai/ModelingToolkit/stable/basics/InputOutput/">input-output system</a>:</p><pre><code class="language-julia hljs">function generate_f_h(model, inputs, outputs) |
|
72 | 72 | x_sym</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">2-element Vector{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}}: |
73 | 73 | ω(t) |
74 | 74 | θ(t)</code></pre><p>Since MTK is an acausal modeling framework, we do not have the control on the state realization chosen by the package. The content of <code>x_sym</code> above shows it settled for the state vector <span>$\mathbf{x}(t) = [\begin{smallmatrix}ω(t) && θ(t)\end{smallmatrix}]'$</span>, that is, the states of the <a href="../nonlinmpc/#man_nonlin">last section</a> in the reverse order. As the same also applies for the parameters, the <code>p_sym</code> object informs on how the <code>p</code> vector is sorted:</p><pre><code class="language-julia hljs">[p_sym p]</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">4×2 Matrix{Any}: |
75 | | - K 1.2 |
| 75 | + L 0.4 |
76 | 76 | m 0.3 |
77 | | - g 9.8 |
78 | | - L 0.4</code></pre><p>We can now construct a <a href="../../public/sim_model/#NonLinModel"><code>NonLinModel</code></a> with this specific state realization:</p><pre><code class="language-julia hljs">vu, vx, vy = ["\$τ\$ (Nm)"], ["\$ω\$ (rad/s)", "\$θ\$ (rad)"], ["\$θ\$ (°)"] |
| 77 | + K 1.2 |
| 78 | + g 9.8</code></pre><p>We can now construct a <a href="../../public/sim_model/#NonLinModel"><code>NonLinModel</code></a> with this specific state realization:</p><pre><code class="language-julia hljs">vu, vx, vy = ["\$τ\$ (Nm)"], ["\$ω\$ (rad/s)", "\$θ\$ (rad)"], ["\$θ\$ (°)"] |
79 | 79 | Ts = 0.1 |
80 | 80 | model = setname!(NonLinModel(f!, h!, Ts, nu, nx, ny; p); u=vu, x=vx, y=vy)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">NonLinModel with a sample time Ts = 0.1 s: |
81 | 81 | ├ solver: RungeKutta(4) |
|
119 | 119 | N = 35 |
120 | 120 | res_ry = sim!(nmpc, N, [180.0], plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0]) |
121 | 121 | plot(res_ry)</code></pre><p><img src="../plot1_MTK.svg" alt="plot1_MTK"/></p><p>and also the output disturbance rejection:</p><pre><code class="language-julia hljs">res_yd = sim!(nmpc, N, [180.0], plant=plant, x_0=[0, π], x̂_0=[0, π, 0], y_step=[10]) |
122 | | -plot(res_yd)</code></pre><p><img src="../plot2_MTK.svg" alt="plot2_MTK"/></p><h2 id="Acknowledgement"><a class="docs-heading-anchor" href="#Acknowledgement">Acknowledgement</a><a id="Acknowledgement-1"></a><a class="docs-heading-anchor-permalink" href="#Acknowledgement" title="Permalink"></a></h2><p>Authored by <code>1-Bart-1</code> and <code>baggepinnen</code>, thanks for the contribution.</p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../nonlinmpc/">« Nonlinear Design</a><a class="docs-footer-nextpage" href="../../public/sim_model/">Plant Models »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.17.0 on <span class="colophon-date" title="Wednesday 1 April 2026 13:26">Wednesday 1 April 2026</span>. Using Julia version 1.12.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
| 122 | +plot(res_yd)</code></pre><p><img src="../plot2_MTK.svg" alt="plot2_MTK"/></p><h2 id="Acknowledgement"><a class="docs-heading-anchor" href="#Acknowledgement">Acknowledgement</a><a id="Acknowledgement-1"></a><a class="docs-heading-anchor-permalink" href="#Acknowledgement" title="Permalink"></a></h2><p>Authored by <code>1-Bart-1</code> and <code>baggepinnen</code>, thanks for the contribution.</p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../nonlinmpc/">« Nonlinear Design</a><a class="docs-footer-nextpage" href="../../public/sim_model/">Plant Models »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.17.0 on <span class="colophon-date" title="Wednesday 1 April 2026 23:21">Wednesday 1 April 2026</span>. Using Julia version 1.12.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
0 commit comments