Skip to content

Commit 97363e3

Browse files
committed
KalmanFilter and SteadyKalmanFilter parametric to avoid dynamic dispatch
1 parent d81f920 commit 97363e3

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

src/estimator/kalman.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
struct SteadyKalmanFilter <: StateEstimator
2-
model::LinModel
1+
struct SteadyKalmanFilter{M<:LinModel} <: StateEstimator
2+
model::M
33
::Vector{Float64}
44
i_ym::Vector{Int}
55
nx̂::Int
@@ -19,7 +19,7 @@ struct SteadyKalmanFilter <: StateEstimator
1919
::Hermitian{Float64, Matrix{Float64}}
2020
::Hermitian{Float64, Matrix{Float64}}
2121
K::Matrix{Float64}
22-
function SteadyKalmanFilter(model, i_ym, nint_ym, Asm, Csm, Q̂, R̂)
22+
function SteadyKalmanFilter{M}(model::M, i_ym, nint_ym, Asm, Csm, Q̂, R̂) where {M<:LinModel}
2323
nx, ny = model.nx, model.ny
2424
nym, nyu = length(i_ym), ny - length(i_ym)
2525
nxs = size(Asm,1)
@@ -119,21 +119,21 @@ you can use 0 integrator on `model` integrating outputs, or the alternative time
119119
[`KalmanFilter`](@ref).
120120
"""
121121
function SteadyKalmanFilter(
122-
model::LinModel;
122+
model::M;
123123
i_ym::IntRangeOrVector = 1:model.ny,
124124
σQ::Vector{<:Real} = fill(0.1, model.nx),
125125
σR::Vector{<:Real} = fill(0.1, length(i_ym)),
126126
nint_ym::IntVectorOrInt = fill(1, length(i_ym)),
127127
σQ_int::Vector{<:Real} = fill(0.1, max(sum(nint_ym), 0))
128-
)
128+
) where {M<:LinModel}
129129
if nint_ym == 0 # alias for no output integrator at all :
130130
nint_ym = fill(0, length(i_ym));
131131
end
132132
Asm, Csm = init_estimstoch(i_ym, nint_ym)
133133
# estimated covariances matrices (variance = σ²) :
134134
= Diagonal{Float64}([σQ ; σQ_int ].^2);
135135
= Diagonal{Float64}(σR.^2);
136-
return SteadyKalmanFilter(model, i_ym, nint_ym, Asm, Csm, Q̂ , R̂)
136+
return SteadyKalmanFilter{M}(model, i_ym, nint_ym, Asm, Csm, Q̂ , R̂)
137137
end
138138

139139
@doc raw"""
@@ -166,8 +166,8 @@ function updatestate!(estim::SteadyKalmanFilter, u, ym, d=Float64[])
166166
end
167167

168168

