ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 1805

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Experiment #6: Proportional – Integral – Derivative Control

Figure 6.1: PID Control Block Diagram

In this section, the incubator will be controlled using PID control and the PID equation will be explored and illustrated.

Circuit Construction

We will use the same circuit from Exercise #5 (Figure…), but you will manually connect the fan to Pin 19 Vin power or regulated 5V when needed for a disturbance.

The following is the full program for this section. We will change values in the PID Control Settings in testing the different areas of control.

Industrial Control Version 1.1 •Page 147

Experiment #6: Proportional – Integral – Derivative Control

'Program 6.1: PID Control with the StampPlot Interface '********* PID CONTROL SETTING ****************

SP

CON

990

' Initialize setpoint to YOUR bias Temp in TENTHS

Range

CON

20

' Allowable temperature range in TENTHS (20=2F)

B

CON

50

' Bias drive setting

Kp

CON

0

' Proportional Gain Setting in TENTHS (10=Gain of 1)

Ki

CON

0

' Integral gain constant in TENTHS (1=Gain of .001)

Ti

CON

24

' Interal Reset time (1=~5 seconds

Kd

CON

0

' Derivative gain constant

MinA

CON

75

' Minimum analog Y axis value

MaxA

CON

120

' Maximum analog X axis value

MaxT

CON

600

' Maximum time in seconds X Axis

'*************************************************

'***** Configure Plot

PAUSE 2000

' Title Plot

DEBUG "!TITL PID Control",CR

DEBUG "!RSET",CR

' Reset Plot

DEBUG "!PNTS 1000",CR

' 1000 data points

DEBUG "!TMAX ",DEC MaxT,CR

' Set maximum time

DEBUG "!AMAX ",DEC MaxA,CR

' Set analog max

DEBUG "!AMIN ",DEC MinA,CR

' Set analog min

DEBUG "!AMUL .1",CR

' Analog multiplier of .1

DEBUG "!TSMP ON",CR

' Enable time-stamping

DEBUG "!SAVM ON",CR

' Save message to file

DEBUG "!CLMM",CR

' Clear min/max on reset

DEBUG "!SHFT ON",CR

' Enable plot shifts

DEBUG "!PLOT ON",CR

' Enable plotting

' Display drive settings

DEBUG "!USRS SP=",dec SP," Kp=",dec Kp," Ki=",dec Ki," Ti=",dec Ti," Kd=",dec Kd,CR

DEBUG "!RSET",CR

'Reset Plot

' ************** Define constants & variables

CS

CON

3

' 0831 chip select active low from BS2 (P3)

CLK

CON

4

' Clock pulse from BS2 (P4) to 0831

Dout

CON

5

' Serial data output from 0831 to BS2 (P5)

Heater

CON

8

' Output pin to heater

Datain

VAR

BYTE

' Incoming Data (0 to 255)

Temp

VAR

WORD

' Hold the converted value representing temp

TempSpan

CON

5000

' Full Scale input span in tenths of degrees.

Offset

CON

700

' Minimum temp. Offset, ADC = 0

Sign

VAR

WORD

' Used to hold sign for calculations

Drive

VAR

WORD

' Amount of total drive

Err

VAR

WORD

' Amount of error present

P

VAR

WORD

' Amount of Proportional drive

I

VAR

WORD

' Amount of Integral Drive

D

VAR

WORD

' Amount of Derivative Drive

PWMCount

VAR

BYTE

' Counter for amount of time to apply PWM

Page 148 •Industrial Control Version 1.1


Experiment #6: Proportional – Integral – Derivative Control

LastErr

VAR

WORD

' Holds last temperature for derivative drive

LastErr = 0

IntCount

VAR

BYTE

' Variable for counting cycles for integral drive

PWMTime

CON

20

' Variable defining how long PWM drive should last

Ei

VAR

WORD

' V (20=~5 seconds)

' Cumulative error for integral calculations

Ei = 0

' Clear cumulative error

'*************** Main loop

Main:

GOSUB Getdata

GOSUB Calc_Temp

GOSUB Calc_Drive

GOSUB Plot_Data

GOSUB Drive_Heater

GOTO Main

Getdata:

'Acquire conversion from 0831

LOW CS

'Select the chip

LOW CLK

'Ready the clock line.

SHIFTIN Dout, CLK, msbpost,[Datain\9]

'Shift in data

HIGH CS

'conversion

RETURN

Calc_Temp:

'Convert digital value to

Temp = TempSpan/255 * Datain/10 + Offset

'temp based on Span &

RETURN

'Offset variables.

Calc_Drive:

'Error Calcs

GOSUB ErrorCalc

GOSUB PropCalc

'Perform proportional error calcs

GOSUB IntCalc

'Perform Integral Calcs

GOSUB DerivCalc

'Perform Derivative calcs

Drive = (B + P + I + D)

'calculate total drive

Sign = Drive

'Sign adjust to max of 100 min 0

GOSUB SetSign

Drive = ABS Drive MAX 100

IF Sign = 1 THEN DriveDone

Drive = 0

