Skip to content

Commit 0abf7b9

Browse files
committed
WIP julia1.0 compatability
1 parent cc2961e commit 0abf7b9

36 files changed

+229
-215
lines changed

LICENSE.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ControlSystems.jl is licensed under the MIT License:
22

3-
> Copyright (c) 2014: Jim Crist and other contributors:
3+
> Copyright (c) 2014-2018: Jim Crist, Mattias Fält, Fredrik Bagge Carlson
4+
> and other contributors:
45
>
56
> Permission is hereby granted, free of charge, to any person obtaining
67
> a copy of this software and associated documentation files (the

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Some of the available commands are:
4646
##### Constructing systems
4747
ss, tf, zpk, ss2tf
4848
##### Analysis
49-
pole, tzero, norm, norminf, ctrb, obsv, gangoffour, margin, markovparam, damp, dampreport, zpkdata, dcgain, covar, gram, sigma, sisomargin
49+
pole, tzero, sysnorm, norminf, ctrb, obsv, gangoffour, margin, markovparam, damp, dampreport, zpkdata, dcgain, covar, gram, sigma, sisomargin
5050
##### Synthesis
5151
care, dare, dlyap, lqr, dlqr, place, pid, leadlink, laglink, leadlinkat, rstd, rstc, dab
5252
##### Time and Frequency response

REQUIRE

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ Polynomials
44
LaTeXStrings
55
OrdinaryDiffEq
66
IterTools
7+
LinearAlgebra
8+
Printf
9+
Colors

docs/src/examples/example.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ end
77

88
# LQR design
99
```julia
10+
using LinearAlgebra # For identity matrix I
1011
h = 0.1
1112
A = [1 h; 0 1]
1213
B = [0;1]
1314
C = [1 0]
1415
sys = ss(A,B,C,0, h)
15-
Q = eye(2)
16-
R = eye(1)
16+
Q = I
17+
R = I
1718
L = dlqr(A,B,Q,R) # lqr(sys,Q,R) can also be used
1819

1920
u(t,x) = -L*x + 1.5(t>=2.5)# Form control law (u is a function of t and x), a constant input disturbance is affecting the system from t≧2.5
@@ -28,13 +29,14 @@ plot(t,x, lab=["Position", "Velocity"]', xlabel="Time [s]")
2829

2930
# LQR design
3031
```julia
32+
using LinearAlgebra # For identity matrix I
3133
h = 0.1
3234
A = [1 h; 0 1]
3335
B = [0;1]
3436
C = [1 0]
3537
sys = ss(A,B,C,0, h)
36-
Q = eye(2)
37-
R = eye(1)
38+
Q = I
39+
R = I
3840
L = dlqr(A,B,Q,R) # lqr(sys,Q,R) can also be used
3941

4042
u(t,x) = -L*x + 1.5(t>=2.5)# Form control law (u is a function of t and x), a constant input disturbance is affecting the system from t≧2.5

docs/src/lib/analysis.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ gangofseven
1616
gram
1717
margin
1818
markovparam
19-
norm
19+
sysnorm
2020
obsv
2121
pole
2222
sigma

docs/src/makeplots.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ A = [1 h; 0 1]
1212
B = [0;1]
1313
C = [1 0]
1414
sys = ss(A,B,C,0, h)
15-
Q = eye(2)
16-
R = eye(1)
15+
Q = Matrix{Float64}(I,2,2n)
16+
R = Matrix{Float64}(I,1,1)
1717
L = dlqr(A,B,Q,R) # lqr(sys,Q,R) can also be used
1818

1919
u(x,t) = -L*x + 1.5(t>=2.5)# Form control law (u is a function of t and x), a constant input disturbance is affecting the system from t≧2.5

src/ControlSystems.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,19 @@ export LTISystem,
6666
nyquist,
6767
sigma,
6868
# utilities
69+
num, #Deprecated
70+
den, #Deprecated
71+
numvec,
72+
denvec,
6973
numpoly,
7074
denpoly
7175

7276

7377
# QUESTION: are these used? LaTeXStrings, Requires, IterTools
74-
using Polynomials, OrdinaryDiffEq, Plots, LaTeXStrings
75-
import Base: +, -, *, /, (./), (==), (.+), (.-), (.*), (!=), isapprox, convert, promote_op
76-
import Base.LinAlg: BlasFloat
78+
using Polynomials, OrdinaryDiffEq, Plots, LaTeXStrings, LinearAlgebra
79+
import Base: +, -, *, /, (==), (!=), isapprox, convert, promote_op
80+
import LinearAlgebra: BlasFloat
81+
import Printf, Colors
7782

7883
abstract type AbstractSystem end
7984

@@ -120,7 +125,10 @@ include("pid_design.jl")
120125

121126
include("plotting.jl")
122127

128+
@deprecate num numvec
129+
@deprecate den denvec
123130

131+
# @deprecate norm sysnorm
124132

125133
# The path has to be evaluated upon initial import
126134
const __CONTROLSYSTEMS_SOURCE_DIR__ = dirname(Base.source_path())

src/analysis.jl

+17-13
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ Display a report of the poles, damping ratio, natural frequency, and time
7272
constant of the system `sys`"""
7373
function dampreport(io::IO, sys::LTISystem)
7474
Wn, zeta, ps = damp(sys)
75-
t_const = 1./(Wn.*zeta)
75+
t_const = 1 ./ (Wn.*zeta)
7676
header =
7777
("| Pole | Damping | Frequency | Time Constant |\n"*
7878
"| | Ratio | (rad/sec) | (sec) |\n"*
7979
"+---------------+---------------+---------------+---------------+")
8080
println(io, header)
8181
for i=eachindex(ps)
8282
p, z, w, t = ps[i], zeta[i], Wn[i], t_const[i]
83-
@printf(io, "| %-13.3e| %-13.3e| %-13.3e| %-13.3e|\n", p, z, w, t)
83+
Printf.@printf(io, "| %-13.3e| %-13.3e| %-13.3e| %-13.3e|\n", p, z, w, t)
8484
end
8585
end
8686
dampreport(sys::LTISystem) = dampreport(STDOUT, sys)
@@ -131,7 +131,7 @@ function tzero(A::Matrix{T}, B::Matrix{T}, C::Matrix{T}, D::Matrix{T}) where T<:
131131
nf = size(A, 1)
132132
m = size(D, 2)
133133
Af = ([A B] * W)[1:nf, 1:nf]
134-
Bf = ([eye(nf) zeros(nf, m)] * W)[1:nf, 1:nf]
134+
Bf = ([I zeros(nf, m)] * W)[1:nf, 1:nf]
135135
zs = eigvals(Af, Bf)
136136
_fix_conjugate_pairs!(zs) # Generalized eigvals does not return exact conj. pairs
137137
else
@@ -180,10 +180,10 @@ function reduce_sys(A::Matrix{T}, B::Matrix{T}, C::Matrix{T}, D::Matrix{T}, meps
180180
end
181181
# Update System
182182
n, m = size(B)
183-
Vm = [V zeros(T, n, m); zeros(T, m, n) eye(T, m)]
183+
Vm = [V zeros(T, n, m); zeros(T, m, n) I]
184184
if sigma > 0
185185
M = [A B; Cbar Dbar]
186-
Vs = [V' zeros(T, n, sigma) ; zeros(T, sigma, n) eye(T, sigma)]
186+
Vs = [V' zeros(T, n, sigma) ; zeros(T, sigma, n) I]
187187
else
188188
M = [A B]
189189
Vs = V'
@@ -223,15 +223,19 @@ If `full` return also `fullPhase`
223223
"""
224224
function margin(sys::LTISystem, w::AbstractVector{S}; full=false, allMargins=false) where S<:Real
225225
ny, nu = size(sys)
226-
vals = (:wgm, :gm, :wpm, :pm, :fullPhase)
226+
227227
if allMargins
228-
for val in vals
229-
eval(:($val = Array{Array{eltype(sys),1}}($ny,$nu)))
230-
end
228+
wgm = Array{Array{numeric_type(sys),1}}(undef, ny,nu)
229+
gm = Array{Array{numeric_type(sys),1}}(undef, ny,nu)
230+
wpm = Array{Array{numeric_type(sys),1}}(undef, ny,nu)
231+
pm = Array{Array{numeric_type(sys),1}}(undef, ny,nu)
232+
fullPhase = Array{Array{numeric_type(sys),1}}(undef, ny,nu)
231233
else
232-
for val in vals
233-
eval(:($val = Array{eltype(sys),2}($ny,$nu)))
234-
end
234+
wgm = Array{numeric_type(sys),2}(undef, ny, nu)
235+
gm = Array{numeric_type(sys),2}(undef, ny, nu)
236+
wpm = Array{numeric_type(sys),2}(undef, ny, nu)
237+
pm = Array{numeric_type(sys),2}(undef, ny, nu)
238+
fullPhase = Array{numeric_type(sys),2}(undef, ny, nu)
235239
end
236240
for j=1:nu
237241
for i=1:ny
@@ -259,7 +263,7 @@ function sisomargin(sys::LTISystem, w::AbstractVector{S}; full=false, allMargins
259263
wgm, = _allPhaseCrossings(w, phase)
260264
gm = similar(wgm)
261265
for i = eachindex(wgm)
262-
gm[i] = 1./abs(freqresp(sys,[wgm[i]])[1][1])
266+
gm[i] = 1 ./ abs(freqresp(sys,[wgm[i]])[1][1])
263267
end
264268
wpm, fi = _allGainCrossings(w, mag)
265269
pm = similar(wpm)

src/connections.jl

+13-17
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ function append(systems::StateSpace...)
2525
if !all(s.Ts == Ts for s in systems)
2626
error("Sampling time mismatch")
2727
end
28-
A = blkdiag([s.A for s in systems]...)
29-
B = blkdiag([s.B for s in systems]...)
30-
C = blkdiag([s.C for s in systems]...)
31-
D = blkdiag([s.D for s in systems]...)
28+
A = blockdiag([s.A for s in systems]...)
29+
B = blockdiag([s.B for s in systems]...)
30+
C = blockdiag([s.C for s in systems]...)
31+
D = blockdiag([s.D for s in systems]...)
3232
return StateSpace(A, B, C, D, Ts)
3333
end
3434

@@ -37,7 +37,7 @@ function append(systems::TransferFunction...)
3737
if !all(s.Ts == Ts for s in systems)
3838
error("Sampling time mismatch")
3939
end
40-
mat = blkdiag([s.matrix for s in systems]...)
40+
mat = blockdiag([s.matrix for s in systems]...)
4141
return TransferFunction(mat, Ts)
4242
end
4343

@@ -53,9 +53,9 @@ function Base.vcat(systems::StateSpace...)
5353
if !all(s.Ts == Ts for s in systems)
5454
error("Sampling time mismatch")
5555
end
56-
A = blkdiag([s.A for s in systems]...)
56+
A = blockdiag([s.A for s in systems]...)
5757
B = vcat([s.B for s in systems]...)
58-
C = blkdiag([s.C for s in systems]...)
58+
C = blockdiag([s.C for s in systems]...)
5959
D = vcat([s.D for s in systems]...)
6060

6161
return StateSpace(A, B, C, D, Ts)
@@ -87,8 +87,8 @@ function Base.hcat(systems::StateSpace...)
8787
if !all(s.Ts == Ts for s in systems)
8888
error("Sampling time mismatch")
8989
end
90-
A = blkdiag([s.A for s in systems]...)
91-
B = blkdiag([s.B for s in systems]...)
90+
A = blockdiag([s.A for s in systems]...)
91+
B = blockdiag([s.B for s in systems]...)
9292
C = hcat([s.C for s in systems]...)
9393
D = hcat([s.D for s in systems]...)
9494

@@ -156,16 +156,12 @@ end
156156
# end
157157
# end
158158

159-
# TODO: could use cat([1,2], mats...) instead
160-
# Empty definition to get rid of warning
161-
Base.blkdiag() = []
162-
function Base.blkdiag(mats::Matrix...)
159+
160+
blockdiag(mats::Matrix...) = blockdiag(promote(mats...)...)
161+
162+
function blockdiag(mats::Matrix{T}...) where T
163163
rows = Int[size(m, 1) for m in mats]
164164
cols = Int[size(m, 2) for m in mats]
165-
T = eltype(mats[1])
166-
for ind=1:length(mats)
167-
T = promote_type(T, eltype(mats[ind]))
168-
end
169165
res = zeros(T, sum(rows), sum(cols))
170166
m = 1
171167
n = 1

src/discrete.jl

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ function c2d(sys::StateSpace, Ts::Real, method::Symbol=:zoh)
1717
ny, nu = size(sys)
1818
nx = nstates(sys)
1919
if method == :zoh
20-
M = expm([A*Ts B*Ts;
20+
M = exp([A*Ts B*Ts;
2121
zeros(nu, nx + nu)])
2222
Ad = M[1:nx, 1:nx]
2323
Bd = M[1:nx, nx+1:nx+nu]
2424
Cd = C
2525
Dd = D
26-
x0map = [eye(nx) zeros(nx, nu)]
26+
x0map = [I zeros(nx, nu)]
2727
elseif method == :foh
28-
M = expm([A*Ts B*Ts zeros(nx, nu);
29-
zeros(nu, nx + nu) eye(nu);
28+
M = exp([A*Ts B*Ts zeros(nx, nu);
29+
zeros(nu, nx + nu) I;
3030
zeros(nu, nx + 2*nu)])
3131
M1 = M[1:nx, nx+1:nx+nu]
3232
M2 = M[1:nx, nx+nu+1:nx+2*nu]
3333
Ad = M[1:nx, 1:nx]
3434
Bd = Ad*M2 + M1 - M2
3535
Cd = C
3636
Dd = D + C*M2
37-
x0map = [eye(nx) -M2]
37+
x0map = [I -M2]
3838
elseif method == :tustin || method == :matched
3939
error("NotImplemented: Only `:zoh` and `:foh` implemented so far")
4040
else
@@ -230,9 +230,9 @@ function lsima(sys::StateSpace, t::AbstractVector, r::AbstractVector{T}, control
230230
dsys, x0map = c2d(sys, dt, :foh)
231231
end
232232
n = size(t, 1)
233-
x = Array{T}(size(sys.A, 1), n)
234-
u = Array{T}(n)
235-
y = Array{T}(n)
233+
x = Array{T}(undef, size(sys.A, 1), n)
234+
u = Array{T}(undef, n)
235+
y = Array{T}(undef, n)
236236
for i=1:n
237237
x[:,i] = x0
238238
y[i] = (sys.C*x0 + sys.D*u[i])[1]

src/freqresp.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function freqresp(sys::LTISystem, w_vec::AbstractVector{S}) where {S<:Real}
1515
end
1616
Tsys = numeric_type(sys)
1717
T = promote_type(typeof(zero(Tsys)/norm(one(Tsys))), Complex64, S)
18-
sys_fr = Array{T}(length(w_vec), noutputs(sys), ninputs(sys))
18+
sys_fr = Array{T}(undef, length(w_vec), noutputs(sys), ninputs(sys))
1919

2020
if isa(sys, StateSpace)
2121
sys = _preprocess_for_freqresp(sys)
@@ -76,7 +76,7 @@ end
7676

7777
function evalfr(G::TransferFunction{<:SisoTf{T0}}, s::Number) where {T0}
7878
T = promote_type(T0, typeof(one(T0)*one(typeof(s))/(one(T0)*one(typeof(s)))))
79-
fr = Array{T}(size(G))
79+
fr = Array{T}(undef, size(G))
8080
for j = 1:ninputs(G)
8181
for i = 1:noutputs(G)
8282
fr[i, j] = evalfr(G.matrix[i, j], s)
@@ -110,7 +110,7 @@ function (sys::TransferFunction)(z_or_omegas::AbstractVector, map_to_unit_circle
110110
@assert !iscontinuous(sys) "It makes no sense to call this function with continuous systems"
111111
vals = sys.(z_or_omegas, map_to_unit_circle)# evalfr.(sys,exp.(evalpoints))
112112
# Reshape from vector of evalfr matrizes, to (in,out,freq) Array
113-
out = Array{eltype(eltype(vals)),3}(length(z_or_omegas), size(sys)...)
113+
out = Array{eltype(eltype(vals)),3}(undef, length(z_or_omegas), size(sys)...)
114114
for i in 1:length(vals)
115115
out[i,:,:] .= vals[i]
116116
end
@@ -150,7 +150,7 @@ frequencies `w`
150150
function sigma(sys::LTISystem, w::AbstractVector)
151151
resp = freqresp(sys, w)
152152
nw, ny, nu = size(resp)
153-
sv = Array{Float64}(nw, min(ny, nu))
153+
sv = Array{Float64}(undef, nw, min(ny, nu))
154154
for i=1:nw
155155
sv[i, :] = svdvals(resp[i, :, :])
156156
end
@@ -162,7 +162,7 @@ function _default_freq_vector(systems::Vector{T}, plot::Symbol) where T<:LTISyst
162162
min_pt_per_dec = 60
163163
min_pt_total = 200
164164
nsys = length(systems)
165-
bounds = Array{Float64}(2, nsys)
165+
bounds = Array{Float64}(undef, 2, nsys)
166166
for i=1:nsys
167167
# TODO : For now we ignore the feature information. In the future,
168168
# these can be used to improve the frequency vector near features.

0 commit comments

Comments
 (0)