PID Matlab (1)

23
PID Control con Matlab Luis Sánchez

description

Transference Function

Transcript of PID Matlab (1)

Diapositiva 1

PID Control con MatlabLuis SnchezControl PID

we will consider the following unity feedback system:The output of a PID controller, equal to the control input to the plant, in the time-domain is as follows:

The transfer function of a PID controller is found by taking the Laplace transform before.

Kp= Proportional gainKi= Integral gainKd= Derivative gainPID en MatlabWe can define a PID controller in MATLAB using the transfer function directly, for example:Kp = 1; Ki = 1; Kd = 1; s = tf('s'); C = Kp + Ki/s + Kd*s

Alternatively, we may use MATLAB'spid controller objectto generate an equivalent continuous-time controller as follows:

C = pid(Kp,Ki,Kd)Let's convert the pid object to a transfer function to see that it yields the same result as above:

tf(C)The Characteristics of PID Controllers

CL RESPONSERISE TIMEOVERSHOOTSETTLING TIMES-S ERRORKpDecreaseIncreaseSmall ChangeDecreaseKiDecreaseIncreaseIncreaseEliminateKdSmall ChangeDecreaseDecreaseNo ChangeA proportional controller (Kp) will have the effect of reducing the rise time and will reduce but never eliminate thesteady-state error. An integral control (Ki) will have the effect of eliminating the steady-state error for a constant or step input, but it may make the transient response slower. A derivative control (Kd) will have the effect of increasing the stability of the system, reducing the overshoot, and improving the transient response.Note that these correlations may not be exactly accurate, because Kp, Ki, andKdare dependent on each other.General Tips for Designing a PID ControllerWhen you are designing a PID controller for a given system, follow the steps shown below to obtain a desired response.Obtain an open-loop response and determine what needs to be improvedAdd a proportional control to improve the rise timeAdd a derivative control to improve the overshootAdd an integral control to eliminate the steady-state errorAdjust each of Kp, Ki, and Kd until you obtain a desired overall response

Ejemplo: Control PID-DC Motor Speed

A common actuator in control systems is the DC motor. It directly provides rotary motion and, coupled with wheels or drums and cables, can provide translational motion. The electric circuit of the armature and the free-body diagram of the rotor are shown in the following figure: Parmetros fsicos para nuestro ejemplo son:(J) Moment of inertia of the rotor 0.01 kg.m^2(b) Motor viscous friction constant 0.1 N.m.s(Ke) Electromotive force constant 0.01 V/rad/sec(Kt) Motor torque constant 0.01 N.m/Amp(R) Electric resistance 1 Ohm(L) Electric inductance 0.5 H

Torque (T):La fuerza electromotriz de retroceso (e):

Requerimientos de diseoFor a 1-rad/sec step reference, the design criteria are the following. Settling time less than 2 seconds Overshoot less than 5% Steady-state error less than 1%

J = 0.01; b = 0.1; K = 0.01; R = 1; L = 0.5; s = tf('s'); P_motor = K/((J*s+b)*(L*s+R)+K^2);Proportional control

Kp = 100; C = pid(Kp); sys_cl = feedback(C*P_motor,1);t = 0:0.01:5; step(sys_cl,t) grid title('Step Response with Proportional Control')PID controlThe addiction an integral term will eliminate the steady-state error to a step reference and a derivative term will often reduce the overshoot. Let's try a PID controller with smallKiandKd. Kp = 75; Ki = 1; Kd = 1; C = pid(Kp,Ki,Kd);sys_cl = feedback(C*P_motor,1); step(sys_cl,[0:1:200]); title('PID Control with Small Ki and Small Kd')

Modifique las gananciasKp = 100; Ki = 200; Kd = 1; C = pid(Kp,Ki,Kd); sys_cl = feedback(C*P_motor,1); step(sys_cl, 0:0.01:4) grid ontitle('PID Control with Large Ki and Small Kd')

In this case, the long tail on the step response graph is due to the fact that the integral gain is small and, therefore, it takes a long time for the integral action to build up and eliminate the steady-state error. This process can be sped up by increasing the value ofKi. Go back to your m-file and changeKito 200 as in the following. Rerun the file and you should get the plot shown below. Again the annotations are added by right-clicking on the figure and choosingCharacteristicsfrom the resulting menu.As expected, the steady-state error is now eliminated much more quickly than before. However, the largeKihas greatly increased the overshoot. Let's increaseKdin an attempt to reduce the overshoot. Go back to the m-file and changeKdto 10 as shown in the following. Rerun your m-file and the plot shown below should be generated.

