Skip to content

Commit 60a9a18

Browse files
authored
Merge pull request #63 from JuliaControl/plot_var_names
added: `setname!` function for variable names in plot labels
2 parents 5150957 + 0c84034 commit 60a9a18

File tree

8 files changed

+143
-48
lines changed

8 files changed

+143
-48
lines changed

docs/src/manual/nonlinmpc.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ end
5353
const par = (9.8, 0.4, 1.2, 0.3)
5454
f(x, u, _ ) = pendulum(par, x, u)
5555
h(x, _ ) = [180/π*x[1]] # [°]
56-
Ts, nu, nx, ny = 0.1, 1, 2, 1
57-
model = NonLinModel(f, h, Ts, nu, nx, ny)
56+
nu, nx, ny, Ts = 1, 2, 1, 0.1
57+
vu, vx, vy = ["\$τ\$ (N m)"], ["\$θ\$ (rad)", "\$ω\$ (rad/s)"], ["\$θ\$ (°)"]
58+
model = setname!(NonLinModel(f, h, Ts, nu, nx, ny); u=vu, x=vx, y=vy)
5859
```
5960

6061
The output function ``\mathbf{h}`` converts the ``θ`` angle to degrees. Note that special
@@ -72,6 +73,8 @@ plot(res, plotu=false)
7273
savefig(ans, "plot1_NonLinMPC.svg"); nothing # hide
7374
```
7475

76+
The [`setname!`](@ref) function allows customizing the Y-axis labels.
77+
7578
![plot1_NonLinMPC](plot1_NonLinMPC.svg)
7679

7780
## Nonlinear Model Predictive Controller
@@ -93,7 +96,7 @@ estimator tuning is tested on a plant with a 25 % larger friction coefficient ``
9396
```@example 1
9497
const par_plant = (par[1], par[2], 1.25*par[3], par[4])
9598
f_plant(x, u, _ ) = pendulum(par_plant, x, u)
96-
plant = NonLinModel(f_plant, h, Ts, nu, nx, ny)
99+
plant = setname!(NonLinModel(f_plant, h, Ts, nu, nx, ny); u=vu, x=vx, y=vy)
97100
res = sim!(estim, N, [0.5], plant=plant, y_noise=[0.5])
98101
plot(res, plotu=false, plotxwithx̂=true)
99102
savefig(ans, "plot2_NonLinMPC.svg"); nothing # hide
@@ -173,8 +176,8 @@ Kalman Filter similar to the previous one (``\mathbf{y^m} = θ`` and ``\mathbf{y
173176
```@example 1
174177
h2(x, _ ) = [180/π*x[1], x[2]]
175178
nu, nx, ny = 1, 2, 2
176-
model2 = NonLinModel(f , h2, Ts, nu, nx, ny)
177-
plant2 = NonLinModel(f_plant, h2, Ts, nu, nx, ny)
179+
model2 = setname!(NonLinModel(f , h2, Ts, nu, nx, ny), u=vu, x=vx, y=[vy; vx[2]])
180+
plant2 = setname!(NonLinModel(f_plant, h2, Ts, nu, nx, ny), u=vu, x=vx, y=[vy; vx[2]])
178181
estim2 = UnscentedKalmanFilter(model2; σQ, σR, nint_u, σQint_u, i_ym=[1])
179182
```
180183

docs/src/public/sim_model.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,10 @@ LinModel
2929
NonLinModel
3030
```
3131

32-
## Differential Equation Solvers
33-
34-
### DiffSolver
35-
36-
```@docs
37-
ModelPredictiveControl.DiffSolver
38-
```
39-
40-
### RungeKutta
32+
## Set Variable Names
4133

4234
```@docs
43-
RungeKutta
35+
setname!
4436
```
4537

