Skip to content

Commit 5981e76

Browse files
committed
added: setstate! now allows cov estimate modification
1 parent 2045d9a commit 5981e76

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

src/controller/execute.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,11 @@ Call `periodsleep(mpc.estim.model)`.
488488
periodsleep(mpc::PredictiveController, busywait=false) = periodsleep(mpc.estim.model, busywait)
489489

490490
"""
491-
setstate!(mpc::PredictiveController, x̂) -> mpc
491+
setstate!(mpc::PredictiveController, x̂, P̂=nothing) -> mpc
492492
493-
Set `mpc.estim.x̂0` to `x̂ - estim.x̂op` from the argument `x̂`.
493+
Call [`setstate!`](@ref) on `mpc.estim` [`StateEstimator`](@ref).
494494
"""
495-
setstate!(mpc::PredictiveController, x̂) = (setstate!(mpc.estim, x̂); return mpc)
496-
495+
setstate!(mpc::PredictiveController, x̂, P̂=nothing) = (setstate!(mpc.estim, x̂, P̂); return mpc)
497496

498497
@doc raw"""
499498
setmodel!(mpc::PredictiveController, model=mpc.estim.model; <keyword arguments>) -> mpc

src/estimator/execute.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,16 +330,28 @@ function validate_args(estim::StateEstimator, ym, d, u=nothing)
330330
end
331331

332332
"""
333-
setstate!(estim::StateEstimator, x̂) -> estim
333+
setstate!(estim::StateEstimator, x̂, P̂=nothing) -> estim
334334
335-
Set `estim.x̂0` to `x̂ - estim.x̂op` from the argument `x̂`.
335+
Set `estim.x̂0` to `x̂ - estim.x̂op` from the argument `x̂`, and `estim.P̂` to `P̂` if applicable.
336+
337+
The covariance error estimate `P̂` can be set only if `estim` computes it.
336338
"""
337-
function setstate!(estim::StateEstimator, x̂)
339+
function setstate!(estim::StateEstimator, x̂, P̂=nothing)
338340
size(x̂) == (estim.nx̂,) || error("x̂ size must be $((estim.nx̂,))")
339341
estim.x̂0 .=.- estim.x̂op
342+
setstate_cov!(estim, P̂)
340343
return estim
341344
end
342345

346+
"Set the covariance error estimate `estim.P̂` to `P̂`."
347+
function setstate_cov!(estim::StateEstimator, P̂)
348+
if !isnothing(P̂)
349+
size(P̂) == (estim.nx̂, estim.nx̂) || error("P̂ size must be $((estim.nx̂, estim.nx̂))")
350+
estim.P̂ .= to_hermitian(P̂)
351+
end
352+
return nothing
353+
end
354+
343355
@doc raw"""
344356
setmodel!(estim::StateEstimator, model=estim.model; <keyword arguments>) -> estim
345357

src/estimator/internal_model.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ function init_internalmodel(As, Bs, Cs, Ds)
226226
return Âs, B̂s
227227
end
228228

229+
"Throw an error if P̂ != nothing."
230+
function setstate_cov!(estim::InternalModel, P̂)
231+
== nothing || error("InternalModel does not compute an estimation covariance matrix P̂.")
232+
return nothing
233+
end
234+
229235
"Update similar values for [`InternalModel`](@ref) estimator."
230236
function setmodel_estimator!(estim::InternalModel, model, _ , _ , _ , _ , _ )
231237
Â, B̂u, Ĉ, B̂d, D̂d, x̂op, f̂op = matrices_internalmodel(model)

src/estimator/luenberger.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ function update_estimate!(estim::Luenberger, y0m, d0, u0)
136136
return predict_estimate_obsv!(estim, y0m, d0, u0)
137137
end
138138

139+
"Throw an error if P̂ != nothing."
140+
function setstate_cov!(estim::Luenberger, P̂)
141+
== nothing || error("Luenberger does not compute an estimation covariance matrix P̂.")
142+
return nothing
143+
end
144+
139145
"Throw an error if `setmodel!` is called on `Luenberger` observer w/o the default values."
140146
function setmodel_estimator!(estim::Luenberger, model, args...)
141147
if estim.model !== model

src/estimator/mhe/execute.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,12 @@ end
650650
"No nonlinear constraints if `model` is a [`LinModel`](@ref), return `g` unchanged."
651651
con_nonlinprog!(g, ::MovingHorizonEstimator, ::LinModel, _ , _ , _ ) = g
652652

653+
"Throw an error if P̂ != nothing."
654+
function setstate_cov!(estim::MovingHorizonEstimator, P̂)
655+
== nothing || error("MovingHorizonEstimator does not compute an estimation covariance matrix P̂.")
656+
return nothing
657+
end
658+
653659
"Update the augmented model, prediction matrices, constrains and data windows for MHE."
654660
function setmodel_estimator!(
655661
estim::MovingHorizonEstimator, model, uop_old, yop_old, dop_old, Q̂, R̂

0 commit comments

Comments
 (0)