Skip to content

Commit ebfaf96

Browse files
committed
added tests for ExtendedKalmanFIlter
1 parent 293ef23 commit ebfaf96

File tree

4 files changed

+64
-11
lines changed

4 files changed

+64
-11
lines changed

src/controller/nonlinmpc.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ end
203203
Invoke [`getinfo(::PredictiveController)`](@ref) and add `:JE` the economic optimum ``J_E``.
204204
"""
205205
function getinfo(mpc::NonLinMPC)
206-
info, sol_summary = invoke(getinfo, Tuple{PredictiveController}, mpc)
206+
sol_summary, info = invoke(getinfo, Tuple{PredictiveController}, mpc)
207207
U, Ŷ, D̂ = info[:U], info[:Ŷ], info[:D̂]
208208
UE = [U; U[(end - mpc.estim.model.nu + 1):end]]
209209
ŶE = [mpc.ŷ; Ŷ]
210210
D̂E = [mpc.d; D̂]
211211
info[:JE] = mpc.JE(UE, ŶE, D̂E)
212-
return info, sol_summary
212+
return sol_summary, info
213213
end
214214

215215
"""

src/predictive_control.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,8 @@ end
268268
269269
Get additional information about `mpc` controller optimum to ease troubleshooting.
270270
271-
Return the dictionary `info` with the additional information, and `sol_summary`, the
272-
optimizer solution summary that can be printed. The dictionary `info` has the following
273-
fields:
271+
Return the optimizer solution summary that can be printed, `sol_summary`, and the dictionary
272+
`info` with the following fields:
274273
275274
- `:ΔU` : optimal manipulated input increments over `Hc` ``(\mathbf{ΔU})``
276275
- `:ϵ` : optimal slack variable ``(ϵ)``
@@ -292,7 +291,7 @@ julia> mpc = LinMPC(LinModel(tf(5, [2, 1]), 3), Nwt=[0], Hp=1, Hc=1);
292291
293292
julia> u = moveinput!(mpc, [10]);
294293
295-
julia> info, sol_summary = getinfo(mpc); round.(info[:Ŷ], digits=3)
294+
julia> sol_summary, info = getinfo(mpc); round.(info[:Ŷ], digits=3)
296295
1-element Vector{Float64}:
297296
10.0
298297
```
@@ -313,7 +312,7 @@ function getinfo(mpc::PredictiveController)
313312
info[:Ŷd] = info[:Ŷ] - info[:Ŷs]
314313
info[:R̂y] = mpc.R̂y
315314
info[:R̂u] = mpc.R̂u
316-
return info, sol_summary
315+
return sol_summary, info
317316
end
318317

319318
"""

test/test_predictive_control.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ end
6363
@test u [1] atol=1e-2
6464
u = mpc1(r)
6565
@test u [1] atol=1e-2
66-
info, _ = getinfo(mpc1)
66+
_ , info = getinfo(mpc1)
6767
@test info[:u] u
6868
@test info[:Ŷ][end] 5 atol=1e-2
6969
mpc2 = LinMPC(LinModel(tf(5, [2, 1]), 3), Nwt=[0], Cwt=Inf, Hp=1000, Hc=1)
@@ -143,7 +143,7 @@ end
143143
@test u [1] atol=1e-3
144144
u = nmpc_lin(r)
145145
@test u [1] atol=1e-3
146-
info, _ = getinfo(nmpc_lin)
146+
_ , info = getinfo(nmpc_lin)
147147
@test info[:u] u
148148
@test info[:Ŷ][end] 5 atol=1e-2
149149
f(x,u,_) = linmodel.A*x + linmodel.Bu*u
@@ -155,7 +155,7 @@ end
155155
@test u [1] atol=1e-2
156156
u = nmpc1(r)
157157
@test u [1] atol=1e-2
158-
info, _ = getinfo(nmpc1)
158+
_ , info = getinfo(nmpc1)
159159
@test info[:u] u
160160
@test info[:Ŷ][end] 5 atol=1e-2
161161
nmpc2 = NonLinMPC(nonlinmodel, Nwt=[0], Cwt=Inf, Hp=1000, Hc=1)

test/test_state_estim.jl

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,58 @@ end
277277
@test initstate!(ukf1, [10, 50], [50, 30+1]) [zeros(3); [1]]
278278
setstate!(ukf1, [1,2,3,4])
279279
@test ukf1. [1,2,3,4]
280-
end
280+
end
281+
282+
@testset "ExtendedKalmanFilter construction" begin
283+
linmodel1 = LinModel(sys,Ts,i_d=[3])
284+
f(x,u,d) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
285+
h(x,d) = linmodel1.C*x + linmodel1.Du*d
286+
nonlinmodel = NonLinModel(f, h, Ts, 2, 4, 2, 1)
287+
288+
ekf1 = ExtendedKalmanFilter(linmodel1)
289+
@test ekf1.nym == 2
290+
@test ekf1.nyu == 0
291+
@test ekf1.nxs == 2
292+
@test ekf1.nx̂ == 6
293+
294+
ekf2 = ExtendedKalmanFilter(nonlinmodel)
295+
@test ekf2.nym == 2
296+
@test ekf2.nyu == 0
297+
@test ekf2.nxs == 2
298+
@test ekf2.nx̂ == 6
299+
300+
ekf3 = ExtendedKalmanFilter(nonlinmodel, i_ym=[2])
301+
@test ekf3.nym == 1
302+
@test ekf3.nyu == 1
303+
@test ekf3.nxs == 1
304+
@test ekf3.nx̂ == 5
305+
306+
ekf4 = ExtendedKalmanFilter(nonlinmodel, σQ=[1,2,3,4], σQ_int=[5, 6], σR=[7, 8])
307+
@test ekf4. Hermitian(diagm(Float64[1, 4, 9 ,16, 25, 36]))
308+
@test ekf4. Hermitian(diagm(Float64[49, 64]))
309+
310+
ekf5 = ExtendedKalmanFilter(nonlinmodel, nint_ym=[2,2])
311+
@test ekf5.nxs == 4
312+
@test ekf5.nx̂ == 8
313+
314+
ekf6 = ExtendedKalmanFilter(nonlinmodel, σP0=[1,2,3,4], σP0_int=[5,6])
315+
@test ekf6.P̂0 Hermitian(diagm(Float64[1, 4, 9 ,16, 25, 36]))
316+
@test ekf6. Hermitian(diagm(Float64[1, 4, 9 ,16, 25, 36]))
317+
@test ekf6.P̂0 !== ekf6.
318+
end
319+
320+
@testset "ExtendedKalmanFilter estimator methods" begin
321+
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
322+
f(x,u,_) = linmodel1.A*x + linmodel1.Bu*u
323+
h(x,_) = linmodel1.C*x
324+
nonlinmodel = setop!(NonLinModel(f, h, Ts, 2, 2, 2), uop=[10,50], yop=[50,30])
325+
ekf1 = ExtendedKalmanFilter(nonlinmodel)
326+
@test updatestate!(ekf1, [10, 50], [50, 30]) zeros(4) atol=1e-9
327+
@test updatestate!(ekf1, [10, 50], [50, 30], Float64[]) zeros(4) atol=1e-9
328+
@test ekf1. zeros(4) atol=1e-9
329+
@test evaloutput(ekf1) ekf1() [50, 30]
330+
@test evaloutput(ekf1, Float64[]) ekf1(Float64[]) [50, 30]
331+
@test initstate!(ekf1, [10, 50], [50, 30+1]) [zeros(3); [1]]
332+
setstate!(ekf1, [1,2,3,4])
333+
@test ekf1. [1,2,3,4]
334+
end

0 commit comments

Comments
 (0)