AutotunerPID Toolkit | ![]() ![]() |
PID_SUPERV Supervisor of a PID autotuner (implmented as an S-function) The supervisor, rules out the autotuning process mainly performing the identification of a process model and the synthesis of the new parameters with various methods. Moreover it is possible to constrain the structure of the regulator to be a PI or a PID, or the structure can be automatically selected by the supervisor. In particular, the autotuner has the following feature: - identification of process description: * a FOPDT model through the areas method * one point of the frequency response through the method of the relay - sythesis of the PID parameters using * KT method * IMC method * First and second ZN method - it's still a work in progress!! ;-) Author: William Spinelli (wspinell@elet.polimi.it) Copyright 2004 W.Spinelli $Revision: 1.0 $ $Date: 2004/02/27 12:00:00 $
0001 function [sys,x0,str,ts] = pid_autotuner(t,x,u,flag,Ts,As) 0002 %PID_SUPERV Supervisor of a PID autotuner (implmented as an S-function) 0003 % 0004 % The supervisor, rules out the autotuning process mainly performing the 0005 % identification of a process model and the synthesis of the new 0006 % parameters with various methods. 0007 % Moreover it is possible to constrain the structure of the regulator to 0008 % be a PI or a PID, or the structure can be automatically selected by the 0009 % supervisor. 0010 % In particular, the autotuner has the following feature: 0011 % - identification of process description: 0012 % * a FOPDT model through the areas method 0013 % * one point of the frequency response through the method of the 0014 % relay 0015 % - sythesis of the PID parameters using 0016 % * KT method 0017 % * IMC method 0018 % * First and second ZN method 0019 % - it's still a work in progress!! ;-) 0020 % 0021 % Author: William Spinelli (wspinell@elet.polimi.it) 0022 % Copyright 2004 W.Spinelli 0023 % $Revision: 1.0 $ $Date: 2004/02/27 12:00:00 $ 0024 0025 switch flag, 0026 % Initialization 0027 case 0, 0028 [sys,x0,str,ts] = mdlInitializeSizes(Ts); 0029 % Outputs 0030 case 3, 0031 [sys] = mdlOutputs(t,x,u,Ts,As); 0032 % Unused flags 0033 case { 1, 2, 4, 9 } 0034 sys = []; 0035 otherwise 0036 error(['Unhandled flag = ',num2str(flag)]); 0037 end 0038 % end pid_superv 0039 0040 0041 %============================================================================= 0042 % mdlInitializeSizes 0043 % Return the sizes, initial conditions, and sample times for the S-function. 0044 %============================================================================= 0045 function [sys,x0,str,ts] = mdlInitializeSizes(Ts) 0046 global Y_AUTOTUNING % store the step (or relay) response used for tuning 0047 0048 Y_AUTOTUNING = []; % initialized to an empty vector 0049 0050 % set up S-function 0051 sizes = simsizes; 0052 0053 sizes.NumContStates = 0; 0054 sizes.NumDiscStates = 0; 0055 sizes.NumOutputs = 2; 0056 sizes.NumInputs = 1; 0057 sizes.DirFeedthrough = 1; 0058 sizes.NumSampleTimes = 1; % at least one sample time is needed 0059 0060 sys = simsizes(sizes); 0061 0062 x0 = []; 0063 str = []; 0064 ts = [Ts 0]; 0065 % end mdlInitializeSizes 0066 0067 0068 %============================================================================= 0069 % mdlOutputs 0070 % Return the block outputs. 0071 %============================================================================= 0072 function [sys] = mdlOutputs(t,x,u,Ts,As) 0073 % sys(1) : auto/manual switch 0074 % sys(2) : autotuning (0 : autotuning in progress; 1 : normal operation) 0075 0076 % u(1) : new sample of the step response 0077 0078 global PIDPARAMETERS 0079 global IDENTIFICATION_METHOD 0080 global TUNING_STRUCTURE 0081 global TUNING_METHOD 0082 global TUNING_PARAM 0083 global AUTOTUNE 0084 global AUTOMAN 0085 0086 global Y_AUTOTUNING 0087 0088 step_steadyThr = 0.05; % threshold on derivative to consider the 0089 % step response to a stedy state 0090 relay_steadyThr = 0.05; % threshold on peaks percentual difference to 0091 % consider the relay response to steady state 0092 0093 if AUTOTUNE 0094 % store the new sample of the step response 0095 Y_AUTOTUNING = [Y_AUTOTUNING; u(1)]; 0096 y = Y_AUTOTUNING; 0097 0098 if strcmp(IDENTIFICATION_METHOD,'STEP') 0099 % check if the step response has reached a steady state (i. e. check if 0100 % the last 10% of the step response is ``flat'' enough) 0101 N = fix(length(y)/10); % last 10% of the step response 0102 if N > 15 0103 % the step response must be made at least by 150 samples 0104 deltay = abs(y(end)-y(1)); 0105 yw = y(end-N:end); 0106 % mean value of the derivative in the window lower than a fraction 0107 % of the maximum value of the derivative 0108 idok = (max(yw)-min(yw)) < step_steadyThr*(max(y)-min(y)); 0109 else 0110 idok = 0; 0111 end 0112 if idok 0113 model = idareas(y,1,Ts); 0114 end 0115 elseif strcmp(IDENTIFICATION_METHOD,'RELAY') 0116 % IDENTIFICATION WITH THE RELAY METHOD 0117 dy = diff(y); 0118 ind = find(dy(1:end-1)>0 & dy(2:end)<0); 0119 0120 % at least 4 oscillations but no more than 10 oscillations 0121 if length(ind)>=4 & length(ind)<10 0122 ym = mean(y); 0123 % mean value of the difference between the last three peaks is 0124 % less than a fraction of the overall range of the response 0125 idok = mean(abs(diff(y(ind(end-2:end))))) < relay_steadyThr*(max(y)-min(y)); 0126 % the last ``period'' must cross the zero 0127 idok = idok & any(abs(y(ind(end-1):ind(end))-ym) < relay_steadyThr/2*abs(max(y)-ym)); 0128 elseif length(ind)>=10 0129 % force the stop of the relay identification 0130 idok = 1; 0131 else 0132 idok = 0; 0133 end 0134 0135 if idok 0136 model.A = max(y)-min(y); 0137 model.T = Ts*(ind(end)-ind(end-1)); 0138 end 0139 end 0140 0141 % autotuning required 0142 if idok 0143 % selection of the regulator structure 0144 regStruct = pid_structure(model,TUNING_STRUCTURE); 0145 0146 % synthesis of the PID parameters 0147 try 0148 [K,Ti,Td,N,b] = pid_tuning(model,TUNING_METHOD,TUNING_PARAM,regStruct,As); 0149 % update PID parameters 0150 PIDPARAMETERS = [K Ti Td N b]; 0151 catch 0152 % no change in the parameters 0153 end 0154 0155 AUTOTUNE = 0; 0156 end 0157 % put the PID in manual mode during autotuning and propagate the 0158 % ``autotuning running'' signal 0159 sys = [AUTOMAN 1]; 0160 else 0161 % empty the step response vector 0162 Y_AUTOTUNING = []; 0163 % put the PID in auto mode during autotuning and propagate the 0164 % ``autotuning not running'' signal 0165 sys = [AUTOMAN 0]; 0166 end 0167 % end mdlOutputs
![]() | idareas | pid_isatd | ![]() |