Skip to content
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
7 changes: 4 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
MatrixEquations = "99c1a7ee-ab34-5fd5-8076-27c950a045f4"
MatrixPencils = "48965c70-4690-11ea-1f13-43a2532b2fa8"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
Expand All @@ -33,14 +33,15 @@ MacroTools = "0.5"
MatrixEquations = "1, 2.1"
MatrixPencils = "1.6"
OrdinaryDiffEq = "5.2, 6.0"
Plots = "0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 1.0"
Polynomials = "1.1.10, 2.0"
RecipesBase = "1"
julia = "1.6"

[extras]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Documenter", "GR"]
test = ["Test", "Documenter", "GR", "Plots"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ CLs = TransferFunction[kp*P/(1 + kp*P) for kp = [1, 5, 15]];

# Plot the step response of the controllers
# Any keyword arguments supported in Plots.jl can be supplied
using Plots
plot(step.(CLs, 5), label=["Kp = 1" "Kp = 5" "Kp = 15"])
```

Expand Down
9 changes: 6 additions & 3 deletions docs/src/examples/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ end
# LQR design
```jldoctest; output = false
using LinearAlgebra # For identity matrix I
using Plots
Ts = 0.1
A = [1 Ts; 0 1]
B = [0 1]' # To handle bug TODO
Expand All @@ -26,7 +27,7 @@ u(x,t) = -L*x .+ 1.5(t>=2.5)# Form control law (u is a function of t and x), a
t =0:Ts:5
x0 = [1,0]
y, t, x, uout = lsim(sys,u,t,x0=x0)
Plots.plot(t,x', lab=["Position" "Velocity"], xlabel="Time [s]")
plot(t,x', lab=["Position" "Velocity"], xlabel="Time [s]")

save_docs_plot("lqrplot.svg"); # hide

Expand All @@ -53,13 +54,14 @@ we notice that the sensitivity function is a bit too high around frequencies ω
function `loopshapingPI` and tell it that we want 60 degrees phase margin at this frequency. The resulting gang of four is plotted for both the constructed controller and for unit feedback.

```jldoctest PIDDESIGN; output = false
using Plots
ωp = 0.8
kp,ki,C = loopshapingPI(P,ωp,phasemargin=60)

p1 = gangoffourplot(P, [tf(1), C]);
p2 = nyquistplot([P, P*C], ylims=(-1,1), xlims=(-1.5,1.5));

Plots.plot(p1,p2, layout=(2,1), size=(800,800))
plot(p1,p2, layout=(2,1), size=(800,800))
# save_docs_plot("pidgofplot2.svg") # hide
# save_docs_plot("pidnyquistplot.svg"); # hide
save_docs_plot("pidgofnyquistplot.svg") # hide
Expand All @@ -71,13 +73,14 @@ save_docs_plot("pidgofnyquistplot.svg") # hide

We could also cosider a situation where we want to create a closed-loop system with the bandwidth ω = 2 rad/s, in which case we would write something like
```jldoctest PIDDESIGN; output = false
using Plots
ωp = 2
kp,ki,C60 = loopshapingPI(P,ωp,rl=1,phasemargin=60, doplot=true)

p1 = gangoffourplot(P, [tf(1), C60]);
p2 = nyquistplot([P, P*C60], ylims=(-2,2), xlims=(-3,3));

Plots.plot(p1,p2, layout=(2,1), size=(800,800))
plot(p1,p2, layout=(2,1), size=(800,800))

# gangoffourplot(P, [tf(1), C60]) # hide
# save_docs_plot("pidgofplot3.svg") # hide
Expand Down
5 changes: 3 additions & 2 deletions docs/src/man/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ TransferFunction{Continuous, ControlSystems.SisoRational{Float64}}
Continuous-time transfer function model
```
## Plotting
Plotting requires some extra care. The ControlSystems package is using `Plots.jl` ([link](https://github.com/tbreloff/Plots.jl)) as interface to generate all the plots. This means that the user is able to freely choose back-end. The plots in this manual are generated using `GR`. If you have several back-ends for plotting then you can select the one you want to use with the corresponding `Plots` call (for `GR` this is `Plots.gr()`, some alternatives are `pyplot(), plotly(), pgfplots()`). A simple example where we generate a plot and save it to a file is
Plotting requires some extra care. The ControlSystems package is using `RecipesBase.jl` ([link](https://github.com/JuliaPlots/RecipesBase.jl)) as interface to generate all the plots. This means that it is up to the user to choose a plotting library that supports `RecipesBase.jl`, a suggestion would be `Plots.jl` with which the user is also able to freely choose a back-end. The plots in this manual are generated using `Plots.jl` with the `GR` backend. If you have several back-ends for plotting then you can select the one you want to use with the corresponding `Plots` call (for `GR` this is `Plots.gr()`, some alternatives are `pyplot(), plotly(), pgfplots()`). A simple example where we generate a plot and save it to a file is
```jldoctest; output=false
using Plots

fig = bodeplot(tf(1,[1,2,1]))

Plots.savefig(fig, "myfile.svg")
savefig(fig, "myfile.svg")

save_docs_plot(fig, "intro_bode.svg") # hide

Expand Down
2 changes: 1 addition & 1 deletion example/autodiff.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ControlSystems, OrdinaryDiffEq, NLopt, BlackBoxOptim, ForwardDiff
using ControlSystems, OrdinaryDiffEq, NLopt, BlackBoxOptim, ForwardDiff, Plots
p0 = [0.2,0.8,1] # Initial guess
K(kp,ki,kd) = pid(kp=kp, ki=ki, kd=kd)
K(p) = K(p...)
Expand Down
8 changes: 4 additions & 4 deletions example/dc_motor_lqg_design.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ControlSystems
using ControlSystems, Plots
"""
Example for designing an LQG speed controller for an electrical DC motor.
"""
Expand Down Expand Up @@ -29,7 +29,7 @@ function motor(Ke, Kt, L, R, J, b=1e-3)
end

p60 = motor(Ke, Kt, L, Rel, J)
f1 = stepplot(p60, 1)
f1 = plot(step(p60, 1))
f2 = bodeplot(p60)

# LQR control
Expand Down Expand Up @@ -60,5 +60,5 @@ S = 1-T

# 1000 logarithmically spaced values from -3 to 3
f3 = bodeplot([Gcl, S, T], exp10.(range(-3, stop=3, length=1000)))
f4 = stepplot(Gcl, 1, label="Closed loop system using LQG")
Plots.plot(f1, f2, f3, f4, layout=(2,2), size=(800, 600))
f4 = plot(step(Gcl, 1), label="Closed loop system using LQG")
plot(f1, f2, f3, f4, layout=(2,2), size=(800, 600))
22 changes: 10 additions & 12 deletions example/delayed_lti_timeresp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function u0(out,t)
return
end

@time y, t, x = lsim(sys, u0, t)
@time res = lsim(sys, u0, t)

plot(t, y')
plot(res)

s = tf("s")
P = delay(2.6)*ss((s+3.0)/(s^2+0.3*s+1))
Expand All @@ -30,8 +30,8 @@ P*C
T = feedback(P*C,1.0)

t = 0:0.1:70
y, t, x = lsim(T, t -> (t<0 ? 0 : 1 ), t)
plot(t, y, c = :blue)
res = lsim(T, t -> (t<0 ? 0 : 1 ), t)
plot(res, c = :blue)

w = 10 .^ (-2:0.01:2)
marginplot(P*C, w)
Expand All @@ -41,24 +41,23 @@ notch = ss(tf([1, 0.2, 1],[1, .8, 1]));
C = ss(0.05 * (1 + 1/s));
Tnotch = feedback(P*C*notch, 1.0)

stepplot(Tnotch)
plot(step(Tnotch))

y, t, x = step(C, method=:zoh)

y2, t2, x2 = step(Tnotch)
stepplot(Tnotch)
plot(step(Tnotch))

stepplot(Tnotch, 40, 0.1)
plot(step(Tnotch, 40))

stepplot(T, 100)
plot(step(T, 100))

G = delay(5)/(s+1)
T = feedback(G, 0.5)
w = 10 .^ (-2:0.01:3)
bodeplot(T, w, plotphase=false)

# Test conversion, promotion
delay(1,Int64) + 3.5

G = 1 + 0.5 * delay(3)
w = 10 .^(-2:0.001:2)
Expand All @@ -67,14 +66,13 @@ bodeplot(G, w, plotphase=false)
G = delay(1) * ((0.8*s^2+s+2)/(s^2+s))
T = feedback(G,1)
# Not possible with direct term
stepplot(T)
plot(step(T))

bodeplot(T)

G = 1/(s+1) + delay(4)
T = feedback(1,G)
# Not possible to lsim with direct term
stepplot(T)
plot(step(T))
bodeplot(T)

s = tf("s")
3 changes: 1 addition & 2 deletions src/ControlSystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ export LTISystem,


# QUESTION: are these used? LaTeXStrings, Requires, IterTools
using Plots, LaTeXStrings, LinearAlgebra
using RecipesBase, LaTeXStrings, LinearAlgebra
import Polynomials
import Polynomials: Polynomial, coeffs
using OrdinaryDiffEq
export Plots
import Base: +, -, *, /, (==), (!=), isapprox, convert, promote_op
import Base: getproperty, getindex
import Base: exp # for exp(-s)
Expand Down
6 changes: 3 additions & 3 deletions src/pid_design.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ See also `Leadlink, leadlinkat`
function leadlinkcurve(start=1)
N = range(start, stop=10, length=50)
dph = 180/pi*map(Ni->atan(sqrt(Ni))-atan(1/sqrt(Ni)), N)
Plots.plot(N,dph, xlabel="N", ylabel="Phase advance [deg]")
RecipesBase.plot(N,dph, xlabel="N", ylabel="Phase advance [deg]")
end


Expand Down Expand Up @@ -253,7 +253,7 @@ function stabregionPID(P, ω = _default_freq_vector(P,Val{:bode}()); kd=0, doplo
phi = angle.(Pv)
kp = -cos.(phi)./r
ki = kd.*ω.^2 .- ω.*sin.(phi)./r
Plots.plot(kp,ki,linewidth = 1.5, xlabel=L"k_p", ylabel=L"k_i", title="Stability region of P, k_d = $(round(kd, digits=4))"), kp, ki
RecipesBase.plot(kp,ki,linewidth = 1.5, xlabel=L"k_p", ylabel=L"k_i", title="Stability region of P, k_d = $(round(kd, digits=4))"), kp, ki
end


Expand All @@ -263,7 +263,7 @@ function stabregionPID(P::Function, ω = exp10.(range(-3, stop=1, length=50)); k
phi = angle.(Pv)
kp = -cos.(phi)./r
ki = kd.*ω.^2 .- ω.*sin.(phi)./r
Plots.plot(kp,ki,linewidth = 1.5, xlabel=L"k_p", ylabel=L"k_i", title="Stability region of P, k_d = $(round(kd, digits=4))"), kp, ki
RecipesBase.plot(kp,ki,linewidth = 1.5, xlabel=L"k_p", ylabel=L"k_i", title="Stability region of P, k_d = $(round(kd, digits=4))"), kp, ki
end


Expand Down
Loading