Sie sind auf Seite 1von 15

Ex.No.2 Date 26.06.

2013

IMPLEMENTATION OF DIGITAL PID ALGORITHM USING C AND MATLAB

AIM
To implement the digital PID algorithm on a plant model using Turbo C and MATLAB.

REQUIREMENT(S)
1. MATLAB. 2. Turbo C Compiler. 3. Personal Computer.

THEORY
A proportional-integral-derivative controller (PID) is a generic control loop controller widely used in industrial control systems. A PID is the most commonly used feedback controller. A PID controller calculates an error value as the difference between a measured process variable and a desired set point. The controller attempts to minimize the error by adjusting the process control inputs. The PID controller algorithm involves three separate constant parameters, and is accordingly sometimes called three-term control: the proportional (P), the integral (I) and derivative (D). Heuristically these values can be interpreted in terms of time. P depends on the present error, I on the accumulation of past errors and D is a prediction of future errors, based on current rate of change. The weighted sum of these three actions is used to adjust the process via a control element called actuators which will vary depending on the application.

Fig.1. Structure of Parallel PID Controller

The PID control scheme is named after its three correcting terms, whose sum constitutes the manipulated variable (MV). The proportional, integral and derivative terms are summed to calculate the output of the PID controller. Defining u(t) as the controller output, the final form of the PID algorithm is

u(t) = MV(t) = K p e(t) + K i e()d + K d 0 Where,


K p Proportional gain, a tuning parameter K i Integral gain, a tuning parameter Derivative gain, a tuning parameter eError = SP PV t Time or instantaneous time (the present)

d dt

e(t)

(1)

DIGITAL PID CONTROLLER


The most versatile way of compensating a discrete data control system is to use a digital controller. It can be implemented by digital network, digital computers, DSPs, etc. Advantage is that, control algorithm can be easily changed by changing the program of controller whereas changing the components of a continuous data controller is rather difficult once the controller has been implemented. To convert the analog (continuous) PID controller into the digital controller, the integral term and derivative terms present in the analog PID controller is to be replaced by summation and difference equations respectively.

Digital PID controller can be written in two ways as, 1. Position form. 2. Velocity form. Position form of PID controller The controller algorithm for position form of PID controller is given by,

u(n) = K p e(n) +

Kp Ts Ti

n i=0 e(i) +

Kp Td (e(n)e(n1)) Ts

+ u0

(2)

Velocity form of PID controller The controller algorithm for velocity form of PID controller is given by,

u(n) = u(n) u(n 1) u(n) = u(n 1) + K p (e(n) e(n 1)) + + PROCEDURE


1. The continuous time second order system is taken from [2] on page no.36. 2. Convert the transfer function into a discrete transfer function with a sampling time of 0.30 seconds using MATLAB. 3. With the help of Turbo C compiler obtain the open-loop response of the system and save those data in a file. 4. Implement the Velocity form (3) using Turbo C compiler and save the corresponding data in separate files. 5. Load those files in MATLAB and plot the open-loop response and closed-loop responses of Velocity form of PID.
Kp Td Ts

K p Ts e(n) Ti (3)

(e(n) 2e(n 1) + e(n 2))

PROGRAM
1. MATLAB Coding to Create and Discretize the Transfer Function clc close all; clear all; num=5.009; den=[1 0.895 5.009]; sys=tf(num,den) dis=c2d(sys,0.3,'tustin') %continuous to discrete

2. Main program code (main.c) in Turbo C complier #include<stdio.h> #include<conio.h> #include"openloop.c" #include"input.c" #include"velocit.c" void main() { clrscr(); printf("\nPID CONTROLLER IMPLEMENTATION VELOCITY FORM"); printf("\n\t\t\t\t1.OPEN LOOP RESPONSE"); openloop(); printf("\n\t\t\t2.CLOSED LOOP RESPONSE"); input(); closedloop(); getch(); }

3. Sub program code (openloop.c) in Turbo C complier #include<stdio.h> #include<conio.h> FILE *z1; struct prog { int ref[500]; double h[500],g[500]; float t; }j; void openloop() { int n; j.h[2]=j.h[1]=j.h[0]=0; j.t=0.24; z1=fopen("OPPX","w"); for(n=0;n<500;n++) { } for(n=2;n<500;n++) { j.g[n]=(0.09038*j.ref[n])+(0.1808*j.ref[n-1])+(0.09038*j.ref[n-2]); j.h[n]=j.g[n]+(1.423*j.h[n-1])-(0.7847*j.h[n-2]); } for(n=0;n<500;n++) { printf("\n"); printf("%f",j.h[n]); fprintf(z1,"%f",j.h[n]); fprintf(z1,"\n"); } fclose(z1); } j.ref[n]=1;

