Skip to content

adds more tests #43

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

Merged
merged 8 commits into from
Apr 30, 2022
Merged
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 src/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ D = Differential(t)
export RealInput, RealOutput, SISO
include("utils.jl")

export Gain, Sum, MatrixGain, Feedback, Add, Product, Division
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division
export Abs, Sign, Sqrt, Sin, Cos, Tan, Asin, Acos, Atan, Atan2, Sinh, Cosh, Tanh, Exp
export Log, Log10
include("math.jl")
Expand Down
36 changes: 22 additions & 14 deletions src/Blocks/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,12 @@ function PID(;name, k=1, Ti=false, Td=false, Nd=10, xi_start=0, xd_start=0)
push!(eqs, connect(err_input, int.input))
push!(eqs, connect(int.output, addPID.input2))
else
push!(eqs, connect(err_input, Izero.input))
push!(eqs, connect(Izero.output, addPID.input2))
end
if with_D
push!(eqs, connect(err_input, der.input))
push!(eqs, connect(der.output, addPID.input3))
else
push!(eqs, connect(err_input, Dzero.input))
push!(eqs, connect(Dzero.output, addPID.input3))
end
ODESystem(eqs, t, [], []; name=name, systems=sys)
Expand Down Expand Up @@ -285,6 +283,7 @@ function LimPID(; name, k=1, Ti=false, Td=false, wp=1, wd=1,
)
with_I = !isequal(Ti, false)
with_D = !isequal(Td, false)
with_AWM = Ni != Inf
if gains
Ti = k / Ti
Td = Td / k
Expand All @@ -302,12 +301,16 @@ function LimPID(; name, k=1, Ti=false, Td=false, wp=1, wd=1,
@named addP = Add(k1=wp, k2=-1)
@named gainPID = Gain(k)
@named addPID = Add3()
@named limiter = Limiter(y_max=u_max, y_min=u_min)
if with_I
@named addI = Add3(k1=1, k2=-1, k3=1)
if with_AWM
@named addI = Add3(k1=1, k2=-1, k3=1)
@named addSat = Add(k1=1, k2=-1)
@named gainTrack = Gain(1/(k * Ni))
else
@named addI = Add(k1=1, k2=-1)
end
@named int = Integrator(k=1/Ti, x_start=xi_start)
@named limiter = Limiter(y_max=u_max, y_min=u_min)
@named addSat = Add(k1=1, k2=-1)
@named gainTrack = Gain(1/(k * Ni))
else
@named Izero = Constant(k=0)
end
Expand All @@ -318,9 +321,12 @@ function LimPID(; name, k=1, Ti=false, Td=false, wp=1, wd=1,
@named Dzero = Constant(k=0)
end

sys = [reference, measurement, ctr_output, addP, gainPID, addPID]
sys = [reference, measurement, ctr_output, addP, gainPID, addPID, limiter]
if with_I
push!(sys, [addI, int, limiter, addSat, gainTrack]...)
if with_AWM
push!(sys, [addSat, gainTrack]...)
end
push!(sys, [addI, int]...)
else
push!(sys, Izero)
end
Expand All @@ -335,16 +341,18 @@ function LimPID(; name, k=1, Ti=false, Td=false, wp=1, wd=1,
connect(measurement, addP.input2),
connect(addP.output, addPID.input1),
connect(addPID.output, gainPID.input),
connect(gainPID.output, limiter.input),
connect(limiter.output, ctr_output),
]
if with_I
push!(eqs, connect(reference, addI.input1))
push!(eqs, connect(measurement, addI.input2))
push!(eqs, connect(gainPID.output, limiter.input))
push!(eqs, connect(limiter.output, ctr_output))
push!(eqs, connect(limiter.input, addSat.input2))
push!(eqs, connect(limiter.output, addSat.input1))
push!(eqs, connect(addSat.output, gainTrack.input))
push!(eqs, connect(gainTrack.output, addI.input3))
if with_AWM
push!(eqs, connect(limiter.input, addSat.input2))
push!(eqs, connect(limiter.output, addSat.input1))
push!(eqs, connect(addSat.output, gainTrack.input))
push!(eqs, connect(gainTrack.output, addI.input3))
end
push!(eqs, connect(addI.output, int.input))
push!(eqs, connect(int.output, addPID.input3))
else
Expand Down
180 changes: 169 additions & 11 deletions test/Blocks/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ an integrator with a constant input is often used together with the system under
sys = structural_simplify(iosys)
prob = ODEProblem(sys, Pair[int.x=>1.0], (0.0, 1.0))
sol = solve(prob, Rodas4())
@test sol[int.output.u][end] ≈ 2
@test all(sol[c.output.u] .≈ 1)
@test sol[int.output.u][end] .≈ 2 # expected solution
end

@testset "Derivative" begin
Expand All @@ -34,7 +35,7 @@ end
sys = structural_simplify(iosys)
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 10.0))
sol = solve(prob, Rodas4())
@test isapprox(sol[source.output.u], sol[int.output.u], atol=1e-1)
@test all(isapprox.(sol[source.output.u], sol[int.output.u], atol=1e-1))
end