4638
## Set Operating Points
@@ -55,3 +47,17 @@ setop!
5547
linearize
5648
linearize!
5749
```
50+
51+
## Differential Equation Solvers
52+
53+
### DiffSolver
54+
55+
```@docs
56+
ModelPredictiveControl.DiffSolver
57+
```
58+
59+
### RungeKutta
60+
61+
```@docs
62+
RungeKutta
63+
```

src/ModelPredictiveControl.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import OSQP, Ipopt
2323

2424
export SimModel, LinModel, NonLinModel
2525
export DiffSolver, RungeKutta
26-
export setop!, setstate!, setmodel!, updatestate!, evaloutput, linearize, linearize!
26+
export setop!, setname!
27+
export setstate!, setmodel!, updatestate!, evaloutput, linearize, linearize!
2728
export StateEstimator, InternalModel, Luenberger
2829
export SteadyKalmanFilter, KalmanFilter, UnscentedKalmanFilter, ExtendedKalmanFilter
2930
export MovingHorizonEstimator

src/model/linearization.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,17 @@ function linearize(model::SimModel{NT}; kwargs...) where NT<:Real
8080
Bd = Matrix{NT}(undef, nx, nd)
8181
Dd = Matrix{NT}(undef, ny, nd)
8282
linmodel = LinModel{NT}(A, Bu, C, Bd, Dd, model.Ts)
83+
linmodel.uname .= model.uname
84+
linmodel.xname .= model.xname
85+
linmodel.yname .= model.yname
86+
linmodel.dname .= model.dname
8387
return linearize!(linmodel, model; kwargs...)
8488
end
8589

8690
"""
8791
linearize!(linmodel::LinModel, model::SimModel; <keyword arguments>) -> linmodel
8892
89-
Linearize `model` and store the result in `linmodel`.
93+
Linearize `model` and store the result in `linmodel` (in-place).
9094
9195
The keyword arguments are identical to [`linearize`](@ref).
9296

src/model/linmodel.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ struct LinModel{NT<:Real} <: SimModel{NT}
1515
dop::Vector{NT}
1616
xop::Vector{NT}
1717
fop::Vector{NT}
18+
uname::Vector{String}
19+
yname::Vector{String}
20+
dname::Vector{String}
21+
xname::Vector{String}
1822
function LinModel{NT}(A, Bu, C, Bd, Dd, Ts) where {NT<:Real}
1923
A, Bu = to_mat(A, 1, 1), to_mat(Bu, 1, 1)
2024
nu, nx = size(Bu, 2), size(A, 2)
@@ -35,8 +39,19 @@ struct LinModel{NT<:Real} <: SimModel{NT}
3539
dop = zeros(NT, nd)
3640
xop = zeros(NT, nx)
3741
fop = zeros(NT, nx)
42+
uname = ["\$u_{$i}\$" for i in 1:nu]
43+
yname = ["\$y_{$i}\$" for i in 1:ny]
44+
dname = ["\$d_{$i}\$" for i in 1:nd]
45+
xname = ["\$x_{$i}\$" for i in 1:nx]
3846
x0 = zeros(NT, nx)
39-
return new(A, Bu, C, Bd, Dd, x0, Ts, nu, nx, ny, nd, uop, yop, dop, xop, fop)
47+
return new{NT}(
48+
A, Bu, C, Bd, Dd,
49+
x0,
50+
Ts,
51+
nu, nx, ny, nd,
52+
uop, yop, dop, xop, fop,
53+
uname, yname, dname, xname
54+
)
4055
end
4156
end
4257

src/model/nonlinmodel.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ struct NonLinModel{NT<:Real, F<:Function, H<:Function, DS<:DiffSolver} <: SimMod
1313
dop::Vector{NT}
1414
xop::Vector{NT}
1515
fop::Vector{NT}
16+
uname::Vector{String}
17+
yname::Vector{String}
18+
dname::Vector{String}
19+
xname::Vector{String}
1620
function NonLinModel{NT, F, H, DS}(
1721
f!::F, h!::H, solver::DS, Ts, nu, nx, ny, nd
1822
) where {NT<:Real, F<:Function, H<:Function, DS<:DiffSolver}
@@ -22,9 +26,18 @@ struct NonLinModel{NT<:Real, F<:Function, H<:Function, DS<:DiffSolver} <: SimMod
2226
dop = zeros(NT, nd)
2327
xop = zeros(NT, nx)
2428
fop = zeros(NT, nx)
29+
uname = ["\$u_{$i}\$" for i in 1:nu]
30+
yname = ["\$y_{$i}\$" for i in 1:ny]
31+
dname = ["\$d_{$i}\$" for i in 1:nd]
32+
xname = ["\$x_{$i}\$" for i in 1:nx]
2533
x0 = zeros(NT, nx)
2634
return new{NT, F, H, DS}(
27-
x0, f!, h!, solver, Ts, nu, nx, ny, nd, uop, yop, dop, xop, fop
35+
x0,
36+
f!, h!,
37+
solver, Ts,
38+
nu, nx, ny, nd,
39+
uop, yop, dop, xop, fop,
40+
uname, yname, dname, xname
2841
)
2942
end
3043
end

0 commit comments

Comments
 (0)