169-
struct KalmanFilter <: StateEstimator
170-
model::LinModel
169+
struct KalmanFilter{M<:LinModel} <: StateEstimator
170+
model::M
171171
::Vector{Float64}
172172
::Hermitian{Float64, Matrix{Float64}}
173173
i_ym::Vector{Int}
@@ -188,7 +188,7 @@ struct KalmanFilter <: StateEstimator
188188
P̂0::Hermitian{Float64, Matrix{Float64}}
189189
::Hermitian{Float64, Matrix{Float64}}
190190
::Hermitian{Float64, Matrix{Float64}}
191-
function KalmanFilter(model, i_ym, nint_ym, Asm, Csm, P̂0, Q̂, R̂)
191+
function KalmanFilter{M}(model::M, i_ym, nint_ym, Asm, Csm, P̂0, Q̂, R̂) where {M<:LinModel}
192192
nx, ny = model.nx, model.ny
193193
nym, nyu = length(i_ym), ny - length(i_ym)
194194
nxs = size(Asm,1)
@@ -247,15 +247,15 @@ KalmanFilter estimator with a sample time Ts = 0.5 s, LinModel and:
247247
```
248248
"""
249249
function KalmanFilter(
250-
model::LinModel;
250+
model::M;
251251
i_ym::IntRangeOrVector = 1:model.ny,
252252
σP0::Vector{<:Real} = fill(10, model.nx),
253253
σQ::Vector{<:Real} = fill(0.1, model.nx),
254254
σR::Vector{<:Real} = fill(0.1, length(i_ym)),
255255
nint_ym::IntVectorOrInt = fill(1, length(i_ym)),
256256
σP0_int::Vector{<:Real} = fill(10, max(sum(nint_ym), 0)),
257257
σQ_int::Vector{<:Real} = fill(0.1, max(sum(nint_ym), 0))
258-
)
258+
) where {M<:LinModel}
259259
if nint_ym == 0 # alias for no output integrator at all :
260260
nint_ym = fill(0, length(i_ym));
261261
end
@@ -264,7 +264,7 @@ function KalmanFilter(
264264
P̂0 = Diagonal{Float64}([σP0 ; σP0_int ].^2);
265265
= Diagonal{Float64}([σQ ; σQ_int ].^2);
266266
= Diagonal{Float64}(σR.^2);
267-
return KalmanFilter(model, i_ym, nint_ym, Asm, Csm, P̂0, Q̂ , R̂)
267+
return KalmanFilter{M}(model, i_ym, nint_ym, Asm, Csm, P̂0, Q̂ , R̂)
268268
end
269269

270270
@doc raw"""

src/predictive_control.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -659,13 +659,13 @@ julia> u = moveinput!(mpc, [5]); round.(u, digits=3)
659659
```
660660
"""
661661
function moveinput!(
662-
mpc::C,
662+
mpc::PredictiveController,
663663
ry::Vector{<:Real},
664664
d ::Vector{<:Real} = Float64[];
665665
R̂y::Vector{<:Real} = repeat(ry, mpc.Hp),
666666
::Vector{<:Real} = repeat(d, mpc.Hp),
667667
ym::Union{Vector{<:Real}, Nothing} = nothing
668-
) where {C<:PredictiveController}
668+
)
669669
lastu = mpc.info.u
670670
x̂d, x̂s = split_state(mpc.estim)
671671
ŷs, Ŷs = predict_stoch(mpc, mpc.estim, x̂s, d, ym)
@@ -752,8 +752,8 @@ Init linear model prediction matrices `F`, `q̃` and `p`.
752752
See [`init_deterpred`](@ref) and [`init_quadprog`](@ref) for the definition of the matrices.
753753
"""
754754
function init_prediction(
755-
mpc::C, model::LinModel, d, D̂, Ŷs, R̂y, x̂d, lastu
756-
) where {C<:PredictiveController}
755+
mpc::PredictiveController, model::LinModel, d, D̂, Ŷs, R̂y, x̂d, lastu
756+
)
757757
F = mpc.Kd*x̂d + mpc.Q*(lastu - model.uop) + Ŷs + mpc.Yop
758758
if model.nd 0
759759
F += mpc.G*(d - model.dop) + mpc.J*(D̂ - mpc.Dop)
@@ -774,7 +774,7 @@ end
774774
775775
Calc `b` vector for the linear model inequality constraints (``\mathbf{A ΔŨ ≤ b}``).
776776
"""
777-
function linconstraint(mpc::C, ::LinModel, lastu, F) where {C<:PredictiveController}
777+
function linconstraint(mpc::PredictiveController, ::LinModel, lastu, F)
778778
b = [
779779
-mpc.con.Umin + mpc.T_Hc*lastu
780780
+mpc.con.Umax - mpc.T_Hc*lastu
@@ -791,7 +791,7 @@ end
791791
792792
Calc `b` without predicted output ``\mathbf{Ŷ}`` constraints for [`NonLinModel`](@ref).
793793
"""
794-
function linconstraint(mpc::C, ::NonLinModel, lastu, _ ) where {C<:PredictiveController}
794+
function linconstraint(mpc::PredictiveController, ::NonLinModel, lastu, _ )
795795
b = [
796796
-mpc.con.Umin + mpc.T_Hc*lastu
797797
+mpc.con.Umax - mpc.T_Hc*lastu

src/state_estim.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function returns the next state of the augmented model, defined as:
161161
\end{aligned}
162162
```
163163
"""
164-
function (estim::E, x̂, u, d) where {E<:StateEstimator}
164+
function (estim::StateEstimator, x̂, u, d)
165165
# `@views` macro avoid copies with matrix slice operator e.g. [a:b]
166166
nx = estim.model.nx
167167
@views return [estim.model.f(x̂[1:nx], u, d); estim.As*x̂[nx+1:end]]
@@ -172,7 +172,7 @@ end
172172
173173
Output function ``\mathbf{ĥ}`` of the augmented model, see [`f̂`](@ref) for details.
174174
"""
175-
function (estim::E, x̂, d) where {E<:StateEstimator}
175+
function (estim::StateEstimator, x̂, d)
176176
# `@views` macro avoid copies with matrix slice operator e.g. [a:b]
177177
nx = estim.model.nx
178178
@views return estim.model.h(x̂[1:nx], d) + estim.Cs*x̂[nx+1:end]

0 commit comments

Comments
 (0)