Optimization-based PI controller tuning for a delay system

Hi,
I am trying to find a good tuning for a PI controler on a first-order-plus-delay system using Optimization. A naive first try combining ControlSystems.jl and Optimization.jl did not work and gave some errors. I wonder if it is possible to twist this approach somehow or if there are better (and convenient) alternatives. Thank you.
Here is the MWE:

using ControlSystems
using Optimization, OptimizationOptimJL, ForwardDiff, Zygote

function loss_tf(x, p)
    Kp, Ti = x[1], x[2]
    C = pid(Kp, Ti)
    G = p
    L = feedback(C*G)
    t_sim = 0.0:0.1:50.0
    y, t = step(L, t_sim)
    ISE = (t[2] - t[1])*sum((1 .- y).^2)
    return ISE
end

x0 = [2.5, 5.0]
p = tf(1.0, [5.0, 1]) * delay(1.0)
f = OptimizationFunction(loss_tf, Optimization.AutoForwardDiff())
prob = OptimizationProblem(f, x0, p, lb = [-Inf, 0.1], ub = [Inf, Inf])
sol = solve(prob, BFGS())

And the error it gave:

ERROR: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(loss_tf), Float64}, Float64, 2})
The type `Float64` exists, but no method is defined for this combination of argument types when trying to construct it.

Have you seen the example that does this in the documentation?
Optimization-based tuning–PID controller

If you’re an academic or personal user, you may also find PID Autotuning · DyadControlSystems useful.

Thanks for the response. I have looked at the documentation and followed it, but when I replaced the double-mass system with my simple FOPD, the code just gives this error:

ERROR: The advanced interface to the function `feedback` (with connection keyword arguments) is currently not supported for LFT systems (such as nonlinear and time-delay systems)

DyadControlSystems is unfortunately not an option for my use case, but thanks for the suggestion.

You can convert the delay system to a rational system using

pade(G, 2)

For a well-tuned PID controller, this approximation is typically very good, since the closed loop should, if the optimization is successful, be rendered insensitive to the phase error of the Padé approximation for high frequencies.

1 Like