AVR221 Discrete PID Controller
AVR221 Discrete PID Controller
Features
Simple discrete PID controller algorithm Supported by all AVR devices PID function uses 534 bytes of code memory and 877 CPU cycles (IAR - low size optimization)
1 Introduction
This application note describes a simple implementation of a discrete ProportionalIntegral-Derivative (PID) controller. When working with applications where control of the system output due to changes in the reference value or state is needed, implementation of a control algorithm may be necessary. Examples of such applications are motor control, control of temperature, pressure, flow rate, speed, force or other variables. The PID controller can be used to control any measurable variable, as long as this variable can be affected by manipulating some other process variables. Many control solutions have been used over the time, but the PID controller has become the industry standard due to its simplicity and good performance. For further information about the PID controller and its implications the reader should consult other sources, e.g. PID Controllers by K. J. Astrom & T. Hagglund (1995).
Figure 1-1. Typical PID regulator response to step change in reference input
12 p pi pid ref
10
0 0 1 2 3 4 5 6 7 8 9 10
Rev. 2558A-AVR-05/06
2 PID controller
In Figure 2-1 a schematic of a system with a PID controller is shown. The PID controller compares the measured process value y with a reference setpoint value, y0. The difference or error, e, is then processed to calculate a new process input, u. This input will try to adjust the measured process value back to the desired setpoint. The alternative to a closed loop control scheme such as the PID controller is an open loop controller. Open loop control (no feedback) is in many cases not satisfactory, and is often impossible due to the system properties. By adding feedback from the system output, performance can be improved. Figure 2-1. Closed Loop System with PID controller
y0 -
PID
System
Unlike simple control algorithms, the PID controller is capable of manipulating the process inputs based on the history and rate of change of the signal. This gives a more accurate and stable control method. The basic idea is that the controller reads the system state by a sensor. Then it subtracts the measurement from a desired reference to generate the error value. The error will be managed in three ways, to handle the present, through the proportional term, recover from the past, using the integral term, and to anticipate the future, through the derivate term. Figure 2-2 shows the PID controller schematics, where Tp, Ti, and Td denote the time constants of the proportional, integral, and derivative terms respectively. Figure 2-2. PID controller schematic
Td e Kp
d dt u
Ti
AVR221
2558A-AVR-05/06
AVR221
2.1 Proportional term
The proportional term (P) gives a system control input proportional with the error. Using only P control gives a stationary error in all cases except when the system control input is zero and the system process value equals the desired value. In Figure 2-3 the stationary error in the system process value appears after a change in the desired value (ref). Using a too large P term gives an unstable system. Figure 2-3. Step response P controller
12
p ref
10
0 0 1 2 3 4 5 6 7 8 9 10
3
2558A-AVR-05/06
10
p i pi ref
0 0 1 2 3 4 5 6 7 8 9 10
AVR221
2558A-AVR-05/06
AVR221
Figure 2-5. Step response D and PD controller
12
10
p d pd ref
0 0 1 2 3 4 5 6 7 8 9 10
Using all the terms together, as a PID controller usually gives the best performance. Figure 2-6 compares the P, PI, and PID controllers. PI improves the P by removing the stationary error, and the PID improves the PI by faster response and no overshoot. Figure 2-6. Step response P, PI and PID controller
12
10
p pi pid ref
0 0 1 2 3 4 5 6 7 8 9 10
5
2558A-AVR-05/06
Further tuning of the parameters is often necessary to optimize the performance of the PID controller. The reader should note there is systems where the PID controller will not work very well, or will only work on a small area around a given system state. Non-linear systems can be such, but generally problems often arise with PID control when systems are unstable and the effect of the input depends on the system state.
AVR221
2558A-AVR-05/06
AVR221
2.5.1 Algorithm background Unlike simple control algorithms, the PID controller is capable of manipulating the process inputs based on the history and rate of change of the signal. This gives a more accurate and stable control method. Figure 2-2 shows the PID controller schematics, where Tp, Ti, and Td denotes the time constants of the proportional, integral, and derivative terms respectively. The transfer function of the system in Figure 2-2:
u 1 ( s ) = H (s ) = K p 1 + T s + Td s e i
This gives u with respect to e in the time domain:
t de (t ) 1 e(t ) + e( ) d + Td u (t ) = K p Ti 0 dt
Approximating the integral and the derivative terms to get the discrete form, using:
e( )d T e(k )
t = nT
Where:
Ki =
K pT Ti
Kd =
K pTd T
To avoid that changes in the desired process value makes any unwanted rapid changes in the control input, the controller is improved by basing the derivative term on the process value only:
u (n ) = K p e(n ) + K i e( k ) + K d ( y (n ) y (n 1))
k =0
7
2558A-AVR-05/06
3 Implementation
A working implementation in C is included with this application note. Full documentation of the source code and compilation information if found by opening the readme.html file included with the source code.
PID_timer
In Figure 3-1 a simplified block diagram of the demo application is shown. The PID controller uses a struct to store its status and parameters. This struct is initialized in main, and only a pointer to it is passed to the Init_PID() and PID() functions. The PID() function must be called for each time interval T, this is done by a timer who sets the PID_timer flag when the time interval has passed. When the PID_timer flag is set the main routine reads the desired process value (setPoint) and system process value, calls PID() and outputs the result to the control input. To increase accuracy the p_factor, i_factor and d_factor are scaled with a factor 1:128. The result of the PID algorithm is later scaled back by dividing by 128. The value 128 is used to allow for optimizing in the compiler.
PFactor = 128K p
Furthermore the effect of the IFactor and DFactor will depend on the sample time T .
AVR221
2558A-AVR-05/06
AVR221
If the controller uses an integral term, this situation can be a problematic. The integral term will sum up as long as the situation last, and when the larger disturbance / load disappear, the PID controller will overcompensate the process input until the integral sum is back to normal. This problem can be avoided in several ways. In this implementation the maximum integral sum is limited by not allowing it to become larger than MAX_I_TERM. The correct size of the MAX_I_TERM will depend on the system and sample time used.
4 Further development
The PID controller presented here is a simplified example. The controller should work fine, but it might be necessary to make the controller even more robust (limit runaway/overflow) in certain applications. Adding saturation correction on the integral term, basing the proportional term on only the system process value can be necessary. In the calculating of IFactor and DFactor the sample time T is a part of the equation. If the sample time T used is much smaller or larger than 1 second, accuracy for either IFactor or DFactor will be poor. Consider rewriting the PID algorithm and scaling so accuracy for the integral and derivate term is kept.
5 Literature references
K. J. Astrom & T. Hagglund, 1995: PID Controllers: Theory, Design, and Tuning. International Society for Measurement and Con.
9
2558A-AVR-05/06
Disclaimer
Atmel Corporation
2325 Orchard Parkway San Jose, CA 95131, USA Tel: 1(408) 441-0311 Fax: 1(408) 487-2600
Atmel Operations
Memory
2325 Orchard Parkway San Jose, CA 95131, USA Tel: 1(408) 441-0311 Fax: 1(408) 436-4314
RF/Automotive
Theresienstrasse 2 Postfach 3535 74025 Heilbronn, Germany Tel: (49) 71-31-67-0 Fax: (49) 71-31-67-2340 1150 East Cheyenne Mtn. Blvd. Colorado Springs, CO 80906, USA Tel: 1(719) 576-3300 Fax: 1(719) 540-1759
Regional Headquarters
Europe
Atmel Sarl Route des Arsenaux 41 Case Postale 80 CH-1705 Fribourg Switzerland Tel: (41) 26-426-5555 Fax: (41) 26-426-5500
Microcontrollers
2325 Orchard Parkway San Jose, CA 95131, USA Tel: 1(408) 441-0311 Fax: 1(408) 436-4314 La Chantrerie BP 70602 44306 Nantes Cedex 3, France Tel: (33) 2-40-18-18-18 Fax: (33) 2-40-18-19-60
Asia
Room 1219 Chinachem Golden Plaza 77 Mody Road Tsimshatsui East Kowloon Hong Kong Tel: (852) 2721-9778 Fax: (852) 2722-1369
ASIC/ASSP/Smart Cards
Zone Industrielle 13106 Rousset Cedex, France Tel: (33) 4-42-53-60-00 Fax: (33) 4-42-53-60-01 1150 East Cheyenne Mtn. Blvd. Colorado Springs, CO 80906, USA Tel: 1(719) 576-3300 Fax: 1(719) 540-1759 Scottish Enterprise Technology Park Maxwell Building East Kilbride G75 0QR, Scotland Tel: (44) 1355-803-000 Fax: (44) 1355-242-743
Japan
9F, Tonetsu Shinkawa Bldg. 1-24-8 Shinkawa Chuo-ku, Tokyo 104-0033 Japan Tel: (81) 3-3523-3551 Fax: (81) 3-3523-7581
Literature Requests
www.atmel.com/literature
Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMELS TERMS AND CONDITIONS OF SALE LOCATED ON ATMELS WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmels products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.
Atmel Corporation 2006. All rights reserved. Atmel, logo and combinations thereof, Everywhere You Are, AVR, and AVR Studio are the registered trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.
2558A-AVR-05/06