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

bring package up to date with Julia 1 #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name = "ControlToolbox"
uuid = "059e8e86-8a00-5752-9d3d-adbce60f63dd"
authors = ["Arda Aytekin <[email protected]>, Niklas Everitt <[email protected]>"]

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
LTISystems = "67f030dc-aa52-5f83-ac42-e4024659685c"
MathProgBase = "fdba3010-5040-5b88-9595-932c9decdf73"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"

[compat]
Compat = "≥ 0.17.0"
LTISystems = "≥ 0.1.0"
MathProgBase = "≥ 0.6.0"
NLopt = "≥ 0.3.5"
Optim = "≥ 0.7.8"
julia = "≥ 0.5.0"

[extras]
NLopt = "76087f3c-5699-56af-9a33-bf431cd00edd"

[targets]
test = ["NLopt"]
9 changes: 6 additions & 3 deletions src/ControlToolbox.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
module ControlToolbox

using Compat
using LinearAlgebra
using Optim
using Polynomials
using Printf
using RationalFunctions
using RecipesBase
using LTISystems

import Base: step, norm
import Base.LinAlg: BlasFloat
import Base: start, next, done
import Base: step
#import Base: BlasFloat
import Base: iterate
import LinearAlgebra: norm
import LTISystems: LtiSystem, StateSpace, TransferFunction, SystemResponse
import MathProgBase: eval_grad_f, eval_f, eval_g, features_available, initialize

