Skip to content

Commit 4ee07ed

Browse files
committed
EMPC additions
- added: moveinput! ry arg default value for pure EMPC - added economic cost JE simple unit test
1 parent cac9419 commit 4ee07ed

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelPredictiveControl"
22
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
33
authors = ["Francis Gagnon"]
4-
version = "0.6.0"
4+
version = "0.6.1"
55

66
[deps]
77
ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e"

src/estimator/kalman.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ control period ``k-1``. See [^2] for details.
298298
Linear Dynamical Systems*, https://web.stanford.edu/class/ee363/lectures/kf.pdf.
299299
"""
300300
function update_estimate!(estim::KalmanFilter, u, ym, d)
301-
return update_state_kf!(estim, estim.Â, estim.Ĉm, u, ym, d)
301+
return update_estimate_kf!(estim, estim.Â, estim.Ĉm, u, ym, d)
302302
end
303303

304304
struct UnscentedKalmanFilter{M<:SimModel} <: StateEstimator
@@ -665,7 +665,7 @@ function update_estimate!(estim::ExtendedKalmanFilter, u, ym, d=Float64[])
665665
= ForwardDiff.jacobian(x̂ -> (estim, x̂, u, d), estim.x̂)
666666
= ForwardDiff.jacobian(x̂ -> (estim, x̂, d), estim.x̂)
667667
Ĥm = Ĥ[estim.i_ym, :]
668-
return update_state_kf!(estim, F̂, Ĥm, u, ym, d)
668+
return update_estimate_kf!(estim, F̂, Ĥm, u, ym, d)
669669
end
670670

671671
"Initialize the covariance estimate `P̂` for the time-varying Kalman Filters"
@@ -692,15 +692,15 @@ function validate_kfcov(nym, nx̂, Q̂, R̂, P̂0=nothing)
692692
end
693693

694694
"""
695-
update_state_kf!(estim, Â, Ĉm, u, ym, d)
695+
update_estimate_kf!(estim, Â, Ĉm, u, ym, d)
696696
697697
Update time-varying/extended Kalman Filter estimates with augmented `Â` and `Ĉm` matrices.
698698
699699
Allows code reuse for the time-varying and extended Kalman filters. They update the state
700700
`x̂` and covariance `P̂` with the same equations. The extended filter substitutes the
701701
augmented model matrices with its Jacobians (`Â = F̂` and `Ĉm = Ĥm`).
702702
"""
703-
function update_state_kf!(estim, Â, Ĉm, u, ym, d)
703+
function update_estimate_kf!(estim, Â, Ĉm, u, ym, d)
704704
x̂, P̂, Q̂, R̂ = estim.x̂, estim.P̂, estim.Q̂, estim.
705705
M = (P̂ * Ĉm') / (Ĉm ** Ĉm' + R̂)
706706
K =* M

src/predictive_control.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ setnonlincon!(::PredictiveController, ::SimModel) = nothing
199199
@doc raw"""
200200
moveinput!(
201201
mpc::PredictiveController,
202-
ry,
202+
ry = mpc.estim.model.yop,
203203
d = Float64[];
204204
R̂y = repeat(ry, mpc.Hp),
205205
D̂ = repeat(d, mpc.Hp),
@@ -246,7 +246,7 @@ julia> u = moveinput!(mpc, [5]); round.(u, digits=3)
246246
"""
247247
function moveinput!(
248248
mpc::PredictiveController,
249-
ry::Vector,
249+
ry::Vector = mpc.estim.model.yop,
250250
d ::Vector = Float64[];
251251
R̂y::Vector = repeat(ry, mpc.Hp),
252252
::Vector = repeat(d, mpc.Hp),
@@ -1025,7 +1025,7 @@ end
10251025

10261026
"Functor allowing callable `PredictiveController` object as an alias for `moveinput!`."
10271027
function (mpc::PredictiveController)(
1028-
ry::Vector,
1028+
ry::Vector = mpc.estim.model.yop,
10291029
d ::Vector = Float64[];
10301030
kwargs...
10311031
)

test/test_predictive_control.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ end
164164
nmpc3 = NonLinMPC(nonlinmodel, Mwt=[0], Nwt=[0], Lwt=[1], ru=[12])
165165
u = moveinput!(nmpc3, [0])
166166
@test u [12] atol=1e-2
167+
Hp = 1000
168+
R̂y = fill(5, Hp)
169+
JE = (_ , ŶE, _ ) -> sum((ŶE[2:end] - R̂y).^2)
170+
nmpc4 = NonLinMPC(nonlinmodel, Mwt=[0], Nwt=[0], Cwt=Inf, Ewt=1, JE=JE, Hp=Hp, Hc=1)
171+
u = moveinput!(nmpc4)
172+
@test u [1] atol=1e-2
167173
end
168174

169175
@testset "NonLinMPC other methods" begin

0 commit comments

Comments
 (0)