Skip to content

Commit 35d90aa

Browse files
committed
lastu0 instead of lastu in StateEstimator
1 parent 4074c77 commit 35d90aa

File tree

6 files changed

+40
-40
lines changed

6 files changed

+40
-40
lines changed

src/controller/linmpc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function write_info!(mpc::LinMPC, ΔŨ, J, ŷs, Ŷs)
233233
mpc.info.ΔŨ = ΔŨ
234234
mpc.info.ϵ = isinf(mpc.C) ? NaN : ΔŨ[end]
235235
mpc.info.J = J
236-
mpc.info.U = mpc.S̃_Hp*ΔŨ + mpc.T_Hp*mpc.estim.lastu
236+
mpc.info.U = mpc.S̃_Hp*ΔŨ + mpc.T_Hp*(mpc.estim.lastu0 + mpc.estim.model.uop)
237237
mpc.info.u = mpc.info.U[1:mpc.estim.model.nu]
238238
mpc.info.= mpc.
239239
mpc.info.= mpc.*ΔŨ + mpc.F

src/estimator/internal_model.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct InternalModel{M<:SimModel} <: StateEstimator
22
model::M
3-
lastu::Vector{Float64}
3+
lastu0::Vector{Float64}
44
::Vector{Float64}
55
x̂d::Vector{Float64}
66
x̂s::Vector{Float64}
@@ -16,7 +16,7 @@ struct InternalModel{M<:SimModel} <: StateEstimator
1616
Âs::Matrix{Float64}
1717
B̂s::Matrix{Float64}
1818
function InternalModel{M}(model::M, i_ym, Asm, Bsm, Csm, Dsm) where {M<:SimModel}
19-
ny = model.ny
19+
nu, ny = model.nu, model.ny
2020
nym, nyu = length(i_ym), ny - length(i_ym)
2121
if isa(model, LinModel)
2222
poles = eigvals(model.A)
@@ -38,12 +38,12 @@ struct InternalModel{M<:SimModel} <: StateEstimator
3838
nxs = size(As,1)
3939
Âs, B̂s = init_internalmodel(As, Bs, Cs, Ds)
4040
i_ym = collect(i_ym)
41-
lastu = copy(model.uop)
41+
lastu0 = zeros(nu)
4242
x̂d == copy(model.x) # x̂ and x̂d are same object (updating x̂d will update x̂)
4343
x̂s = zeros(nxs)
4444
return new(
4545
model,
46-
lastu, x̂, x̂d, x̂s,
46+
lastu0, x̂, x̂d, x̂s,
4747
i_ym, nx̂, nym, nyu, nxs,
4848
As, Bs, Cs, Ds,
4949
Âs, B̂s

src/estimator/kalman.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct SteadyKalmanFilter <: StateEstimator
22
model::LinModel
3-
lastu::Vector{Float64}
3+
lastu0::Vector{Float64}
44
::Vector{Float64}
55
i_ym::Vector{Int}
66
nx̂::Int
@@ -21,7 +21,7 @@ struct SteadyKalmanFilter <: StateEstimator
2121
::Hermitian{Float64, Matrix{Float64}}
2222
K::Matrix{Float64}
2323
function SteadyKalmanFilter(model, i_ym, nint_ym, Asm, Csm, Q̂, R̂)
24-
nx, ny = model.nx, model.ny
24+
nu, nx, ny = model.nu, model.nx, model.ny
2525
nym, nyu = length(i_ym), ny - length(i_ym)
2626
nxs = size(Asm,1)
2727
nx̂ = nx + nxs
@@ -39,13 +39,13 @@ struct SteadyKalmanFilter <: StateEstimator
3939
end
4040
end
4141
i_ym = collect(i_ym)
42-
lastu = copy(model.uop)
42+
lastu0 = zeros(nu)
4343
= [copy(model.x); zeros(nxs)]
4444
= Hermitian(Q̂, :L)
4545
= Hermitian(R̂, :L)
4646
return new(
4747
model,
48-
lastu, x̂,
48+
lastu0, x̂,
4949
i_ym, nx̂, nym, nyu, nxs,
5050
As, Cs, nint_ym,
5151
Â, B̂u, B̂d, Ĉ, D̂d,
@@ -170,7 +170,7 @@ end
170170

171171
struct KalmanFilter <: StateEstimator
172172
model::LinModel
173-
lastu::Vector{Float64}
173+
lastu0::Vector{Float64}
174174
::Vector{Float64}
175175
::Hermitian{Float64, Matrix{Float64}}
176176
i_ym::Vector{Int}
@@ -192,7 +192,7 @@ struct KalmanFilter <: StateEstimator
192192
::Hermitian{Float64, Matrix{Float64}}
193193
::Hermitian{Float64, Matrix{Float64}}
194194
function KalmanFilter(model, i_ym, nint_ym, Asm, Csm, P̂0, Q̂, R̂)
195-
nx, ny = model.nx, model.ny
195+
nu, nx, ny = model.nu, model.nx, model.ny
196196
nym, nyu = length(i_ym), ny - length(i_ym)
197197
nxs = size(Asm,1)
198198
nx̂ = nx + nxs
@@ -201,15 +201,15 @@ struct KalmanFilter <: StateEstimator
201201
Â, B̂u, Ĉ, B̂d, D̂d = augment_model(model, As, Cs)
202202
Ĉm, D̂dm = Ĉ[i_ym, :], D̂d[i_ym, :] # measured outputs ym only
203203
i_ym = collect(i_ym)
204-
lastu = copy(model.uop)
204+
lastu0 = zeros(nu)
205205
= [copy(model.x); zeros(nxs)]
206206
P̂0 = Hermitian(P̂0, :L)
207207
= Hermitian(Q̂, :L)
208208
= Hermitian(R̂, :L)
209209
= copy(P̂0)
210210
return new(
211211
model,
212-
lastu, x̂, P̂,
212+
lastu0, x̂, P̂,
213213
i_ym, nx̂, nym, nyu, nxs,
214214
As, Cs, nint_ym,
215215
Â, B̂u, B̂d, Ĉ, D̂d,
@@ -322,7 +322,7 @@ end
322322

323323
struct UnscentedKalmanFilter{M<:SimModel} <: StateEstimator
324324
model::M
325-
lastu::Vector{Float64}
325+
lastu0::Vector{Float64}
326326
::Vector{Float64}
327327
::Hermitian{Float64, Matrix{Float64}}
328328
i_ym::Vector{Int}
@@ -343,23 +343,23 @@ struct UnscentedKalmanFilter{M<:SimModel} <: StateEstimator
343343
function UnscentedKalmanFilter{M}(
344344
model::M, i_ym, nint_ym, Asm, Csm, P̂0, Q̂, R̂, α, β, κ
345345
) where {M<:SimModel}
346-
nx, ny = model.nx, model.ny
346+
nu, nx, ny = model.nu, model.nx, model.ny
347347
nym, nyu = length(i_ym), ny - length(i_ym)
348348
nxs = size(Asm,1)
349349
nx̂ = nx + nxs
350350
validate_kfcov(nym, nx̂, Q̂, R̂, P̂0)
351351
As, _ , Cs, _ = stoch_ym2y(model, i_ym, Asm, [], Csm, [])
352352
nσ, γ, m̂, Ŝ = init_ukf(nx̂, α, β, κ)
353353
i_ym = collect(i_ym)
354-
lastu = copy(model.uop)
354+
lastu0 = zeros(nu)
355355
= [copy(model.x); zeros(nxs)]
356356
P̂0 = Hermitian(P̂0, :L)
357357
= Hermitian(Q̂, :L)
358358
= Hermitian(R̂, :L)
359359
= copy(P̂0)
360360
return new(
361361
model,
362-
lastu, x̂, P̂,
362+
lastu0, x̂, P̂,
363363
i_ym, nx̂, nym, nyu, nxs,
364364
As, Cs, nint_ym,
365365
P̂0, Q̂, R̂,

src/estimator/luenberger.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct Luenberger <: StateEstimator
22
model::LinModel
3-
lastu::Vector{Float64}
3+
lastu0::Vector{Float64}
44
::Vector{Float64}
55
i_ym::Vector{Int}
66
nx̂::Int
@@ -19,7 +19,7 @@ struct Luenberger <: StateEstimator
1919
D̂dm ::Matrix{Float64}
2020
K::Matrix{Float64}
2121
function Luenberger(model, i_ym, nint_ym, Asm, Csm, L)
22-
nx, ny = model.nx, model.ny
22+
nu, nx, ny = model.nu, model.nx, model.ny
2323
nym, nyu = length(i_ym), ny - length(i_ym)
2424
nxs = size(Asm,1)
2525
nx̂ = nx + nxs
@@ -29,11 +29,11 @@ struct Luenberger <: StateEstimator
2929
Ĉm, D̂dm = Ĉ[i_ym, :], D̂d[i_ym, :] # measured outputs ym only
3030
K = L
3131
i_ym = collect(i_ym)
32-
lastu = copy(model.uop)
32+
lastu0 = zeros(nu)
3333
= [copy(model.x); zeros(nxs)]
3434
return new(
3535
model,
36-
lastu, x̂,
36+
lastu0, x̂,
3737
i_ym, nx̂, nym, nyu, nxs,
3838
As, Cs, nint_ym,
3939
Â, B̂u, B̂d, Ĉ, D̂d,

src/predictive_control.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function moveinput!(
263263
ΔŨ, J = optim_objective!(mpc, p)
264264
write_info!(mpc, ΔŨ, J, ŷs, Ŷs)
265265
Δu = ΔŨ[1:mpc.estim.model.nu] # receding horizon principle: only Δu(k) is used (1st one)
266-
u = mpc.estim.lastu + Δu
266+
u = mpc.estim.lastu0 + mpc.estim.model.uop + Δu
267267
return u
268268
end
269269

@@ -344,23 +344,23 @@ end
344344

345345

346346
@doc raw"""
347-
initpred!(mpc, model::LinModel, d, D̂, Ŷs, R̂y, x̂d, lastu)
347+
initpred!(mpc, model::LinModel, d, D̂, Ŷs, R̂y)
348348
349349
Init linear model prediction matrices `F`, `q̃` and `p`.
350350
351351
See [`init_deterpred`](@ref) and [`init_quadprog`](@ref) for the definition of the matrices.
352352
"""
353353
function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, Ŷs, R̂y)
354-
mpc.F[:] = mpc.Kd*mpc.x̂d + mpc.Q*(mpc.estim.lastu - model.uop) + Ŷs + mpc.Yop
354+
mpc.F[:] = mpc.Kd*mpc.x̂d + mpc.Q*mpc.estim.lastu0 + Ŷs + mpc.Yop
355355
if model.nd 0
356356
mpc.F .+= mpc.G*(d - model.dop) + mpc.J*(D̂ - mpc.Dop)
357357
end
358358
= mpc.F - R̂y
359359
mpc.q̃[:] = 2(mpc.M_Hp*mpc.Ẽ)'*
360360
p ='*mpc.M_Hp*
361361
if ~isempty(mpc.R̂u)
362-
= (mpc.T_Hp*mpc.estim.lastu - mpc.R̂u)
363-
mpc. .+= 2(mpc.L_Hp*mpc.T_Hp)'*
362+
= mpc.T_Hp*(mpc.estim.lastu0 + model.uop) - mpc.R̂u
363+
mpc.[:] = mpc.+ 2(mpc.L_Hp*mpc.T_Hp)'*
364364
p +='*mpc.L_Hp*
365365
end
366366
#d0 = zeros(model.nd, 0) # only used for NonLinModel objects
@@ -369,7 +369,7 @@ function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, Ŷs, R̂
369369
end
370370

371371
@doc raw"""
372-
initpred!(mpc::PredictiveController, model::NonLinModel, d, D̂, Ŷs, _ , _ , _ )
372+
initpred!(mpc::PredictiveController, model::NonLinModel, d, D̂, Ŷs, _ )
373373
374374
Init `F`, `d0` and `D̂0` prediction matrices for [`NonLinModel`](@ref).
375375
@@ -387,14 +387,14 @@ function initpred!(mpc::PredictiveController, model::NonLinModel, d, D̂, Ŷs ,
387387
end
388388

389389
@doc raw"""
390-
linconstraint!(mpc::PredictiveController, ::LinModel, lastu)
390+
linconstraint!(mpc::PredictiveController, model::LinModel)
391391
392392
Calc `b` vector for the linear model inequality constraints (``\mathbf{A ΔŨ ≤ b}``).
393393
"""
394-
function linconstaint!(mpc::PredictiveController, ::LinModel)
394+
function linconstaint!(mpc::PredictiveController, model::LinModel)
395395
mpc.con.b[:] = [
396-
-mpc.con.Umin + mpc.T_Hc*mpc.estim.lastu
397-
+mpc.con.Umax - mpc.T_Hc*mpc.estim.lastu
396+
-mpc.con.Umin + mpc.T_Hc*(mpc.estim.lastu0 + model.uop)
397+
+mpc.con.Umax - mpc.T_Hc*(mpc.estim.lastu0 + model.uop)
398398
-mpc.con.ΔŨmin
399399
+mpc.con.ΔŨmax
400400
-mpc.con.Ŷmin + mpc.F
@@ -403,14 +403,14 @@ function linconstaint!(mpc::PredictiveController, ::LinModel)
403403
end
404404

405405
@doc raw"""
406-
linconstraint!(mpc::PredictiveController, ::NonLinModel, lastu)
406+
linconstraint!(mpc::PredictiveController, model::NonLinModel)
407407
408408
Calc `b` without predicted output ``\mathbf{Ŷ}`` constraints for [`NonLinModel`](@ref).
409409
"""
410-
function linconstaint!(mpc::PredictiveController, ::NonLinModel)
410+
function linconstaint!(mpc::PredictiveController, model::NonLinModel)
411411
mpc.con.b[:] = [
412-
-mpc.con.Umin + mpc.T_Hc*mpc.estim.lastu
413-
+mpc.con.Umax - mpc.T_Hc*mpc.estim.lastu
412+
-mpc.con.Umin + mpc.T_Hc*(mpc.estim.lastu0 + model.uop)
413+
+mpc.con.Umax - mpc.T_Hc*(mpc.estim.lastu0 + model.uop)
414414
-mpc.con.ΔŨmin
415415
+mpc.con.ΔŨmax
416416
]

src/state_estim.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ end
4848
4949
Remove operating points on inputs `u`, measured outputs `ym` and disturbances `d`.
5050
51-
Also store current inputs `u` in `estim.lastu`. This field is used for
52-
[`PredictiveController`](@ref) computations.
51+
Also store current inputs without operating points `u0` in `estim.lastu0`. This field is
52+
used for [`PredictiveController`](@ref) computations.
5353
"""
5454
function remove_op!(estim::StateEstimator, u, d, ym)
5555
u0 = u - estim.model.uop
5656
d0 = d - estim.model.dop
5757
ym0 = ym - estim.model.yop[estim.i_ym]
58-
estim.lastu[:] = u
58+
estim.lastu0[:] = u0
5959
return u0, d0, ym0
6060
end
6161

@@ -220,8 +220,8 @@ julia> x̂ = initstate!(estim, [1], [3 - 0.1])
220220
"""
221221
function initstate!(estim::StateEstimator, u, ym, d=Float64[])
222222
model = estim.model
223-
# --- init lastu, used in PredictiveController ---
224-
estim.lastu[:] = u
223+
# --- init lastu0 for PredictiveControllers ---
224+
estim.lastu0[:] = u - model.uop
225225
# --- deterministic model states ---
226226
x̂d = init_deterstate(model, estim, u, d)
227227
# --- stochastic model states (integrators) ---

0 commit comments

Comments
 (0)