Adding PID controller to a MIMO state space system

Here are two ways to achieve this, one using feedback and one using connect with named systems from RobustAndOptimalControl.jl

using ControlSystemsBase, Plots
# Define the state space system
A = [1.0 0.5; 0.2 0.8]
B = [1.0 0.3; 0.4 1.1]
C = [2.0 1.5]
D = [0.5 0.2]
sys = ss(A, B, C, D)

Kp = 1.0
Ki = 0.5
Kd = 0.1
Tf = 0.1  # Filter time constant
C = pid(Kp, Ki, Kd, Tf=Tf, state_space=true, Ts=1.0, form=:parallel)

sysd = c2d(sys, C.Ts) # Discretize plant

# ==============================================================================
# Connection using feedback
# ==============================================================================
U2 = [1] # Index of the input of plant that you want to connect
W2 = [2] # Index of the plant to keep as external input
W1 = [1] # Index of C to keep as external input
Z1 = []  # Index of C to keep as external output
Z2 = (:) # Index of the plant to keep as external output
G2 = feedback(C, sysd; U2, W1, W2, Z1, Z2)

res = step(G2, 10)
plot(res, layout=2, sp=[1 2])

# ==============================================================================
# Using Named Statespace systems
# ==============================================================================
using RobustAndOptimalControl
Pn = named_ss(sysd, "P")
Cn = named_ss(C, "C")
fb = sumblock("e = r - y", Ts=C.Ts) # Feedback node
connections = [
    :Py => :y
    :e => :Cu
    :Cy => :Pu1
]

z1 = [:Py]          # External outputs
w1 = [:r, :Pu2]     # External inputs
G3 = connect([Pn, Cn, fb], connections; z1, w1)

# ==============================================================================
## Compare
# ==============================================================================
bodeplot(G2)
bodeplot!(G3)

As the plot indicates, the two results are identical.


Edit: I made a new release that allows the user in the latest example with named systems to instead pass external inputs and outputs using the more descriptive keyword arguments external_inputs, external_outputs instead of w1, z1 (the latter option will continue to work).

1 Like