MATLAB: How to set the saturation output for PID objects in MATLAB

MATLAB

I want to set the saturation output of a PID object in MATLAB to enable the implementation of an anti-windup PID control scheme.

Best Answer

  • PID objects in the Control System Toolbox are LTI (linear time-invariant) systems of a special type. Because of that, there is no property for output saturation in PID objects. To simulate "anti-windup" in MATLAB, you can setup a custom state-space system and call the "lsim" function to simulate the time response of that system with nonlinear properties such as saturation. Below is a simple script that performs closed-loop PI control with back calculation that can be used as a starting point.   
    UseBackCalculation = true; % enable/disable anti-windup (back calculation)
    G = ss(tf(1,[1 2 1])); % plant
    Kp = 2; Ki = 2; % PI controller gains (parallel)
    Ts = 0.1; % controller sample time
    tau = 1; % reset time constant
    UB = 1.2; LB = -1.2; % saturation limits
    % initial condition
    r = 1;
    y = 0;
    x = zeros(2,1);
    actionI = 0;
    % closed-loop simulation (200 steps)
    N = 200;
    for ct=1:N
        % error
        e = r - y;
        
        % control action
        actionP = Kp*e;
        u = actionP + actionI;
        
        % saturation control action
        u_sat = max(min(u,UB),LB);
        
        % anti windup
        if UseBackCalculation
            actionI = actionI + (Ki*e + (u_sat-u)/tau)*Ts;
        else
            actionI = actionI + Ki*e*Ts;
        end
        
        % plant output
        [Y,T,X] = lsim(G,[u_sat;u_sat],[0;Ts],x);
        
        % for next iteration
        x = X(end,:)';
        y = Y(end);
        y_vec(ct) = y;
        u_vec(ct) = u_sat;
    end
    clf;
    figure(1);
    plot(1:N,y_vec,'*r',1:N,u_vec,'+b');
    xlabel('Time'); ylabel('Signal');
    legend('Model Response','Control Signal')
    if UseBackCalculation
        title('With Anti-Windup (Back Calculation)');
    else
        title('Without Anti-Windup');
    end