Skip to content

Commit 20512dd

Browse files
committed
changed: D0 length does not change with mhe.direct
The vector will always include one additionnal data point compared to the other data windows.
1 parent 0dc72a0 commit 20512dd

2 files changed

Lines changed: 24 additions & 33 deletions

File tree

src/estimator/mhe/construct.jl

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ struct MovingHorizonEstimator{
157157
= zeros(NT, nZ̃)
158158
X̂op = repeat(x̂op, He)
159159
X̂0, Y0m = zeros(NT, nx̂*He), zeros(NT, nym*He)
160-
nD0 = direct ? nd*(He+1) : nd*He
161-
U0, D0 = zeros(NT, nu*He), zeros(NT, nD0)
160+
U0, D0 = zeros(NT, nu*He), zeros(NT, nd*(He+1))
162161
= zeros(NT, nx̂*He)
163162
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd, nk, He, nε)
164163
x̂0arr_old = zeros(NT, nx̂)
@@ -1038,14 +1037,14 @@ produces an estimator in the current form, while the prediction form is obtained
10381037
&= \mathbf{E Z + F}
10391038
\end{aligned}
10401039
```
1041-
in which ``\mathbf{U_0}`` and ``\mathbf{Y_0^m}`` respectively include the deviation values of
1042-
the manipulated inputs ``\mathbf{u_0}(k-j+p)`` from ``j=N_k`` to ``1`` and measured outputs
1043-
``\mathbf{y_0^m}(k-j+1)`` from ``j=N_k`` to ``1``. The vector ``\mathbf{D_0}`` comprises one
1044-
additional measured disturbance if ``p=0``, that is, it includes the deviation vectors
1045-
``\mathbf{d_0}(k-j+1)`` from ``j=N_k+1-p`` to ``1``. The constant ``\mathbf{B}`` is the
1046-
contribution for non-zero state ``\mathbf{x̂_{op}}`` and state update ``\mathbf{f̂_{op}}``
1047-
operating points (for linearization, see [`augment_model`](@ref) and [`linearize`](@ref)).
1048-
The method also returns the matrices for the estimation error at arrival:
1040+
in which ``\mathbf{U_0}`` and ``\mathbf{Y_0^m}`` respectively include the deviation values
1041+
of the manipulated inputs ``\mathbf{u_0}(k-j+p)`` from ``j=N_k`` to ``1`` and measured
1042+
outputs ``\mathbf{y_0^m}(k-j+1)`` from ``j=N_k`` to ``1``. The vector ``\mathbf{D_0}``
1043+
includes the the measured disturbance deviation values ``\mathbf{d_0}(k-j)`` from from
1044+
``j=N_k`` to ``0``. The constant ``\mathbf{B}`` is the contribution for non-zero state
1045+
``\mathbf{x̂_{op}}`` and state update ``\mathbf{f̂_{op}}`` operating points (for linearization,
1046+
see [`augment_model`](@ref) and [`linearize`](@ref)). The method also returns the matrices
1047+
for the estimation error at arrival:
10491048
```math
10501049
\mathbf{x̄} = \mathbf{x̂_0^†}(k-N_k+p) - \mathbf{x̂_0}(k-N_k+p) = \mathbf{e_x̄ Z + f_x̄}
10511050
```
@@ -1108,10 +1107,10 @@ see [`initpred!(::MovingHorizonEstimator, ::LinModel)`](@ref) and [`linconstrain
11081107
\vdots & \vdots & \ddots & \vdots \\
11091108
\mathbf{Ĉ^m}\mathbf{Â}^{H_e-2}\mathbf{B̂_u} & \mathbf{Ĉ^m}\mathbf{Â}^{H_e-3}\mathbf{B̂_u} & \cdots & \mathbf{0} \end{bmatrix} \\
11101109
\mathbf{J} &= - \begin{bmatrix}
1111-
\mathbf{D̂_d^m} & \mathbf{0} & \cdots & \mathbf{0} \\
1112-
\mathbf{Ĉ^m}\mathbf{Â}^{0}\mathbf{B̂_d} & \mathbf{D̂_d^m} & \cdots & \mathbf{0} \\
1113-
\vdots & \vdots & \ddots & \vdots \\
1114-
\mathbf{Ĉ^m}\mathbf{Â}^{H_e-2}\mathbf{B̂_d} & \mathbf{Ĉ^m}\mathbf{Â}^{H_e-3}\mathbf{B̂_d} & \cdots & \mathbf{D̂_d^m} \end{bmatrix} \\
1110+
\mathbf{0} & \mathbf{D̂_d^m} & \mathbf{0} & \cdots & \mathbf{0} \\
1111+
\mathbf{0} & \mathbf{Ĉ^m}\mathbf{Â}^{0}\mathbf{B̂_d} & \mathbf{D̂_d^m} & \cdots & \mathbf{0} \\
1112+
\vdots & \vdots & \vdots & \ddots & \vdots \\
1113+
\mathbf{0} & \mathbf{Ĉ^m}\mathbf{Â}^{H_e-2}\mathbf{B̂_d} & \mathbf{Ĉ^m}\mathbf{Â}^{H_e-3}\mathbf{B̂_d} & \cdots & \mathbf{D̂_d^m} \end{bmatrix} \\
11151114
\mathbf{B} &= - \begin{bmatrix}
11161115
\mathbf{0} \\
11171116
\mathbf{Ĉ^m S}(0) \\
@@ -1138,8 +1137,8 @@ see [`initpred!(::MovingHorizonEstimator, ::LinModel)`](@ref) and [`linconstrain
11381137
\vdots & \vdots & \ddots & \vdots \\
11391138
\mathbf{Â}^{H_e-1}\mathbf{B̂_d} & \mathbf{Â}^{H_e-2}\mathbf{B̂_d} & \cdots & \mathbf{Â}^{0}\mathbf{B̂_d} \end{bmatrix} \ , \quad
11401139
\mathbf{J_x̂} = \begin{cases}
1141-
[\begin{smallmatrix} \mathbf{J_x̂^†} & \mathbf{0} \end{smallmatrix}] & p=0 \\
1142-
\mathbf{J_x̂^†} & p=1 \end{cases} \\
1140+
[\begin{smallmatrix} \mathbf{J_x̂^†} & \mathbf{0} \end{smallmatrix}] & p=0 \\
1141+
[\begin{smallmatrix} \mathbf{0} & \mathbf{J_x̂^†} \end{smallmatrix}] & p=1 \end{cases} \\
11431142
\mathbf{B_x̂} &= \begin{bmatrix}
11441143
\mathbf{S}(0) \\
11451144
\mathbf{S}(1) \\
@@ -1218,22 +1217,20 @@ function init_predmat_mhe(
12181217
# --- measured disturbances D ---
12191218
nĈm_Âpow_B̂d = reduce(vcat, getpower(nĈm_Âpow3D, i)*B̂d for i=0:He-1)
12201219
nĈm_Âpow_B̂d = [-D̂dm; nĈm_Âpow_B̂d]
1221-
J = zeros(NT, nym*He, nd*(He+1-p))
1222-
col_begin = iszero(p) ? 1 : 0
1223-
col_end = iszero(p) ? He+1 : He
1224-
i=0
1225-
for j=col_begin:col_end-1
1220+
J = zeros(NT, nym*He, nd*(He+1))
1221+
i = 0
1222+
for j=1:He
12261223
iRow = (1 + i*nym):(nym*He)
12271224
iCol = (1:nd) .+ j*nd
12281225
J[iRow, iCol] = nĈm_Âpow_B̂d[1:length(iRow) ,:]
12291226
i+=1
12301227
end
12311228
iszero(p) && @views (J[:, 1:nd] = nĈm_Âpow_B̂d[nym+1:end, :])
12321229
Âpow_B̂d = reduce(vcat, getpower(Âpow3D, i)*B̂d for i=0:He-1)
1233-
Jx̂ = zeros(NT, nx̂*He, nd*(He+1-p))
1230+
Jx̂ = zeros(NT, nx̂*He, nd*(He+1))
12341231
for j=0:He-1
12351232
iRow = (1 + j*nx̂):(nx̂*He)
1236-
iCol = (1:nd) .+ j*nd
1233+
iCol = (1:nd) .+ j*nd .+ p
12371234
Jx̂[iRow, iCol] = Âpow_B̂d[1:length(iRow) ,:]
12381235
end
12391236
# --- state x̂op and state update f̂op operating points ---

src/estimator/mhe/execute.jl

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ function init_estimate_cov!(estim::MovingHorizonEstimator, _ , d0, u0)
1010
estim.H̃ .= 0
1111
estim.q̃ .= 0
1212
estim.r .= 0
13-
if estim.direct
14-
# add u0(-1) and d0(-1) to the data windows:
15-
estim.U0[1:estim.model.nu] .= u0
16-
estim.D0[1:estim.model.nd] .= d0
17-
end
13+
estim.direct && estim.U0[1:estim.model.nu] .= u0 # add u0(-1) to the data windows
14+
estim.D0[1:estim.model.nd] .= d0 # add d0(-1) to the data windows
1815
estim.lastu0 .= u0
19-
# estim.cov.P̂_0 is in fact P̂(-1|-1) is estim.direct==false, else P̂(-1|0)
16+
# estim.cov.P̂_0 is P̂(-1|-1) if estim.direct==false, else P̂(-1|0)
2017
invert_cov!(estim, estim.cov.P̂_0)
2118
estim.P̂arr_old .= estim.cov.P̂_0
2219
estim.x̂0arr_old .= 0
@@ -323,10 +320,7 @@ function add_data_windows!(estim::MovingHorizonEstimator, y0m, d0, u0=estim.last
323320
estim.Nk .= estim.He
324321
else
325322
estim.Y0m[(1 + nym*(Nk-1)):(nym*Nk)] .= y0m
326-
if nd > 0
327-
# D0 include 1 additional measured disturbance if direct==true (p==0):
328-
estim.D0[(1 + nd*(Nk-p)):(nd*Nk+1-p)] .= d0
329-
end
323+
nd > 0 && (estim.D0[(1 + nd*Nk):(nd*(Nk+1))] .= d0)
330324
estim.U0[(1 + nu*(Nk-1)):(nu*Nk)] .= u0
331325
estim.X̂0[(1 + nx̂*(Nk-1)):(nx̂*Nk)] .= x̂0
332326
estim.Ŵ[(1 + nŵ*(Nk-1)):(nŵ*Nk)] .=

0 commit comments

Comments
 (0)