Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump #159

Merged
merged 3 commits into from
Feb 15, 2025
Merged

bump #159

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ModelPredictiveControl"
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
authors = ["Francis Gagnon"]
version = "1.3.1"
version = "1.3.2"

[deps]
ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e"
Expand Down
4 changes: 2 additions & 2 deletions src/controller/execute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,11 @@ Call `savetime!(mpc.estim.model)` and return the time `t`.
savetime!(mpc::PredictiveController) = savetime!(mpc.estim.model)

"""
periodsleep(mpc::PredictiveController) -> nothing
periodsleep(mpc::PredictiveController, busywait=false) -> nothing

Call `periodsleep(mpc.estim.model)`.
"""
periodsleep(mpc::PredictiveController) = periodsleep(mpc.estim.model)
periodsleep(mpc::PredictiveController, busywait=false) = periodsleep(mpc.estim.model, busywait)

"""
setstate!(mpc::PredictiveController, x̂) -> mpc
Expand Down
4 changes: 2 additions & 2 deletions src/estimator/execute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,11 @@ Call `savetime!(estim.model)` and return the time `t`.
savetime!(estim::StateEstimator) = savetime!(estim.model)

"""
periodsleep(estim::StateEstimator) -> nothing
periodsleep(estim::StateEstimator, busywait=false) -> nothing

Call `periodsleep(estim.model)`.
"""
periodsleep(estim::StateEstimator) = periodsleep(estim.model)
periodsleep(estim::StateEstimator, busywait=false) = periodsleep(estim.model, busywait)


"""
Expand Down
10 changes: 10 additions & 0 deletions test/0_test_module.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@testmodule SetupMPCtests begin
using ControlSystemsBase
Ts = 400.0
sys = [ tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]);
tf(-0.74,[800.0,1]) tf(0.74,[800.0,1]) tf(-0.74,[800.0,1]) ]
sys_ss = minreal(ss(sys))
Gss = c2d(sys_ss[:,1:2], Ts, :zoh)
Gss2 = c2d(sys_ss[:,1:2], 0.5Ts, :zoh)
export Ts, sys, sys_ss, Gss, Gss2
end
15 changes: 11 additions & 4 deletions test/test_sim_model.jl → test/1_test_sim_model.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@testitem "LinModel construction" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(sys, Ts, i_u=1:2)
@test linmodel1.nx == 2
@test linmodel1.nu == 2
Expand Down Expand Up @@ -97,6 +98,7 @@
end

@testitem "LinModel sim methods" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase
linmodel1 = setop!(LinModel(Gss), uop=[10,50], yop=[50,30])
@test updatestate!(linmodel1, [10, 50]) ≈ zeros(2)
@test updatestate!(linmodel1, [10, 50], Float64[]) ≈ zeros(2)
Expand All @@ -117,25 +119,27 @@ end
end

@testitem "LinModel real time simulations" setup=[SetupMPCtests] begin
linmodel1 = LinModel(tf(2, [10, 1]), 0.1)
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(tf(2, [10, 1]), 0.25)
times1 = zeros(5)
for i=1:5
times1[i] = savetime!(linmodel1)
updatestate!(linmodel1, [1])
periodsleep(linmodel1)
end
@test all(isapprox.(diff(times1[2:end]), 0.1, atol=0.01))
linmodel2 = LinModel(tf(2, [0.1, 1]), 0.001)
@test all(isapprox.(diff(times1[2:end]), 0.25, atol=0.01))
linmodel2 = LinModel(tf(2, [0.1, 1]), 0.25)
times2 = zeros(5)
for i=1:5
times2[i] = savetime!(linmodel2)
updatestate!(linmodel2, [1])
periodsleep(linmodel2, true)
end
@test all(isapprox.(diff(times2[2:end]), 0.001, atol=0.0001))
@test all(isapprox.(diff(times2[2:end]), 0.25, atol=0.0001))
end

@testitem "NonLinModel construction" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
f1(x,u,_,model) = model.A*x + model.Bu*u
h1(x,_,model) = model.C*x
Expand Down Expand Up @@ -243,6 +247,7 @@ end
end

@testitem "NonLinModel sim methods" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
f1(x,u,_,model) = model.A*x + model.Bu*u
h1(x,_,model) = model.C*x
Expand All @@ -262,6 +267,7 @@ end
end

@testitem "NonLinModel linearization" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, ForwardDiff
Ts = 1.0
f1(x,u,d,_) = x.^5 + u.^4 + d.^3
h1(x,d,_) = x.^2 + d
Expand Down Expand Up @@ -317,6 +323,7 @@ end
end

