Skip to content

Commit eec8d12

Browse files
committed
debug: MHE now work s as expected!
1 parent 70112d0 commit eec8d12

File tree

1 file changed

+16
-33
lines changed

1 file changed

+16
-33
lines changed

src/estimator/mhe.jl

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct MovingHorizonEstimator{
7575
X̂, Ym = zeros(NT, nx̂*He), zeros(NT, nym*He)
7676
U, D, Ŵ = zeros(NT, nu*He), zeros(NT, nd*He), zeros(NT, nx̂*He)
7777
x̂0_past = zeros(NT, nx̂)
78-
Nk = [1]
78+
Nk = [0]
7979
estim = new{NT, SM, JM}(
8080
model, optim, W̃,
8181
lastu0, x̂, P̂, He,
@@ -361,7 +361,7 @@ function init_estimate_cov!(estim::MovingHorizonEstimator, _ , _ , _ )
361361
estim.U .= 0
362362
estim.D .= 0
363363
estim.Ŵ .= 0
364-
estim.Nk .= 1
364+
estim.Nk .= 0
365365
return nothing
366366
end
367367

@@ -374,27 +374,27 @@ TBW
374374
"""
375375
function update_estimate!(estim::MovingHorizonEstimator{NT}, u, ym, d) where NT<:Real
376376
model, optim, x̂ = estim.model, estim.optim, estim.
377-
nx̂, nym, nu, nd, nŵ = estim.nx̂, estim.nym, model.nu, model.nd, estim.nx̂
378-
Nk, He = estim.Nk[], estim.He
379-
nŴ, nYm, nX̂ = nx̂*Nk, nym*Nk, nx̂*(Nk+1)
380-
W̃var::Vector{VariableRef} = optim[:W̃var]
377+
nx̂, nym, nu, nd, nŵ, He = estim.nx̂, estim.nym, model.nu, model.nd, estim.nx̂, estim.He
381378
= zeros(nŵ) # ŵ(k) = 0 for warm-starting
382-
if Nk < He
383-
estim.X̂[ (1 + nx̂*(Nk-1)):(nx̂*Nk)] =
384-
estim.Ym[(1 + nym*(Nk-1)):(nym*Nk)] = ym
385-
estim.U[ (1 + nu*(Nk-1)):(nu*Nk)] = u
386-
estim.D[ (1 + nd*(Nk-1)):(nd*Nk)] = d
387-
estim.Ŵ[ (1 + nŵ*(Nk-1)):(nŵ*Nk)] =
388-
else
379+
estim.Nk .+= 1
380+
Nk = estim.Nk[]
381+
if Nk > He
389382
estim.X̂[:] = [estim.X̂[nx̂+1:end] ; x̂]
390383
estim.Ym[:] = [estim.Ym[nym+1:end]; ym]
391384
estim.U[:] = [estim.U[nu+1:end] ; u]
392385
estim.D[:] = [estim.D[nd+1:end] ; d]
393386
estim.Ŵ[:] = [estim.Ŵ[nŵ+1:end] ; ŵ]
387+
estim.Nk[:] = [He]
388+
else
389+
estim.X̂[(1 + nx̂*(Nk-1)):(nx̂*Nk)] =
390+
estim.Ym[(1 + nym*(Nk-1)):(nym*Nk)] = ym
391+
estim.U[(1 + nu*(Nk-1)):(nu*Nk)] = u
392+
estim.D[(1 + nd*(Nk-1)):(nd*Nk)] = d
393+
estim.Ŵ[(1 + nŵ*(Nk-1)):(nŵ*Nk)] =
394394
end
395-
# TODO: vérifier pourquoi ya une discontinuité exactement quand k = He,
396-
# tout est linéaire ici et les contraintes sont éloignées, ça devrait marcher il me
397-
# semble !
395+
Nk = estim.Nk[]
396+
nŴ, nYm, nX̂ = nx̂*Nk, nym*Nk, nx̂*(Nk+1)
397+
W̃var::Vector{VariableRef} = optim[:W̃var]
398398
Ŷm = Vector{NT}(undef, nYm)
399399
= Vector{NT}(undef, nX̂)
400400
estim.x̂0_past[:] = estim.X̂[1:nx̂]
@@ -434,33 +434,16 @@ function update_estimate!(estim::MovingHorizonEstimator{NT}, u, ym, d) where NT<
434434
estim.Ŵ[1:nŴ] = estim.W̃[nx̂+1:nx̂+nŴ] # update Ŵ with optimum for next time step
435435
Ŷm, X̂ = predict!(Ŷm, X̂, estim, model, estim.W̃)
436436
x̂[:] = X̂[(1 + nx̂*Nk):(nx̂*(Nk+1))]
437-
438-
439437
if Nk == He
440438
# update the arrival covariance with an Extended Kalman Filter:
441439
# TODO: also support UnscentedKalmanFilter, and KalmanFilter for LinModel
442440
= ForwardDiff.jacobian(x̂ -> (estim, estim.model, x̂, u, d), estim.x̂)
443-
println(F̂)
444441
= ForwardDiff.jacobian(x̂ -> (estim, estim.model, x̂, d), estim.x̂)
445442
Ĥm = Ĥ[estim.i_ym, :]
446443
update_estimate_kf!(estim, F̂, Ĥm, u, ym, d, updatestate=false)
447444
= estim.
448445
estim.invP̄.data[:] = Hermitian(inv(P̄), :L) # .data is necessary for Hermitian
449446
end
450-
451-
#ng = length(estim.i_g)
452-
#println(ng)
453-
#g = zeros(ng)
454-
#g = con_nonlinprog!(g, estim, model, X̂)
455-
#println(g)
456-
J = obj_nonlinprog(estim, model, Ŷm, W̃curr)
457-
458-
println(J)
459-
println(solution_summary(optim, verbose=false))
460-
461-
462-
463-
estim.Nk[] = Nk < He ? Nk + 1 : He
464447
return nothing
465448
end
466449

0 commit comments

Comments
 (0)