## Design Iterative PID Tuner using MATLAB and Simulink

### Peter (view profile)

on 1 Apr 2011
Hi everyone,
I have been developing an iterative method of tuning a PID controller to find the optimum coefficients for a given plant. There cannot be any overshoot.
I have decided to firstly perform a coarse search (say 200 iterations) through Kp, Ki and Kd. I will then discard any results which overshoot, select the 3 'best' responses and then perform finer iterations around those values.
So far I have the following code;
{
%% Servo Motor Position Control Parameters
%**Change file name at the code end to run for current model** % Electrical Variables
Ra = 0.1; %Armature Resistance (Ohm) La = 0.0041; %Armature Inductance (Henry) Kt = 5.4; %Torque Constant
% Mechanical Variables
J = 405; %Inertia (kgm^2) B = 0; %Damping (Ns/m)
Gi = 1; %Current Gain
% PID Iteration
for Kp = 0:100:600 for Ki = 0:100:600 for Kd = 0:100:600
sim('DynamicModel_3_PID');
if (max(Position)<10)
plot(tout,Position)
hold on
end
end
end
end
}
NB Dynamic Model returns the step response of position, which is what we are tuning for.
This 'works' in the sense that it will return a plot of all the traces which pass the ‘no overshoot’ criteria (ie the coarse search mentioned above). The issue I have now is that I lose the information about which PID gains these correspond to.
• Is there a way I can output the PID gains for specific curves to the workspace?
• Further to this I will need to sort these into ‘best to worst response’, to perform the finer iterations.
I am relatively new to coding in Matlab so appreciate any references or help files that could be useful.
Many thanks,
Peter

### Paulo Silva (view profile)

on 1 Apr 2011

%this code saves the PID values that you want (max(Position)<10)
%and also max(Position) in the array GoodValues
GoodValues=[];
for Kp = 0:100:600
for Ki = 0:100:600
for Kd = 0:100:600
sim('DynamicModel_3_PID');
if (max(Position)<10)
plot(tout,Position)
hold on
GoodValues=[GoodValues;Kp Ki Kd max(Position)];
end
end
end
end
%If you want to find the best values of the gains
[PositionMinValues,PositionMinValuesLine]=min(GoodValues(:,3))
%Display the best gains
GoodValues(PositionMinValuesLine,1:3)

### Davide Ferraro (view profile)

on 1 Apr 2011

There is not a single solution, so let me list some ideas on how to approach your problem:
- It seems that you look for the quality graphically, if you can define your goal (in optimization called "objective function") you may directly save your optimal condition into a variable in the loop.
- You may consider saving your interesting Kp, Ki and Kd into a structure along with the handle of the curve you have created. Then if you select the curve graphically and type:
gco
you can obtain the corresponding handle and associate it to your coefficients.