@testitem "NonLinModel real time simulations" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(tf(2, [10, 1]), 0.1)
nonlinmodel1 = NonLinModel(
(x,u,_,_)->linmodel1.A*x + linmodel1.Bu*u,
Expand Down
37 changes: 32 additions & 5 deletions test/test_state_estim.jl → test/2_test_state_estim.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@testitem "SteadyKalmanFilter construction" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
skalmanfilter1 = SteadyKalmanFilter(linmodel1)
@test skalmanfilter1.nym == 2
Expand Down Expand Up @@ -61,6 +62,7 @@
end

@testitem "SteadyKalmanFilter estimator methods" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
skalmanfilter1 = SteadyKalmanFilter(linmodel1, nint_ym=[1, 1])
preparestate!(skalmanfilter1, [50, 30])
Expand Down Expand Up @@ -112,6 +114,7 @@ end
end

@testitem "SteadyKalmanFilter set model" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
skalmanfilter = SteadyKalmanFilter(linmodel, nint_ym=0)
Expand All @@ -122,19 +125,21 @@ end
end

@testitem "SteadyKalmanFilter real-time simulations" setup=[SetupMPCtests] begin
linmodel1 = LinModel(tf(2, [10, 1]), 0.1)
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(tf(2, [10, 1]), 0.25)
skalmanfilter1 = SteadyKalmanFilter(linmodel1)
times1 = zeros(5)
for i=1:5
times1[i] = savetime!(skalmanfilter1)
preparestate!(skalmanfilter1, [1])
updatestate!(skalmanfilter1, [1], [1])
periodsleep(skalmanfilter1)
periodsleep(skalmanfilter1, true)
end
@test all(isapprox.(diff(times1[2:end]), 0.1, atol=0.01))
@test all(isapprox.(diff(times1[2:end]), 0.25, atol=0.01))
end

@testitem "KalmanFilter construction" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
kalmanfilter1 = KalmanFilter(linmodel1)
@test kalmanfilter1.nym == 2
Expand Down Expand Up @@ -186,6 +191,7 @@ end
end

@testitem "KalmanFilter estimator methods" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
kalmanfilter1 = KalmanFilter(linmodel1)
preparestate!(kalmanfilter1, [50, 30])
Expand Down Expand Up @@ -232,6 +238,7 @@ end
end

@testitem "KalmanFilter set model" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
kalmanfilter = KalmanFilter(linmodel, nint_ym=0)
Expand Down Expand Up @@ -260,6 +267,7 @@ end
end

@testitem "Luenberger construction" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
lo1 = Luenberger(linmodel1)
@test lo1.nym == 2
Expand Down Expand Up @@ -300,6 +308,7 @@ end
end

@testitem "Luenberger estimator methods" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
lo1 = Luenberger(linmodel1, nint_ym=[1, 1])
preparestate!(lo1, [50, 30])
Expand Down Expand Up @@ -345,6 +354,7 @@ end
end

@testitem "Luenberger set model" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
lo = Luenberger(linmodel, nint_ym=0)
Expand All @@ -353,6 +363,7 @@ end
end

@testitem "InternalModel construction" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
internalmodel1 = InternalModel(linmodel1)
@test internalmodel1.nym == 2
Expand Down Expand Up @@ -422,6 +433,7 @@ end
end

@testitem "InternalModel estimator methods" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]) , uop=[10,50], yop=[50,30])
internalmodel1 = InternalModel(linmodel1)
preparestate!(internalmodel1, [50, 30] .+ 1)
Expand Down Expand Up @@ -459,6 +471,7 @@ end
end

@testitem "InternalModel set model" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
internalmodel = InternalModel(linmodel)
Expand All @@ -484,6 +497,7 @@ end
end

@testitem "UnscentedKalmanFilter construction" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(sys,Ts,i_d=[3])
f(x,u,d,_) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
h(x,d,_) = linmodel1.C*x + linmodel1.Du*d
Expand Down Expand Up @@ -541,6 +555,7 @@ end
end

@testitem "UnscentedKalmanFilter estimator methods" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
f(x,u,_,_) = linmodel1.A*x + linmodel1.Bu*u
h(x,_,_) = linmodel1.C*x
Expand Down Expand Up @@ -589,6 +604,7 @@ end
end

@testitem "UnscentedKalmanFilter set model" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
ukf1 = UnscentedKalmanFilter(linmodel, nint_ym=0)
Expand Down Expand Up @@ -625,6 +641,7 @@ end
end

