Skip to content

Commit a262420

Browse files
committed
doc: arguments MHE
1 parent 6e3f417 commit a262420

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,6 @@ for more detailed examples.
121121
- [x] measured outputs
122122
- [x] bumpless manual to automatic transfer for control with a proper intial estimate
123123
- [x] observers in predictor form to ease control applications
124-
- [ ] moving horizon estimator that supports:
125-
- [ ] inequality state constraints
124+
- [x] moving horizon estimator that supports:
125+
- [x] inequality state constraints
126126
- [ ] zero process noise equality constraint to reduce the problem size

src/estimator/mhe.jl

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,31 @@ N_k = \begin{cases}
131131
k + 1 & k < H_e \\
132132
H_e & k ≥ H_e \end{cases}
133133
```
134+
See [`SteadyKalmanFilter`](@ref) for details on ``\mathbf{R̂}, \mathbf{Q̂}`` covariances and
135+
model augmentation.
136+
137+
# Arguments
138+
- `model::SimModel` : (deterministic) model for the estimations.
139+
- `He=nothing`: estimation horizon ``H_e``, must be specified.
140+
- `optim=JuMP.Model(Ipopt.Optimizer)` : nonlinear optimizer used in the moving horizon
141+
estimator, provided as a [`JuMP.Model`](https://jump.dev/JuMP.jl/stable/api/JuMP/#JuMP.Model)
142+
(default to [`Ipopt.jl`](https://github.com/jump-dev/Ipopt.jl) optimizer).
143+
- `<keyword arguments>` of [`SteadyKalmanFilter`](@ref) constructor.
144+
- `<keyword arguments>` of [`KalmanFilter`](@ref) constructor.
145+
146+
# Examples
147+
```jldoctest
148+
julia> model = NonLinModel((x,u,_)->0.1x+u, (x,_)->2x, 10.0, 1, 1, 1);
149+
150+
julia> estim = MovingHorizonEstimator(model, He=5, σR=[1], σP0=[0.01])
151+
MovingHorizonEstimator estimator with a sample time Ts = 10.0 s, NonLinModel and:
152+
5 estimation steps He
153+
1 manipulated inputs u (0 integrating states)
154+
2 states x̂
155+
1 measured outputs ym (1 integrating states)
156+
0 unmeasured outputs yu
157+
0 measured disturbances d
158+
```
134159
"""
135160
function MovingHorizonEstimator(
136161
model::SM;
@@ -157,6 +182,21 @@ function MovingHorizonEstimator(
157182
)
158183
end
159184

185+
@doc raw"""
186+
MovingHorizonEstimator(model, He, i_ym, nint_u, nint_ym, P̂0, Q̂, R̂, optim)
187+
188+
Construct the estimator from the augmented covariance matrices `P̂0`, `Q̂` and `R̂`.
189+
190+
This syntax allows nonzero off-diagonal elements in ``\mathbf{P̂}_{-1}(0), \mathbf{Q̂, R̂}``.
191+
"""
192+
function MovingHorizonEstimator(
193+
model::SM, He, i_ym, nint_u, nint_ym, P̂0, Q̂, R̂, optim
194+
) where {NT<:Real, SM<:SimModel{NT}}
195+
return MovingHorizonEstimator{NT, SM}(
196+
model, He, i_ym, nint_u, nint_ym, P̂0, Q̂ , R̂, optim
197+
)
198+
end
199+
160200

161201
"""
162202
init_optimization!(estim::MovingHorizonEstimator, optim::JuMP.GenericModel)
@@ -244,8 +284,6 @@ function init_optimization!(
244284
register(optim, sym, nvar, gfunc[i_end_X̂min+i], autodiff=true)
245285
end
246286
end
247-
# TODO: moved this call to setconstraint! when the function will handle MHE
248-
setnonlincon!(estim, estim.model)
249287
return nothing
250288
end
251289

@@ -464,6 +502,13 @@ function obj_nonlinprog(
464502
return dot(x̄0, invP̄, x̄0) + dot(Ŵ, invQ̂_Nk, Ŵ) + dot(V̂, invR̂_Nk, V̂)
465503
end
466504

505+
"""
506+
predict!(Ŷm, X̂, estim::MovingHorizonEstimator, model::SimModel, W̃) -> Ŷm, X̂
507+
508+
Compute the predicted measured outputs `Ŷm` and states `X̂` for the `MovingHorizonEstimator`.
509+
510+
The method mutates `Ŷm` and `X̂` vector arguments.
511+
"""
467512
function predict!(
468513
Ŷm, X̂, estim::MovingHorizonEstimator, model::SimModel, W̃::Vector{T}
469514
) where {T<:Real}
@@ -480,6 +525,11 @@ function predict!(
480525
return Ŷm, X̂
481526
end
482527

528+
"""
529+
con_nonlinprog!(g, estim::MovingHorizonEstimator, model::SimModel, X̂)
530+
531+
Nonlinear constrains for [`MovingHorizonEstimator`](@ref).
532+
"""
483533
function con_nonlinprog!(g, estim::MovingHorizonEstimator, ::SimModel, X̂)
484534
nX̂con, nX̂ = length(estim.X̂min), estim.Nk[]*estim.nx̂
485535
for i in eachindex(g)

test/test_state_estim.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ end
453453
linmodel3 = LinModel{Float32}(0.5*ones(1,1), ones(1,1), ones(1,1), zeros(1,0), zeros(1,0), 1.0)
454454
ukf3 = UnscentedKalmanFilter(linmodel3)
455455
= updatestate!(ukf3, [0], [0])
456-
@test [0, 0]
456+
@test [0, 0] atol=1e-3
457457
@test isa(x̂, Vector{Float32})
458458
end
459459

0 commit comments

Comments
 (0)