Microcontroller based applied digital control (D. Ibrahim, 2006)
.pdfPID CONTROLLER IMPLEMENTATIONS |
251 |
wk
|
|
Kp |
ek |
α |
pk |
+ |
pk−1 |
z−1 |
+ |
uk |
|
qk |
|
|
ek |
+ |
|
|
|
β |
|
z−1 −1
ek−1
α = KpT/Ti β = KpTD/T
Figure 10.13 PID controller as a parallel structure
The z-transform of the PID controller was derived in (9.19), and is reproduced here for convenience:
|
K T |
|
|
|
K T (1 |
− |
z−1) |
|
|||
D(z) = K p + |
|
p |
|
|
|
+ |
p d |
|
. |
(10.27) |
|
T (1 |
− |
z |
− |
1) |
T |
|
|
||||
|
i |
|
|
|
|
|
|
|
|
As shown in Figure 10.13, this transfer function may be implemented as a parallel structure by summing the proportional, integral and derivative terms.
With reference to Figure 10.13 we can write the following difference equations: for the proportional section,
wk = K p ek . |
(10.28) |
for the integral section, |
|
pk = αek + pk−1; |
(10.29) |
and for the derivative section, |
|
qk = β(ek − ek−1). |
(10.30) |
The output is given by |
|
uk = wk + pk + qk . |
(10.31) |
An alternative implementation of the PID would be to find a second order transfer function for (10.27) and then use the direct structure to implement it. Equation (10.27) can be written as
D(z) = K p (1 − z−1) + K p T / Ti + (K p Td / T )(1 − z−1)2
1 − z−1
= |
K p − K p z−1 + K p T / Ti + K p Td / T + (K p Td / T )z−2 − 2(K p Td / T )z−1 |
|||||||||
|
1 |
− |
z |
− |
1 |
|
||||
|
|
|
|
|
|
|||||
= |
K p + K p T / Ti + K p Td / T − (K p + 2K p Td / T )z−1 + (K p Td / T )z−2 |
|||||||||
1 |
− |
z |
− |
1 |
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
MICROCONTROLLER IMPLEMENTATIONS |
253 |
|||||||||||||
By taking the z-transform of (10.34) we obtain |
|
|
|
|
|
|
|
|
|
|
||||||||||
U (z) |
= |
z−1U (z) |
+ |
a E (z) |
+ |
bz−1 E (z) |
+ |
cz−2 E (z) |
|
|||||||||||
or |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
D(z) |
= |
U (z) |
= |
|
a + bz−1 + cz−2 |
. |
(10.35) |
|||||||||||
|
|
|
|
|
|
|
||||||||||||||
|
|
|
|
E (z) |
|
|
|
1 |
− |
z |
− |
1 |
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
Equation (10.35) can easily be implemented using a direct realization. Notice that if only proportional plus integral (PI) action is required, the derivative constant Td can be set to zero and we get the PI equation
D(z) |
U (z) |
|
|
a + bz−1 |
, |
(10.36) |
||||
|
|
|
||||||||
= E (z) |
= |
1 |
− |
z |
− |
1 |
|
|
||
|
|
|
|
with
a = K p + K p T , Ti
b = K p .
Equation (10.36) can easily be implemented as a first-order transfer function.
10.5 MICROCONTROLLER IMPLEMENTATIONS
The final stage of a digital control system design is the implementation of the controller algorithm (set of difference equations) on a digital computer. In this section, we shall explore the implementation of digital controller algorithms on PIC microcontrollers. The PIC 16F877 microcontroller will be used in the examples since this microcontroller has a built-in A/D converter and a reasonable amount of program memory and data memory. There are many other microcontrollers in the PIC family with built-in A/D converters, and in general any of these can be used since the operation of microcontrollers in the PIC family with similar features is identical.
Microcontrollers have traditionally been programmed using the assembly language of the target hardware. Assembly language has several important disadvantages and is currently less popular than it used to be. One important disadvantage is that the code generated using the assembly language can only run on the specific target hardware. For example, the assembly program developed for a PIC microcontroller cannot be used, say, on an Intel 8051 microcontroller. Assembly language programs also tend to be more difficult to develop, test and maintain.
In this section, the Hi-Tech PICC language as described in Chapter 4 is used in the implementation of the algorithms. As described in Section 1.6 there are several methods that can be used to implement the controller algorithm. One of the most common, which has the advantage of accurate implementation, is the use of a timer interrupt to generate the required loop delay (or the sampling interval). In this method the software consists of two parts: the main program and the interrupt service routine. As shown in Figure 10.15(a) Figure 10.15, in the main program various variables, as well as the A/D converter and the timer interrupt mechanism, are initialized. The timer is set to interrupt at an interval equivalent to the sampling interval of the required digital controller. The main program then enters a loop waiting for the timer interrupts to occur. Whenever a timer interrupt occurs the program jumps the interrupt service
254 CONTROLLER REALIZATION
Enter |
ISR: |
Enter |
Read setpoint, sk
Read reference, sk Read output, yk Input section
Calculate error, ek
Initialize A/D |
Calculate output, yk |
Output section |
|
Initialize
Update variables Update section
Timer interrupts
Return from interrupt
Wait for interrupt
Figure 10.15 Controller implementation: (a) main program; (b) interrupt service routine
routine (ISR) as shown in Figure 10.15(b), and it is within this routine that the actual controller algorithm is implemented. The error signal is obtained by calculating the difference between the reference values and measured values. The algorithm is then implemented and the output sample for the current sampling time is obtained. A preprocessing step is then performed by updating the variables for the next sample. On return from the ISR, the program waits in the main program until the next sampling interval, and the above process repeats.
10.5.1 Implementing Second-Order Modules
In Section 10.2 we saw how a second-order module can be realized using adders, multipliers and delay elements. The second-order module is shown in Figure 10.16. The difference equations describing such a module are (10.19) and (10.20). If we let
M1 = −b1rk−1 − b2rk−2
and
M2 = a1rk−1 + a2rk−2
then the difference equations for the second-order module become
rk = ek + M1,
uk = a0rk + M2.
The implementation of the second-order module is shown as a flow-chart in Figure 10.17. This figure does not show the initialization of the variables T1, T2, r1, r2, the A/D initialization,
256 CONTROLLER REALIZATION
Figure 10.18 Circuit diagram of the microcontroller
Assume that the digital controller to be implemented is in the form of a second-order module, and write a program in C to implement this controller. The controller parameters are assumed to be
a0 = 1, a1 = 0.8, |
a2 = 1.2, b1 = 1.85, |
b2 = 0.92, |
||
i.e. the required controller transfer function is |
|
|
||
D(z) |
= |
1 + 0.8z−1 + 1.2z−2 |
. |
|
1 + 1.85z−1 + 0.92z−2 |
|
|||
|
|
|
Also assume that the required sampling interval is T = 0.01s.
Solution
The controller hardware is based on a PIC16F877 microcontroller. The microcontroller is operated from a 4 MHz crystal, connected to OSC1 and OSC2 inputs. With a 4 MHz crystal, the basic clock rate of the microcontroller is 1µs (the crystal frequency is divided by 4 to obtain the basic timing rate). The analog output of the plant (y) is connected to A/D converter channel AN0 of the microcontroller. Similarly, port B digital outputs of the microcontroller are connected to an AD7302 type D/A converter. The operation of this D/A converter is very simple. Digital data is applied to eight inputs D0–D7, while the write control input, WR, is at logic high. The analog data appears at the output after the WR input is lowered to logic low. The WR input of the D/A converter is controlled from port pin RC0 of the microcontroller. The D/A converter is set to operate with a full-scale reference voltage of +5 V. The resolution of the converter is 8 bits, i.e. there are 28 = 256 quantization levels. With a full-scale reference voltage of +5 V,
MICROCONTROLLER IMPLEMENTATIONS |
257 |
the resolution is 5000/256 = 19.53 mV, i.e. the digital bit pattern ‘00000001’ corresponds to 19.53 mV, the bit pattern ‘00000010’ corresponds to 2 × 19.53 = 39.06 mV and so on.
The first program attempt is shown in Figure 10.19. Comments are used to describe operation of various parts of the program. The program consists of the main program and the functions:
Initialize AD, Initialize Timer, Read AD Input, and ISR.
Main program. The coefficients of the controller are defined at the beginning of the main program. Also, the A/D converter and the timer initialization functions are called here. The main program then enables global interrupts and enters an endless loop waiting for timer interrupts to occur.
ISR. This is the interrupt service routine. The program jumps to this function every 10 ms. The function reads a sample, and calculates the error term ek . The output value yk is then calculated and sent to the D/A converter. In the final part of the ISR, the variables are updated for the next sample, and the timer interrupt is re-enabled.
Initialize Timer. This function initializes the timer TMR0 so that timer interrupts can be generated at 10 ms intervals. As described in Chapter 3, the timing interval depends on the clock frequency, the pre-scaler value, and the data loaded into the TMR0 register. It can be shown that the timing interval is given by:
Timing interval = 4*clock period*prescaler*(256 − TMR0 value)
and the value to be loaded into TMR0 register for a required timing interval is given by:
TMR0 value = 256 − timing interval/(4*clock period*prescaler)
The clock frequency is chosen as 4 MHz, i.e. the clock period = 0.25 µs. If we choose a prescaler value of 64, the value to be loaded into the timer register for a 10 ms (10 000 µs) timing interval can be calculated as:
TMR0 value = 256 − 10000 ms/(4*0.25*64) = 99.75
We can choose 100 as the nearest value. This will give a timing interval of
Timing interval = 4*0.25*64*(256 − 100) = 9.984ms,
which is very close to the required value.
Thus, the Initialize Timer function is programmed as follows:
/* This function initilizes the timer TMR0 so that interrupts can be generated at every 10ms intervals */
void Initialize Timer(void)
{
T0CS = 0; |
/* Select f/4 clock for the TMR0 */ |
PSA = 0; |
/* Select pre-scaler */ |
PS0 = 1; |
/* Set pre-scaler to 64 */ |
PS1 = 0; |
/* PS2,PS1,PS0 = 101 */ |
PS2 = 1; |
|
TMR0 = 100; |
/* Load TMR0 = 100 */ |
T0IE = 1; |
/* Enable TMR0 interrupts */ |
T0IF = 0; |
/* Clear TMR0 interrupt flag */ |
}