Expand Down
4 changes: 2 additions & 2 deletions src/analysis/damp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Compute the natural frequencies, `Wn`, and damping ratios, `zeta`, of the
poles, `ps`, of `sys`
"""
function damp{T,S}(sys::LtiSystem{T,S})
function damp(sys::LtiSystem{T,S}) where {T,S}
ps = poles(sys)
if isdiscrete(sys)
#Ts = sys.Ts == -1 ? 1 : sys.Ts
Expand All @@ -26,7 +26,7 @@ constant of the system `sys`
"""
function dampreport(io::IO, sys::LtiSystem)
Wn, zeta, ps = damp(sys)
t_const = 1./(Wn.*zeta)
t_const = 1 ./(Wn.*zeta)
header =
("| Pole | Damping | Frequency | Time Constant |\n"*
"| | Ratio | (rad/sec) | (sec) |\n"*
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/isstable.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

isstable{S}(s::StateSpace{S,Val{:disc}}) = maximum(abs(poles(s))) < 1
isstable(s::StateSpace{S,Val{:disc}}) where {S} = maximum(abs(poles(s))) < 1

isstable{S}(s::StateSpace{S,Val{:cont}}) = maximum(real(poles(s))) < 0
isstable(s::StateSpace{S,Val{:cont}}) where {S} = maximum(real(poles(s))) < 0

#isstable(s::LtiSystem{Val{:mimo}}) = map(isstable, getmatrix(s))

Expand Down
2 changes: 1 addition & 1 deletion src/analysis/margins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ gainmargin(sys::LtiSystem, dB::Bool=false) = gainmargin(tf(sys), dB=dB)


# Compute the real and imaginary parts of a polynomial assuming the argument is complex (=jw)
function polysplit{T<:Real}(p::Poly{T})
function polysplit(p::Poly{T}) where {T<:Real}
rp = copy(coeffs(p))
ip = copy(rp)

Expand Down
21 changes: 10 additions & 11 deletions src/analysis/rootlocus.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type RootLocusResponse{T} <: SystemResponse
mutable struct RootLocusResponse{T} <: SystemResponse
K::Vector{T} # gains
systf::TransferFunction{Val{:siso}}
real_p::Matrix{T} # real part of poles
Expand All @@ -8,9 +8,9 @@ type RootLocusResponse{T} <: SystemResponse
d0::T # radius
tloc::Int

function (::Type{RootLocusResponse}){S<:Real,U<:Real,V<:Real}(
K::Vector{S}, systf::TransferFunction{Val{:siso}}, real_p::Matrix{U},
imag_p::Matrix{V}, real_m0::Real, imag_m0::Real, d0::Real)
function (::Type{RootLocusResponse})(K::Vector{S},
systf::TransferFunction{Val{:siso}}, real_p::Matrix{U}, imag_p::Matrix{V},
real_m0::Real, imag_m0::Real, d0::Real) where {S<:Real,U<:Real,V<:Real}
if size(real_p) != size(imag_p)
warn("RootLocusResponse: mag and phase must have same dimensions")
throw(DomainError())
Expand Down Expand Up @@ -40,9 +40,8 @@ type RootLocusResponse{T} <: SystemResponse
end

# Iteration interface
start(rls::RootLocusResponse) = (rls.tloc = 1)
done(rls::RootLocusResponse, state) = state >= length(rls.K)
next(rls::RootLocusResponse, state) = (state+=1; rls.tloc = state; (rls, state))
iterate(rls::RootLocusResponse) = begin rls.tloc = 1; (rls, 1) end
iterate(rls::RootLocusResponse, state) = state >= length(rls.K) ? nothing : (state+=1; rls.tloc = state; (rls, state))

# Plot some outputs for some inputs
@recipe function f(rls::RootLocusResponse)
Expand Down Expand Up @@ -134,14 +133,14 @@ julia> sys = tf([2., 5, 1], [1., 2, 3]);

julia> rl = rootlocus(sys);
"""
function rootlocus{S}(sys::LtiSystem{Val{:siso}}, K::AbstractVector{S}=Float64[];
kwargs...)
function rootlocus(sys::LtiSystem{Val{:siso}}, K::AbstractVector{S}=Float64[];
kwargs...) where {S}
systf = tf(sys)
rootlocus(systf, K; kwargs...)
end

function rootlocus{S<:Real}(systf::TransferFunction{Val{:siso}},
K::AbstractVector{S}=Float64[]; N::Int=100)
function rootlocus(systf::TransferFunction{Val{:siso}},
K::AbstractVector{S}=Float64[]; N::Int=100) where {S<:Real}
r = systf.mat[1]
nump = num(r)
denp = den(r)
Expand Down
46 changes: 23 additions & 23 deletions src/c2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using LTISystems: LtiSystem, StateSpace, TransferFunction
@compat abstract type Method end

# Zero-Order-Hold
immutable ZOH <: Method
struct ZOH <: Method
end

@compat function (m::ZOH)(A::AbstractMatrix, B::AbstractMatrix, C::AbstractMatrix,
Expand All @@ -32,13 +32,13 @@ end
Ad, Bd, Cd, Dd, Ts, x0map = m(s.A, s.B, s.C, s.D, Ts)
ss(Ad, Bd, Cd, Dd, Ts), x0map
end
@compat (m::ZOH){T}(s::TransferFunction{Val{T},Val{:cont}}, Ts::Real) =
@compat (m::ZOH)(s::TransferFunction{Val{T},Val{:cont}}, Ts::Real) where {T} =
tf(m(ss(s), Ts)[1])
@compat (m::ZOH){T}(s::LtiSystem{Val{T},Val{:cont}}, Ts::Real) =
@compat (m::ZOH)(s::LtiSystem{Val{T},Val{:cont}}, Ts::Real) where {T} =
m(ss(s), Ts)[1]

# First-Order-Hold
immutable FOH <: Method
struct FOH <: Method
end

@compat function (m::FOH)(A::AbstractMatrix, B::AbstractMatrix, C::AbstractMatrix,
Expand All @@ -65,15 +65,15 @@ end
Ad, Bd, Cd, Dd, Ts, x0map = m(s.A, s.B, s.C, s.D, Ts)
ss(Ad, Bd, Cd, Dd, Ts), x0map
end
@compat (m::FOH){T}(s::TransferFunction{Val{T},Val{:cont}}, Ts::Real) =
@compat (m::FOH)(s::TransferFunction{Val{T},Val{:cont}}, Ts::Real) where {T} =
tf(m(ss(s), Ts)[1])
@compat (m::FOH){T}(s::LtiSystem{Val{T},Val{:cont}}, Ts::Real) =
@compat (m::FOH)(s::LtiSystem{Val{T},Val{:cont}}, Ts::Real) where {T} =
m(ss(s), Ts)[1]

# Generalized Bilinear Transformation
immutable Bilinear{T<:Real} <: Method
struct Bilinear{T<:Real} <: Method
α::T
@compat function (::Type{Bilinear}){T}(α::T = 0.5)
@compat function (::Type{Bilinear})(α::T = 0.5) where {T}
@assert α ≥ 0. && α ≤ 1. "Bilinear: α must be between 0 and 1"
new{T}(α)
end
Expand All @@ -87,7 +87,7 @@ end
ima = I - α*Ts*A
Ad = ima\(I + (1.0-α)*Ts*A)
Bd = ima\(Ts*B)
Cd = (ima.'\C.').'
Cd = transpose(transpose(ima)\transpose(C))
Dd = D + α*(C*Bd)
x0map = [speye(nx) spzeros(nx, nu)]
Ad, Bd, Cd, Dd, Ts, x0map
Expand All @@ -100,13 +100,13 @@ end
Ad, Bd, Cd, Dd, Ts, x0map = m(s.A, s.B, s.C, s.D, Ts)
ss(Ad, Bd, Cd, Dd, Ts), x0map
end
@compat (m::Bilinear){T}(s::TransferFunction{Val{T},Val{:cont}}, Ts::Real) =
@compat (m::Bilinear)(s::TransferFunction{Val{T},Val{:cont}}, Ts::Real) where {T} =
tf(m(ss(s), Ts)[1])
@compat (m::Bilinear){T}(s::LtiSystem{Val{T},Val{:cont}}, Ts::Real) =
@compat (m::Bilinear)(s::LtiSystem{Val{T},Val{:cont}}, Ts::Real) where {T} =
m(ss(s), Ts)[1]

# Forward Euler
immutable ForwardEuler <: Method
struct ForwardEuler <: Method
end

@compat function (m::ForwardEuler)(A::AbstractMatrix, B::AbstractMatrix,
Expand All @@ -128,13 +128,13 @@ end
Ad, Bd, Cd, Dd, Ts, x0map = m(s.A, s.B, s.C, s.D, Ts)
ss(Ad, Bd, Cd, Dd, Ts), x0map
end
@compat (m::ForwardEuler){T}(s::TransferFunction{Val{T},Val{:cont}}, Ts::Real) =
@compat (m::ForwardEuler)(s::TransferFunction{Val{T},Val{:cont}}, Ts::Real) where {T} =
tf(m(ss(s), Ts)[1])
@compat (m::ForwardEuler){T}(s::LtiSystem{Val{T},Val{:cont}}, Ts::Real) =
@compat (m::ForwardEuler)(s::LtiSystem{Val{T},Val{:cont}}, Ts::Real) where {T} =
m(ss(s), Ts)[1]

# Backward Euler
immutable BackwardEuler <: Method
struct BackwardEuler <: Method
end

@compat function (m::BackwardEuler)(A::AbstractMatrix, B::AbstractMatrix,
Expand All @@ -144,7 +144,7 @@ end
ima = I - Ts*A
Ad = ima\eye(nx)
Bd = ima\(Ts*B)
Cd = (ima.'\C.').'
Cd = transpose(transpose(ima)\transpose(C))
Dd = D + C*Bd
x0map = [speye(nx) spzeros(nx, nu)]
Ad, Bd, Cd, Dd, Ts, x0map
Expand All @@ -157,9 +157,9 @@ end
Ad, Bd, Cd, Dd, Ts, x0map = m(s, Ts)
ss(Ad, Bd, Cd, Dd, Ts), x0map
end
@compat (m::BackwardEuler){T}(s::TransferFunction{Val{T},Val{:cont}}, Ts::Real) =
@compat (m::BackwardEuler)(s::TransferFunction{Val{T},Val{:cont}}, Ts::Real) where {T} =
tf(m(ss(s), Ts)[1])
@compat (m::BackwardEuler){T}(s::LtiSystem{Val{T},Val{:cont}}, Ts::Real) =
@compat (m::BackwardEuler)(s::LtiSystem{Val{T},Val{:cont}}, Ts::Real) where {T} =
m(ss(s), Ts)[1]

end
Expand Down Expand Up @@ -199,15 +199,15 @@ The generalized bilinear transform uses the parameter α and is based on [1].
- [1] G. Zhang, X. Chen, and T. Chen, Digital redesign via the generalized
bilinear transformation, Int. J. Control, vol. 82, no. 4, pp. 741-754, 2009.
"""
function c2d{T}(s::StateSpace{T,Val{:cont}}, Ts::Real,
method = Discretization.ZOH())
function c2d(s::StateSpace{T,Val{:cont}}, Ts::Real,
method = Discretization.ZOH()) where {T}
@assert Ts > zero(Ts) && !isinf(Ts) "c2d: Ts must be a positive number"
sys, x0map = method(s, Ts)
return sys::StateSpace{T,Val{:disc}}, x0map::AbstractMatrix
end
function c2d{T}(s::LtiSystem{T,Val{:cont}}, Ts::Real,
method = Discretization.ZOH())
function c2d(s::LtiSystem{T,Val{:cont}}, Ts::Real,
method = Discretization.ZOH()) where {T}
@assert Ts > zero(Ts) && !isinf(Ts) "c2d: Ts must be a positive number"
method(s, Ts)::LtiSystem{T,Val{:disc}}
end
c2d{T}(method::Function, s::LtiSystem{T,Val{:cont}}, Ts::Real) = c2d(s, Ts, method)
c2d(method::Function, s::LtiSystem{T,Val{:cont}}, Ts::Real) where {T} = c2d(s, Ts, method)
4 changes: 2 additions & 2 deletions src/d2c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Recovers the continuous time which was sampled with a zero-order-hold to obtain
the discrete time system `s`.
"""
function d2c{S}(s::StateSpace{S,Val{:disc}})
function d2c(s::StateSpace{S,Val{:disc}}) where {S}
A, B, C, D = s.A, s.B, s.C, s.D
for λ in eig(A)[1]
println(λ)
Expand All @@ -23,4 +23,4 @@ function d2c{S}(s::StateSpace{S,Val{:disc}})
ss(Ac, Bc, Cc, Dc)
end

d2c{S}(s::LtiSystem{S,Val{:disc}}) = d2c(ss(s))
d2c(s::LtiSystem{S,Val{:disc}}) where {S} = d2c(ss(s))
Loading