|
74 | 74 | inputs, outputs = [mtk_model.τ], [mtk_model.y] |
75 | 75 | f!, h!, p, nu, nx, ny, vx = generate_f_h(mtk_model, inputs, outputs) |
76 | 76 | Ts = 0.1 |
77 | | -vu, vy = ["\$τ\$ (Nm)"], ["\$θ\$ (°)"]</code></pre><p>A <a href="../../public/sim_model/#NonLinModel"><code>NonLinModel</code></a> can now be constructed:</p><pre><code class="language-julia hljs">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, RungeKutta(4) solver and: |
78 | | - 1 manipulated inputs u |
79 | | - 2 states x |
80 | | - 1 outputs y |
81 | | - 0 measured disturbances d</code></pre><p>We also instantiate a plant model with a 25 % larger friction coefficient <span>$K$</span>:</p><pre><code class="language-julia hljs">mtk_model.K = defaults(mtk_model)[mtk_model.K] * 1.25 |
| 77 | +vu, vy = ["\$τ\$ (Nm)"], ["\$θ\$ (°)"]</code></pre><p>A <a href="../../public/sim_model/#NonLinModel"><code>NonLinModel</code></a> can now be constructed:</p><pre><code class="language-julia hljs">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: |
| 78 | +├ solver: RungeKutta(4) |
| 79 | +├ jacobian: AutoForwardDiff |
| 80 | +└ dimensions: |
| 81 | + ├ 1 manipulated inputs u |
| 82 | + ├ 2 states x |
| 83 | + ├ 1 outputs y |
| 84 | + └ 0 measured disturbances d</code></pre><p>We also instantiate a plant model with a 25 % larger friction coefficient <span>$K$</span>:</p><pre><code class="language-julia hljs">mtk_model.K = defaults(mtk_model)[mtk_model.K] * 1.25 |
82 | 85 | f_plant, h_plant, p = generate_f_h(mtk_model, inputs, outputs) |
83 | | -plant = setname!(NonLinModel(f_plant, h_plant, 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, RungeKutta(4) solver and: |
84 | | - 1 manipulated inputs u |
85 | | - 2 states x |
86 | | - 1 outputs y |
87 | | - 0 measured disturbances d</code></pre><h2 id="Controller-Design"><a class="docs-heading-anchor" href="#Controller-Design">Controller Design</a><a id="Controller-Design-1"></a><a class="docs-heading-anchor-permalink" href="#Controller-Design" title="Permalink"></a></h2><p>We can than reproduce the Kalman filter and the controller design of the <a href="../nonlinmpc/#man_nonlin">last section</a>:</p><pre><code class="language-julia hljs">α=0.01; σQ=[0.1, 1.0]; σR=[5.0]; nint_u=[1]; σQint_u=[0.1] |
| 86 | +plant = setname!(NonLinModel(f_plant, h_plant, 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: |
| 87 | +├ solver: RungeKutta(4) |
| 88 | +├ jacobian: AutoForwardDiff |
| 89 | +└ dimensions: |
| 90 | + ├ 1 manipulated inputs u |
| 91 | + ├ 2 states x |
| 92 | + ├ 1 outputs y |
| 93 | + └ 0 measured disturbances d</code></pre><h2 id="Controller-Design"><a class="docs-heading-anchor" href="#Controller-Design">Controller Design</a><a id="Controller-Design-1"></a><a class="docs-heading-anchor-permalink" href="#Controller-Design" title="Permalink"></a></h2><p>We can than reproduce the Kalman filter and the controller design of the <a href="../nonlinmpc/#man_nonlin">last section</a>:</p><pre><code class="language-julia hljs">α=0.01; σQ=[0.1, 1.0]; σR=[5.0]; nint_u=[1]; σQint_u=[0.1] |
88 | 94 | estim = UnscentedKalmanFilter(model; α, σQ, σR, nint_u, σQint_u) |
89 | 95 | Hp, Hc, Mwt, Nwt = 20, 2, [0.5], [2.5] |
90 | 96 | nmpc = NonLinMPC(estim; Hp, Hc, Mwt, Nwt, Cwt=Inf) |
91 | 97 | umin, umax = [-1.5], [+1.5] |
92 | | -nmpc = setconstraint!(nmpc; umin, umax)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">NonLinMPC controller with a sample time Ts = 0.1 s, Ipopt optimizer, SingleShooting transcription, UnscentedKalmanFilter estimator and: |
93 | | - 20 prediction steps Hp |
94 | | - 2 control steps Hc |
95 | | - 0 slack variable ϵ (control constraints) |
96 | | - 1 manipulated inputs u (1 integrating states) |
97 | | - 3 estimated states x̂ |
98 | | - 1 measured outputs ym (0 integrating states) |
99 | | - 0 unmeasured outputs yu |
100 | | - 0 measured disturbances d</code></pre><p>The 180° setpoint response is identical:</p><pre><code class="language-julia hljs">using Plots |
| 98 | +nmpc = setconstraint!(nmpc; umin, umax)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">NonLinMPC controller with a sample time Ts = 0.1 s: |
| 99 | +├ estimator: UnscentedKalmanFilter |
| 100 | +├ model: NonLinModel |
| 101 | +├ optimizer: Ipopt |
| 102 | +├ transcription: SingleShooting |
| 103 | +├ gradient: AutoForwardDiff |
| 104 | +├ jacobian: AutoForwardDiff |
| 105 | +└ dimensions: |
| 106 | + ├ 20 prediction steps Hp |
| 107 | + ├ 2 control steps Hc |
| 108 | + ├ 0 slack variable ϵ (control constraints) |
| 109 | + ├ 1 manipulated inputs u (1 integrating states) |
| 110 | + ├ 3 estimated states x̂ |
| 111 | + ├ 1 measured outputs ym (0 integrating states) |
| 112 | + ├ 0 unmeasured outputs yu |
| 113 | + └ 0 measured disturbances d</code></pre><p>The 180° setpoint response is identical:</p><pre><code class="language-julia hljs">using Plots |
101 | 114 | N = 35 |
102 | 115 | res_ry = sim!(nmpc, N, [180.0], plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0]) |
103 | 116 | 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]) |
104 | | -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.14.1 on <span class="colophon-date" title="Saturday 6 September 2025 23:18">Saturday 6 September 2025</span>. Using Julia version 1.11.6.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
| 117 | +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.14.1 on <span class="colophon-date" title="Sunday 7 September 2025 21:22">Sunday 7 September 2025</span>. Using Julia version 1.11.6.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
0 commit comments