@testitem "ExtendedKalmanFilter construction" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(sys,Ts,i_d=[3])
f(x,u,d,_) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
h(x,d,_) = linmodel1.C*x + linmodel1.Du*d
Expand Down Expand Up @@ -678,6 +695,7 @@ end
end

@testitem "ExtendedKalmanFilter estimator methods" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
f(x,u,_,_) = linmodel1.A*x + linmodel1.Bu*u
h(x,_,_) = linmodel1.C*x
Expand Down Expand Up @@ -726,6 +744,7 @@ end
end

@testitem "ExtendedKalmanFilter set model" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
ekf1 = ExtendedKalmanFilter(linmodel, nint_ym=0)
Expand Down Expand Up @@ -762,6 +781,7 @@ end
end

@testitem "MovingHorizonEstimator construction" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt
linmodel1 = LinModel(sys,Ts,i_d=[3])
f(x,u,d,_) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
h(x,d,_) = linmodel1.C*x + linmodel1.Du*d
Expand Down Expand Up @@ -843,6 +863,7 @@ end
end

@testitem "MovingHorizonEstimator estimation and getinfo" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt, ForwardDiff
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2], i_d=[3]), uop=[10,50], yop=[50,30], dop=[5])
f(x,u,d,_) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
h(x,d,_) = linmodel1.C*x + linmodel1.Dd*d
Expand Down Expand Up @@ -935,12 +956,12 @@ end
preparestate!(mhe2, [50, 30], [5])
updatestate!(mhe2, [11, 52], [50, 30], [5])
end
@test mhe2([5]) ≈ [50, 30] atol=1e-3
@test mhe2([5]) ≈ [50, 30] atol=1e-2
for i in 1:40
preparestate!(mhe2, [51, 32], [5])
updatestate!(mhe2, [10, 50], [51, 32], [5])
end
@test mhe2([5]) ≈ [51, 32] atol=1e-3
@test mhe2([5]) ≈ [51, 32] atol=1e-2
linmodel3 = LinModel{Float32}(0.5*ones(1,1), ones(1,1), ones(1,1), zeros(1,0), zeros(1,0), 1.0)
mhe3 = MovingHorizonEstimator(linmodel3, He=1)
preparestate!(mhe3, [0])
Expand Down Expand Up @@ -973,6 +994,7 @@ end
end

@testitem "MovingHorizonEstimator fallbacks for arrival covariance estimation" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = setop!(LinModel(sys,Ts,i_u=[1,2], i_d=[3]), uop=[10,50], yop=[50,30], dop=[5])
f(x,u,d,_) = linmodel.A*x + linmodel.Bu*u + linmodel.Bd*d
h(x,d,_) = linmodel.C*x + linmodel.Dd*d
Expand Down Expand Up @@ -1017,6 +1039,7 @@ end
end

@testitem "MovingHorizonEstimator set constraints" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
mhe1 = MovingHorizonEstimator(linmodel1, He=1, nint_ym=0, Cwt=1e3)
setconstraint!(mhe1, x̂min=[-51,-52], x̂max=[53,54])
Expand Down Expand Up @@ -1098,6 +1121,7 @@ end
end

@testitem "MovingHorizonEstimator constraint violation" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
mhe = MovingHorizonEstimator(linmodel1, He=1, nint_ym=0)

Expand Down Expand Up @@ -1196,6 +1220,7 @@ end
end

@testitem "MovingHorizonEstimator set model" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
mhe = MovingHorizonEstimator(linmodel, He=1, nint_ym=0, direct=false)
Expand Down Expand Up @@ -1240,6 +1265,7 @@ end
end

@testitem "MovingHorizonEstimator v.s. Kalman filters" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = setop!(LinModel(sys,Ts,i_d=[3]), uop=[10,50], yop=[50,30], dop=[20])
mhe = MovingHorizonEstimator(linmodel1, He=3, nint_ym=0, direct=false)
kf = KalmanFilter(linmodel1, nint_ym=0, direct=false)
Expand Down Expand Up @@ -1316,6 +1342,7 @@ end
end

@testitem "MovingHorizonEstimator LinModel v.s. NonLinModel" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt
linmodel = setop!(LinModel(sys,Ts,i_d=[3]), uop=[10,50], yop=[50,30], dop=[20])
f = (x,u,d,_) -> linmodel.A*x + linmodel.Bu*u + linmodel.Bd*d
h = (x,d,_) -> linmodel.C*x + linmodel.Dd*d
Expand Down
Loading