Skip to content

Commit e341934

Browse files
committed
starting moving horizon estimator
1 parent 4709a3d commit e341934

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

docs/src/public/predictive_control.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ NonLinMPC
7878
setconstraint!
7979
```
8080

81-
## Move Manipulated Input
81+
## Move Manipulated Input u
8282

8383
```@docs
8484
moveinput!

src/estimator/mhe.jl

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
struct NonLinMHE{M<:SimModel} <: StateEstimator
2+
model::M
3+
optim::JuMP.Model
4+
lastu0::Vector{Float64}
5+
::Vector{Float64}
6+
::Hermitian{Float64, Matrix{Float64}}
7+
i_ym::Vector{Int}
8+
nx̂::Int
9+
nym::Int
10+
nyu::Int
11+
nxs::Int
12+
As::Matrix{Float64}
13+
Cs::Matrix{Float64}
14+
nint_ym::Vector{Int}
15+
P̂0::Hermitian{Float64, Matrix{Float64}}
16+
::Hermitian{Float64, Matrix{Float64}}
17+
::Hermitian{Float64, Matrix{Float64}}
18+
He::Int
19+
X̂max::Vector{Float64}
20+
X̂min::Vector{Float64}
21+
function NonLinMHE{M}(model::M, i_ym, nint_ym, P̂0, Q̂, R̂, He) where {M<:SimModel}
22+
nu, nx, ny = model.nu, model.nx, model.ny
23+
nym, nyu = length(i_ym), ny - length(i_ym)
24+
Asm, Csm, nint_ym = init_estimstoch(i_ym, nint_ym)
25+
nxs = size(Asm,1)
26+
nx̂ = nx + nxs
27+
validate_kfcov(nym, nx̂, Q̂, R̂, P̂0)
28+
As, _ , Cs, _ = stoch_ym2y(model, i_ym, Asm, [], Csm, [])
29+
nσ, γ, m̂, Ŝ = init_mhe(nx̂, He)
30+
i_ym = collect(i_ym)
31+
lastu0 = zeros(nu)
32+
= [zeros(model.nx); zeros(nxs)]
33+
P̂0 = Hermitian(P̂0, :L)
34+
= Hermitian(Q̂, :L)
35+
= Hermitian(R̂, :L)
36+
= copy(P̂0)
37+
return new(
38+
model,
39+
lastu0, x̂, P̂,
40+
i_ym, nx̂, nym, nyu, nxs,
41+
As, Cs, nint_ym,
42+
P̂0, Q̂, R̂,
43+
nσ, γ, m̂, Ŝ
44+
)
45+
end
46+
end
47+
48+
@doc raw"""
49+
NonLinMHE(model::SimModel; <keyword arguments>)
50+
51+
Construct a nonlinear moving horizon estimator with the [`SimModel`](@ref) `model`.
52+
53+
Both [`LinModel`](@ref) and [`NonLinModel`](@ref) are supported. The process model is
54+
identical to [`UnscentedKalmanFilter`](@ref).
55+
56+
# Arguments
57+
- `model::SimModel` : (deterministic) model for the estimations.
58+
- `He::Int=10` : estimation horizon.
59+
- `<keyword arguments>` of [`SteadyKalmanFilter`](@ref) constructor.
60+
- `<keyword arguments>` of [`KalmanFilter`](@ref) constructor.
61+
62+
# Examples
63+
```jldoctest
64+
julia> model = NonLinModel((x,u,_)->0.1x+u, (x,_)->2x, 10.0, 1, 1, 1);
65+
66+
```
67+
"""
68+
function NonLinMHE(
69+
model::M;
70+
i_ym::IntRangeOrVector = 1:model.ny,
71+
σP0::Vector = fill(1/model.nx, model.nx),
72+
σQ::Vector = fill(1/model.nx, model.nx),
73+
σR::Vector = fill(1, length(i_ym)),
74+
nint_ym::IntVectorOrInt = fill(1, length(i_ym)),
75+
σP0_int::Vector = fill(1, max(sum(nint_ym), 0)),
76+
σQ_int::Vector = fill(1, max(sum(nint_ym), 0)),
77+
He::Int = 10
78+
) where {M<:SimModel}
79+
# estimated covariances matrices (variance = σ²) :
80+
P̂0 = Diagonal{Float64}([σP0 ; σP0_int ].^2);
81+
= Diagonal{Float64}([σQ ; σQ_int ].^2);
82+
= Diagonal{Float64}(σR.^2);
83+
return NonLinMHE{M}(model, i_ym, nint_ym, P̂0, Q̂ , R̂, He)
84+
end
85+
86+
@doc raw"""
87+
NonLinMHE{M<:SimModel}(model::M, i_ym, nint_ym, P̂0, Q̂, R̂, He)
88+
89+
Construct the estimator from the augmented covariance matrices `P̂0`, `Q̂` and `R̂`.
90+
91+
This syntax allows nonzero off-diagonal elements in ``\mathbf{P̂}_{-1}(0), \mathbf{Q̂, R̂}``.
92+
"""
93+
NonLinMHE{M}(model::M, i_ym, nint_ym, P̂0, Q̂, R̂, He) where {M<:SimModel}

0 commit comments

Comments
 (0)