Using ControlSystems; c2d usage

I am new to Julia. I am trying to use ControlSystems.

using ControlSystems
sys = tf(1,[1,1])
sys_d = c2d(sys,1)

How can I pass zoh or foh as input to c2d?
Thanks,
Bupesh

c2d(sys,1,:zoh)
ERROR: MethodError: no method matching c2d(::TransferFunction{Continuous,ControlSystems.SisoRational{Int64}}, ::Int64, ::Symbol)
Closest candidates are:
c2d(::StateSpace, ::Real, ::Symbol) at C:\Users\bupandit.julia\packages\ControlSystems\H3Br2\src\discrete.jl:12
c2d(::TransferFunction, ::Any; kwargs…) at C:\Users\bupandit.julia\packages\ControlSystems\H3Br2\src\discrete.jl:213
c2d(::DelayLtiSystem, ::Real, ::Any) at C:\Users\bupandit.julia\packages\ControlSystems\H3Br2\src\delay_systems.jl:58

Stacktrace:
[1] top-level scope at REPL[73]:1

I was able to reproduce this error, and I believe it is a bug (although I am not an expert). As a workaround, it seems like you can do this:
sys_d = c2d(ss(sys), 1) # transfer function to state space, then to discrete

Looking at the source, it looks like the method for transfer functions erroneously assumes that method is a keyword argument:

sys = ss(G)
sysd = c2d(sys, h, kwargs...)[1]

whereas the signature for state-space treats it as an optional (not a keyword) argument.

Would you be willing to file an issue? Also, your example would be good to build in as a test.

Zero order hold is the default so you do not need to supply that. Other discretization methods have not made it into Controlsystems.jl yet, but I have some on my working branch
https://github.com/baggepinnen/ControlSystems.jl/blob/ee19974fb7161fc84c92a0c984dfa18965a6a82e/src/discrete.jl#L12
that will appear eventually.

The docs say
[sysd, x0map] = c2d(sys, Ts, method=:zoh)
with no semi-colon before method. It is true that the method can be left out for default :zoh. But if you want :foh you have to include it as an optional argument, and it doesn’t work with transfer functions:

julia> c2d(sys, 1, :foh)
ERROR: MethodError: no method matching c2d(::TransferFunction{Continuous,ControlSystems.SisoRational{Int64}}, ::Int64, ::Symbol)
Closest candidates are:
  c2d(::StateSpace, ::Real, ::Symbol) at /Users/artkuo/.julia/packages/ControlSystems/H3Br2/src/discrete.jl:12
  c2d(::TransferFunction, ::Any; kwargs...) at /Users/artkuo/.julia/packages/ControlSystems/H3Br2/src/discrete.jl:213
  c2d(::DelayLtiSystem, ::Real, ::Any) at /Users/artkuo/.julia/packages/ControlSystems/H3Br2/src/delay_systems.jl:58

The same does work with state-space.
EDIT: Forgot to say docs for v0.5.3

It seems both zoh and foh methods are implemented in the code. But, why does it throw errors if I am writing:

c2d(sys,1,:zoh)

You are right, there is a bug in the code. The function that catches transferfunction inputs belives that the method is submitted as a kwarg, but the statespace version assumes an optional argument.
https://github.com/JuliaControl/ControlSystems.jl/blob/af81aee13e43fadf727a7c691b7e1ff8c072ee91/src/discrete.jl#L213
I will submitt a PR to solve this within the hour. Fixed in PR https://github.com/JuliaControl/ControlSystems.jl/pull/392
which we can hopefully pull soon.

2 Likes