Discrete PID Controller On tinyAVR and megaAVR Devices
Discrete PID Controller On tinyAVR and megaAVR Devices
Features
• Simple discrete PID controller algorithm
• Supported by all AVR devices 8-bit
• PID function uses 534 bytes of code memory and 877 CPU cycles (IAR - low size
optimization)
Microcontrollers
Figure 1-1. Typical PID regulator response to step change in reference input
12 p
pi
pid
10
ref
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 e u y
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
d
Td
dt
e Kp u
Ti
2 AVR221
2558A-AVR-05/06
AVR221
12 p
ref
10
0
0 1 2 3 4 5 6 7 8 9 10
3
2558A-AVR-05/06
Figure 2-4. Step response I and PI controller
12 p
i
pi
10
ref
0
0 1 2 3 4 5 6 7 8 9 10
4 AVR221
2558A-AVR-05/06
AVR221
Figure 2-5. Step response D and PD controller
12 p
d
pd
10
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 p
pi
pid
10
ref
0
0 1 2 3 4 5 6 7 8 9 10
5
2558A-AVR-05/06
2.4 Tuning the parameters
The best way to find the needed PID parameters is from a mathematical model of the
system, parameters can then be calculated to get the desired response. Often a
detailed mathematical description of the system is unavailable, experimental tuning of
the PID parameters has to be performed. Finding the terms for the PID controller can
be a challenging task. Good knowledge about the systems properties and the way the
different terms work is essential. The optimum behavior on a process change or
setpoint change depends on the application at hand. Some processes must not allow
overshoot of the process variable from the setpoint. Other processes must minimize
the energy consumption in reaching the setpoint. Generally, stability is the strongest
requirement. The process must not oscillate for any combinations or setpoints.
Furthermore, the stabilizing effect must appear within certain time limits.
Several methods for tuning the PID loop exist. The choice of method will depend
largely on whether the process can be taken off-line for tuning or not. Ziegler-Nichols
method is a well-known online tuning strategy. The first step in this method is setting
the I and D gains to zero, increasing the P gain until a sustained and stable oscillation
(as close as possible) is obtained on the output. Then the critical gain Kc and the
oscillation period Pc is recorded and the P, I and D values adjusted accordingly using
Table 2-1.
Table 2-1. Ziegler-Nichols parameters
Controller Kp Ti Td
P 0.5 * Kc
PD 0.65 * Kc 0.12 * Pc
PI 0.45 * Kc 0.85 * Pc
PID 0.65 * Kc 0.5 * Pc 0.12 * Pc
6 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:
⎛ ⎞
( s ) = H (s ) = K p ⎜⎜1 +
u 1
+ Td s ⎟⎟
e ⎝ Ti s ⎠
⎛ 1
t
de (t ) ⎞
u (t ) = K p ⎜ e(t ) + ∫ e(σ ) dσ + Td
⎜ ⎟
⎟
⎝ Ti 0 dt ⎠
Approximating the integral and the derivative terms to get the discrete form, using:
de (t ) e(n ) − e(n − 1)
t n
K pT K pTd
Ki = Kd =
Ti 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:
n
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.
p_factor, i_factor
pid
main() Init_PID()
LAST_PROCESS_VALUE
d_factor SUM_ERROR
P_FACTOR
I_FACTOR
setPoint, processValue D_FACTOR
PID_timer MAX_ERROR
PID()
MAX_SUM_ERROR
control input
PFactor = 128K p
Furthermore the effect of the IFactor and DFactor will depend on the sample time T .
T
IFactor = 128 K p
Ti
T
DFactor = 128 K p d
T
8 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 Atmel Operations
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 ATMEL’S TERMS AND
CONDITIONS OF SALE LOCATED ON ATMEL’S 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. Atmel’s 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