Kp = 100; Ki = 200; Kd = 10; C = pid(Kp,Ki,Kd); sys_cl = feedback(C*P_motor,1); step(sys_cl, 0:0.01:4) grid title('PID Control with Large Ki and Large Kd')

Ejemplo Control PID de un sistema de control de posicion con una perturbacion dRequerimientos de diseoSettling time less than 0.040 secondsOvershoot less than 16%No steady-state error, even in the presence of a step disturbance input

Now let's design a PID controller and add it into the system. First create a newm-fileand type in the following commands (refer to main problem for the details of getting these commands).J = 3.2284E-6; b = 3.5077E-6; K = 0.0274; R = 4; L = 2.75E-6; s = tf('s'); P_motor = K/(s*((J*s+b)*(L*s+R)+K^2));Recall that the transfer function for a PID controller has the following form.

Proportional controlLet's first try using a proportional controller with gain ranging from 1 to 21. Anarray of LTI models, each with a different proportional gain, can be built using aforloop. The closed-loop transfer functions can be generated using thefeedback command. Add the following code to the end of your m-file and run it in the MATLAB command window:

Kp = 1; for i = 1:3 C(:,:,i) = pid(Kp); Kp = Kp + 10; end sys_cl = feedback(C*P_motor,1);Now let's see what the step responses look like. Add the following code to the end of your m-file and again run it in the command window. You should generate the plot shown in the figure below.

t = 0:0.001:0.2; step(sys_cl(:,:,1),sys_cl(:,:,2),sys_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Reference with Different Values of K_p') legend('K_p = 1', 'K_p = 11', 'K_p = 21')Let's also consider the system's response to a step disturbance. In this case, we will assume a reference of zero and look at the how the system responds to the disturbance by itself.figuredist_cl = feedback(P_motor,C); step(dist_cl(:,:,1), dist_cl(:,:,2), dist_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Disturbance with Different Values of K_p') legend('K_p = 1', 'K_p = 11','K_p = 21')

PI controlLet's first try a PI controller to get rid of the steady-state error due to the disturbance. We will setKp= 21 and test integral gains Kiranging from 100 to 500. Change your m-file to the following and run in the command window. You should generate a figure like the one shown below.Kp = 21; Ki = 100; for i = 1:5 C(:,:,i) = pid(Kp,Ki); Ki = Ki + 200; end sys_cl = feedback(C*P_motor,1); t = 0:0.001:0.4; step(sys_cl(:,:,1), sys_cl(:,:,2), sys_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Reference with K_p = 21 and Different Values of K_i') legend('K_i = 100', 'K_i = 300', 'K_i = 500')Now let's see what happened to the step disturbance response. Change the following commands in your m-file and re-run in the command window. You should generate a plot like the one shown in the figure below.dist_cl = feedback(P_motor,C); step(dist_cl(:,:,1), dist_cl(:,:,2), dist_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Disturbance with K_p = 21 and Different Values of K_i') legend('K_i = 100', 'K_i = 300', 'K_i = 500')

PID control

Adding a derivative term to the controller means that we now have all three terms of the PID controller. We will investigate derivative gainsKdranging from 0.05 to 0.25. Go back to the m-file and make the following changes. Running the altered m-file will generate a graph like the one shown below.Kp = 21; Ki = 500; Kd = 0.05; for i = 1:3 C(:,:,i) = pid(Kp,Ki,Kd); Kd = Kd + 0.1; end sys_cl = feedback(C*P_motor,1); t = 0:0.001:0.1; step(sys_cl(:,:,1), sys_cl(:,:,2), sys_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Reference with K_p = 21, K_i = 500 and Different Values of K_d') legend('K_d = 0.05', 'K_d = 0.15', 'K_d = 0.25')Let's see what happened to the step disturbance response, change the following commands in your m-file and re-run at the command line.dist_cl = feedback(P_motor,C); t = 0:0.001:0.2; step(dist_cl(:,:,1), dist_cl(:,:,2), dist_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Disturbance with K_p = 21, K_i = 500 and Different values of K_d') legend('K_d = 0.05', 'K_d = 0.15', 'K_d = 0.25')Automatic PID TuningMATLAB provides tools for automatically choosing optimal PID gains which makes the trial and error process described above unnecessary. You can access the tuning algorithm directly usingpidtuneor through a nice graphical user interface (GUI) usingpidtool.

pidtool(P,'p')