Вы находитесь на странице: 1из 14

12/6/04 BAE 3023 1

Advanced Embedded Systems Design


Lecture 14 Implementation of a PID controller
BAE 5030 - 003
Fall 2004
Instructor: Marvin Stone
Biosystems and Agricultural Engineering
Oklahoma State University
12/6/04 BAE 3023 2
Goals for Class Today
Questions over reading / homework (CAN Implementation)
Zigbee and 802.14.5 (Kyle)
PID implementation (Stone)
12/6/04 BAE 3023 3
Elements of a feedback control system
Review elements and
variables
Gc G
2
G
3
Error
c
Manipulated
Variable
D
Controlled
Variable
u
out
+
+
G
1
u
out
-
+
u
out
Load
u
in
Setpoint
u
set
u
out
u
out_measured
12/6/04 BAE 3023 4
) 1 ( ) 1 (
2 3
2
2 3
1
G G G
G G
G G G
G
c
c set
c
in
out
+
+
+
=
u u
u
) (
_ measured out set c
G D u u =
2 1
DG G
in out
+ =u u
3 _
G
out measured out
u u =
Output (u
out
) is readily calculated as a function of:
Load (u
in
) and
Setpoint (u
set
)
Manipulation is a simple function of the controller TF and error.
12/6/04 BAE 3023 5
Digital form of a classic feedback controlled system
If sampling rate is fast and holds are employed, this
system approaches the analog system
Gc G
2
G
3
Error
c-
Manipulated
Variable
D* D
Controlled
Variable
u
out
+
+
G
1
u
out
-
+
u
out
Load
u
in
Setpoint
u
set
u
out
u
out_measured
u
out_measured
*
Computer based controller
12/6/04 BAE 3023 6
One of the conventional models used to express a PID controller is:
|
|
.
|

\
|
+ + =
}
=
dt
de
edt e K M
d
t
t
i
c
t
t
0
1
rate Derivitive
rate Reset
signal Error e
gain Controller K
on Manipulati M
d
i
c
=
=
=
=
=
t
t
Time Domain PID Controller Equation
Where:
12/6/04 BAE 3023 7
Derivitive Form of a PID Controller
A convenient way to implement this equation in a controller is as the
derivative of manipulation known as the velocity form of the equation
as shown below:
|
|
.
|

\
|
+ + =
2
2
dt
e d e
dt
de
K
dt
dM
d
i
c
t
t
In a practical system this equation will work well and does not require
any steady-state references, but eliminating the t
i
and t
d
term
completely results in:
|
.
|

\
|
=
dt
de
K
dt
dM
c
de K dM
c
=
or,
This equation has no positional reference and error accumulation is a
problem. Use velocity form only for PI or PID modes.
12/6/04 BAE 3023 8
Conversion of the DE to a Difference Equation
To begin the conversion of the PID equation to a difference
equation, the equation is multiplied by dt.

|
|
.
|

\
|
|
.
|

\
|
+ + =
dt
de
d
edt
de K dM
d
i
c
t
t
Note that since M is a differential and e
ss
is zero, this equation
conveniently applies to the absolute variables as well as the
deviation variables.
For small At, the equation can be approximated as:
|
|
.
|

\
|
|
.
|

\
|
A
A
A +
A
+ A = A
t
t
K m
d
i
c
c
t
t
c
c
12/6/04 BAE 3023 9
Representation with Discrete Time Variables
Each of the differences (A) can be expressed as discrete values of
each of the variables ( m and c ) at the times 0, 1, and 2 as shown
below:
t
0
t
2
t
1
M
c
c
1
c
2
c
0
M
0
M
1
M
2
The equation can be simplified with the assumption that At is
constant:
( )
|
|
.
|

\
|
A A
A
+
A
+ A = A c
t
t
c
c
t
t
K m
d
i
c
12/6/04 BAE 3023 10
Discrete form of PID controller
Replacement of the differences (A) with the discrete
variables ( m and c ) results in:
( ) ( ) ( )
|
|
.
|

\
|
A A
A
+
A
+ =
1 2
2
1 2 1 2
) ( c c
t
t
c
c c
t
t
K m m
d
i
c
( ) ( ) ( )
|
|
.
|

\
|

A
+
A
+ =
0 1 1 2
2
1 2 1 2
) ( c c c c
t
t
c
c c
t
t
K m m
d
i
c
( )
|
|
.
|

\
|
+
A
+
A
+ =
0 1 2
2
1 2 1 2
2 ) ( c c c
t
t
c
c c
t
t
K m m
d
i
c
Note that At is assumed to be a constant. If At varies, the equations
should be derived with that in mind.
12/6/04 BAE 3023 11
Discrete form of PID controller
This equation can be solved for the current manipulation, m
2
, in terms
of values known at time t
2
: m
1
, e
2
, e
1
, and e
0
.
The other parameters in the equation are constants.
|
|
.
|

\
|
A
+
|
.
|

\
|
A

|
|
.
|

\
|
A
+
A
+ + =
0 1 2 1 2
2
1 1 c
t
c
t
c
t
t t t t
t
K m m
d d d
i
c
Where C
1
,C
2
, and C
3
are constants and the current manipulation is
expressed in terms of known values, the current and past errors.
( )
0 3 1 2 2 1 1 2
c c c c c c K m m
c
+ + =
or,
12/6/04 BAE 3023 12
Translation of PID Equation into Algorithm
This equation may be translated directly into a computer language, for
example:

m2 = m1 + k*(C1*e2 C2*e1 + C3*e0);

Within a computer program, the current error
is calculated from the current measurement of
the controlled variable and the setpoint, for example:

e2 = T_setpoint T_measured;

The current manipulation m2 is then computed using the previous
controller equation, and finally, at the end of the time step, each of the
variables is shifted forward for the next calculation.
12/6/04 BAE 3023 13
Translation of PID Equation into Algorithm
For example in C, the code might look like:

measure_and_manipulate() //Call once per delta T
begin
T_measured = measure_T(); //Get the measured temperature
e2 = T_setpoint T_measured //Calculate the current error
m2 = m1 + k*(C1*e2 C2*e1 + C3*e0); //Calculate the manipulation
set_manupilation(m2); //Output the manipulation
e0 = e1; //Shift the error and manipulation
e1 = e2; //forward one time step
m1= m2;
end;

Note that the time step At is controlled by the time required to execute
the loop. C1,C2 and C3 are all functions of At. The equation will
probably be executed as floats! (Or very special care must be taken
with scaling.
12/6/04 BAE 3023 14
Assignment
Complete CAN message demo
Turn in course portfolio by 5:00 PM Wednesday Dec.
8th

Вам также может понравиться