@testset "PT1" begin
Expand Down Expand Up @@ -88,6 +89,9 @@ end
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())
# initial condition
@test sol[ss.x[1]][1] ≈ 0 atol=1e-3
@test sol[ss.x[2]][1] ≈ 0 atol=1e-3
# equilibrium point is at [1, 0]
@test sol[ss.x[1]][end] ≈ 1 atol=1e-3
@test sol[ss.x[2]][end] ≈ 0 atol=1e-3
Expand All @@ -108,7 +112,8 @@ function Plant(;name, x_start=zeros(2))
end

@testset "PI" begin
@named ref = Constant(; k=2)
re_val = 2
@named ref = Constant(; k=re_val)
@named pi_controller = PI(k=1, T=1)
@named plant = Plant()
@named fb = Feedback()
Expand All @@ -125,11 +130,13 @@ end
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())
@test sol[ref.output.u - plant.output.u][end] ≈ 0 atol=1e-3 # zero control error after 100s
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
@test sol[plant.output.u][end] ≈ re_val atol=1e-3 # zero control error after 100s
end

@testset "PID" begin
@named ref = Constant(; k=2)
re_val = 2
@named ref = Constant(; k=re_val)
@named pid_controller = PID(k=3, Ti=0.5, Td=100)
@named plant = Plant()
@named fb = Feedback()
Expand All @@ -146,7 +153,46 @@ end
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())
@test sol[ref.output.u - plant.output.u][end] ≈ 0 atol=1e-3 # zero control error after 100s
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
@test sol[plant.output.u][end] ≈ re_val atol=1e-3 # zero control error after 100s

@testset "PI" begin
@named pid_controller = PID(k=3, Ti=0.5, Td=false)
@named model = ODESystem(
[
connect(ref.output, fb.input1),
connect(plant.output, fb.input2),
connect(fb.output, pid_controller.err_input),
connect(pid_controller.ctr_output, plant.input),
],
t,
systems=[pid_controller, plant, ref, fb]
)
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
@test sol[plant.output.u][end] ≈ re_val atol=1e-3 # zero control error after 100s
end

@testset "PD" begin
@named pid_controller = PID(k=10, Ti=false, Td=1)
@named model = ODESystem(
[
connect(ref.output, fb.input1),
connect(plant.output, fb.input2),
connect(fb.output, pid_controller.err_input),
connect(pid_controller.ctr_output, plant.input),
],
t,
systems=[pid_controller, plant, ref, fb]
)
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
@test sol[plant.output.u][end] > 1 # without I there will be a steady-state error
end
end

@test_skip begin
Expand Down Expand Up @@ -236,7 +282,8 @@ end
end

@testset "LimPI" begin
@named ref = Constant(; k=1)
re_val = 1
@named ref = Constant(; k=re_val)
@named pi_controller_lim = LimPI(k=3, T=0.5, u_max=1.5, u_min=-1.5, Ta=0.1)
@named pi_controller = PI(k=3, T=0.5)
@named sat = Limiter(y_max=1.5, y_min=-1.5)
Expand Down Expand Up @@ -279,15 +326,19 @@ end
sol = solve(prob, Rodas4())
end

@test sol[ref.output.u - plant.output.u][end] ≈ 0 atol=1e-3 # zero control error after 100s
@test sol_lim[ref.output.u - plant.output.u][end] ≈ 0 atol=1e-3 # zero control error after 100s
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
@test all(isapprox.(sol_lim[ref.output.u], re_val, atol=1e-3)) # check reference
@test sol[plant.output.u][end] ≈ re_val atol=1e-3 # zero control error after 100s
@test sol_lim[plant.output.u][end] ≈ re_val atol=1e-3 # zero control error after 100s
@test all(-1.5 .<= sol_lim[pi_controller_lim.ctr_output.u] .<= 1.5) # test limit

# Plots.plot(sol; vars=[plant.output.u]) # without anti-windup measure
# Plots.plot!(sol_lim; vars=[plant.output.u]) # with anti-windup measure
end

