Skip to content

Commit 46a2588

Browse files
committed
debug: correct steady-state formula for initstate on InternalModel
and minor doc corrections
1 parent a078f35 commit 46a2588

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

src/controller/execute.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,8 @@ predictstoch!(mpc::PredictiveController, ::StateEstimator, _ , _ ) = (mpc.F .= 0
247247
248248
Set `b` vector for the linear model inequality constraints (``\mathbf{A ΔŨ ≤ b}``).
249249
250-
Also init ``\mathbf{f_x̂} = \mathbf{g_x̂ d}(k) + \mathbf{j_x̂ D̂} + \mathbf{k_x̂ x̂}_{k-1}(k)
251-
+ \mathbf{v_x̂ u}(k-1) + \mathbf{b_x̂}`` vector for the terminal constraints, see
252-
[`init_predmat`](@ref).
250+
Also init ``\mathbf{f_x̂} = \mathbf{g_x̂ d}(k) + \mathbf{j_x̂ D̂} + \mathbf{k_x̂ x̂}_{k-1}(k) + \mathbf{v_x̂ u}(k-1) + \mathbf{b_x̂}``
251+
vector for the terminal constraints, see [`init_predmat`](@ref).
253252
"""
254253
function linconstraint!(mpc::PredictiveController, model::LinModel)
255254
nU, nΔŨ, nY = length(mpc.con.U0min), length(mpc.con.ΔŨmin), length(mpc.con.Y0min)

src/estimator/construct.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,18 @@ end
105105
106106
Augment [`LinModel`](@ref) state-space matrices with the stochastic ones `As` and `Cs`.
107107
108-
If ``\mathbf{x}`` are `model.x0` states, and ``\mathbf{x_s}``, the states defined at
108+
If ``\mathbf{x_0}`` are `model.x0` states, and ``\mathbf{x_s}``, the states defined at
109109
[`init_estimstoch`](@ref), we define an augmented state vector ``\mathbf{x̂} =
110-
[ \begin{smallmatrix} \mathbf{x} \\ \mathbf{x_s} \end{smallmatrix} ]``. The method
110+
[ \begin{smallmatrix} \mathbf{x_0} \\ \mathbf{x_s} \end{smallmatrix} ]``. The method
111111
returns the augmented matrices `Â`, `B̂u`, `Ĉ`, `B̂d` and `D̂d`:
112112
```math
113113
\begin{aligned}
114-
\mathbf{}(k+1) &= \mathbf{Â }(k) + \mathbf{B̂_u u}(k) + \mathbf{B̂_d d}(k) \\
115-
\mathbf{}(k) &= \mathbf{Ĉ }(k) + \mathbf{D̂_d d}(k)
114+
\mathbf{x̂_0}(k+1) &= \mathbf{Â x̂_0}(k) + \mathbf{B̂_u u_0}(k) + \mathbf{B̂_d d_0}(k) \\
115+
\mathbf{ŷ_0}(k) &= \mathbf{Ĉ x̂_0}(k) + \mathbf{D̂_d d_0}(k)
116116
\end{aligned}
117117
```
118118
An error is thrown if the augmented model is not observable and `verify_obsv == true`. The
119-
augmented operating points `x̂_{op}` and `f̂_{op}` are simply ``\mathbf{x_{op}}`` and
119+
augmented operating points `x̂op` and `f̂op` are simply ``\mathbf{x_{op}}`` and
120120
``\mathbf{f_{op}}`` vectors appended with zeros (see [`setop!`](@ref)).
121121
"""
122122
function augment_model(model::LinModel{NT}, As, Cs_u, Cs_y; verify_obsv=true) where NT<:Real

src/estimator/execute.jl

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,65 @@ function remove_op!(estim::StateEstimator, u, ym, d)
1515
end
1616

1717
@doc raw"""
18-
f̂!(x̂next, û, estim::StateEstimator, model::SimModel, x̂, u, d) -> nothing
18+
f̂!(x̂next0, û0, estim::StateEstimator, model::SimModel, x̂0, u0, d0) -> nothing
1919
2020
Mutating state function ``\mathbf{f̂}`` of the augmented model.
2121
22-
By introducing an augmented state vector ``\mathbf{}`` like in [`augment_model`](@ref), the
22+
By introducing an augmented state vector ``\mathbf{x̂_0}`` like in [`augment_model`](@ref), the
2323
function returns the next state of the augmented model, defined as:
2424
```math
2525
\begin{aligned}
26-
\mathbf{}(k+1) &= \mathbf{f̂}\Big(\mathbf{}(k), \mathbf{u}(k), \mathbf{d}(k)\Big) \\
27-
\mathbf{}(k) &= \mathbf{ĥ}\Big(\mathbf{}(k), \mathbf{d}(k)\Big)
26+
\mathbf{x̂_0}(k+1) &= \mathbf{f̂}\Big(\mathbf{x̂_0}(k), \mathbf{u_0}(k), \mathbf{d_0}(k)\Big) \\
27+
\mathbf{ŷ_0}(k) &= \mathbf{ĥ}\Big(\mathbf{x̂_0}(k), \mathbf{d_0}(k)\Big)
2828
\end{aligned}
2929
```
30-
where ``\mathbf{x̂}(k+1)`` is stored in `x̂next` argument. The method mutates `x̂next` and `û`
31-
in place, the latter stores the input vector of the augmented model ``\mathbf{u + ŷ_{s_u}}``.
30+
where ``\mathbf{x̂_0}(k+1)`` is stored in `x̂next0` argument. The method mutates `x̂next0` and
31+
`û0` in place, the latter stores the input vector of the augmented model
32+
``\mathbf{u0 + ŷ_{s_u}}``.
3233
"""
33-
function f̂!(x̂next, û, estim::StateEstimator, model::SimModel, x̂, u, d)
34+
function f̂!(x̂next0, û0, estim::StateEstimator, model::SimModel, x̂0, u0, d0)
3435
# `@views` macro avoid copies with matrix slice operator e.g. [a:b]
35-
@views x̂d, x̂s = [1:model.nx], [model.nx+1:end]
36-
@views x̂d_next, x̂s_next = x̂next[1:model.nx], x̂next[model.nx+1:end]
37-
mul!(, estim.Cs_u, x̂s)
38-
.+= u
39-
f!(x̂d_next, model, x̂d, û, d)
36+
@views x̂d, x̂s = x̂0[1:model.nx], x̂0[model.nx+1:end]
37+
@views x̂d_next, x̂s_next = x̂next0[1:model.nx], x̂next0[model.nx+1:end]
38+
mul!(û0, estim.Cs_u, x̂s)
39+
û0 .+= u0
40+
f!(x̂d_next, model, x̂d, û0, d0)
4041
mul!(x̂s_next, estim.As, x̂s)
4142
return nothing
4243
end
4344

4445
"""
45-
f̂!(x̂next, _ , estim::StateEstimator, model::LinModel, x̂, u, d) -> nothing
46+
f̂!(x̂next0, _ , estim::StateEstimator, model::LinModel, x̂0, u0, d0) -> nothing
4647
4748
Use the augmented model matrices if `model` is a [`LinModel`](@ref).
4849
"""
49-
function f̂!(x̂next, _ , estim::StateEstimator, ::LinModel, x̂, u, d)
50-
mul!(x̂next, estim.Â, )
51-
mul!(x̂next, estim.B̂u, u, 1, 1)
52-
mul!(x̂next, estim.B̂d, d, 1, 1)
50+
function f̂!(x̂next0, _ , estim::StateEstimator, ::LinModel, x̂0, u0, d0)
51+
mul!(x̂next0, estim.Â, x̂0)
52+
mul!(x̂next0, estim.B̂u, u0, 1, 1)
53+
mul!(x̂next0, estim.B̂d, d0, 1, 1)
5354
return nothing
5455
end
5556

5657
@doc raw"""
57-
ĥ!(, estim::StateEstimator, model::SimModel, x̂, d) -> nothing
58+
ĥ!(ŷ0, estim::StateEstimator, model::SimModel, x̂0, d0) -> nothing
5859
5960
Mutating output function ``\mathbf{ĥ}`` of the augmented model, see [`f̂!`](@ref).
6061
"""
61-
function ĥ!(, estim::StateEstimator, model::SimModel, x̂, d)
62+
function ĥ!(ŷ0, estim::StateEstimator, model::SimModel, x̂0, d0)
6263
# `@views` macro avoid copies with matrix slice operator e.g. [a:b]
63-
@views x̂d, x̂s = [1:model.nx], [model.nx+1:end]
64-
h!(, model, x̂d, d)
65-
mul!(, estim.Cs_y, x̂s, 1, 1)
64+
@views x̂d, x̂s = x̂0[1:model.nx], x̂0[model.nx+1:end]
65+
h!(ŷ0, model, x̂d, d0)
66+
mul!(ŷ0, estim.Cs_y, x̂s, 1, 1)
6667
return nothing
6768
end
6869
"""
69-
ĥ!(, estim::StateEstimator, model::LinModel, x̂, d) -> nothing
70+
ĥ!(ŷ0, estim::StateEstimator, model::LinModel, x̂0, d0) -> nothing
7071
7172
Use the augmented model matrices if `model` is a [`LinModel`](@ref).
7273
"""
73-
function ĥ!(, estim::StateEstimator, ::LinModel, x̂, d)
74-
mul!(, estim.Ĉ, )
75-
mul!(, estim.D̂d, d, 1, 1)
74+
function ĥ!(ŷ0, estim::StateEstimator, ::LinModel, x̂0, d0)
75+
mul!(ŷ0, estim.Ĉ, x̂0)
76+
mul!(ŷ0, estim.D̂d, d0, 1, 1)
7677
return nothing
7778
end
7879

@@ -130,8 +131,9 @@ init_estimate_cov!(::StateEstimator, _ , _ , _ ) = nothing
130131
131132
Init `estim.x̂0` estimate with the steady-state solution if `model` is a [`LinModel`](@ref).
132133
133-
Using `u0`, `ym0` and `d0` arguments, the steady-state problem combined to the equality
134-
constraint ``\mathbf{ŷ_0^m} = \mathbf{y_0^m}`` engenders the following system to solve:
134+
Using `u0`, `ym0` and `d0` arguments (deviation values, see [`setop!`](@ref)), the
135+
steadystate problem combined to the equality constraint ``\mathbf{ŷ_0^m} = \mathbf{y_0^m}``
136+
engenders the following system to solve:
135137
```math
136138
\begin{bmatrix}
137139
\mathbf{I} - \mathbf{Â} \\

src/estimator/internal_model.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,20 @@ function matrices_internalmodel(model::SimModel{NT}) where NT<:Real
149149
end
150150

151151
@doc raw"""
152-
f̂!(x̂next, _ , estim::InternalModel, model::NonLinModel, x̂, u, d)
152+
f̂!(x̂next0, _ , estim::InternalModel, model::NonLinModel, x̂0, u0, d0)
153153
154154
State function ``\mathbf{f̂}`` of [`InternalModel`](@ref) for [`NonLinModel`](@ref).
155155
156-
It calls `model.f!(x̂next, x̂, u ,d)` since this estimator does not augment the states.
156+
It calls `model.f!(x̂next0, x̂0, u0 ,d0)` since this estimator does not augment the states.
157157
"""
158-
f̂!(x̂next, _ , ::InternalModel, model::NonLinModel, x̂, u, d) = model.f!(x̂next, x̂, u, d)
158+
f̂!(x̂next0, _, ::InternalModel, model::NonLinModel, x̂0, u0, d0) = model.f!(x̂next0, x̂0, u0, d0)
159159

160160
@doc raw"""
161-
ĥ!(, estim::InternalModel, model::NonLinModel, x̂, d)
161+
ĥ!(ŷ0, estim::InternalModel, model::NonLinModel, x̂0, d0)
162162
163163
Output function ``\mathbf{ĥ}`` of [`InternalModel`](@ref), it calls `model.h!`.
164164
"""
165-
ĥ!(x̂next, ::InternalModel, model::NonLinModel, x̂, d) = model.h!(x̂next, x̂, d)
165+
ĥ!(x̂next0, ::InternalModel, model::NonLinModel, x̂0, d0) = model.h!(x̂next0, x̂0, d0)
166166

167167

168168
@doc raw"""
@@ -260,10 +260,10 @@ Init `estim.x̂0`/`x̂d`/`x̂s` estimate at steady-state for [`InternalModel`](@
260260
261261
The deterministic estimates `estim.x̂d` start at steady-state using `u0` and `d0` arguments:
262262
```math
263-
\mathbf{x̂_d} = \mathbf{(I - A)^{-1} (B_u u + B_d d)}
263+
\mathbf{x̂_d} = \mathbf{(I - A)^{-1} (B_u u_0 + B_d d_0 + f_{op} - x_{op})}
264264
```
265-
Based on `ym` argument and current stochastic outputs estimation ``\mathbf{ŷ_s}``, composed
266-
of the measured ``\mathbf{ŷ_s^m} = \mathbf{y^m} - \mathbf{ŷ_d^m}`` and unmeasured
265+
Based on `ym0` argument and current stochastic outputs estimation ``\mathbf{ŷ_s}``, composed
266+
of the measured ``\mathbf{ŷ_s^m} = \mathbf{y_0^m} - \mathbf{ŷ_{d0}^m}`` and unmeasured
267267
``\mathbf{ŷ_s^u = 0}`` outputs, the stochastic estimates also start at steady-state:
268268
```math
269269
\mathbf{x̂_s} = \mathbf{(I - Â_s)^{-1} B̂_s ŷ_s}
@@ -274,7 +274,7 @@ This estimator does not augment the state vector, thus ``\mathbf{x̂ = x̂_d}``.
274274
function init_estimate!(estim::InternalModel, model::LinModel{NT}, u0, ym0, d0) where NT<:Real
275275
x̂d, x̂s = estim.x̂d, estim.x̂s
276276
# also updates estim.x̂0 (they are the same object):
277-
x̂d .= (I - model.A)\(model.Bu*u0 + model.Bd*d0 - estim.f̂op + estim.x̂op)
277+
x̂d .= (I - model.A)\(model.Bu*u0 + model.Bd*d0 + model.fop - model.xop)
278278
ŷd0 = Vector{NT}(undef, model.ny)
279279
h!(ŷd0, model, x̂d, d0)
280280
ŷs = zeros(NT, model.ny)

0 commit comments

Comments
 (0)