Skip to content

Commit 3833301

Browse files
authored
Merge pull request #117 from JuliaControl/mtk_obsv_error
Added: details in observability error message, modify CI for new release and MTK exemple debug
2 parents 44dba57 + 8009752 commit 3833301

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ jobs:
1919
fail-fast: false
2020
matrix:
2121
version:
22-
- '1.6'
2322
- '1'
23+
- '1.10'
2424
- 'nightly'
2525
os:
2626
- ubuntu-latest

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ JuMP = "1"
1616
LinearAlgebra = "1.6"
1717
Logging = "1.6"
1818
Plots = "1"
19-
ModelingToolkit = "9"
19+
ModelingToolkit = "~9.41"

docs/src/manual/mtk.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ the last section.
2222
as a basic starting template to combine both packages. There is no guarantee that it
2323
will work for all corner cases.
2424

25+
!!! compat
26+
The example only work with `ModelingToolkit.jl` v9.41 releases.
27+
2528
We first construct and instantiate the pendulum model:
2629

2730
```@example 1
@@ -76,14 +79,18 @@ function generate_f_h(model, inputs, outputs)
7679
end
7780
return nothing
7881
end
79-
h_ = ModelingToolkit.build_explicit_observed_function(io_sys, outputs; inputs)
82+
return_inplace = true
83+
(_, h_ip) = ModelingToolkit.build_explicit_observed_function(
84+
io_sys, outputs; inputs, return_inplace
85+
)
86+
println(h_ip)
8087
u_nothing = fill(nothing, nu)
8188
function h!(y, x, _ , p)
82-
y .= try
89+
try
8390
# MTK.jl supports a `u` argument in `h_` function but not this package. We set
8491
# `u` as a vector of nothing and `h_` function will presumably throw an
8592
# MethodError it this argument is used inside the function
86-
h_(x, u_nothing, p, nothing)
93+
h_ip(y, x, u_nothing, p, nothing)
8794
catch err
8895
if err isa MethodError
8996
error("NonLinModel only support strictly proper systems (no manipulated "*

src/estimator/construct.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ function augment_model(model::LinModel{NT}, As, Cs_u, Cs_y; verify_obsv=true) wh
156156
if verify_obsv && !ControlSystemsBase.observability(Â, Ĉ)[:isobservable]
157157
error("The augmented model is unobservable. You may try to use 0 integrator on "*
158158
"model integrating outputs with nint_ym parameter. Adding integrators at both "*
159-
"inputs (nint_u) and outputs (nint_ym) can also violate observability.")
159+
"inputs (nint_u) and outputs (nint_ym) can also violate observability. If the "*
160+
"model is still unobservable without any integrators, you may need to call "*
161+
"sminreal or minreal on your system.")
160162
end
161163
x̂op, f̂op = [model.xop; zeros(nxs)], [model.fop; zeros(nxs)]
162164
return Â, B̂u, Ĉ, B̂d, D̂d, x̂op, f̂op

src/estimator/kalman.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ struct SteadyKalmanFilter{NT<:Real, SM<:LinModel} <: StateEstimator{NT}
3838
Â, B̂u, Ĉ, B̂d, D̂d, x̂op, f̂op = augment_model(model, As, Cs_u, Cs_y)
3939
Ĉm, D̂dm = Ĉ[i_ym, :], D̂d[i_ym, :]
4040
validate_kfcov(nym, nx̂, Q̂, R̂)
41+
if ny == nym
42+
R̂_y =
43+
else
44+
R̂_y = zeros(NT, ny, ny)
45+
R̂_y[i_ym, i_ym] =
46+
R̂_y = Hermitian(R̂_y, :L)
47+
end
4148
= try
42-
Q̂_kalman = Matrix(Q̂) # Matrix() required for Julia 1.6
43-
R̂_kalman = zeros(NT, ny, ny)
44-
R̂_kalman[i_ym, i_ym] =
45-
ControlSystemsBase.kalman(Discrete, Â, Ĉ, Q̂_kalman, R̂_kalman; direct)[:, i_ym]
49+
ControlSystemsBase.kalman(Discrete, Â, Ĉ, Q̂, R̂_y; direct)[:, i_ym]
4650
catch my_error
4751
if isa(my_error, ErrorException)
4852
error("Cannot compute the optimal Kalman gain K̂ for the "*

src/model/solver.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,11 @@ function get_solver_functions(NT::DataType, solver::RungeKutta, fc!, hc!, Ts, _
4747
k4_cache::DiffCache{Vector{NT}, Vector{NT}} = DiffCache(zeros(NT, nx), Nc)
4848
f! = function inner_solver_f!(xnext, x, u, d, p)
4949
CT = promote_type(eltype(x), eltype(u), eltype(d))
50-
# dummy variable for get_tmp, necessary for PreallocationTools + Julia 1.6 :
51-
var::CT = 0
52-
xcur = get_tmp(xcur_cache, var)
53-
k1 = get_tmp(k1_cache, var)
54-
k2 = get_tmp(k2_cache, var)
55-
k3 = get_tmp(k3_cache, var)
56-
k4 = get_tmp(k4_cache, var)
50+
xcur = get_tmp(xcur_cache, CT)
51+
k1 = get_tmp(k1_cache, CT)
52+
k2 = get_tmp(k2_cache, CT)
53+
k3 = get_tmp(k3_cache, CT)
54+
k4 = get_tmp(k4_cache, CT)
5755
xterm = xnext
5856
@. xcur = x
5957
for i=1:solver.supersample

0 commit comments

Comments
 (0)