DriveDone:

RETURN

'********* Drive the heater Drive_Heater:

FOR PWMCount = 1 TO PWMTime 'Apply pwm at 220 mSec for each PWMTime repetion PWM Heater,drive * 255/100,220

NEXT

RETURN

'********* Plot Data

Plot_Data:

Industrial Control Version 1.1 •Page 149


Experiment #6: Proportional – Integral – Derivative Control

DEBUG DEC Temp,CR

'** Nicely formatted message output for reading (4 lines) DEBUG "Set:", DEC SP," Temp:", DEC Temp

DEBUG " %Err:",SDEC Err," %B=", DEC B, " %P=", SDEC P DEBUG " %I=",SDEC I," %D=", SDEC D

DEBUG " %Drive:",SDEC Drive, CR

'** Comma-seperated message output for import into spreadsheet

'DEBUG ",",DEC Temp,",",SDEC Err,",",SDEC P,",",SDEC I,",",SDEC D,",",SDEC Drive,CR RETURN

'********** Calculate %Error - Sign adjusted

ErrorCalc:

(SP - Temp)

'Calculate temperature error

Err =

Sign = Err

GOSUB

SetSign

'Calculate % error

Err =

ABS Err*100/Range

Err =

Err * Sign

Return

'*********** Proportional Drive - Sign adjusted

PropCalc:

Sign = Err

GOSUB

SetSign

'Prop err = %Err * Kp /10 to scale, +5 to round

P = ABS Err * KP + 5/10

P = P

* Sign

RETURN

'********** Integral Drive - Sign Adjusted

IntCalc:

Ei = Ei + Err

IntCount = IntCount + 1

IF IntCount < Ti Then IntDone

Sign = Ei

Gosub SetSign

Ei = ABS Ei / Ti

Ei = Ei * Ki + 5 /10

Ei = Ei * Sign

I = I + Ei

Sign = I

GOSUB SetSign

I = ABS I MAX 100

I = I * Sign

IntCount = 0

Ei = 0

IntDone:

RETURN

'*********** DERIVATIVE DRIVE

DerivCalc:

D = (Err-LastErr) * KD

DerivDone

'Accumulate %err each time 'Add to counter for reset time 'Not at reset count? -- done

'Find average error over time 'Int err = int. err * Ki

'Add error to total int. error

'Limit to 100-prevent windup

'Reset int. counter and accumulator

'Calculate amount of derivative drive

'based on the difference of last error

Page 150 •Industrial Control Version 1.1


Experiment #6: Proportional – Integral – Derivative Control

LastErr = Err

' Store current error for next deriv calc

RETURN

'********** Set sign of value SetSign:

IF Sign.bit15 = 0 THEN SignPos 'If signbit is 1, then negative Sign = -1

Return SignPos:

Sign = 1 SignDone:

Return

Industrial Control Version 1.1 •Page 151

Experiment #6: Proportional – Integral – Derivative Control

Figure 6.2: Main Process Flow

Figure 6.2 is a flowchart of the main loop for the PID program. Specifics of each of the processes will be discussed as they arise.

Page 152 •Industrial Control Version 1.1

Experiment #6: Proportional – Integral – Derivative Control

All microcontrollers have their limitations, as do other systems, such as Programmable Logic Controllers (PLCs). In programming complex operations such as PID, it is important to understand the limitations and finding alternative means.

We’ve been dealing with the restriction of integer values, such as temperature being in tenths of degrees. One other limitation we’ll deal need to deal with is that of negative numbers. While the BASIC Stamp can use negative values, it cannot divide them or use the MIN and MAX instructions to set limits on their size. In this section both of these will be important. The values of drive for PID will be negative or positive depending if drive should be added to the total or subtracted. We will also need to limit the maximum values so that we do not exceed 100% in certain circumstances, such as total drive.

To perform these tasks, a routine called SetSign is used. Several routines call it using a GOSUB. The possibly negative value to be manipulated is saved to a word variable Sign. When SetSign is called, the sign bit (bit15) is examined. If the sign bit is 1, it is a negative value and the variable Sign is set to -1. If the sign bit is 0, it is positive and Sign is set to positive 1. Back in our calling routine, the absolute value of our possibly negative number is manipulated. The result is then multiplied by Sign to return the value back to positive or negative. The range of signed values can be from –32,768 to +32767.

PropCalc:

Sign = Err

GOSUB SetSign

'Prop err = %Err * Kp /10 to scale, +5

P = ABS Err * KP + 5/10

to round

P = P * Sign

RETURN

...

SetSign:

'If signbit is 1, then negative

IF Sign.bit15 = 0 THEN SignPos

Sign = -1

Return

SignPos:

Sign = 1

SignDone:

Return

The first part of program to consider is the total drive calculations. Figure 6.3 is a flowchart for these routines. As discussed, the total drive is the sum of 3 different evaluations based on the error. The total drive % will then be applied to the heater using PWM for 5 seconds. This allows a long on-time compared to a relatively short off-time when performing other operations.

Industrial Control Version 1.1 •Page 153