@testset "LimPID" begin
@named ref = Constant(; k=1)
re_val = 1
@named ref = Constant(; k=re_val)
@named pid_controller = LimPID(k=3, Ti=0.5, Td=100, u_max=1.5, u_min=-1.5, Ni=0.1/0.5)
@named plant = Plant()
@named model = ODESystem(
Expand All @@ -304,5 +355,112 @@ end
sol = solve(prob, Rodas4())

# Plots.plot(sol, vars=[plant.output.u, plant.input.u])
@test sol[ref.output.u - plant.output.u][end] ≈ 0 atol=1e-3 # zero control error after 100s
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
@test sol[plant.output.u][end] ≈ re_val atol=1e-3 # zero control error after 100s
@test all(-1.5 .<= sol[pid_controller.ctr_output.u] .<= 1.5) # test limit

@testset "PI" begin
@named pid_controller = LimPID(k=3, Ti=0.5, Td=false, u_max=1.5, u_min=-1.5, Ni=0.1/0.5)
@named model = ODESystem(
[
connect(ref.output, pid_controller.reference),
connect(plant.output, pid_controller.measurement),
connect(pid_controller.ctr_output, plant.input),
],
t,
systems=[pid_controller, plant, ref]
)
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())

# Plots.plot(sol, vars=[plant.output.u, plant.input.u])
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
@test sol[plant.output.u][end] ≈ re_val atol=1e-3 # zero control error after 100s
@test all(-1.5 .<= sol[pid_controller.ctr_output.u] .<= 1.5) # test limit
end
@testset "PD" begin
@named pid_controller = LimPID(k=10, Ti=false, Td=1, u_max=1.5, u_min=-1.5)
@named model = ODESystem(
[
connect(ref.output, pid_controller.reference),
connect(plant.output, pid_controller.measurement),
connect(pid_controller.ctr_output, plant.input),
],
t,
systems=[pid_controller, plant, ref]
)
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())

# Plots.plot(sol, vars=[plant.output.u, plant.input.u])
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
@test sol[plant.output.u][end] > 0.5 # without I there will be a steady-state error
@test all(-1.5 .<= sol[pid_controller.ctr_output.u] .<= 1.5) # test limit
end
@testset "set-point weights" begin
@testset "wp" begin
@named pid_controller = LimPID(k=3, Ti=0.5, Td=100, u_max=1.5, u_min=-1.5, Ni=0.1/0.5, wp=0, wd=1)
@named model = ODESystem(
[
connect(ref.output, pid_controller.reference),
connect(plant.output, pid_controller.measurement),
connect(pid_controller.ctr_output, plant.input),
],
t,
systems=[pid_controller, plant, ref]
)
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())

# Plots.plot(sol, vars=[plant.output.u, plant.input.u])
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
sol[pid_controller.addP.output.u] == -sol[pid_controller.measurement.u]
@test sol[plant.output.u][end] ≈ re_val atol=1e-3 # zero control error after 100s
@test all(-1.5 .<= sol[pid_controller.ctr_output.u] .<= 1.5) # test limit
end
@testset "wd" begin
@named pid_controller = LimPID(k=3, Ti=0.5, Td=100, u_max=1.5, u_min=-1.5, Ni=0.1/0.5, wp=1, wd=0)
@named model = ODESystem(
[
connect(ref.output, pid_controller.reference),
connect(plant.output, pid_controller.measurement),
connect(pid_controller.ctr_output, plant.input),
],
t,
systems=[pid_controller, plant, ref]
)
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())

# Plots.plot(sol, vars=[plant.output.u, plant.input.u])
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
@test sol[plant.output.u][end] ≈ re_val atol=1e-3 # zero control error after 100s
sol[pid_controller.addD.output.u] == -sol[pid_controller.measurement.u]
@test all(-1.5 .<= sol[pid_controller.ctr_output.u] .<= 1.5) # test limit
end
end
@testset "PI without AWM" begin
@named pid_controller = LimPID(k=3, Ti=0.5, Td=false, u_max=1.5, u_min=-1.5, Ni=Inf)
@named model = ODESystem(
[
connect(ref.output, pid_controller.reference),
connect(plant.output, pid_controller.measurement),
connect(pid_controller.ctr_output, plant.input),
],
t,
systems=[pid_controller, plant, ref]
)
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())

# Plots.plot(sol, vars=[plant.output.u, plant.input.u])
@test all(isapprox.(sol[ref.output.u], re_val, atol=1e-3)) # check reference
@test sol[plant.output.u][end] ≈ re_val atol=1e-3 # zero control error after 100s
@test all(-1.5 .<= sol[pid_controller.ctr_output.u] .<= 1.5) # test limit
end
end
Loading