Skip to content

Create a PID tunning function #657

Description

@hildogjr

Create a PID tunning function and also wrap it to matlab compatibility.

It must heritage the tf class as answer and print in the format Kp + Ki * 1/s + Kd * s instead num / den format.

Originally posted by @kingaza in #139 (comment)

Activity

  1. billtubbs commented on Oct 13, 2021

    @billtubbs
    Contributor

    There are probably much more knowledgeable people on here than me, but from what I've learned so far, there are so many different methods of PID tuning (and conventions for parameter definitions) that it would be hard to make a function or library that caters to everyone. My gut feel is that PID tuning modules could be better developed outside but built on top of the Python Control Library by importing the basic building blocks like tf and LinearIOsystem. (I'd be interested in collaborating with someone who wants to give this a go).

    What do others think?

  2. sawyerbfuller commented on Oct 13, 2021

    @sawyerbfuller
    Contributor
  3. hildogjr commented on Oct 13, 2021

    @hildogjr
    Author

    Totally agree.

    @billtubbs, do you propose to code a different Python package to do that or just include a module on this control package (my intention)?

    I just coded a example to my students, that was enough to Power Electronics.

    def pidtune(GH, BW, PM):
        '''Evaluate a PID control used on power eletronics.
    
        Parameters:
            Open loop plant*sensor transfer function.
            Bandwidth in rad/s.
            Phase margin in degrees.
        '''
        from math import sqrt, tan, radians
        from numpy import angle
        # Evaluate the plant transfer function on the intended bandwidth frequency.
        evaluated_BW = GH(1j * BW)
        evaluation_gain = abs(evaluated_BW)
        evaluation_angle = angle(evaluated_BW)
        # Evaluate the gain and margin phase that the control must apply.
        pi_gain = 1 / evaluation_gain
        pi_phase = radians(PM) - (pi - evaluation_angle)
        # Convert the gain and margin to control gain parameters.
        Kp_Ki = 1 / BW * tan( pi_phase + pi / 2 )
        Ki = pi_gain / sqrt(1 / BW ** 2 + Kp_Ki ** 2)
        Kp = Ki * Kp_Ki
        Kd = 0
        return Kp + Ki / s + Kd * s

    But I plan some more general, also overloading a Control class based on control.xferfcn.TransferFunction to create the .Kp, .Ki, .Kd... return method (mimicking Matlab behavior).

    Thanks, @sawyerbfuller. I though some more like:

    % Matlab code:
    PIopts = pidtuneOptions('PhaseMargin', 90);
    Cv = pidtune(GHv, 'pi', 2 * pi * Cv_BW, PIopts);
  4. hildogjr commented on Oct 13, 2021

    @hildogjr
    Author

    Matlab refers
    Åström, K. J. and Hägglund, T. Advanced PID Control, Research Triangle Park, NC: Instrumentation, Systems, and Automation Society, 2006

    Maybe we could model as a optimization problem and pass the transfer function to scipy to get the solution of the coefficients. This approach will make possible to be used to other controls (phase lag/lead, ...).

  5. billtubbs commented on Oct 13, 2021

    @billtubbs
    Contributor

    Hi Bill, i think it probably wouldn’t hurt to have a system to facilitate tuning of pid gains of some sort. One possibility: I have created one that is built on top of sisotool that works by letting you click points on the root locus plot to adjust one PID gain at a time. So it is by hand, and indeed it is but one of many ways to do it. Ill plan to to submit a PR for comment. S

    I agree. A system that facilitates manual PID tuning is a great idea (like MATLAB PID Tuner). I was responding to the idea of some kind of 'black box' function that automatically spits out the PID parameters given a system model and a performance specification of some sort (not sure if that's what the OP meant). That could be dangerous! (and not very educational for the user).

  6. hildogjr commented on Oct 13, 2021

    @hildogjr
    Author

    Some "PID tuner" graphical toolbox will be nice, I just don't known what approach to that to this implementation.

    My original idea was the "black box". In my area, usually the plant TF are simple and we design the controller by Bode response (BW and pahse margin), like the Matlab code the I post above.

  7. billtubbs commented on Oct 13, 2021

    @billtubbs
    Contributor

    I see MATLAB does have an all-in-one PID tuning function: pidtune. So maybe I am wrong and this would be a good idea.

    pidtune tunes the parameters of the PID controller C to balance performance (response time) and robustness (stability margins).

  8. hildogjr commented on Oct 13, 2021

    @hildogjr
    Author

    No worry @billtubbs, I think we work in different areas here. In mine (Power Electronics) the plant TF is quite simple (for the classical circuits), we most change its values by power supply, load and so on... The pidtune allows us the typical project setting the BW and phase margin (that reflects microcontroller and transistors restrictions).

    So I though of "simple" define some optimization function and pass to scipy.

  9. hildogjr commented on Oct 13, 2021

    @hildogjr
    Author

    I want to share this:

    # Trying a optimization PID parameters evaluation to my Gi.
    GH = GHi
    BW = 2*pi*Ci_BW
    PM = 60
    print(GH, BW, PM)
    
    from scipy.optimize import minimize
    def optimize_fnc(x):
        PID = x[0] + x[1] / s
        gm, pm, wg, wp = margin(GH * PID)
        return (pm - PM) ** 2 + (BW - wp) ** 2
    
    r = minimize(optimize_fnc, [0, 0], method='nelder-mead', options={'xtol': 1e-8, 'disp': True})
    print('Kp, Ki = ', r.x)

    Result is amost the gotten by the analytic approximation that I posted above:

          400
    ---------------
    0.0025 s + 0.01
     1256.6370614359173 60
    Optimization terminated successfully.
             Current function value: 0.000000
             Iterations: 137
             Function evaluations: 256
    Kp, Ki =  [0.00678925 4.96200919]
    

    What do you think?

  10. ilayn commented on Oct 14, 2021

    @ilayn

    A mildly acceptable autotuner work is a lot of work. Under the pidtune of matlab there is the same machine that also runs hinfstruct etc. in a nutshell solves a NON3 (nontrivial nonsmooth nonconvex) optimization problem. Something that comes close to it is HIFOO solver but the authors (which I knew some of them) refused to unGPL it to get it out of matlab domain. So I skipped it in the past and I need to find some time to read the article version.

    Otherwise it won't move far from returning meaningful results for bunch of 2nd order systems but fail on almost everything else.

  11. hildogjr commented on Oct 14, 2021

    @hildogjr
    Author

    Good point, even because this will make possible Hinf, H2 and other control calculations.

    The most advanced solver that I know in Python is cvxpy (convex solver), that is not the needed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions