Skip to content

Commit 8f629df

Browse files
committed
handle MIMO sys in Num conversion
1 parent da68d05 commit 8f629df

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/SymbolicControlSystems.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,19 @@ function SymPy.Sym(sys::TransferFunction)
5252
end
5353

5454
function Num(sys::StateSpace{<:Any,Num})
55+
# ControlSystemsBase.issiso(sys) || throw(ArgumentError("Only SISO systems are supported"))
5556
A, B, C, D = ControlSystemsBase.ssdata(sys)
5657
λ = isdiscrete(sys) ? Symb.@variables(z) : Symb.@variables(s)
5758
λ = λ[]
58-
ex = (C*inv* I(size(A, 1)) - A)*B+D)[1]
59+
ex = (C*inv* I(size(A, 1)) - A)*B+D)
5960
if sys.nx < 4
60-
ex = Symb.simplify(ex)
61+
ex = Symb.simplify.(ex)
6162
end
62-
ex
63+
length(ex) == 1 ? ex[] : ex
6364
end
6465

6566
function Num(sys::TransferFunction)
67+
ControlSystemsBase.issiso(sys) || (return Num(ss(sys)))
6668
λ = isdiscrete(sys) ? Symb.@variables(z) : Symb.@variables(s)
6769
λ = λ[]
6870
num = sum(((i, t),) -> t * λ^(i-1), enumerate(reverse(numvec(sys)[]))) |> Symb.simplify
@@ -110,9 +112,22 @@ function ControlSystemsBase.minreal(sys::TransferFunction{<:Any,<:ControlSystems
110112
Sym(sys) |> simplify |> tf
111113
end
112114

113-
function ControlSystemsBase.tf(sys::StateSpace{<:Any,<:Sym})
115+
function ControlSystemsBase.tf(sys::StateSpace{TE,<:Sym}) where TE
114116
n, p = simplify.(sp.Poly.(simplify.(sp.fraction(simplify(Sym(sys)))), s))
115-
tf(simplify(n / p))
117+
tf(simplify(n / p), sys.timeevol)
118+
end
119+
120+
function Base.:(*)(A::AbstractMatrix{Bool}, B::AbstractMatrix{<:Sym})
121+
# This is a hack to allow ROC.connect with named systems of Syms
122+
@show A
123+
@show B
124+
try
125+
@show Bb = convert.(Bool, B)
126+
return float.(A) * Bb
127+
catch
128+
@show (1.0 .* B)
129+
return float.(A) * (1.0 .* B)
130+
end
116131
end
117132

118133

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ s = SymbolicControlSystems.s
55
z = SymbolicControlSystems.z
66
import Symbolics
77
import Symbolics: Num
8+
using LinearAlgebra
89

910

1011
macro test_both(G,sym)
@@ -304,6 +305,9 @@ end
304305
# Test that denominator is still monic
305306
@test_both_symb tf([b, a], [1, c, 1]) (b*s + a)/(s^2 + c*s + 1)
306307
@test_both_symb tf([b, a], [1, c, 1], 0.1) (b*z + a)/(z^2 + c*z + 1)
308+
309+
# MIMO
310+
Num(tf([a], [1, c, b]) .* LinearAlgebra.diagm([1, 1]))
307311

308312
end
309313

0 commit comments

Comments
 (0)