Skip to content

Commit

Permalink
handle MIMO sys in Num conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Jan 16, 2025
1 parent da68d05 commit 8f629df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/SymbolicControlSystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,19 @@ function SymPy.Sym(sys::TransferFunction)
end

function Num(sys::StateSpace{<:Any,Num})
# ControlSystemsBase.issiso(sys) || throw(ArgumentError("Only SISO systems are supported"))
A, B, C, D = ControlSystemsBase.ssdata(sys)
λ = isdiscrete(sys) ? Symb.@variables(z) : Symb.@variables(s)
λ = λ[]
ex = (C*inv* I(size(A, 1)) - A)*B+D)[1]
ex = (C*inv* I(size(A, 1)) - A)*B+D)
if sys.nx < 4
ex = Symb.simplify(ex)
ex = Symb.simplify.(ex)
end
ex
length(ex) == 1 ? ex[] : ex
end

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

function ControlSystemsBase.tf(sys::StateSpace{<:Any,<:Sym})
function ControlSystemsBase.tf(sys::StateSpace{TE,<:Sym}) where TE
n, p = simplify.(sp.Poly.(simplify.(sp.fraction(simplify(Sym(sys)))), s))
tf(simplify(n / p))
tf(simplify(n / p), sys.timeevol)
end

function Base.:(*)(A::AbstractMatrix{Bool}, B::AbstractMatrix{<:Sym})
# This is a hack to allow ROC.connect with named systems of Syms
@show A
@show B
try
@show Bb = convert.(Bool, B)
return float.(A) * Bb
catch
@show (1.0 .* B)
return float.(A) * (1.0 .* B)
end
end


Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ s = SymbolicControlSystems.s
z = SymbolicControlSystems.z
import Symbolics
import Symbolics: Num
using LinearAlgebra


macro test_both(G,sym)
Expand Down Expand Up @@ -304,6 +305,9 @@ end
# Test that denominator is still monic
@test_both_symb tf([b, a], [1, c, 1]) (b*s + a)/(s^2 + c*s + 1)
@test_both_symb tf([b, a], [1, c, 1], 0.1) (b*z + a)/(z^2 + c*z + 1)

# MIMO
Num(tf([a], [1, c, b]) .* LinearAlgebra.diagm([1, 1]))

end

Expand Down

0 comments on commit 8f629df

Please sign in to comment.