10

4. Sub program code (input.c) in Turbo C complier #include<stdio.h> #include<conio.h> struct input {int r,ref[500]; double h[500],c[500],u[500],e[500],g[500]; float kp,ti,td,t; }k1; void input() { printf("\n Enter kp Value : "); scanf("\n %f",&k1.kp); printf("\n Enter Ti Value : "); scanf("\n %f",&k1.ti); printf("\n Enter Td value : "); scanf("\n %f",&k1.td); } 5. Sub program code (VEL.CPP) in Turbo C complier #include<stdio.h> #include<conio.h> FILE *z2; void closedloop() { int n; k1.r=1; k1.t=.24; z2=fopen("VELLX","w"); for(n=2;n<500;n++) { k1.c[n]=k1.h[n-1]; k1.e[n]=k1.r-k1.c[n]; k1.u[n]=k1.u[n-1]+(k1.kp*(1+(k1.t/k1.ti)+(k1.td/j.t))*k1.e[n])((k1.kp*(1+(2*k1.td)/j.t))*k1.e[n-1]+((k1.kp*(k1.td/k1.t))*k1.e[n-2])); k1.g[n]=(0.09038*k1.u[n])+(0.1808*k1.u[n-1])+(0.09038*k1.u[n-2]); k1.h[n]=k1.g[n]+(1.423*k1.h[n-1])-(0.7847*k1.h[n-2]);

11

SIMULATION RESPONSE:

OPEN LOOP RESPONSE


2
SYSTEM OUTPUT

1.5

0.5

50

100

150

200

250

300

350

400

450

500

TIME (sec)

Fig.2. Open Loop Response of the Second Order System

12

} printf("\n"); for(n=0;n<500;n++) { printf("\n"); printf("%f",k1.h[n]); } for(n=0;n<500;n++) { fprintf(z2,"%f",k1.h[n]); fprintf(z2,"\n"); } fclose(z2); } 6. MATLAB coding to read the file from Turbo C compiler and Plot the response clc close all; clear all; load C:\TurboC++\Disk\TurboC3\BIN\oppx; figure(1) plot(oppx) title('OPEN LOOP RESPONSE','FontSize',16,'FontName','Times New Roman'); % Create title xlabel('TIME (sec)','FontSize',14,'FontName','Times New Roman'); % Create xlabel % Create ylabel

ylabel('SYSTEM OUTPUT','FontSize',14,'FontName','Times New Roman'); load C:\TurboC++\Disk\TurboC3\BIN\vellx; figure(2) plot(vellx)

title('CLOSED LOOP RESPONSE','FontSize',16,'FontName','Times New Roman');% Create title xlabel('TIME (sec)','FontSize',14,'FontName','Times New Roman'); % Create xlabel ylabel('SYSTEM OUTPUT','FontSize',14,'FontName','Times New Roman'); % Create ylabel

13

The PID controller parameters are,

= 0.032 = 0.095 sec = 0.01 sec

CLOSED LOOP RESPONSE


1
SYSTEM OUTPUT

0.8 0.6 0.4 0.2 0

50

100

150

200

250

300

350

400

450

500

TIME (sec)
Fig.3. Closed Loop Response of the Second Order System

14

OUTPUT
Transfer function of second order under damped system [2] on page no. 36.

5.009 s 2 + 0.895s + 5.009


Discretized transfer function with sampling interval 0.3 seconds.

0.09038 z 2 + 0.1808 z + 0.09038 z 2 1.423 z + 0.7847

INFERENCES
1. A second order system with complex poles is always a under damped system 2. By tuning the controller parameters, the peak overshoot is eliminated in closed loop system.

REFERENCE
[1] Katsuhiko Ogata, Modern Control Engineering, Prentice Hall of India, Fourth Edition, New Delhi, 2002. [2] Jing-Chung Shen, New tuning method for pid control of a plant with Under-damped response, Asian Journal of Control, Vol. 2, No. 1, pp. 31-41, March 2000. [3] http://issuu.com/xcelljournal/docs/xcell_journal_issue_81/38 [4] www.xilinx.com/support/documentation/application.../xapp1163.pdf

RESULT
Thus a digital PID controller is implemented on a plant model using Turbo C complier and MATLAB.

15

Das könnte Ihnen auch gefallen