MATLAB Answers

Peter
0

Design Iterative PID Tuner using MATLAB and Simulink

Asked by Peter
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

  0 Comments

Sign in to comment.

2 Answers

Answer by Paulo Silva
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)

  0 Comments

Sign in to comment.


Answer by Davide Ferraro 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.
- Have you perhaps access to Simulink Control Design:
This tool allows you to tune your PID controller without testing many values (this is based on optimization approaches that will follow a path to an optimal solution without testing on all the possible values.
- More in general you may also have a look at Optimization Toolbox:
that contains many efficient methods and code approaches for this type of optimization algorithms.

  0 Comments

Sign in to comment.