Professional Documents
Culture Documents
(4.5)
Taking the Inverse Laplace transform of equation 4.5, we get
c(t) = 1 e
t
(4.6)
from the above equation it is clear that for t=0 the value of c(t) is zero. For t= ,
c(t) approaches unity. Also as the value of tbecomes equal to , the value of c(t)
becomes 0.632. The is called as the time constant and represents the speed of
response of the system. But it should be noted that, the smaller the time constant,
the faster the system response.
By getting the value of , one can identify the transfer function of the system.
Consider the system to be rst order. We try to t a rst order transfer function
of the form
G(s) =
K
s + 1
(4.7)
to single board heater system. Because the transfer function approach uses de-
viation variables,G(s) denotes the Laplace transform, of the gain of the system
between the change in heater current and the change in the system temperature.
Let the change in the heater current be denoted by u. We denote both the time
domain and the Laplace transform variable by the same lower case variable. Let
the change in temperature be denoted by y.Suppose that the current changes by a
step of size u.Then, we obtain the following relation between the current and the
temperature.
y(s) = G(s)u(s) (4.8)
y(s) =
K
s + 1
u
s
(4.9)
48
Figure 4.3: Output of the scilab code firstorder.sce
Note that u is the height of the step and hence is a constant. On inversion, we
obtain
y(s) = K[1 e
t
]u (4.10)
Copy the step test data le to the folder Kp-tau-order1. Change the scilab
working directory to Kp-tau-order1 folder under Step Analysis folder. Open
the le firstorder.sce and put the name of the data le (with extention) in the
lename eld. Save and run this code and obtain the plot as shown in gure 4.3.
This code uses the routines label.sci and costf 1.sci
49
The plot thus obtained is reasonably good. See the Scilab console to get the values
of and K. The gure 4.4 shows a screen shot of the same. We obtain =
35.61087, K=0.4201. The transfer function obtained here is at the operating point
of 30 pwm of heat. If the experiment is repeated at a dierent operating point, the
transfer function obtained will be dierent. The gain will correspondingly be
more at a higher operating point. This means that the plant is faster at higher
temperature. Thus the transfer function of the plant varies with the operating
point. Let the transfer function we obtain in this experiment be denoted as G
s
.We
obtain
G
s
(s) =
0.4201
35.61s + 1
(4.11)
4.3 Determination of second order transfer function
In this section, we explore the ecacy of a second order model of the form
G(s) =
K
(
1
s + 1)(
2
s + 1)
(4.12)
The response of the system to a step input of height u is given by
y(s) =
K
(
1
s + 1)(
2
s + 1)
u
s
(4.13)
Splitting this into partial fraction expansion, we obtain
y(s) =
K
2
1
_
s +
1
1
_ _
s +
1
2
_ =
A
s
+
B
s +
1
1
+
C
s +
1
2
Through heaviside expansion method, we determine the coecients:
A = K
B =
K
1
1
2
C =
K
2
1
2
50
Figure 4.4: The value of time constant and gain as shown on the console by
rstorder.sce
51
On substitution and inversion, we obtain
y(t) = K
_
1
1
1
2
_
1
e
t/
1
2
e
t/
2
_
_
(4.14)
We have to determine three parameters, K,
1
and
2
through optimization.
Once again, we follow a procedure identical to the rst order model. The only
dierence is that we now have to determine three parameters. Scilab code
secondorder.sce does these calculations and outputs a the gain and two time
constants. Again change the scilab working diectory to the folder Kp-tau-order2.
Copy the step test data le in this folder. Run the code secondorder.sce with
the appropriate data le name. The plot shown in 4.5 is obtained. It corresponds
to the following transfer function with the parameters written at the top of the plot.
G
s
(s) =
0.420
(34.4s + 1)(1s + 1)
(4.15)
The t is much better now. In particular, the initial inexion is well captured
by this second order transfer function.
4.4 Discussion
We summarize our ndings now. For the rstorder analysis the gain is 0.4201 and
the time constant is 35.61 sec. For the second order analysis, the initial inexion
is well captured with the two time constants
1
=34.4,
2
= 1 and gain = 0.420.
Negative steps can also be introduced to make the experiment more informative.
One can even need not keep a particular input constant. By varying both the
inputs, one can imagine it to be like a step varying disturbance signal.
4.5 Conducting Step Test on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained
in section 3.4. The required .sce le is stepc.sce. You will nd this le in the
StepTest directory under virtual folder. Please note that the analysis code of
step test data obtained by a virtual experiment is slightly dierent. The procedure
to use the analysis code however remains the same as explained earlier. To do a
rst order analysis, one has to use the le firstorder virtual.sce. Similarly,
secondorder virtual.sce for second order analysis. You will nd this le in
52
Figure 4.5: Output of the scilab code secondorder.sce
53
the StepTest directory under virtual folder. The necessary codes are listed in
the section 4.6.
4.6 Scilab Code
Scilab Code 4.1 label.sci
1 / / U p d a t e d ( 9 1 2 0 6 ) , w r i t t e n b y I n d e r p r e e t A r o r a
2 / / I n p u t a r g u m e n t s : t i t l e , x l a b e l , y l a b e l a n d t h e i r
f o n t s i z e s
3
4 f unc t i on l a b e l ( tname , t f o n t , l a be l x , l a be l y , xyf ont )
5 a = get ( c u r r e n t a x e s )
6 x t i t l e ( tname , l a be l x , l a b e l y )
7 xgri d
8 t = a . t i t l e ;
9 t . f o n t s i z e = t f o n t ; / / T i t l e f o n t s i z e
10 t . f o n t s t y l e = 2; / / T i t l e f o n t s t y l e
11 t . t e x t = t name ;
12
13 u = a . x l a b e l ;
14 u . f o n t s i z e = xyf ont ; / / L a b e l f o n t s i z e
15 u . f o n t s t y l e = 2; / / L a b e l f o n t s t y l e
16
17 v = a . y l a b e l ;
18 v . f o n t s i z e = xyf ont ; / / L a b e l f o n t s i z e
19 v . f o n t s t y l e = 2; / / L a b e l f o n t s t y l e
20
21 / / a . l a b e l f o n t s i z e = 3 ;
22
23 endf unct i on ;
Scilab Code 4.2 costf 1.sci
1 f unc t i on [ f , g , i nd ] = c o s t f 1 ( x , i nd )
2 kp = x ( 1 ) ; t a u = x ( 2 ) ;
3 y p r e d i c t i o n = kp
*
( 1 exp( t / t a u ) ) ;
54
4 f = ( norm( yy p r e d i c t i o n , 2 ) ) 2 ;
5 g = numdi ff ( f unc 1 , x ) ;
6 endf unct i on
7
8 f unc t i on f = f unc 1 ( x )
9 kp = x ( 1 ) ; t a u = x ( 2 ) ;
10 y p r e d i c t i o n = kp
*
( 1 exp( t / t a u ) ) ;
11 f = ( norm( yy p r e d i c t i o n , 2 ) ) 2 ;
12 endf unct i on
Scilab Code 4.3 firstorder.sce
1 mode ( 0 )
2 f uncprot ( 0 )
3 f i l e na me = s t e p d a t a
4 c l f
5 exec ( c o s t f 1 . s c i ) ;
6 exec ( l a b e l . s c i ) ;
7 da t a = f scanf Mat ( f i l e na me ) ;
8 t i me = da t a ( : , 1) ;
9 h e a t e r = i nt ( da t a ( : , 2) ) ;
10 f an = i nt ( da t a ( : , 3) ) ;
11 temp = da t a ( : , 4) ;
12
13
14 l e n = l e ngt h ( h e a t e r ) ;
15
16 t i me2 = t i me t i me ( 1 ) ;
17 / / t i m e 2 = t i m e 1 / 1 0 0 0 ;
18
19 l e n = l e ngt h ( h e a t e r ) ;
20 h e a t e r s 1 = [ h e a t e r ( 1 ) ; h e a t e r ( 1 : l en 1) ] ;
21 d e l h e a t = h e a t e r h e a t e r s 1 ;
22 i nd = f i nd ( d e l h e a t >1) ;
23
24 s t e p i n s t a n t = i nd ( $ ) 1;
25
26 t = t i me ( s t e p i n s t a n t : l e n ) ;
55
27 t = t t ( 1 ) ;
28 H = h e a t e r ( s t e p i n s t a n t : l e n ) ;
29 F = f an ( s t e p i n s t a n t : l e n ) ;
30 T = temp ( s t e p i n s t a n t : l e n ) ;
31 T = T T( 1 ) ;
32 d e l t a u = h e a t e r ( s t e p i n s t a n t + 1) h e a t e r (
s t e p i n s t a n t ) ;
33
34 / / f i n d i n g Kp a n d T a u b e t w e e n H e a t e r ( H ) a n d
T e m p e r a t u r e ( T )
35 y = T; / / t e m p e r a t u r e
36 gl obal ( y , t ) ;
37 x0 = [ . 3 4 0 ] ;
38 / / [ f , x o p t , g o p t ] = o p t i m ( c o s t f 1 , b , [ 0 . 1 0 . 1 ] , [ 5 1 0 0 ] ,
x 0 , a r )
39 [ f , xopt ] = optim ( c o s t f 1 , x0 ) ;
40 kp = xopt ( 1 ) ;
41 t a u = xopt ( 2 ) ;
42 y p r e d i c t i o n = kp
*
( 1 exp( t / t a u ) ) ;
43 pl ot 2d ( t , y p r e d i c t i o n ) ;
44 pl ot 2d ( t , y ) ;
45 l a b e l ( Showing F i r s t Or der Model and Expe r i me nt a l
Re s u l t s , 4 , Time ( s ) , Change i n t e mp e r a t u r e (K)
, 4) ;
46 kp = kp / d e l t a u
47 t a u
Scilab Code 4.4 costf 2.sci
1 f unc t i on [ f , g , i nd ] = c o s t f 2 ( x , i nd )
2 kp = x ( 1 ) ; t a u1 = x ( 2 ) ; t a u2 = x ( 3 ) ;
3 y p r e d i c t i o n = kp
*
d e l t a u
*
( 1 . . .
4 ( t a u1
*
exp ( ( t ) / t a u1 ) t a u2
*
exp ( ( t ) / t a u2 ) ) . . .
5 / ( t au1 t a u2 ) ) ;
6 f = ( norm( Ty p r e d i c t i o n , 2 ) ) 2 ;
7 g = numdi ff ( f unc 2 , x ) ;
8 endf unct i on ;
9 f unc t i on f = f unc 2 ( x )
56
10 kp = x ( 1 ) ; t a u1 = x ( 2 ) ; t a u2 = x ( 3 ) ;
11 y p r e d i c t i o n = kp
*
d e l t a u
*
( 1 . . .
12 ( t a u1
*
exp ( ( t ) / t a u1 ) t a u2
*
exp ( ( t ) / t a u2 ) ) . . .
13 / ( t au1 t a u2 ) ) ;
14 f = ( norm( Ty p r e d i c t i o n , 2 ) ) 2 ;
15 endf unct i on ;
Scilab Code 4.5 order 2 heater.sci
1 f unc t i on l s t e r r = o r d e r 2 ( t , H, T, l i mi t s , no )
2 x0 = [ 2 200 150] ;
3 / / d e l t a u = u ( 2 ) u ( 1 ) ; u = u u ( 1 ) ; y = y y ( 1 ) ;
4
5 d e l t a u = H( 2 ) H( 1 ) ;
6
7
8 [ f , xopt , gopt ] = optim ( c o s t f 2 , b , [ 0 2 1] , [ 18 300
350] , x0 , a r , 200 , 200)
9 kp = xopt ( 1 ) ; t a u1 = xopt ( 2 ) ; t a u2 = xopt ( 3 ) ; l s t e r r =
s qr t ( f ) ;
10 y p r e d i c t i o n = kp
*
d e l t a u
*
( 1 . . .
11 ( t a u1
*
exp ( ( t ) / t a u1 ) t a u2
*
exp ( ( t ) / t a u2 ) ) . . .
12 / ( t au1 t a u2 ) ) ;
13 format ( v , 6) ; or d = [H T y p r e d i c t i o n ] ; x = [ t t t ] ;
14 / / x b a s c ( ) ;
15 pl ot 2d ( x , ord , r e c t =l i mi t s ) , xgri d ( ) ;
16 t i t l e = Compar i son of model wi t h da t a ( t a u1=
17 t i t l e = t i t l e +s t r i ng ( t a u1 ) + , t a u2=+s t r i ng ( t a u2 )
18 t i t l e = t i t l e + , K=+s t r i ng ( kp )
19 t i t l e = t i t l e + , e r r o r =+s t r i ng ( l s t e r r ) + )
20 l a b e l ( t i t l e , 4 , t i me ( s ) , Change i n t e mp e r a t u r e (K)
, 4) ;
21 endf unct i on ;
Scilab Code 4.6 secondorder.sce
1 mode ( 0 )
2 f uncprot ( 0 )
57
3 f i l e na me = s t e p d a t a
4 c l f
5 exec ( c o s t f 2 . s c i ) ;
6 exec ( l a b e l . s c i ) ;
7 exec ( o r d e r 2 h e a t e r . s c i ) ;
8
9
10 da t a = f scanf Mat ( f i l e na me ) ;
11 t i me = da t a ( : , 1) ;
12 h e a t e r = i nt ( da t a ( : , 2) ) ;
13 f an = i nt ( da t a ( : , 3) ) ;
14 temp = da t a ( : , 4) ;
15
16 / / t i m e s = [ t i m e ( 1 ) ; t i m e ( 1 : $ 1 ) ] ;
17 t i me2 = t i me t i me ( 1 ) ;
18 / / t i m e 2 = t i m e 1 / 1 0 0 0 ;
19
20
21 / / f i n d w h e r e t h e s t e p c h a n g e h a p p e n s
22
23 l e n = l e ngt h ( h e a t e r ) ;
24 h e a t e r s 1 = [ h e a t e r ( 1 ) ; h e a t e r ( 1 : l en 1) ] ;
25 d e l h e a t = h e a t e r h e a t e r s 1 ;
26 i nd = f i nd ( d e l h e a t >1) ;
27
28 s t e p i n s t a n t = i nd ( $ ) 1;
29 t = t i me ( s t e p i n s t a n t : l e n ) ;
30 t = t t ( 1 ) ;
31 H = h e a t e r ( s t e p i n s t a n t : l e n ) ;
32 F = f an ( s t e p i n s t a n t : l e n ) ;
33 T = temp ( s t e p i n s t a n t : l e n ) ;
34 T = T T( 1 ) ;
35
36 l i mi t s = [ 0 , 0 , 1 0 0 0 , 1 0] ; no =10000; / / f i r s t s t e p
37 / / l i m i t s = [ 4 0 0 , 0 , 9 0 0 , 2 6 ] ; n o = 5 0 0 0 ; / / s e c o n d s t e p
38 l s t e r r = o r d e r 2 ( t , H, T, l i mi t s , no )
Scilab Code 4.7 ser init.sce
58
1 / / T o l o a d t h e s e r i a l t o o l b o x a n d o p e n t h e s e r i a l p o r t
2 exec l o a d e r . s ce
3
4
5
6 handl = o p e n s e r i a l ( / dev / t t yUSB0 , 9600 , n , 8 , 0 )
7
8 / / t h e o r d e r i s : p o r t n u m b e r , b a u d , p a r i t y , d a t a b i t s ,
s t o p b i t s
9 / / h e r e 9 i s t h e p o r t n u m b e r
10 / / I n t h e c a s e o f S BHS , s t o p b i t s = 0 , s o i t i s n o t
s p e c i f i e d i n t h e f u n c t i o n h e r e
11 / / L i n u x u s e r s s h o u l d g i v e t h i s a s ( / , 9 6 0 0 , n , 8 , 0 )
12
13 i f ( a s c i i ( handl ) = [ ] )
14 di s p ( COM Por t Opened ) ;
15 end
Scilab Code 4.8 step test.sci
1 gl obal temp he a t f an
2
3 f unc t i on temp = s t e p t e s t ( heat , f an )
4
5 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / I n p u t H e a t e r ,
w r i t e s e r i a l a c c e p t s
s t r i n g s ; s o
c o n v e r t 2 5 4 i n t o i t s
s t r i n g
e q u i v a l e n t
6 wr i t e s e r i a l ( handl , a s c i i ( he a t ) ) ;
7 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ; / / I n p u t F a n
8 wr i t e s e r i a l ( handl , a s c i i ( f an ) ) ;
9 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ; / / T o r e a d T e m p
10 s l e e p ( 100) ;
11
12 temp = a s c i i ( r e a d s e r i a l ( handl ) ) ; / / R e a d s e r i a l
r e t u r n s a s t r i n g , s o
59
c o n v e r t i t t o
i t s i n t e g e r ( a s c i i )
e q u i v a l e n t
13 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ; / / c o n v e r t t o t e m p w i t h
d e c i m a l p o i n t s
e g : 4 0 . 7
14
15 endf unct i on
Scilab Code 4.9 stepc.sce
1 mode ( 0 ) ;
2 / / F o r s c i l a b 5 . 1 . 1 o r l o w e r v e r s i o n u s e r s , u s e s c i c o s
c o m m a n d t o o p e n s c i c o s d i a g r a m s i n s t e a d o f x c o s
3
4 gl obal f df h f d t f n c r fncw m e r r c o u n t y
5
6 f n c r = s c i l a b r e a d . s ce ;
7 f d t = mopen( f n c r ) ;
8 mseek ( 0 ) ;
9
10 e r r c o u n t = 0; / / i n i t i a l i s i n g e r r o r c o u n t f o r n e t w o r k
e r r o r
11 m =1;
12 exec ( s t e p t e s t . s c i ) ;
13 A = [ 0 . 1 , 0 , 1 0 0 ,m] ;
14 f df h = f i l e ( open , s c i l a b wr i t e . s ce , unknown ) ;
15 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
16 f i l e ( c l o s e , f df h ) ;
17 s l e e p ( 2000) ;
18 a = mget l ( f dt , 1 ) ;
19 mseek ( 0 ) ;
20 i f a = [ ] / / o p e n x c o s o n l y i f c o m m u n i c a t i o n i s
t h r o u g h ( i e r e p l y h a s c o m e f r o m s e r v e r )
21 xcos ( s t e p t e s t . xcos ) ;
22 e l s e
23 di s p ( NO NETWORK CONNECTION! ) ;
24 ret urn
60
25 end
Scilab Code 4.10 steptest.sci
1 mode ( 0 ) ;
2 f unc t i on [ y , s t op ] = s t e p t e s t ( heat , f an )
3 gl obal f df h f d t f n c r fncw m e r r c o u n t s t op
4
5
6 f n c r = s c i l a b r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
7 fncw = s c i l a b wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
8
9 a = mget l ( f dt , 1 ) ;
10 b = e vs t r ( a ) ;
11 byt e = mt e l l ( f d t ) ;
12 mseek ( byt e , f dt , s e t ) ;
13
14 i f a = [ ]
15 t emps = b ( 1 , 4 ) ; h e a t s = b ( 1 , 2 ) ;
16 f a ns = b ( 1 , 3 ) ; y = t emps ;
17
18 i f heat >100
19 he a t = 100;
20 e l s e i f heat <0
21 he a t = 0;
22 end ;
23
24 i f f an >100
25 f an = 100;
26 e l s e i f f an <0
27 f an = 0;
28 end ;
29
30 A = [m, heat , f an , m] ;
31 f df h = f i l e ( open , s c i l a b wr i t e . s ce , unknown ) ;
32 f i l e ( l a s t , f df h )
61
33 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
34 f i l e ( c l o s e , f df h ) ;
35 m = m+1;
36
37 e l s e
38 y = 0;
39 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
40 i f e r r c o u n t > 200
41 di s p ( NO NETWORK COMMUNICATION! ) ;
42 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
43 end
44 end
45
46 ret urn
47 endf unct i on
Scilab Code 4.11 firstorder virtual.sce
1 mode ( 0 )
2 f i l e na me = 27 J an2012 20 13 32 . t x t
3 c l f
4 exec ( c o s t f 1 . s c i ) ;
5 exec ( l a b e l . s c i ) ;
6 da t a = f scanf Mat ( f i l e na me ) ;
7 t i me = da t a ( : , 5) ;
8 h e a t e r = i nt ( da t a ( : , 2) ) ;
9 f an = i nt ( da t a ( : , 3) ) ;
10 temp = da t a ( : , 4) ;
11
12
13 l e n = l e ngt h ( h e a t e r ) ;
14
15 t i me1 = t i me t i me ( 1 ) ;
16 t i me2 = t i me1 / 1 0 0 0;
17
18 l e n = l e ngt h ( h e a t e r ) ;
19 h e a t e r s 1 = [ h e a t e r ( 1 ) ; h e a t e r ( 1 : l en 1) ] ;
62
20 d e l h e a t = h e a t e r h e a t e r s 1 ;
21 i nd = f i nd ( d e l h e a t >1) ;
22
23 s t e p i n s t a n t = i nd ( $ ) 1;
24
25 t = t i me2 ( s t e p i n s t a n t : l e n ) ;
26 t = t t ( 1 ) ;
27 H = h e a t e r ( s t e p i n s t a n t : l e n ) ;
28 F = f an ( s t e p i n s t a n t : l e n ) ;
29 T = temp ( s t e p i n s t a n t : l e n ) ;
30 T = T T( 1 ) ;
31 d e l t a u = h e a t e r ( s t e p i n s t a n t + 1) h e a t e r (
s t e p i n s t a n t ) ;
32
33 / / f i n d i n g Kp a n d T a u b e t w e e n H e a t e r ( H ) a n d
T e m p e r a t u r e ( T )
34 y = T; / / t e m p e r a t u r e
35 gl obal ( y , t ) ;
36 x0 = [ . 3 4 0 ] ;
37 / / [ f , x o p t , g o p t ] = o p t i m ( c o s t f 1 , b , [ 0 . 1 0 . 1 ] , [ 5 1 0 0 ] ,
x 0 , a r )
38 [ f , xopt ] = optim ( c o s t f 1 , x0 ) ;
39 kp = xopt ( 1 ) ;
40 t a u = xopt ( 2 ) ;
41 y p r e d i c t i o n = kp
*
( 1 exp( t / t a u ) ) ;
42 pl ot 2d ( t , y p r e d i c t i o n ) ;
43 pl ot 2d ( t , y ) ;
44 l a b e l ( Showing F i r s t Or der Model and Expe r i me nt a l
Re s u l t s , 4 , Time ( s ) , Change i n t e mp e r a t u r e (K)
, 4) ;
45 kp = kp / d e l t a u
46 t a u
Scilab Code 4.12 secondorder virtual.sce
1 mode ( 0 )
2 f i l e na me = 27 J an2012 20 13 32 . t x t
3 c l f
63
4 exec ( c o s t f 2 . s c i ) ;
5 exec ( l a b e l . s c i ) ;
6 exec ( o r d e r 2 h e a t e r . s c i ) ;
7
8
9 da t a = f scanf Mat ( f i l e na me ) ;
10 t i me = da t a ( : , 5) ;
11 h e a t e r = i nt ( da t a ( : , 2) ) ;
12 f an = i nt ( da t a ( : , 3) ) ;
13 temp = da t a ( : , 4) ;
14
15 / / t i m e s = [ t i m e ( 1 ) ; t i m e ( 1 : $ 1 ) ] ;
16 t i me1 = t i me t i me ( 1 ) ;
17 t i me2 = t i me1 / 1 0 0 0;
18
19
20 / / f i n d w h e r e t h e s t e p c h a n g e h a p p e n s
21
22 l e n = l e ngt h ( h e a t e r ) ;
23 h e a t e r s 1 = [ h e a t e r ( 1 ) ; h e a t e r ( 1 : l en 1) ] ;
24 d e l h e a t = h e a t e r h e a t e r s 1 ;
25 i nd = f i nd ( d e l h e a t >1) ;
26
27 s t e p i n s t a n t = i nd ( $ ) 1;
28 t = t i me2 ( s t e p i n s t a n t : l e n ) ;
29 t = t t ( 1 ) ;
30 H = h e a t e r ( s t e p i n s t a n t : l e n ) ;
31 F = f an ( s t e p i n s t a n t : l e n ) ;
32 T = temp ( s t e p i n s t a n t : l e n ) ;
33 T = T T( 1 ) ;
34
35 l i mi t s = [ 0 , 0 , 5 0 0 , 1 0 ] ; no =10000; / / f i r s t s t e p
36 / / l i m i t s = [ 4 0 0 , 0 , 9 0 0 , 2 6 ] ; n o = 5 0 0 0 ; / / s e c o n d s t e p
37 l s t e r r = o r d e r 2 ( t , H, T, l i mi t s , no )
64
Chapter 5
Identication of transfer function of
a Single Board Heater System
through Ramp response experiments
The Aim of this experiment is to perform Ramp test on the Single Board Heater
System and to identify the system transfer function using Ramp response data.
The target group is anyone who has basic knowledge of Control Engineering.
5.1 About this Experiment
We have used Scilab-5.2.2 and Xcos for sending and receiving data. This interface
is shown in Fig.5.1. Heater current and fan speed are the two inputs to the SBHS
system. They are given in PWM units. These inputs can be varied through the
Xcos interface by setting the properties of the input blocks in Xcos. The data
acquired in the process is stored in the local drive using the Write to output le
block and is available to the user for further analysis.
5.2 Theory
Identication of the transfer function of a system is quite important since it helps
us to model the physical system mathematically. Once the transfer function is
obtained one can nd out the response of the system, to various inputs, without
actually applying them to the system. Consider the standard rst order transfer
65
Figure 5.1: Xcos for Ramp Test experiment
function given below
G(s) =
C(s)
R(s)
(5.1)
G(s) =
K
s + 1
(5.2)
Rewriting the equation by substituting equation 4.2 in equation 4.1, we get
C(s) = K
_
R(s)
s + 1
_
(5.3)
Let us consider the case of giving a ramp input to this rst order system. The
Laplace Transform of a ramp function with slope = is
s
2
. Substituting R(s) =
s
2
in equation 5.3, we obtain
C(s) =
K
s + 1
s
2
(5.4)
=
A
s
+
B
s
2
+
C
s + 1
(5.5)
66
Solving C(s) using Heaviside expansion approach, we get
C(s) = K
_
1
s
2
s
+
2
s + 1
_
(5.6)
Taking the Inverse Laplace transform of the above equation, we get
c(t) = K
_
t + e
t
_
(5.7)
The dierence between the reference and output signal is the error signal e(t).
Therefore,
e(t) = r(t) c(t) (5.8)
e(t) = Kt Kt + K Ke
t
(5.9)
e(t) = K(1 e
t
) (5.10)
Normalizing equation 5.10 for t >> , we get
e(t) = (5.11)
This means that the error in following the ramp input is equal to for large value
of t [7]. Hence, the smaller the time constant , the smaller is the steady state
error.
5.3 Step by step procedure to perform Ramp Test
Change the scilab working directory to Ramp Test folder. Execute the code
ser init.sce and ramp test.sci. Open the Xcos code ramp test.xcos.
Give a ramp input to the system with some value for slope. For this experiment,
we have chosen slope = 0.1. Double click on the ramp input block labled as
Heater input. Put the following values in the respective elds. Slope = 0.1, start
time = 300, initial output = 20. Keep the fan constant at 100. Note that the value
of heater current will not exceed 40 PWM units due to the use of limit blocks.
Running the xcos code may warn you with a message saying No continuous-
time states. Thresholds are ignored. Ignore the message by clicking on OK
button. The data thus obtained is stored using Write to output le Xcos block
as shown in Fig.5.1. The rst column of Table 5.1 denotes time in seconds. The
second column denotes heater current. The third column denotes the fan speed. It
has been held constant at 100 units. The last column denotes the plate temperature.
67
Figure 5.2: Screen shot of Ramp Test Experiment
0.000E+00 0.100E+02 0.100E+03 0.216E+02
0.100E+00 0.100E+02 0.100E+03 0.216E+02
.
.
0.251E+03 0.300E+02 0.100E+03 0.291E+02
0.251E+03 0.300E+02 0.100E+03 0.291E+02
Table 5.1: Ramp data obtained after performing the Ramp Test
68
5.4 Ramp Analysis
After completing the ramp test experiment, let us do the analysis. Change the
directory to Ramp Analysis. Execute the le ramp.sce. On executing this le,
you get the values of Kp, tau and Kp approx and tau approx on the Scilab Con-
sole Window. You will also get a plot of the ramp response calculated using the
equation 5.7 for Kp and tau values.
Figure 5.3: Ramp response for Kp and tau
5.5 Discussion
We summarize our ndings now. The experiment has been performed by vary-
ing the heater current and keeping the fan speed constant. However, the user is
encouraged to try out the experiment using dierent combinations of fan speed
69
and heater current. Negative ramp can also be tried out to make the experiment
more informative. It is not necessary to keep a particular input constant. For ex-
ample, you can try giving a step input to the disturbance signal, i.e., the fan input.
The system can also be treated as a second order system. This consideration is
necessary since it increases the accuracy of the acquired transfer function.[6]
5.6 Conducting Ramp Test on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained
in section 3.4. The required .sce le is ramptest.sce. You will nd this le
in the RampTest directory under virtual folder. Please note that the analysis
code of ramp test data obtained by a virtual experiment is slightly dierent. The
procedure to use the analysis code however remains the same as explained earlier.
To do a rst order analysis, one has to use the le firstorder virtual.sce.
These les are available in the Ramp Analysis folder under the virtual folder.
The necessary codes are listed in the section 5.7.
5.7 Scilab Code
Scilab Code 5.1 ramp test.sci
1 mode( 1) ;
2 gl obal temp he a t f an
3
4 f unc t i on temp = r a mp t e s t ( heat , f an )
5
6 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / I n p u t H e a t e r ,
w r i t e s e r i a l a c c e p t s s t r i n g s ; s o c o n v e r t 2 5 4 i n t o
i t s s t r i n g
e q u i v a l e n t
7 wr i t e s e r i a l ( handl , a s c i i ( he a t ) ) ;
8 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ; / / I n p u t F a n
9 wr i t e s e r i a l ( handl , a s c i i ( f an ) ) ;
10 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ; / / T o r e a d T e m p
11 s l e e p ( 100) ;
12
70
13 temp = a s c i i ( r e a d s e r i a l ( handl ) ) ; / / R e a d s e r i a l
r e t u r n s a s t r i n g , s o
c o n v e r t i t t o
i t s i n t e g e r ( a s c i i ) e q u i v a l e n t
14 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ; / / c o n v e r t t o t e m p w i t h
d e c i m a l p o i n t s
e g : 4 0 . 7
15
16 endf unct i on
Scilab Code 5.2 label.sci
1 mode( 1) ;
2 / / U p d a t e d ( 9 1 2 0 6 ) , w r i t t e n b y I n d e r p r e e t A r o r a
3 / / I n p u t a r g u m e n t s : t i t l e , x l a b e l , y l a b e l a n d t h e i r
f o n t s i z e s
4
5 f unc t i on l a b e l ( tname , t f o n t , l a be l x , l a be l y , xyf ont )
6 a = get ( c u r r e n t a x e s )
7 x t i t l e ( tname , l a be l x , l a b e l y )
8 xgri d
9 t = a . t i t l e ;
10 t . f o n t s i z e = t f o n t ; / / T i t l e f o n t s i z e
11 t . f o n t s t y l e = 2; / / T i t l e f o n t s t y l e
12 t . t e x t = t name ;
13
14 u = a . x l a b e l ;
15 u . f o n t s i z e = xyf ont ; / / L a b e l f o n t s i z e
16 u . f o n t s t y l e = 2; / / L a b e l f o n t s t y l e
17
18 v = a . y l a b e l ;
19 v . f o n t s i z e = xyf ont ; / / L a b e l f o n t s i z e
20 v . f o n t s t y l e = 2; / / L a b e l f o n t s t y l e
21
22 / / a . l a b e l f o n t s i z e = 3 ;
23
24 endf unct i on ;
71
Scilab Code 5.3 cost.sci
1 f unc t i on f = f unc 1 ( x )
2 k = x ( 1 ) ;
3 t a u = x ( 2 ) ;
4 y p r e d i c t i o n = k
*
( t + t a u
*
( exp( t / t a u ) 1) ) ;
5 f = ( norm( y y p r e d i c t i o n , 2 ) ) 2 ;
6 endf unct i on
7
8 f unc t i on [ f , g , i nd1 ] = c o s t ( x , i nd1 )
9 k = x ( 1 ) ;
10 t a u = x ( 2 ) ;
11 y p r e d i c t i o n = k
*
( t + t a u
*
( exp( t / t a u ) 1) ) ;
12 f = ( norm( y y p r e d i c t i o n , 2 ) ) 2 ;
13 g = numdi ff ( f unc 1 , x ) ;
14 endf unct i on
Scilab Code 5.4 cost approx.sci
1 f unc t i on f = f unc a ppr ox ( x )
2 k = x ( 1 ) ;
3 t a u = x ( 2 ) ;
4 y p a ppr ox = k
*
( t a p p r o x t a u ) ;
5 f = ( norm( y appr ox y p appr ox , 2 ) ) 2 ;
6 endf unct i on
7
8 f unc t i on [ f , g , i nd ] = c o s t a p p r o x ( x , i nd )
9 k = x ( 1 ) ;
10 t a u = x ( 2 ) ;
11 y p a ppr ox = k
*
( t a p p r o x t a u ) ;
12 f = ( norm( y appr ox y p appr ox , 2 ) ) 2 ;
13 g = numdi ff ( f unc appr ox , x ) ;
14 endf unct i on
Scilab Code 5.5 ramptest.sci
1 mode ( 0 ) ;
2 f unc t i on [ y , s t op ] = r a mp t e s t ( heat , f an )
72
3 gl obal f df h f d t f n c r fncw m e r r c o u n t s t op
4
5
6 f n c r = s c i l a b r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
7 fncw = s c i l a b wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
8
9 a = mget l ( f dt , 1 ) ;
10 b = e vs t r ( a ) ;
11 byt e = mt e l l ( f d t ) ;
12 mseek ( byt e , f dt , s e t ) ;
13
14 i f a = [ ]
15 t emps = b ( 1 , 4 ) ; h e a t s = b ( 1 , 2 ) ;
16 f a ns = b ( 1 , 3 ) ; y = t emps ;
17
18 i f heat >100
19 he a t = 100;
20 e l s e i f heat <0
21 he a t = 0;
22 end ;
23
24 i f f an >100
25 f an = 100;
26 e l s e i f f an <0
27 f an = 0;
28 end ;
29
30 A = [m, heat , f an , m] ;
31 f df h = f i l e ( open , s c i l a b wr i t e . s ce , unknown ) ;
32 f i l e ( l a s t , f df h )
33 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
34 f i l e ( c l o s e , f df h ) ;
35 m = m+1;
36
37 e l s e
38 y = 0;
73
39 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
40 i f e r r c o u n t > 300
41 di s p ( NO NETWORK COMMUNICATION! ) ;
42 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
43 end
44 end
45
46 ret urn
47 endf unct i on
Scilab Code 5.6 ramptest.sce
1 mode ( 0 ) ;
2 / / F o r s c i l a b 5 . 1 . 1 o r l o w e r v e r s i o n u s e r s , u s e s c i c o s
c o m m a n d t o o p e n s c i c o s d i a g r a m s i n s t e a d o f x c o s
3
4 gl obal f df h f d t f n c r fncw m e r r c o u n t y
5
6 f n c r = s c i l a b r e a d . s ce ;
7 f d t = mopen( f n c r ) ;
8 mseek ( 0 ) ;
9
10 e r r c o u n t = 0; / / i n i t i a l i s i n g e r r o r c o u n t f o r n e t w o r k
e r r o r
11 m =1;
12 exec ( r a mp t e s t . s c i ) ;
13 A = [ 0 . 1 , m, 0 , 1 0 0 ] ;
14 f df h = f i l e ( open , s c i l a b wr i t e . s ce , unknown ) ;
15 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
16 f i l e ( c l o s e , f df h ) ;
17 s l e e p ( 2000) ;
18 a = mget l ( f dt , 1 ) ;
19 mseek ( 0 ) ;
20 i f a = [ ] / / o p e n x c o s o n l y i f c o m m u n i c a t i o n i s
t h r o u g h ( i e r e p l y h a s c o m e f r o m s e r v e r )
21 xcos ( r a mp t e s t . xcos ) ;
22 e l s e
74
23 di s p ( NO NETWORK CONNECTION! ) ;
24 ret urn
25 end
Scilab Code 5.7 ramp virtual.sce
1 mode( 1) ;
2
3 f i l e na me = 16 Feb2012 12 18 19 . t x t ; / / c o m p l e t e p a t h
o f t h e s a v e d d a t a f i l e
4 s l ope = 0 . 1 ; / / c h a n g e t h i s t o t h e s l o p e t h a t y o u h a v e
u s e d i n t h e e x p e r i m e n t
5 i nd1 =3;
6 / / R a m p A n a l y s i s
7 exec ( c o s t a p p r o x . s c i ) ;
8 exec ( c o s t . s c i ) ;
9 exec ( l a b e l . s c i ) ;
10
11 da t a = f scanf Mat ( f i l e na me ) ;
12 t i me = da t a ( : , 5) ;
13 h e a t e r = i nt ( da t a ( : , 2) ) ;
14 f an = i nt ( da t a ( : , 3) ) ;
15 temp = da t a ( : , 4) ;
16
17
18 l e n = l e ngt h ( h e a t e r ) ;
19 h e a t e r s 1 = [ h e a t e r ( 1 ) ; h e a t e r ( 1 : $1) ] ;
20 d e l h e a t = abs ( h e a t e r h e a t e r s 1 ) ;
21 i nd = f i nd ( d e l h e a t >. 5) ;
22
23 t = t i me ( i nd ( 2 ) : i nd ( $1) ) ;
24 H = h e a t e r ( i nd ( 2 ) : i nd ( $1) ) ;
25 T = temp ( i nd ( 2 ) : i nd ( $1) ) ;
26
27 t = t t ( 1 ) ;
28 T = T T( 1 ) ;
29
30 y = T;
75
31 x0 = [ . 5 100]
32 gl obal ( y , t ) ;
33
34 [ f , xopt ] = optim ( cos t , x0 ) ;
35 kp = xopt ( 1 ) / s l ope
36 t a u = xopt ( 2 )
37
38 l e n = l e ngt h ( t ) ;
39 hal f way = c e i l ( l e n / 2 ) ;
40
41 t a p p r o x = t ( hal f way : l e n ) ;
42 y appr ox = y ( hal f way : l e n ) ;
43 gl obal ( y appr ox , t a p p r o x ) ;
44
45 [ f appr ox , xopt a ppr ox ] = optim ( c os t a ppr ox , x0 ) ;
46 kp appr ox = xopt a ppr ox ( 1 ) / s l ope ;
47 t a u a pp r ox = xopt a ppr ox ( 2 ) ;
48
49 / / D i s p l a y a n d P l o t
50 di s p ( kp = ) ;
51 di s p ( kp ) ;
52 di s p ( t a u = ) ;
53 di s p ( t a u ) ;
54 di s p ( kp appr ox = ) ;
55 di s p ( kp appr ox ) ;
56 di s p ( t a u a pp r ox = ) ;
57 di s p ( t a u a pp r ox ) ;
58
59 y p = kp
*
s l ope
*
( t + t a u
*
( exp( t / t a u ) 1) ) ;
60 y p a ppr ox = kp appr ox
*
s l ope
*
( t a p p r o x t a u a ppr ox ) ;
61 y p a ppr ox = y p appr ox ;
62 pl ot 2d ( t , [ y p , T] ) ;
63 l a b e l ( Showing F i r s t Or der Model and Expe r i me nt a l
Re s u l t s f o r kp and t a u , 4 , Time ( s ) , Change i n
Temper at ur e ( Pr e di c t e d , Act ual ) , 4) ;
64 l e ge nd ( [ Pr e d i c t e d ; Act ual ] ) ;
76
Chapter 6
Frequency Response Analysis of a
Single Board Heater System by the
application of Sine Wave
The aim of this experiment is to do a Frequency Response Analysis of a Single
Board Heater System by the application of Sine Wave.The target group is anyone
who has basic knowledge of Control Engineering. We have used Scilab with Xcos
as an interface for sending and receiving data. This interface is shown in Fig.6.1.
Heater current and Fan speed are the two inputs to the system. The Heater current
is varied sinusoidally. A provision is made to set the parameters related to it, like
Frequency, Amplitude and Oset. The temperature prole thus obtained is the
output. In this experiment we are applying a sine change in the heater current
by keeping the Fan speed constant. After application of sine change, wait for
sucient amount of time to allow the temperature to reach a steady-state.
6.1 Theory
Frequency Response of a system means its steady-state response to a sinusoidal
input. For obtaining a Frequency Response of a system, we vary the frequency of
the input signal over a spectrum of interest. The analysis is actually quite useful
and also simple because it can be carried out with the available signal generators
and measuring devices. Consider a sinusoidal input
U(t) = Asint (6.1)
77
Figure 6.1: Xcos for this experiment
78
The Laplace Transform of the above equation yields
U(s) =
A
s
2
+
2
(6.2)
Consider the standard rst order transfer function given below
G(s) =
Y(s)
U(s)
=
K
s + 1
(6.3)
Putting the value of U(s) from equation, we get
Y(s) =
KA
(s + 1)(s
2
+
2
)
(6.4)
=
KA
2
+ 1
_
2
s + 1
s
s
2
+
2
+
s
2
+
2
_
(6.5)
Taking Laplace Inverse, we get
y(t) =
_
KA
2
+ 1
_
_
e
t
cos(t) + sin(t)
_
(6.6)
The above equation has an exponential term e
t
2
+ 1
_
_
sin(t) +
_
(6.7)
where,
= tan
1
() (6.8)
By observing the above equation one can easily make out that for a sinusoidal
input the output is also sinusoidal but has some phase dierence. Also, the ampli-
tude of the output signal,
A, has become a function of the input signal frequency,
.
A =
KA
2
+ 1
(6.9)
79
The amplitude ratio (AR) can be calculated by dividing both sides by the input
signal amplitude A.
AR =
A
A
=
K
2
+ 1
(6.10)
Dividing the above equation by the process gain K yields the normalized ampli-
tude ratio (AR
n
)
AR
n
=
AR
K
=
1
2
+ 1
(6.11)
Because the process steady state gain is constant, the normalized amplitude ratio
often is used for frequency response analysis.[8]
(6.12)
6.2 Step by step procedure to perform Sine Test
Change the current working directory of scilab to the folder Sine Test. Execute
the code ser init.sce and sine test.sci. Open the Xcos code sine test.xcos.
Initiate a sine input to the system by setting Sinusoid generator block properties
with some value of the frequency (here 0.007Hz) and amplitude(hear 10). Note
that at high frequencies the plant output is not sinusoidal,which is not of any use.
Hence,avoid choosing frequencies above 0.04Hz.
Refering to table 6.1 the rst column represents time. The second column rep-
resents heater current. Here, it is sinusoidally varied. The third column represents
fan speed. Note that its value is 100 throughout the experiment. The fourth col-
umn represents the output temperature. It should be taken in to consideration that
all the values mentioned in the data le are in PWM (Pulse Width Modulation)
units, except for the temperature which is in C.
Now let us see the step for calculating Amplitude Ratio and Phase Dierence.
Change the current working directory of scilab to the folder Sine Analysis.
Copy the data les generated after the completion of the experiment in to the
Sine Analysis folder. Input the arguments f and filename in the scilab code
sine2.sce for the calculation of the above said parameters and execute it. Here
f means input frequency. It could be seen from gure 6.3 that the Amplitude
80
Figure 6.2: Plot for sine input 0.007Hz
0.100E+00 0.200E+02 0.100E+03 0.239E+02
0.200E+00 0.201E+02 0.100E+03 0.238E+02
0.300E+00 0.201E+02 0.100E+03 0.238E+02
.
.
.
0.749E+03 0.300E+02 0.100E+03 0.301E+02
0.749E+03 0.300E+02 0.100E+03 0.302E+02
0.749E+03 0.300E+02 0.100E+03 0.302E+02
Table 6.1: Data obtained after application of sine input of 0.04Hz
81
Figure 6.3: Scilab Output
82
Figure 6.4: Plot of Input and Output vs time
83
Figure 6.5: Bode Plot obtained from the plant
Ratio turns out to be 2.047dB and phase dierence to be 60.732. The plot
thus obtained is shown in gure 6.4
Repeat this calculation over a range of frequencies and note down the values
of Amplitude Ratio in dB and Phase Dierence. Input these values for the ap-
propriate frequencies in to the Scilab code BodePlot.sce and execute it to get a
Bode Plot of the plant which is illustrated in gure 6.5.
Bode Plot can be obtained directly from the plants Secon order Transfer func-
tion [6] with the help of scilab code TFbode.sce, as shown in gure 6.6. A
visual comparison of the two bode plots can be done to validate the bode diagram
obtained from the plant.
For comparing above two plots we are plotting it on same graph as shown in
gure 6.7
84
Figure 6.6: Bode Plot obtained through plants Transfer function
85
Figure 6.7: Comparison of Bode Plots
86
6.3 Conducting Sine Test on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained
in section 3.4. The required .sce le is sinetest.sce. You will nd this le
in the SineTest directory under virtual folder. Please note that the analysis
code of sine test data obtained by a virtual experiment is slightly dierent. The
procedure to use the analysis code however remains the same as explained earlier.
To calculate the Amplitude Ratio and Phase Difference, one has to use the
le sine2 virtual.sce. These les are available in the Sine Analysis folder
under the virtual folder. The necessary codes are listed in the section 6.4.
6.4 Scilab Code
Scilab Code 6.1 sine test.sci
1 gl obal temp he a t f an
2
3 f unc t i on temp = s i n e t e s t ( heat , f an )
4
5 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / I n p u t H e a t e r ,
w r i t e s e r i a l a c c e p t s
s t r i n g s ; s o
c o n v e r t 2 5 4 i n t o i t s
s t r i n g
e q u i v a l e n t
6 wr i t e s e r i a l ( handl , a s c i i ( he a t ) ) ;
7 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ; / / I n p u t F a n
8 wr i t e s e r i a l ( handl , a s c i i ( f an ) ) ;
9 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ; / / T o r e a d T e m p
10 s l e e p ( 100) ;
11
12 temp = a s c i i ( r e a d s e r i a l ( handl ) ) ; / / R e a d s e r i a l
r e t u r n s a s t r i n g , s o
c o n v e r t i t t o
i t s i n t e g e r ( a s c i i )
e q u i v a l e n t
87
13 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ; / / c o n v e r t t o t e m p w i t h
d e c i m a l p o i n t s
e g : 4 0 . 7
14
15 endf unct i on
Scilab Code 6.2 sinetest.sce
1 mode ( 0 ) ;
2 / / F o r s c i l a b 5 . 1 . 1 o r l o w e r v e r s i o n u s e r s , u s e s c i c o s
c o m m a n d t o o p e n s c i c o s d i a g r a m s i n s t e a d o f x c o s
3
4 gl obal f df h f d t f n c r fncw m e r r c o u n t y
5
6 f n c r = s c i l a b r e a d . s ce ;
7 f d t = mopen( f n c r ) ;
8 mseek ( 0 ) ;
9
10 e r r c o u n t = 0; / / i n i t i a l i s i n g e r r o r c o u n t f o r n e t w o r k
e r r o r
11 m =1;
12 exec ( s i n e t e s t . s c i ) ;
13 A = [ 0 . 1 , m, 0 , 1 0 0 ] ;
14 f df h = f i l e ( open , s c i l a b wr i t e . s ce , unknown ) ;
15 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
16 f i l e ( c l o s e , f df h ) ;
17 s l e e p ( 2000) ;
18 a = mget l ( f dt , 1 ) ;
19 mseek ( 0 ) ;
20 i f a = [ ] / / o p e n x c o s o n l y i f c o m m u n i c a t i o n i s
t h r o u g h ( i e r e p l y h a s c o m e f r o m s e r v e r )
21 xcos ( s i n e t e s t . xcos ) ;
22 e l s e
23 di s p ( NO NETWORK CONNECTION! ) ;
24 ret urn
25 end
Scilab Code 6.3 sinetest.sci
88
1 mode ( 0 ) ;
2 f unc t i on [ y , s t op ] = s i n e t e s t ( heat , f an )
3 gl obal f df h f d t f n c r fncw m e r r c o u n t s t op
4
5
6 f n c r = s c i l a b r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
7 fncw = s c i l a b wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
8
9 a = mget l ( f dt , 1 ) ;
10 b = e vs t r ( a ) ;
11 byt e = mt e l l ( f d t ) ;
12 mseek ( byt e , f dt , s e t ) ;
13
14 i f a = [ ]
15 t emps = b ( 1 , 4 ) ; h e a t s = b ( 1 , 2 ) ;
16 f a ns = b ( 1 , 3 ) ; y = t emps ;
17
18 i f heat >100
19 he a t = 100;
20 e l s e i f heat <0
21 he a t = 0;
22 end ;
23
24 i f f an >100
25 f an = 100;
26 e l s e i f f an <0
27 f an = 0;
28 end ;
29
30 A = [m, heat , f an , m] ;
31 f df h = f i l e ( open , s c i l a b wr i t e . s ce , unknown ) ;
32 f i l e ( l a s t , f df h )
33 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
34 f i l e ( c l o s e , f df h ) ;
35 m = m+1;
36
89
37 e l s e
38 y = 0;
39 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
40 i f e r r c o u n t > 300
41 di s p ( NO NETWORK COMMUNICATION! ) ;
42 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
43 end
44 end
45
46 ret urn
47 endf unct i on
Scilab Code 6.4 sine2.sce
1 mode ( 0 ) ;
2 f i l e na me= s i ne 001 ; / / E n t e r t h e d a t a f i l e n a m e i n
s i n g l e q u o t e s
3 f =0. 001; / / E n t e r t h e f r e q u e n c y
4 da t a 7=f scanf Mat ( f i l e na me ) ;
5 exec ( l a b e l b o d e . s c i ) ;
6 T = da t a 7 ( : , 1 ) ; f an = da t a 7 ( : , 3 ) ; / / T i s t i m e , f a n i s
f a n s p e e d
7 u = da t a 7 ( : , 2 ) da t a 7 ( 1 , 2 ) ; y = da t a 7 ( : , 4 ) da t a 7 ( 1 , 4 ) ;
/ / u i s c u r r e n t , y i s t e m p e r a t u r e
8 pe r i od=c e i l ( 1 / f ) ;
9 p=l e ngt h ( u ) ;
10 s ampl i ng = T( 3 ) T( 2 ) ; / / s a m p l i n g t i m e
11 i ndex = round ( ( p e r i o d ) / s ampl i ng ) ; / / c a l c u l a t i n g t h e
d u r a t i o n o f l a s t c y c l e o f w a v e f o r m
12 t i me s =T( $i ndex : $ ) ;
13 temp = y ( $i ndex : $ ) ; / / o u t p u t f o r l a s t c y c l e
14 h e a t e r = u ( $i ndex : $ ) ; / / i n p u t f o r l a s t c y c l e
15 [ max heat er , p o i n t e r 1 ] = max( h e a t e r ) ; / / d e t e r m i n i n g m a x
a m p l i t u d e a n d i n d e x f o r l a s t c y c l e o f i n p u t ( i n d e x
i s r e l a t i v e t o l a s t c y c l e )
16 [ max temp , p o i n t e r 2 ] = max( temp ) ; / / d e t e r m i n i n g m a x
a m p l i t u d e a n d i n d e x f o r l a s t c y c l e o f i n p u t ( i n d e x
90
i s r e l a t i v e t o l a s t c y c l e )
17 p o i n t e r 1 = p o i n t e r 1 + ( pi ndex ) ; / / c o n v e r s i o n o f i n d e x
f o r i n p u t i n t e r m s o f c o m p l e t e d a t a p e r i o d
18 p o i n t e r 2 = p o i n t e r 2 + ( pi ndex ) ; / / c o n v e r s i o n o f i n d e x
f o r o u t p u t i n t e r m s o f c o m p l e t e d a t a p e r i o d
19 Ampl i t ude r a t i o dB = 20
*
l og10 ( y ( p o i n t e r 2 ) / u ( p o i n t e r 1 ) )
/ / T o f i n d g a i n i n d B
20 P h a s e d i f f e r e n c e = 360
*
f
*
( poi nt e r 1 p o i n t e r 2 )
*
s ampl i ng
/ / p h a s e d i f f e r e n c e i n d e g r e e s
21 / / P h a s e d i f f e r e n c e = ( ( p o i n t e r 1 p o i n t e r 2 ) / ( 1 / f ) )
*
3 6 0
22
23 pl ot 2d ( T , [ u y ] ) ;
24 l a b e l ( Pl o t of s i n e i n p u t i n h e a t e r and t he
c or r e s pondi ng t e mp e r a t u r e p r o f i l e , 4 , Time ( s ) ,
Change i n t e mp e r a t u r e and h e a t e r , 4) ;
25 / / l e g e n d ( [ H e a t e r ; T e m p e r a t u r e ] ) ;
Scilab Code 6.5 label.sci
1 / / U p d a t e d ( 9 1 2 0 6 ) , w r i t t e n b y I n d e r p r e e t A r o r a
2 / / I n p u t a r g u m e n t s : t i t l e , x l a b e l , y l a b e l a n d t h e i r
f o n t s i z e s
3 f unc t i on l a b e l ( tname , t f o n t , l a be l x , l a be l y , xyf ont )
4 a = get ( c u r r e n t a x e s )
5 x t i t l e ( tname , l a be l x , l a b e l y )
6 xgri d
7 t = a . t i t l e ;
8 t . f o n t s i z e = t f o n t ; / / T i t l e f o n t s i z e
9 t . f o n t s t y l e = 2; / / T i t l e f o n t s t y l e
10 t . t e x t = t name ;
11 u = a . x l a b e l ;
12 u . f o n t s i z e = xyf ont ; / / L a b e l f o n t s i z e
13 u . f o n t s t y l e = 2; / / L a b e l f o n t s t y l e
14 v = a . y l a b e l ;
15 v . f o n t s i z e = xyf ont ; / / L a b e l f o n t s i z e
16 v . f o n t s t y l e = 2; / / L a b e l f o n t s t y l e
17 / / a . l a b e l f o n t s i z e = 3 ;
18 endf unct i on ;
91
Scilab Code 6.6 bodeplot.sce
1 / / b o d e p l o t
2 exec ( l a b e l b o d e . s c i ) ;
3 x =[ 0 . 0 0 1 , 0 . 0 0 3 5 , 0 . 0 0 4 , 0 . 0 0 5 , 0 . 0 0 6 , 0 . 0 0 7 , . . .
4 0 . 0 0 8 , 0 . 0 0 9 , 0 . 0 1 , 0 . 0 2 , 0 . 0 3 , 0 . 0 4 ] ; / / I n p u t f r e q u e n c y ( H z )
5 y =[ 3. 87 , 5. 67 , 7. 53 , 7. 53 , 8. 17 , 8. 64 , . . .
6 8. 87 , 8. 90 , 9. 11 , 13. 55 , 15. 39 , 16. 47] ; / / A m p l i t u d e
r a t i o ( d B )
7 s ubpl ot ( 2 , 1 , 1 ) ;
8 pl ot 2d ( x , y , r e c t =[ 0. 001 , 20 , 0. 04 , 0] , l o g f l a g = l n ) ;
9 xgri d ( ) ;
10 y =[ 25. 2 , 28. 98 , 33. 11 , 41. 4 , 60. 48 , 70. 56 , . . .
11 77. 76 , 87. 48 , 90 , 129. 6 , 151. 2 , 172. 8] ; / / P h a s e
d i f f e r e n c e ( d e g r e e )
12 t i t l e =
13 l a b e l ( t i t l e , 4 , Hz , Ampl i t ude r a t i o i n dB , 4) ;
14 s ubpl ot ( 2 , 1 , 2 ) ;
15 pl ot 2d ( x , y , r e c t =[ 0. 001 , 180 , 0. 04 , 20] , l o g f l a g = l n ) ;
16 l a b e l ( t i t l e , 4 , , Phase d i f f e r e n c e , 4) ;
17 s ubpl ot ( 2 , 1 , 2 ) ;
18 xgri d ( ) ;
19
20 / / s = p o l y ( 0 , s )
21 / / h = s y s l i n ( c , ( 0 . 4 7 5 / ( 1 2 4 . 8 2 7
*
s 2 + 5 7 . 2 6
*
s + 1 ) ) )
22 / / b o d e ( h , 0 . 0 0 1 , 0 . 0 4 ) ;
Scilab Code 6.7 labelbode.sci
1 / / U p d a t e d ( 9 1 2 0 6 ) , w r i t t e n b y I n d e r p r e e t A r o r a
2 / / I n p u t a r g u m e n t s : t i t l e , x l a b e l , y l a b e l a n d t h e i r
f o n t s i z e s
3
4 f unc t i on l a b e l ( tname , t f o n t , l a be l x , l a be l y , xyf ont )
5 a = get ( c u r r e n t a x e s )
6 x t i t l e ( tname , l a be l x , l a b e l y )
92
7 xgri d
8 t = a . t i t l e ;
9 t . f o n t s i z e = t f o n t ; / / T i t l e f o n t s i z e
10 t . f o n t s t y l e = 2; / / T i t l e f o n t s t y l e
11 t . t e x t = t name ;
12 u = a . x l a b e l ;
13 u . f o n t s i z e = xyf ont ; / / L a b e l f o n t s i z e
14 u . f o n t s t y l e = 2; / / L a b e l f o n t s t y l e
15 v = a . y l a b e l ;
16 v . f o n t s i z e = xyf ont ; / / L a b e l f o n t s i z e
17 v . f o n t s t y l e = 2; / / L a b e l f o n t s t y l e
18 / / a . l a b e l f o n t s i z e = 3 ;
19 endf unct i on ;
Scilab Code 6.8 TFbode.sce
1 s=pol y ( 0 , s )
2 dt =10; / / d e l a y t i m e
3 / / h = s y s l i n ( c , ( ( 0 . 5 1 0 / ( 6 5 . 4 9
*
s + 1 ) ) ) ) / / t r a n s f e r
f u n c t i o n u s i n g f i r s t o r d e r p a d e a p p r o x i m a t i o n
4 t f =( ( 0 . 4 7 5 / ( 3 6
*
s +1) )
*
( ( dt / 2 )
*
s +1/ ( dt / 2 )
*
s +1) ) ;
5 bode ( h , 0 . 0 0 1 , 1 0 ) ;
Scilab Code 6.9 comparison.sce
1 s=pol y ( 0 , s ) ;
2 f r q = [ 0 . 0 0 1 , 0 . 0 0 3 5 , 0 . 0 0 4 , 0 . 0 0 5 , 0 . 0 0 6 , 0 . 0 0 7 , . . .
3 0 . 0 0 8 , 0 . 0 0 9 , 0 . 0 1 , 0 . 0 2 , 0 . 0 3 , 0 . 0 4 ] ; / / I n p u t f r e q u e n c y ( H z )
4 dt =10; / / d e l a y t i m e
5
6 t f =( ( 0 . 4 7 5 / ( 3 6
*
s +1) )
*
( ( dt / 2 )
*
s +1/ ( dt / 2 )
*
s +1) ) ; / /
t r a n s f e r f u n c t i o n u s i n g p a d e a p p r o x i m a t i o n
7 h=s y s l i n ( c , t f ) ;
8
9 [ f r q1 , r ep ]= repf req ( h , f r q ) ;
10 [ dB1 , phi 1 ]=dbphi ( r ep ) ;
11 t i t l e = From a c t u a l p l a n t da t a ;
12 dB = [ 3. 87 , 5. 67 , 7. 53 , 7. 53 , 8. 17 , 8. 64 , . . .
93
13 8. 87 , 8. 90 , 9. 11 , 13. 55 , 15. 39 , 16. 47] ; / / A m p l i t u d e
r a t i o ( d B )
14 phi = [ 25. 2 , 28. 98 , 33. 11 , 41. 4 , 60. 48 , . . .
15 70. 56 , 77. 76 , 87. 48 , 90 , 129. 6 , 151. 2 , 172. 8] ; / / P h a s e
d i f f e r e n c e ( d e g r e e )
16 bode ( [ f r q ] , [ dB; dB1 ] , [ phi ; phi 1 ] )
17 l e ge nd ( [ Pl a n t da t a ; $\ f r a c { 0 . 4 2 } { 3 6 s +1} e { 10 s } $ ] )
18
19 / / t r a n s f e r f u n c t i o n u s i n g p a d e a p p r o x i m a t i o n
Scilab Code 6.10 sine2 virtual.sce
1 mode ( 0 ) ;
2 f i l e na me= 16 Feb2012 17 28 57 . t x t ; / / E n t e r t h e d a t a
f i l e n a m e i n s i n g l e q u o t e s
3 f =0. 01; / / E n t e r t h e f r e q u e n c y
4 da t a 6=f scanf Mat ( f i l e na me ) ;
5 da t a 7=da t a 6 ( 2 : $ , : ) ;
6 exec ( l a b e l b o d e . s c i ) ;
7 T = da t a 7 ( : , 5 ) ; f an = da t a 7 ( : , 3 ) ; / / T i s t i m e , f a n i s
f a n s p e e d
8 u = da t a 7 ( : , 2 ) da t a 7 ( 1 , 2 ) ; y = da t a 7 ( : , 4 ) da t a 7 ( 1 , 4 ) ;
/ / u i s c u r r e n t , y i s t e m p e r a t u r e
9 pe r i od=c e i l ( 1 / f ) ;
10 p=l e ngt h ( u ) ;
11 s ampl i ng = T( 3 ) T( 2 ) ; / / s a m p l i n g t i m e
12 s ampl i ng = s ampl i ng / 1 000;
13 i ndex = round ( ( p e r i o d ) / s ampl i ng ) ; / / c a l c u l a t i n g t h e
d u r a t i o n o f l a s t c y c l e o f w a v e f o r m
14 t i me s =T( $i ndex : $ ) ;
15 temp = y ( $i ndex : $ ) ; / / o u t p u t f o r l a s t c y c l e
16 h e a t e r = u ( $i ndex : $ ) ; / / i n p u t f o r l a s t c y c l e
17 [ max heat er , p o i n t e r 1 ] = max( h e a t e r ) ; / / d e t e r m i n i n g m a x
a m p l i t u d e a n d i n d e x f o r l a s t c y c l e o f i n p u t ( i n d e x
i s r e l a t i v e t o l a s t c y c l e )
18 [ max temp , p o i n t e r 2 ] = max( temp ) ; / / d e t e r m i n i n g m a x
a m p l i t u d e a n d i n d e x f o r l a s t c y c l e o f i n p u t ( i n d e x
i s r e l a t i v e t o l a s t c y c l e )
94
19 p o i n t e r 1 = p o i n t e r 1 + ( pi ndex ) ; / / c o n v e r s i o n o f i n d e x
f o r i n p u t i n t e r m s o f c o m p l e t e d a t a p e r i o d
20 p o i n t e r 2 = p o i n t e r 2 + ( pi ndex ) ; / / c o n v e r s i o n o f i n d e x
f o r o u t p u t i n t e r m s o f c o m p l e t e d a t a p e r i o d
21 Ampl i t ude r a t i o dB = 20
*
l og10 ( y ( p o i n t e r 2 ) / u ( p o i n t e r 1 ) )
/ / T o f i n d g a i n i n d B
22 P h a s e d i f f e r e n c e = 360
*
f
*
( poi nt e r 1 p o i n t e r 2 )
*
s ampl i ng
/ / p h a s e d i f f e r e n c e i n d e g r e e s
23 / / P h a s e d i f f e r e n c e = ( ( p o i n t e r 1 p o i n t e r 2 ) / ( 1 / f ) )
*
3 6 0
24
25 pl ot 2d ( T , [ u y ] ) ;
26 l a b e l ( Pl o t of s i n e i n p u t i n h e a t e r and t he
c or r e s pondi ng t e mp e r a t u r e p r o f i l e , 4 , Time ( s ) ,
Change i n t e mp e r a t u r e and h e a t e r , 4) ;
27 / / l e g e n d ( [ H e a t e r ; T e m p e r a t u r e ] ) ;
95
Chapter 7
Controlling Single Board Heater
System by PID controller
The aim of this experiment is to apply a PID controller to the single board heater
system. The target group is anyone who has basic knowledge of Control Engi-
neering. We have used Scilab with Xcos as an interface for sending and receiving
data. This interface is shown in Fig.7.1. Heater current and fan speed are the two
inputs for this system. They are given in PWM units.A provision is made to set
the parameters related to PID controller (K,
i
,
d
) in Xcos.In this experiment we
keep the Fan speed constant. The output temperature prole, read by the sensor,
is also plotted. The data acquired in the process is stored on the local drive and is
available to the user for further calculations.
7.1 Theory
A PID controller is one which tries to minimize the error between measured vari-
able and the set point by calculating the error and then putting a suitable corrective
action. Note that the output of interest of a process is called the measured variable
or process variable, the dierence between the set point and the measured variable
is called the error and the control action taken to adjust the process is called the
manipulated variable. A PID controller does not simply add or subtract the control
action but instead it manipulates it using three distinct control features, namely,
Proportional, Integral and Derivative. Thus, a PID controller has three separate
parameters.
96
Figure 7.1: Xcos interface for this experiment
7.1.1 Proportional Control Action
This parameter generates a control action based on the current value of the er-
ror. In a more simplied sense, if the error is +2, the control action is -2. The
proportional action can be generated by multiplying the error with a Proportional
constant K
p
. Mathematical representation of the same is given below,
P = K
p
e(t) (7.1)
where,
P is the proportional output
K
p
is the proportional gain
e(t) is the error signal
The value of K
p
is very important. A large value of K
p
may lead to instability
of the system. In contrast, a smaller value of K
P
may decrease the controllers
sensitivity towards error. The problem involved in using only Proportional action
is that, the control action will never settle down to its target value and will always
retain a steady-state error.
97
7.1.2 Integral Control Action
This parameter generates a control action depending on the history of errors. It
means that the action is based on the sum of the recent errors. It is proportional
to both the magnitude as well as duration of the error. The summation of the error
over a period of time gives a value of the oset that should have been corrected
previously. The integral action can thus be generated by multiplying this accumu-
lated error with an integral gain K
i
. Mathematical representation of the same is
given below.
I = K
i
_
t
0
e(t)dt (7.2)
where,
I is the integral output
K
i
is the integral gain (K
i
= K
p
/
i
, where,
i
is the integral time)
The integral action tends to accelerate the control action. However, since it
looks only at the past values of the error, there is always a possibility of it causing
the present values to overshoot the set point values.
7.1.3 Derivative Control Action
As the name suggests, a derivative parameter generates a control action by calcu-
lating the rate of change of error. A derivative action is thus generated by multi-
plying the value of rate of change of error with a derivative gain K
d
. Mathematical
representation of the same is given below.
D = K
d
d
dt
e(t) (7.3)
where,
D is the derivative output
K
d
is the derivative gain (K
d
= K
p
/
d
, where,
d
is the derivative time)
The derivative action slows down the rate of change of the controller output. A
derivative controller is quite useful when the error is continuously changing with
time. One should, however, avoid using it alone. This is because there is no output
when the error is zero and when the rate of change of error is constant.
When all the above control actions are summed up and used together, the nal
98
equation becomes
PID = Ke(t) + K
i
_
t
0
e(t)dt + K
d
d
dt
e(t) (7.4)
The above equation represents an ideal form of PID controller. This means that
the integral controller was used independently. However, it is not a good deci-
sion since, the integral action begins only after the error exits for some amount
of time. The proportional controller however begins as soon as the error starts
existing. Hence, the integral controller is often used in conjunction with a pro-
portional controller. This is popularly known as PI controller and the equation for
Proportional Integral action becomes,
PI = K
p
e(t) +
_
K
p
/
i
_
_
t
0
e(t)dt (7.5)
= K
p
_
e(t) + (1/
i
)
_
t
0
e(t)dt
_
(7.6)
Similarly, as discussed before, independent use of derivative controller is also not
desirable. Moreover, if the process contains high frequency noise then the deriva-
tive action will tend to amplify the noise. Hence, derivative controller is also
used in conjunction with Proportional or Proportional Integral controller popu-
larly known as PD or PID, respectively. Therefore the equation for Proportional
Derivative action becomes,
PD = K
p
e(t) + K
p
d
d
dt
e(t) (7.7)
= K
p
_
e(t) +
d
d
dt
e(t)
_
(7.8)
Finally, writing the equation for PID controller,
PID = K
_
e(t) +
1
i
_
t
0
e(t)dt +
d
d
dt
e(t)
_
(7.9)
7.2 Ziegler-Nichols Rule for Tuning PIDControllers
There are many rules to tune a PID controller. We shall see the two popular
methods suggested by Ziegler-Nichols.
99
Figure 7.2: Reaction Curve[5]
7.2.1 First Method
Ziegler-Nichols give rule for determining the values of gain K, integral time
i
and
derivative time
d
based on the step response characteristics of a given plant.In this
method one can experimentally obtain the response of a plant to a step input, as
shown in gure 7.2. This method is applicable only when the response to the step
input exhibits S-shaped curve.[7]
As shown in gure 7.2, by drawing the tangent line at the inection point and
determining the intersection of the tangent line with the time axis and the line
c(t) = K ,we get two constants, namely, delay time L and time constant T.
Ziegler and Nichols suggested to set the values of K,
i
,
d
according the for-
mula shown in table 7.1. Notice that the PID controller tuned by the Ziegler-
100
Type of controller K
i
d
P
1
RL
0
PI
0.9
RL
3L 0
PID
1.2
RL
2L 0.5L
Table 7.1: Ziegler-Nichols tuning rule based on step response of plant
Nichols rule gives,
G
c
(s) = K
p
_
1 +
1
T
i
s
+ T
d
s
_
(7.10)
= 1.2
T
L
_
1 +
1
2Ls
+ 0.5Ls
_
(7.11)
= 0.6T
_
s +
1
L
_
2
s
(7.12)
Thus the PID controller has a pole at the origin and double zeros at s = 1/L.
7.2.2 Second Method
The second method is also known as instability method[5]. This is a closed loop
method in which the Integral and Derivative gains of the PID controller are made
zero with a unity value for proportional gain. A setpoint change is made and the
temperature prole is observed for some time. The temperature would most likely
maintain a steady-state with some oset. The gain is increased to a next distinct
value (say 2) with a change in the setpoint. The procedure is repeated until the
temperature rst varies with sustained oscillations. It is necessary that the output
(temperature) should have neither under damped nor over damped oscillations.
At this particular frequency of sustained oscillations, the corresponding value of
K
p
is noted and is called as the critical gain K
cr
. The corresponding period of
oscillation is known as P
cr
. Refer Fig. 7.3.
The various P, PI and PID parameters are then calculated with the help of table
7.2.
101
Figure 7.3: Ziegler-Nichols instability tuning method
Type of controller K
i
d
P 0.5K
u
0
PI 0.45K
u
1
0.2
P
u
0
PID 0.6K
u
0.5P
u
0.125P
u
Table 7.2: Ziegler-Nichols tuning rule for instability tuning method
102
Figure 7.4: Refer Step Testexperiment[6]
Using the Ziegler-Nichols rst method explained earlier, the following values
were obtained. Refer gure 7.4.
L = 6 s
T = 193 s
For PI
K = 6.031
i
= 18
For PID
K = 8
i
= 12
d
= 3
While performing the experiment ne tunning of K,
i
,
d
may be required.
103
Figure 7.5: Xcos for PI controller available as pi.xcos
7.3 Implementing PI controller using Trapezoidal
Approximation
Fig.7.5 shows Xcos for implementing PI controller. The PI controller in continu-
ous time is given by
u(t) = K
_
e(t) +
1
i
_
t
0
e(t)dt
_
(7.13)
On taking the Laplace transforms,we obtain
u(t) = K
_
1 +
1
i
s
_
e(t) (7.14)
By mapping controller given in Eq.7.14 to the discrete time domain using trape-
zoidal approximation
u(n) = K
_
1 +
T
s
2
i
z + 1
z 1
_
e(n) (7.15)
104
On cross multiplying,we obtain
(z 1)u(n) = K
_
(z 1) +
T
s
2
i
(z + 1)
_
e(n) (7.16)
We divide by z and then by using shifting theorem we obtain
u(n) u(n 1) = K
_
e(n) e(n 1) +
T
s
2
i
e(n) +
T
s
2
i
e(n 1)
_
(7.17)
The PI controller is usually written as
u(n) = u(n 1) + s
0
e(n) + s
1
e(n 1) (7.18)
Where
s
0
= K
_
1 +
T
s
2
i
_
(7.19)
s
1
= K
_
1 +
T
s
2
i
_
(7.20)
For implementing above PI controller, scilab code is given in pi ta.sci le,
listed at the end of this document. Change the current working directory to the
folder pid controller. Execute the le ser init.sce with the appropriate
com port number and then execute the le pi ta.sci for loading the function.
Run the xcos le pi ta.xcos. Output of Xcos is shown in below g.7.6. Figure
shows three plots. First subplot shows Setpoint and output temperature prole.
Second sub plot shows control eort and third subplot shows error between set-
point and plant output.
7.3.1 Implementing PI controller using Trapezoidal Approxi-
mation on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained in
section 3.4. The required .sce le is pi ta virtual.sce. You will nd this le
in the pid controller directory under virtual folder. The necessary codes are
listed in the section 7.9
105
Figure 7.6: PI controller (Trapezoidal Approximation) output
7.4 Implementing PI controller using Backward Dif-
ference Approximation
The PI controller in continuous time is given by
u(t) = K
_
e(t) +
1
i
_
t
0
e(t)dt
_
(7.21)
On taking the Laplace transform,we obtain
u(t) = K
_
1 +
1
i
s
_
e(t) (7.22)
By mapping controller given in Eq.7.22 to the discrete time domain using Back-
ward dierence approximation :
u(n) = K
_
1 +
T
s
i
z
z 1
_
e(n) (7.23)
On cross multiplying,we obtain
(z 1)u(n) = K
_
(z 1) +
T
s
i
(z)
_
e(n) (7.24)
106
Figure 7.7: PI controller (Backward Dierence Approximation) output
We divide by z and then by using shifting theorem we obtain
u(n) u(n 1) = K
_
e(n) e(n 1) +
T
s
i
e(n)
_
(7.25)
The PI controller is usually written as
u(n) = u(n 1) + s
0
e(n) + s
1
e(n 1) (7.26)
Where
s
0
= K
_
1 +
T
s
i
_
(7.27)
s
1
= K (7.28)
For implementing above PI controller scilab code is given in pi bda.sci le,
listed at the end of this document. Change the current working directory to the
folder pid controller. Execute the le ser init.sce with the appropriate
com port number and then execute the le pi bda.sci for loading the function.
Run the xcos le pi bda.xcos.Output of Xcos is shown in below g.7.7 Figure
shows three plots. First subplot shows Setpoint and output temperature prole.
Second sub plot shows control eort and third subplot shows error between set-
point and plant output.
107
7.4.1 Implementing PI controller using Backward Dierence
Approximation on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained in
section 3.4. The required .sce le is pi bda virtual.sce. You will nd this le
in the pid controller directory under virtual folder. The necessary codes are
listed in the section 7.9
7.5 Implementing PI controller using Forward Dif-
ference Approximation
The PI controller in continuous time is given by
u(t) = K
_
e(t) +
1
i
_
t
0
e(t)dt
_
(7.29)
On taking the Laplace transforms,we obtain
u(t) = K
_
1 +
1
i
s
_
e(t) (7.30)
By mapping controller given in Eq.7.30 to the discrete time domain using forward
dierence formula :
u(n) = K
_
1 +
T
s
i
1
z 1
_
e(n) (7.31)
On cross multiplying,we obtain
(z 1)u(n) = K
_
(z 1) +
T
s
i
_
e(n) (7.32)
We divide by z and then by using shifting theorem we obtain
u(n) u(n 1) = K
_
e(n) e(n 1) +
T
s
i
e(n 1)
_
(7.33)
108
Figure 7.8: PI controller implementation (forward dierence approximation)
The PI controller is usually written as
u(n) = u(n 1) + s
0
e(n) + s
1
e(n 1) (7.34)
Where
s
0
= K (7.35)
s
1
= K
_
1 +
T
s
i
_
(7.36)
For implementing above PI controller scilab code is given in pi fda.sci le,
listed at the end of this document. Change the current working directory to the
folder pid controller. Execute the le ser init.sce with the appropriate
com port number and then execute the le pi fda.sci for loading the function.
Run the xcos le pi fda.xcos.Output of Xcos is shown in below g.7.8 Figure
shows three plots. First subplot shows Setpoint and output temperature prole.
Second sub plot shows control eort and third subplot shows error between set-
point and plant output.
7.5.1 Implementing PI controller using forward Dierence Ap-
proximation on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained in
section 3.4. The required .sce le is pi fda virtual.sce. You will nd this le
109
Figure 7.9: Xcos for PID controller available as pid bda.xcos
in the pid controller directory under virtual folder. The necessary codes are
listed in the section 7.9
7.6 Implementing PID controller using Backward
dierence approximation
Fig.7.9 shows Xcos for implementing PID controller . The PID controller in con-
tinuous time is given by
u(t) = K
_
e(t) +
1
i
_
t
0
e(t)dt +
d
de(t)
dt
_
(7.37)
On taking the Laplace transforms,we obtain
u(t) = K
_
1 +
1
i
s
+
d
s
_
e(t) (7.38)
110
By mapping controller given in Eq.7.38 to the discrete time domain using back-
ward dierence formula :
u(n) = K
_
1 +
T
s
i
z
z 1
+
d
T
s
z 1
z
_
e(n) (7.39)
On cross multiplying,we obtain
(z
2
z)u(n) = K
_
(z
2
z) +
T
s
i
z
2
+
d
T
s
(z 1)
2
_
e(n) (7.40)
We divide by z
2
and by using shifting theorem we obtain
u(n) u(n 1) = K
_
e(n) e(n 1) +
T
s
i
e(n)
+
d
T
s
[e(n) 2e(n 1) + e(n 2)]
_
(7.41)
The PID controller is usually written as
u(n) = u(n 1) + s
0
e(n) + s
1
e(n 1) + s
2
e(n 2) (7.42)
Where
s
0
= K
_
1 +
T
s
i
+
d
T
s
_
(7.43)
s
1
= K
_
1 2
d
T
s
_
(7.44)
s
2
= K
_
d
T
s
_
(7.45)
For implementing above PID controller scilab code is given in pid bda.sci
le,listed at the end of this document. Change the current working directory to
the folder pid controller. Execute the le ser init.sce with the appropriate
com port number and then execute the le pid bda.sci for loading the func-
tion. Run the xcos le pid dda.xcos. Output of Xcos is shown in below g.7.10
Figure shows three plots. First subplot shows Setpoint and output temperature
prole. Second sub plot shows control eort and third subplot shows error be-
tween setpoint and plant output.
111
Figure 7.10: PID controller (Backward Dierence Approximation) Output
7.6.1 Implementing PID controller using backward Dierence
Approximation on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained in
section 3.4. The required .sce le is pid bda virtual.sce. You will nd this
le in the pid controller directory under virtual folder. The necessary codes
are listed in the section 7.9
7.7 Implementing PID controller using trapezoidal
approximation for integral mode and Backward
dierence approximation for the derivative mode
The PID controller in continuous time is given by
u(t) = K
_
e(t) +
1
i
_
t
0
e(t)dt +
d
de(t)
dt
_
(7.46)
112
On taking the Laplace transforms,we obtain
u(t) = K
_
1 +
1
i
s
+
d
s
_
e(t) (7.47)
By mapping controller given in Eq.7.47 to the discrete time domain using trape-
zoidal approximation for integral mode and Backward dierence approximation
for the derivative mode
u(n) = K
_
1 +
T
s
2
i
z + 1
z 1
+
d
T
s
z 1
z
_
e(n) (7.48)
On cross multiplying,we obtain
(z
2
z)u(n) = K
_
(z
2
z) +
T
s
2
i
(z
2
+ z)
d
T
s
(z 1)
2
_
e(n) (7.49)
We divide by z
2
and then by using shifting theorem we obtain
u(n) u(n 1) = K
_
e(n) e(n 1) +
T
s
2
i
e(n) + e(n 1)
+
d
T
s
[e(n) 2e(n 1) + e(n 2)]
_
(7.50)
The PID controller is usually written as
u(n) = u(n 1) = s
0
e(n) + s
1
e(n 1) + s
2
e(n 2) (7.51)
Where
s
0
= K
_
1 +
T
s
2
i
+
d
T
s
_
(7.52)
s
1
= K
_
1 +
T
s
2
i
2
d
T
s
_
(7.53)
s
2
= K
d
T
s
(7.54)
For implementing above PID controller scilab code is given in pid ta bda.sci
le,listed at the end of this document. Change the current working directory to
113
Figure 7.11: PID controller (TA - BDA) implementation
the folder pid controller. Execute the le ser init.sce with the appropri-
ate com port number and then execute the le pid ta bda.sci for loading the
function. Run the xcos le pid ta bda.xcos. Output of Xcos is shown in below
g.7.11 Figure shows three plots. First subplot shows Setpoint and output temper-
ature prole. Second sub plot shows control eort and third subplot shows error
between setpoint plant output i.e. temperature.
7.7.1 Implementing PID controller using trapezoidal approx-
imation for integral mode and Backward dierence ap-
proximation for the derivative mode on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained in
section 3.4. The required .sce le is pid ta bda virtual.sce. You will nd
this le in the pid controller directory under virtual folder. The necessary
codes are listed in the section 7.9
Due to the introduction of derivative action control eort shows lots of uctua-
tions. By using ltered form of PID we can make derivative mode implementable.
114
Figure 7.12: Xcos for PID controller with ltering available as pidN.xcos
7.8 Implementing PID controller with ltering us-
ing Backward dierence approximation
Fig.7.12 shows Xcos for implementing PID controller with ltering
PID ltered form is given by
u(t) = K
_
1 +
1
i
s
+
d
s
1 +
d
s
N
_
e(t) (7.55)
Where N is large number,of the order of 100. By maping controller given in
Eq.7.55 to the discrete time domain using backward dierence formula :
u(n) = K
_
_
1 +
T
s
i
1
1 z
1
+
d
(1 z
1
)
1 +
d
(1z
1
)
N
_
_
e(n) (7.56)
u(n) = K
_
1 +
T
s
i
1
1 z
1
+
Nr
1
(1 z
1
)
1 + r
1
z
1
_
e(n) (7.57)
115
Where
r
1
=
d
N
d
N
+ T
s
(7.58)
On cross multiplying,we obtain
(1 z
1
)(1 + r
1
z
1
)u(n) = K[(1 z
1
)(1 + r
1
z
1
)
+
T
s
i
(1 + r
1
z
1
) +
d
T
s
(1 z
1
)
2
]e(n) (7.59)
Simplifying and then by using shifting theorem we obtain
u(n) + (r
1
1)u(n 1)
r
1
u(n 2) = K
_
1 +
T
s
i
Nr
1
_
e(n)
+ K
_
r
1
(1 +
T
s
i
+ 2N) 1
_
e(n 1)
K [r
1
(1 + N)] e(n 2) (7.60)
hence
u(n) = r
1
u(n 2) (r
1
1)u(n 1)
+ s
0
e(n) + s
1
e(n 1) + s
2
e(n 2) (7.61)
Where
s
0
= K
_
1 +
T
s
i
Nr
1
_
(7.62)
s
1
= K
_
r
1
(1 +
T
s
i
+ 2N) 1
_
(7.63)
s
2
= K [r
1
(1 + N)] (7.64)
For implementing above PID controller scilab code is given in pid filter.sci
le,listed at the end of this document. Change the current working directory to
the folder pid controller. Execute the le ser init.sce with the appropriate
com port number and then execute the le pid filter.sci for loading the func-
tion. Run the xcos le pidN.xcos. Output of Xcos is shown in below g.7.13
116
Figure 7.13: PID controller (with ltering) implementation
Figure shows three plots. First subplot shows Setpoint and output temperature
prole. Second sub plot shows control eort and third subplot shows error be-
tween setpoint and plant output. By comparing g.7.10 and g.7.13 it is clear that
introduction of ltered form of PID reduces uctuations in control eort.
7.8.1 Implementing PID controller with ltering using Back-
ward dierence approximation on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained in
section 3.4. The required .sce le is pid filter virtual.sce. You will nd
this le in the pid controller directory under virtual folder. The necessary
codes are listed in the section 7.9
7.9 Scilab Code
7.9.1 Scilab code for serial communication
Scilab Code 7.1 ser init.sci used for serial communication
117
1 / / T o l o a d t h e s e r i a l t o o l b o x a n d o p e n t h e s e r i a l p o r t
2 exec l o a d e r . s ce
3
4 handl = o p e n s e r i a l ( 6 , 9600 , n , 8 )
5
6 / / t h e o r d e r i s : p o r t n u m b e r , b a u d , p a r i t y , d a t a b i t s ,
s t o p b i t s
7 / / h e r e 9 i s t h e p o r t n u m b e r
8 / / I n t h e c a s e o f S BHS , s t o p b i t s = 0 , s o i t i s n o t
s p e c i f i e d i n t h e f u n c t i o n h e r e
9 / / L i n u x u s e r s s h o u l d g i v e t h i s a s ( / , 9 6 0 0 , n , 8 , 0 )
10
11 i f ( a s c i i ( handl ) = [ ] )
12 di s p ( COM Por t Opened ) ;
13 end
7.9.2 Scilab code for PI controller
Scilab Code 7.2 pi ta.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g t r a p e z o i d a l a p p r o x i m a t i o n .
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , C0 , e new ] = p i t a ( s e t p o i n t , d i s t u r b a n c e ,
K, Ti )
9
10 gl obal temp h e a t i n f a n i n C0 u ol d u new e o l d e new
11
12 e new = s e t p o i n t temp ;
13
14 Ts =0. 4;
15 S0=K(1+Ts / ( 2
*
Ti ) ) ;
118
16 S1=K
*
( 1+( Ts / ( 2
*
Ti ) ) ) ;
17 u new = u ol d +( S0
*
e new ) +( S1
*
e o l d ) ;
18
19
20 i f u new> 39
21 u new = 39;
22 end ;
23
24 i f u new< 0
25 u new = 0;
26 end ;
27
28 C0=u new ;
29 h e a t i n = C0 ;
30 f a n i n = d i s t u r b a n c e ;
31 u ol d = u new ;
32 e o l d = e new ;
33
34 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
35 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
36 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
37 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
38 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
39 s l e e p ( 1 ) ;
40 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
41 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
42
43 endf unct i on ;
Scilab Code 7.3 pi bda.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g b a c k w a r d d i f f e r e n c e f o r m u l a
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
119
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , h e a t i n , e new ] = pi bda ( s e t p o i n t ,
d i s t u r b a n c e , K, Ti )
9
10 gl obal temp h e a t i n f a n i n C0 u ol d u new e o l d e new
11
12
13 e new = s e t p o i n t temp ;
14
15 Ts =0. 5;
16 S0=K( 1+( Ts / Ti ) ) ;
17 S1=K;
18
19
20
21 u new = u ol d+ S0
*
e new+ S1
*
e o l d ;
22
23
24 u ol d = u new ;
25 e o l d = e new ;
26
27 h e a t i n = u new ;
28 f a n i n = d i s t u r b a n c e ;
29
30 i f h e a t i n >100
31 h e a t i n = 100;
32 e l s e i f h e a t i n < 0
33 h e a t i n = 0;
34 end ;
35
36 i f f a n i n >100
37 f a n i n = 100;
38 e l s e i f f a n i n < 0
39 f a n i n = 0;
40 end ;
41
42 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
120
43 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
44 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
45 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
46 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
47 s l e e p ( 1 ) ;
48 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
49 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
50
51 endf unct i on ;
Scilab Code 7.4 pi fda.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g f o r w a r d d i f f e r e n c e f o r m u l a
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , C0 , e new ] = p i f d a ( s e t p o i n t , d i s t u r b a n c e
, K, Ti )
9
10 gl obal temp h e a t i n f a n i n C0 u ol d u new e o l d e new
11
12 e new = s e t p o i n t temp ;
13
14 Ts =0. 4;
15 S0=K
*
( 1+( ( Ts / Ti ) ) ) ;
16 S1=K;
17 u new = u ol d +( S0
*
e new ) +( S1
*
e o l d ) ;
18
19
20 i f u new> 39;
21 u new = 39;
22 end ;
23
121
24 i f u new< 0;
25 u new = 0;
26 end ;
27
28 C0=u new ;
29 h e a t i n = C0 ;
30 f a n i n = d i s t u r b a n c e ;
31 u ol d = u new ;
32 e o l d = e new ;
33
34 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
35 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
36 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
37 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
38 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
39 s l e e p ( 1 ) ;
40 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
41 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
42
43 endf unct i on ;
7.9.3 Scilab code for PID controller
Scilab Code 7.5 pid bda.sci
1 mode( 1) ;
2 f unc t i on [ temp , heat , e t ] = pi d bda ( s e t p o i n t , f an , K, Ti , Td
)
3 gl obal temp h e a t i n f a n i n C0 u ol d u new e o l d e new
e o l d o l d
4
5
6 e new = s e t p o i n t temp ;
7
8 Ts =0. 5;
9
10 S0=K
*
( 1+( Ts / Ti ) +( Td / Ts ) ) ;
11 S1=K
*
( 1 ((2
*
Td ) / Ts ) ) ;
122
12 S2=K
*
( Td / Ts ) ;
13
14 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
15
16 u ol d = u new ;
17 e o l d o l d = e o l d ;
18 e o l d = e new ;
19
20
21 he a t = u new ;
22
23 i f he a t >40
24 he a t = 40;
25 e l s e i f he a t < 0
26 he a t = 0;
27 end ;
28
29 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
30 wr i t e s e r i a l ( handl , a s c i i ( he a t ) ) ;
31 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
32 wr i t e s e r i a l ( handl , a s c i i ( f an ) ) ;
33 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
34 s l e e p ( 1 ) ;
35 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
36 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
37 endf unct i on ;
Scilab Code 7.6 pid ta bda.sci
1 mode ( 0 ) ;
2 / / P I D C o n t r o l l e r u s i n g t r a p e z o i d a l a p p r o x i m a t i o n f o r
t h e i n t e g r a l m o d e a n d
3 / / b a c k w a r d d i f f e r e n c e f o r m u l a f o r d e r i v a t i v e m o d e .
4 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u n e w .
5 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
6 / / R a n g e o f F a n i n p u t : 6 0 t o 2 5 2
123
7 / / T e m p e r a t u r e i s r e a d
8
9 f unc t i on [ temp , CO, e t ] = p i d t a b d a ( s e t p o i n t ,
d i s t u r b a n c e , K, Ti , Td )
10 gl obal temp h e a t i n f a n i n e t SP CO e t i u ol d u new
e o l d e new e o l d o l d
11
12 e new = s e t p o i n t temp ;
13
14 Ts =1. 4;
15 S0=K
*
( 1+( Ts / ( 2
*
Ti ) ) +( Td / Ts ) ) ;
16 S1=K
*
( 1+( Ts / ( 2
*
Ti ) ) (2
*
Td / Ts ) ) ;
17 S2=(K
*
Td / Ts ) ;
18 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
19 e t = s e t p o i n t temp ;
20 CO = u new ;
21
22 i f CO>39
23 CO = 39;
24 end ;
25
26 i f CO<0
27 CO = 0;
28 end ;
29
30 u new = CO;
31
32 u ol d = u new ;
33 e o l d o l d = e o l d ;
34 e o l d = e new ;
35
36
37 h e a t i n = CO;
38 f a n i n = d i s t u r b a n c e ;
39
40 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
41 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
42 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
124
43 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
44 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
45 s l e e p ( 1 ) ;
46 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
47 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
48 endf unct i on ;
Scilab Code 7.7 pid lter.sci
1 mode ( 0 ) ;
2 / / P I D C o n t r o l l e r ( w i t h f i l t e r i n g )
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u n e w .
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 6 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , CO, e t ] = pi d ( s e t p o i n t , d i s t u r b a n c e , K, Ti ,
Td , N)
9 gl obal temp h e a t i n f a n i n e t SP CO e t i u ol d
u o l d o l d u new e o l d e new e o l d o l d
10
11 e new = s e t p o i n t temp ;
12
13 Ts =0. 5;
14
15 r 1 =(( Td / N) / ( ( Td / N) +Ts ) ) ;
16
17 S0=K
*
( 1+( Ts / Ti ) (N
*
r 1 ) ) ;
18 S1=K
*
( ( r 1
*
( 1+( Ts / Ti ) +(2
*
N) ) ) 1) ;
19 S2=K
*
r 1
*
(1+N) ;
20
21 u new = r 1
*
u ol d ol d ( r1 1)
*
u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
22 e t = s e t p o i n t temp ;
23 CO = u new ;
24
125
25 i f CO>39
26 CO = 39;
27 end ;
28
29 i f CO<0
30 CO = 0;
31 end ;
32
33 u new = CO;
34
35 u ol d = u new ;
36 e o l d o l d = e o l d ;
37 e o l d = e new ;
38
39
40 h e a t i n = CO;
41 f a n i n = d i s t u r b a n c e ;
42
43 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
44 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
45 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
46 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
47 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
48 s l e e p ( 1 ) ;
49 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
50 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
51 endf unct i on ;
Scilab Code 7.8 pid bda virtual.sce
1 mode ( 0 ) ;
2 / / F o r s c i l a b 5 . 1 . 1 o r l o w e r v e r s i o n u s e r s , u s e s c i c o s
c o m m a n d t o o p e n s c i c o s d i a g r a m s i n s t e a d o f x c o s
3
4 gl obal f df h f d t f n c r fncw m e r r c o u n t y
5
6 f n c r = s c i l a b r e a d . s ce ;
7 f d t = mopen( f n c r ) ;
126
8 mseek ( 0 ) ;
9
10 e r r c o u n t = 0; / / i n i t i a l i s i n g e r r o r c o u n t f o r n e t w o r k
e r r o r
11 m =1;
12 exec ( p i d b d a v i r t u a l . s c i ) ;
13 A = [ 0 . 1 , m, 0 , 1 0 0 ] ;
14 f df h = f i l e ( open , s c i l a b wr i t e . s ce , unknown ) ;
15 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
16 f i l e ( c l o s e , f df h ) ;
17 s l e e p ( 2000) ;
18 a = mget l ( f dt , 1 ) ;
19 mseek ( 0 ) ;
20 i f a = [ ] / / o p e n x c o s o n l y i f c o m m u n i c a t i o n i s
t h r o u g h ( i e r e p l y h a s c o m e f r o m s e r v e r )
21 xcos ( p i d b d a v i r t u a l . xcos ) ;
22 e l s e
23 di s p ( NO NETWORK CONNECTION! ) ;
24 ret urn
25 end
Scilab Code 7.9 pid bda virtual.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g t r a p e z o i d a l a p p r o x i m a t i o n .
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , heat , e new , s t op ] = p i d b d a v i r t u a l (
s e t p o i n t , d i s t u r b a n c e , K, Ti , Td )
9
10 gl obal temp he a t f an C0 u ol d u new e o l d e new
e o l d o l d f df h f d t f n c r fncw m e r r c o u n t s t op
11
127
12 f n c r = s c i l a b r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
13 fncw = s c i l a b wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
14
15 a = mget l ( f dt , 1 ) ;
16 b = e vs t r ( a ) ;
17 byt e = mt e l l ( f d t ) ;
18 mseek ( byt e , f dt , s e t ) ;
19
20 i f a = [ ]
21 temp = b ( 1 , 4 ) ; h e a t s = b ( 1 , 2 ) ;
22 f a ns = b ( 1 , 3 ) ; y = temp ;
23
24 e new = s e t p o i n t temp ;
25
26
27 Ts =1;
28 S0=K
*
( 1+( Ts / Ti ) +( Td / Ts ) ) ;
29 S1=K
*
( 1 ((2
*
Td ) / Ts ) ) ;
30 S2=K
*
( Td / Ts ) ;
31
32 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
33
34
35
36 i f u new> 100
37 u new = 100;
38 end ;
39
40 i f u new< 0
41 u new = 0;
42 end ;
43
44
45 he a t =u new ;
46 f an = d i s t u r b a n c e ;
47
128
48 i f f an > 100
49 f an = 100;
50 end ;
51
52 i f f an < 0
53 f an = 0;
54 end ;
55
56 u ol d = u new ;
57 e o l d o l d = e o l d ;
58 e o l d = e new ;
59
60 A = [m, heat , f an , m] ;
61 f df h = f i l e ( open , s c i l a b wr i t e . s ce , unknown ) ;
62 f i l e ( l a s t , f df h )
63 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
64 f i l e ( c l o s e , f df h ) ;
65 m = m+1;
66
67 e l s e
68 y = 0;
69 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
70 i f e r r c o u n t > 300
71 di s p ( NO NETWORK COMMUNICATION! ) ;
72 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
73 end
74 end
75
76 ret urn
77 endf unct i on
129
Chapter 8
Implementing Two Degrees of
FreedomController for First order
systems on a Single Board Heater
System
The aim of this experiment is to implement a 2DOF controller on a single board
heater system. The target group is anyone who has basic knowledge of Control
Engineering. We have used Scilab with Xcos as an interface for sending and
receiving data. This interface is shown in Fig.8.1. Fan speed and Heater current
are the two inputs to the system. For this experiment, the heater current is used
as a control eort generated by inputting the various 2-DOF controller parameters
like Rc, Sc, Tc and gamma. The fan input could be thought of as an external
disturbance.
8.1 Theory
Degree of freedom as far as the control theory is concerned is the number of pa-
rameters on which the plant is no more dependent or the number of parameters that
are free to vary. This means that a higher degree of freedom controller makes the
plant less susceptible to disturbances. Controllers are broadly classied as feed-
back and feed forward controllers. Feedback controllers are further classied as
One Degree of Freedom controller and Two Degree of Freedom controller. Feed
forward controllers are those who take the control action before a disturbance dis-
130
Figure 8.1: Xcos interface for this experiment
131
r
G
c
(z)
e
G(z)
u
v
y
cl
r(n) +
R
c
cl
v(n) (8.12)
where
cl
= R
c
(z)A(z) + z
k
B(z)S
c
(z) (8.13)
and is known as the closed-loop characteristic polynomial.
Now, we want the following conditions to be satised.
1. The zeros of
cl
should be inside the unit circle, so that the closed-loop
system becomes stable.
2. The value of z
k BT
c
cl
must be close to unity so that reference tracking is
achieved
3. The value of
R
c
cl
must be as small as possible to achieve disturbance rejection
We would now see the pole placement controller approach to design a 2DOF
controller.[5]
8.2 Designing 2-DOF controller using pole placement
control approach
A 2DOF pole placement controller is as shown in the gure 8.4 It should be noted
that the eect of external disturbance will not be considered for this section. We
134
T
c
(z)
R
c
(z)
r
G = z
k
B(z)
A(z)
u
y
S
c
R
c
cl
R(z) (8.14)
Here, Y
m
(z) means the model output.
cl
is nothing but the closed loop character-
istic polynomial obtained by the desired location analysis.
The value of gamma is chosen in such a way so that at steady-state the output of
the model is equal to the setpoint.
=
cl(1)
B
r
(1)
(8.15)
Simplifying the block diagram shown in gure 8.4 yields
Y = z
k
BT
c
AR
c
+ z
k
BS
c
R (8.16)
Here we have dropped the argument of z for convenience
On comparing equation 8.14 and 8.16 we can see that
BT
c
AR
c
+ z
k
BS
c
=
B
r
cl
(8.17)
135
Here after factorization of the LHS we can expect some cancellations between
the numerator and the denominator thereby making the degB
r
< degB. But the
cancellations ,if any, must be between stable poles and zeros. One should avoid
the cancellation of an unstable pole with a zero.
Hence, we dierentiate the factors as good and bad factors. Therefore we write A
and B as
A = A
g
A
b
(8.18)
B = B
g
B
b
(8.19)
We also split R
c
, S
c
and T
c
as shown
R
c
= B
g
R
1
(8.20)
S
c
= A
g
S
1
(8.21)
T
c
= A
g
T
1
(8.22)
Hence, the equation 8.17 becomes
B
g
B
b
A
g
T
1
A
g
A
b
B
g
R
1
+ z
k
B
g
B
b
A
g
S
1
=
B
r
cl
(8.23)
After appropriate cancellations, we obtain
B
b
T
1
A
b
R
1
+ z
k
B
b
S
1
=
B
r
cl
(8.24)
Equating the LHS and RHS of equation 8.24 we obtain
B
b
T
1
= B
r
(8.25)
A
b
R
1
+ z
k
B
b
S
1
=
cl
(8.26)
Equation 8.26 is known as the aryabhattas identity and can be used to solve for
R
1
and S
1
. There are many options to choose for the value of T
1
. By choosing T
1
to be equal to S
1
the 2-DOF controller is reduced to 1-DOF controller. We usually
choose T
1
=1.
136
Equation 8.25 becomes
B
b
= B
r
(8.27)
hence the expression of gamma is now changed to
=
cl(1)
B
b
(1)
(8.28)
and the desired closed loop transfer function now becomes
Y
m
(z) = z
k
B
b
cl
R(z) (8.29)
This implies that the open loop model imposes two limitations on the closed loop
model.
The bad portion of the open loop model cannot be canceled out and it ap-
pears in the closed loop model.
The open loop plant delay cannot be removed or minimized,i.e. the closed
loop model cannot be made faster then the open loop model.
8.3 Step by step procedure to design and implement
a 2-DOF controller
We obtain a rst order transfer function of the plant using the step test approach.The
model so obtained is
G(s) =
0.42
35.61s + 1
(8.30)
with time constant = 35.6sec and gain K = 0.42
After discretization with sampling time = 1 second, we obtain
G(z) =
0.0116304
z 0.9723086
(8.31)
=
0.0116304z
1
1 0.9723086z
1
(8.32)
137
We would now dene good and bad terms
A
g
= 1 0.9723086z
1
A
b
= 1
B
g
= 0.0116304
B
b
= 1
Let us now dene the transient specications. We choose,
Rise time = 100 seconds
No. of samples per rise time (N
r
) is calculated as
N
r
Rise time
Sampling time
= 100
next
=
2N
r
= 0.015708
We choose,
Overshoot() = 0.05.........i.e5%
/
= 0.860
Let us now calculate 2DOF Controller parameters. The closed loop characteristic
polynomial is given by
cl
= 1 z
1
2cos +
2
z
2
= 1 1.7198065z
1
+ 0.7396z
2
But according to equation 8.26
A
b
R
1
+ z
k
B
b
S
1
=
cl
138
Recall that we had not considered external disturbance in the block diagramshown
in gure8.4. However, we can still, up to some extent, take care of the distur-
bances. This is achieved by using the internal model principle. If a model of step
is present inside the loop, step disturbances can be rejected. We can apply this by
forcing R
c
to have this term. A step model is given by
1(z) =
1
1 z
1
Let the denominator of the step model be denoted as
= 1 z
1
Therefore,
R
c
= B
g
R
1
has a root which lies on the unit circle. Hence it has to be treated as a bad
part and should not be canceled out. Hence, we should make sure that all of the
occurrences of R
1
have this term.
Therefore,
cl
= A
b
R
1
+ z
k
B
b
S
1
(8.33)
Hence,
A
b
R
1
+ z
k
B
b
S
1
= 1 1.7198065z
1
+ 0.7396z
2
The expression is known as the Aryabhatta Identity and is solved using rigorous
Matrix calculations. The explanation of this operation is not considered here. You
may refer to the book Digital Control by Prof. Kannan Moudgalya [5]
R
c
= R
c1
+ R
c2
z
1
+ R
c3
z
2
= 0.0116304 0.0229175z
1
+ 0.0112871z
2
S
c
= S
c1
+ S
c2
z
1
= 0.0004641 0.0004512z
1
T
c
= T
c1
+ T
c2
z
1
= 1 0.9723z
1
= 0.0004641
139
Scilab code twodof para.sce does these calculations. This code utilizes
various other scilab codes provided at the end of this document. Execute this
scilab code with the rst order transfer function for your SBHS. You would ob-
tain a Z-Transformed transfer function for the continuous time transfer function
you input. You would also obtain the various parameters of 2dof controller as
shown in gure 8.5
1
After execution of twodof para.sce, run the Xcos code
twodof.xcos with required setpoint value and observe the temperature prole.
The performance of the controller is shown in gure 8.6Make sure that you input
the sampling time(Clock period) same as the one you used for discretization of
the continuous time plant transfer function. It could be seen that the output (tem-
perature) tracks the setpoint irrespective of the step changes in the fan speed. We
can see that the Over shoot turns out to be 6% and rise time turns out to be 60
seconds, which is acceptable.
To implement a second order transfer function, input the correct second order
transfer function in twodof para.sce. Also, make sure you comment the rst
order control law equation and uncomment the second order control law equation
in twodof.sci le.
8.3.1 Implementing 2dof controller on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained
in section 3.4. The required .sce le is twodof.sce. You will nd this le in the
2dof controller directory under virtual folder. The necessary code is listed in
the section 8.4
8.4 Scilab Code
Scilab Code 8.1 c2d.sce
1 mode ( 0 )
2 gl obal NUM c hi
3 s=pol y ( 0 , s ) ; / / D e f i n e s s t o b e a p o l y n o m i a l v a r i a b l e
4 TFcont = s y s l i n ( c , [ 0 . 5 3 2 / ( ( 3 0 . 0 9
*
s +1)
*
( 6. 971
*
s +1) ) ] )
/ / C r e a t i n g c o n t t i m e t r a n s f e r f u n c t i o n
5 SScont = t f 2 s s ( TFcont ) ; / / C o n v e r t i n g c o n t t i m e
t r a n s f e r f u n c t i o n t o s t a t e s p a c e m o d e l
1
NOTE:- The scilab codes are given at the end of this document.
140
Figure 8.5: Scilab output for 2DOF para.sce
141
Figure 8.6: Implementation of 2DOF controller
142
6 Ts =0. 5; / / S a m p l i n g t i m e
7 SSdi s c=dscr ( SScont , Ts ) ; / / D i s c r e t i z i n g c o n t t i m e s t a t e
s p a c e m o d e l
8 TFdi s c=s s 2 t f ( SSdi s c ) / / C o n v e r t i n g d i s c r t i m e s s m o d e l
t o t f
9 [ Ds ,NUM, c hi ]= s s 2 t f ( SSdi s c )
Scilab Code 8.2 2-DOF para.sce
1 mode ( 0 )
2 f uncprot ( 0 )
3 gl obal temp NUM c hi h e a t i n f a n i n CO u new u ol d
u o l d o l d r o l d y ol d Rc1 Rc2 Rc3 Sc1 Sc2 Tc1 Tc2
gamm A B
4
5 exec ci ndep . s c i ;
6 exec c l c o e f . s c i ;
7 exec c o l s p l i t . s c i ;
8 exec c o s f i l i p . s c i ;
9 exec d e s i r e d . s c i ;
10 exec i ndep . s c i ;
11 exec l e f t p r m . s c i ;
12 exec makezer o . s c i ;
13 exec move s ci . s c i ;
14 exec pol mul . s c i ;
15 exec p o l s i z e . s c i ;
16 exec p o l s p l i t 3 . s c i ;
17 exec pol yno . s c i ;
18 exec pp i m . s c i ;
19 exec r owj oi n . s c i ;
20 exec s e s h f t . s c i ;
21 exec t 1 c a l c . s c i ;
22 exec xdync . s c i ;
23 exec zpowk . s c i ;
24
25
26 / / T r a n s f e r f u n c t i o n
27 t f =c o e f f ( c hi ) ;
143
28 B = c o e f f (NUM) ;
29 A = [ t f ( 2 ) t f ( 1 ) ] ; k=1;
30
31 / / T r a n s i e n t s p e c i f i c a t i o n s
32 r i s e = 5;
33 e p s i l o n = 0 . 1 ;
34 Ts = 0 . 5 ;
35 phi = d e s i r e d ( Ts , r i s e , e p s i l o n ) ;
36
37 / / C o n t r o l l e r d e s i g n
38 De l t a = [ 1 1] ; / / i n t e r n a l m o d e l o f s t e p u s e d
39 [ Rc , Sc , Tc , gamm] = pp i m ( B, A, k , phi , De l t a ) ;
40
41 / / p a r a m e t e r s f o r t w o d o f . c o s
42 gamm
43 [ Tcp1 , Tcp2 ] = c o s f i l i p ( Tc , 1 ) ; / / T c / 1
44 Tc=c o e f f ( Tcp1 )
45 Tc1=Tc ( 1 , 2 )
46 Tc2=Tc ( 1 , 1 )
47 [ Rcp1 , Rcp2 ] = c o s f i l i p ( 1 , Rc ) ; / / 1 / R c
48 Rc=c o e f f ( Rcp2 )
49 Rc1=Rc ( 1 , 3 )
50 Rc2=Rc ( 1 , 2 )
51 Rc3=Rc ( 1 , 1 )
52 [ Scp1 , Scp2 ] = c o s f i l i p ( Sc , 1 ) ; / / S c / 1
53 Sc=c o e f f ( Scp1 )
54 Sc1=Sc ( 1 , 2 )
55 Sc2=Sc ( 1 , 1 )
56
57 / / e x e c 2 d o f . s c i ;
Scilab Code 8.3 2dof.sci
1 mode ( 0 )
2 / / 2 DOF C o n t r o l l e r
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t CO
144
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l ( d i s t u r b a n c e )
5 / / R a n g e o f F a n i n p u t : 6 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , heat , e new ] = t wodof ( s e t p o i n t , f an )
9 gl obal temp CO u new u ol d u o l d o l d r o l d y ol d Rc1
Rc2 Rc3 Sc1 Sc2 Tc1 Tc2 gamm
10
11 e new = s e t p o i n t temp ;
12 r new = s e t p o i n t ;
13 y new = temp ;
14
15
16
17 u new = ( 1 / Rc1 )
*
( gamm
*
Tc1
*
r new + gamm
*
Tc2
*
r ol d Sc1
*
y new Sc2
*
y ol d Rc2
*
u ol d Rc3
*
u o l d o l d ) ;
18
19 CO = u new ;
20
21 i f CO>39
22 CO = 39;
23 end ;
24
25 i f CO<0
26 CO =0;
27 end ;
28
29 u new = CO;
30
31 u o l d o l d = u ol d ;
32 u ol d = u new ;
33 r o l d = r new ;
34 y ol d = y new ;
35
36 he a t = u new ;
37
145
38 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / I n p u t H e a t e r ,
w r i t e s e r i a l a c c e p t s s t r i n g s ; s o c o n v e r t 2 5 4 i n t o
i t s s t r i n g e q u i v a l e n t
39 wr i t e s e r i a l ( handl , a s c i i ( he a t ) ) ;
40 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ; / / I n p u t F a n
41 wr i t e s e r i a l ( handl , a s c i i ( f an ) ) ;
42 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ; / / T o r e a d T e m p
43 s l e e p ( 100) ;
44
45 temp = a s c i i ( r e a d s e r i a l ( handl ) ) ; / / R e a d s e r i a l
r e t u r n s a s t r i n g , s o c o n v e r t i t t o i t s i n t e g e r (
a s c i i ) e q u i v a l e n t
46 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ; / / c o n v e r t t o t e m p w i t h
d e c i m a l p o i n t s e g : 4 0 . 7
47
48 endf unct i on ;
Scilab Code 8.4 cindep.sci
1 / / U p d a t e d No c h a n g e
2 / / f u n c t i o n b = c i n d e p ( S , g a p )
3 / / u s e d i n XD + YN = C . a l l r o w s e x c e p t t h e l a s t o f
a r e a s s u m e d t o
4 / / b e i n d e p e n d e n t . T h e a i m i s t o c h e c k i f t h e l a s t
r o w i s d e p e n d e n t o n t h e
5 / / r e s t a n d i f s o h o w . T h e c o e f f i c i e n t s o f d e p e n d e n c e
i s s e n t i n b
6 f unc t i on b = ci ndep ( S , gap )
7
8 i f argn ( 2 ) == 1
9 gap = 1. 0 e8 ;
10 end
11 eps = 2. 2204 e 016;
12 [ rows , c o l s ] = s i z e ( S) ;
13 i f rows > c o l s
14 i nd = 0;
15 e l s e
16 si gma = svd ( S) ;
146
17 l e n = l e ngt h ( si gma ) ;
18 i f ( si gma ( l e n ) / si gma ( 1 ) <= ( eps
*
max( i , c o l s ) ) )
19 i nd = 0; / / n o t i n d e p e n d e n t
20 e l s e
21 i f or ( si gma ( 1 : l en 1) . / si gma ( 2 : l e n )>=gap )
22 i nd = 0; / / n o t d e p e n d e n t
23 e l s e
24 i nd = 1; / / i n d e p e n d e n t
25 end
26 end
27 end
28 i f i nd
29 b = [ ] ;
30 e l s e
31 b = S( rows , : ) / S ( 1 : rows 1 , : ) ;
32 b = makezer o ( b , gap ) ;
33 end
34 endf unct i on
Scilab Code 8.5 clcoef.sci
1 / / U p d a t e d No c h a n g e
2 / / H . K w a k e r n a a k , J u l y , 1 9 9 0
3 / / M o d i f i e d b y K a n n a n M o u d g a l y a i n N o v . 1 9 9 2
4
5 f unc t i on [ P , degP ] = c l c o e f (Q, degQ)
6
7 [ rQ , cQ] = p o l s i z e (Q, degQ) ;
8
9 i f and ( and (Q==0) )
10 P = zeros ( rQ , cQ) ;
11 degP = 0;
12 e l s e
13 P = Q; degP = degQ ; r P = rQ ; cP = cQ;
14 j = degP +1;
15 whi l e j >= 0
16 X = P ( : , ( j 1)
*
cP+1: j
*
cP )
17 i f max( sum( abs (X ) ) ) < ( 1 e 8)
*
max( sum( abs ( P) ) )
147
18 P = P ( : , 1 : ( j 1)
*
cP ) ;
19 degP = degP 1;
20 e l s e
21 j = 0;
22 end
23 j = j 1;
24 end
25 end
26 endf unct i on
Scilab Code 8.6 clcoef.sci
1 / / U p d a t e d No c h a n g e
2 / / H . K w a k e r n a a k , J u l y , 1 9 9 0
3 / / M o d i f i e d b y K a n n a n M o u d g a l y a i n N o v . 1 9 9 2
4
5 f unc t i on [ P , degP ] = c l c o e f (Q, degQ)
6
7 [ rQ , cQ] = p o l s i z e (Q, degQ) ;
8
9 i f and ( and (Q==0) )
10 P = zeros ( rQ , cQ) ;
11 degP = 0;
12 e l s e
13 P = Q; degP = degQ ; r P = rQ ; cP = cQ;
14 j = degP +1;
15 whi l e j >= 0
16 X = P ( : , ( j 1)
*
cP+1: j
*
cP )
17 i f max( sum( abs (X ) ) ) < ( 1 e 8)
*
max( sum( abs ( P) ) )
18 P = P ( : , 1 : ( j 1)
*
cP ) ;
19 degP = degP 1;
20 e l s e
21 j = 0;
22 end
23 j = j 1;
24 end
25 end
26 endf unct i on
148
Scilab Code 8.7 cosfil ip.sci
1 / / U p d a t e d ( 3 1 7 0 7 )
2 / / I n p u t a r g u m e n t s a r e n u m e r a t o r a n d d e n o m i n a t o r
3 / / p o l y n o m i a l s c o e f f i c i e n t s i n a s c e n d i n g
4 / / p o w e r s o f z 1
5
6 / / S c i c o s b l o c k s n e e d i n p u t p o l y n o m i a l s
7 / / w i t h p o s i t i v e p o w e r s o f z
8
9 f unc t i on [ nume , deno ] = c o s f i l i p ( num, den )
10
11 [ Nn , Nd] = pol yno ( num, z ) ;
12 [ Dn , Dd] = pol yno ( den , z ) ;
13 nume = Nn
*
Dd ;
14 deno = Nd
*
Dn ;
15
16 endf unct i on ;
Scilab Code 8.8 desired.sci
1 / / U p d a t e d ( 2 6 7 0 7 )
2 / / 9 . 4
3 f unc t i on [ phi , dphi ] = d e s i r e d ( Ts , r i s e , e p s i l o n )
4
5 Nr = r i s e / Ts ; omega = %pi / 2 / Nr ; r ho = e p s i l o n ( omega /
%pi ) ;
6 phi = [ 1 2
*
r ho
*
cos ( omega ) r ho 2 ] ; dphi = l e ngt h ( phi )
1;
7 endf unct i on ;
Scilab Code 8.9 indep.sci
1 / / U p d a t e d No c h a n g e
2 / / f u n c t i o n b = i n d e p ( S , g a p )
3 / / d e t e r m i n e s t h e f i r s t r o w t h a t i s d e p e n d e n t o n t h e
p r e v i o u s r o w s o f S .
149
4 / / T h e c o e f f i c i e n t s o f d e p e n d e n c e i s r e t u r n e d i n b
5 f unc t i on b = i ndep ( S , gap )
6
7 i f argn ( 2 ) == 1
8 gap = 1. 0 e8 ;
9 end
10 [ rows , c o l s ] = s i z e ( S) ;
11 i nd = 1;
12 i = 2;
13 eps = 2. 2204 e 016;
14 whi l e i nd & i <= rows
15 si gma = svd ( S ( 1 : i , : ) ) ;
16 l e n = l e ngt h ( si gma ) ;
17 i f ( si gma ( l e n ) / si gma ( 1 ) < ( eps
*
max( i , c o l s ) ) )
18 i nd =0;
19 e l s e
20 s h s i g = [ si gma ( 2 : l e n ) ; si gma ( l e n ) ] ;
21 i f or ( ( si gma . / s h s i g ) > gap )
22 i nd = 0;
23 e l s e
24 i nd = 1;
25 i = i +1;
26 end
27 end
28
29 end
30 i f i nd
31 b =[ ] ;
32
33 e l s e
34 c = S( i , : ) / S ( 1 : i 1 , : ) ;
35 c = makezer o ( c , gap ) ;
36 b = [ c 1 ] ;
37 end
38 endf unct i on
Scilab Code 8.10 left prm.sci
150
1 / / f u n c t i o n [ B , d e g B , A , d e g A , Y , d e g Y , X , d e g X ] = . . .
2 / / l e f t p r m ( N , d e g N , D , d e g D , j o b , g a p )
3 / /
4 / / d o e s t h r e e d i f f e r e n t t h i n g s a c c o r d i n g t o i n t e g e r s
t h a t j o b t a k e s
5 / / j o b = 1 .
6 / / t h i s i s t h e d e f a u l t . I t i s a l w a y s d o n e f o r a l l
j o b s .
7 / / 1 1
1
8 / / G i v e n ND , r e t u r n s c o p r i m e B a n d A w h e r e ND = A
B
9 / / I t i s e n o u g h i f o n e s e n d s t h e f i r s t f o u r i n p u t
a r g u m e n t s
10 / / I f g a p i s r e q u i r e d t o b e s e n t , t h e n o n e c a n s e n d
e i t h e r 1 o r a n u l l
11 / / e n t r y f o r j o b
12 / / j o b = 2 .
13 / / f i r s t s o l v e f o r j o b = 1 a n d t h e n s o l v e XA + YB = I
14 / / j o b = 3 .
15 / / u s e d i n s o l v i n g XD + YN = C
16 / / a f t e r f i n d i n g c o p r i m e f a c t o r i z a t i o n , d a t a a r e
r e t u r n e d
17 / /
18 / / c o n v e n t i o n : t h e v a r i a b l e w i t h p r e f i x d e g s t a n d f o r
d e g r e e s
19 / / o f t h e c o r r e s p o n d i n g p o l y n o m i a l m a t r i c e s
20 / /
21 / / i n p u t :
22 / / N : r i g h t f r a c t i o n n u m e r a t o r p o l y n o m i a l m a t r i x
23 / / D : r i g h t f r a c t i o n d e n o m i n a t o r p o l y n o m i a l m a t r i x
24 / / N a n d D a r e n o t n e c c e s s a r i l y c o p r i m e
25 / / g a p : v a r i a b l e u s e d t o z e r o e n t r i e s ; d e f a u l t v a l u e
i s 1 . 0 e + 8
26 / /
27 / / o u t p u t
28 / / b a n d A a r e l e f t c o p r i m e n u m . a n d d e n . p o l y n o m i a l
m a t r i c e s
151
29 / / X a n d Y a r e s o l u t i o n s t o A r y a b h a t t a i d e n t i t y , o n l y
f o r j o b = 2
30
31 f unc t i on [ B, degB , A, degA , Y, degY , X, degX] = l e f t p r m (N,
degN , D, degD , j ob , gap )
32 i f argn ( 2 ) == 4 | argn ( 2 ) == 5
33 gap = 1. 0 e8 ;
34 end
35 / / p a u s e
36 i f argn ( 2 ) == 4 ,
37 j ob = 1; end
38 [ F , degF ] = r owj oi n (D, degD , N, degN) ;
39 [ Frows , Fbc ol s ] = p o l s i z e ( F , degF ) ; / / F b c o l s =
b l o c k c o l u m n s
40 Fc ol s = Fbcol s
*
( degF+1) ; / / a c t u a l
c o l u m n s o f F
41 T1 = [ ] ; pr =[ ] ; degT1 = 0; T1rows = 0; s h f t = 0;
42 S=F ; s e l = ones ( Frows , 1 ) ; T1bcol s =1;
43 a ba r = ( Fbc ol s + 1) : Frows ; / / a s u p e r b a r
o f B C . C h a n g
44 whi l e i s empt y ( T1 ) | T1rows < Frows Fbcol s
45 Srows = Frows
*
T1bcol s ; / / m a x a c t u a l c o l u m n s o f
r e s u l t
46 [ T1 , T1rows , s e l , pr ] = . . .
47 t 1 c a l c ( S , Srows , T1 , T1rows , s e l , pr , Frows ,
Fbcol s , abar , gap ) ;
48 [ T1rows , T1col s ] = s i z e ( T1 ) ;
49 i f T1rows < Frows Fbcol s
50 T1 = [ T1 zeros ( T1rows , Frows ) ] ;
51 T1bcol s = T1bcol s + 1; / / m a x . b l o c k
c o l u m n s o f r e s u l t
52 degT1 = degT1 + 1; / / d e g r e e o f
r e s u l t
53 s h f t = s h f t +Fbcol s ;
54 S = s e s h f t ( S , F , s h f t ) ;
55 s e l = [ s e l ; s e l ( Srows Frows +1: Srows ) ] ;
56 rowvec = ( T1bcol s 1)
*
Frows +( Fbcol s +1) : T1bcol s
*
Frows ;
152
57 a ba r = [ a ba r rowvec ] ; / / A s u p e r b a r
o f B C . c h a n g
58 end
59 end
60
61 [ B, degB , A, degA] = c o l s p l i t ( T1 , degT1 , Fbcol s , Frows
Fbcol s ) ;
62 [ B, degB] = c l c o e f ( B, degB) ;
63 B = B;
64 [A, degA] = c l c o e f (A, degA) ;
65 / / p a u s e
66 i f j ob == 2
67 S = S( mt l b l o g i c a l ( s e l ) , : ) ;
/ / c o l u m n s
68 [ r edSr ows , Sc ol s ] = s i z e ( S) ;
69 C = [ eye ( Fbcol s , Fbc ol s ) zeros ( Fbcol s , Scol s
Fbcol s ) ] ; / / a p p e n d w i t h z e r o s
70 T2 = C/ S ;
71 T2 = makezer o ( T2 , gap ) ;
72 T2 = move s ci ( T2 , f i nd ( s e l ) , Srows ) ;
73 [X, degX , Y, degY] = c o l s p l i t ( T2 , degT1 , Fbcol s , Frows
Fbcol s ) ;
74 [X, degX] = c l c o e f (X, degX) ;
75 [Y, degY] = c l c o e f (Y, degY) ;
76 e l s e i f j ob == 3
77 Y = S ;
78 degY = s e l ;
79 X = degT1 ;
80 degX = Fbcol s ;
81 e l s e
82 i f j ob = 1
83 error ( Message from l e f t p r m : no l e g a l j ob
number s p e c i f i e d )
84 end
85 end
86 endf unct i on
Scilab Code 8.11 makezero.sci
153
1 / / U p d a t e d
2 / / f u n c t i o n B = m a k e z e r o ( B , g a p )
3 / / w h e r e B i s a v e c t o r a n d g a p a c t s a s a t o l e r a n c e
4
5 f unc t i on B = makezer o ( B, gap )
6
7 i f argn ( 2 ) == 1
8 gap = 1. 0 e8 ;
9 end
10 temp = B( f i nd ( B) ) ; / / n o n z e r o e n t r i e s o f B
11 temp = gs or t ( abs ( temp ) ) ; / / a b s o l u t e v a l u e s s o r t e d
i n d e s c e n d i n g o r d e r
12 l e n = l e ngt h ( temp ) ;
13 r a t i o = temp ( 1 : l en 1) . / temp ( 2 : l e n ) ; / / e a c h r a t i o >1
14 mi n i nd = min ( f i nd ( r a t i o >gap ) ) ;
15 i f i s empt y ( mi n i nd )
16 our e ps = temp ( mi n i nd +1) ;
17 z e r o i n d = f i nd ( abs ( B)<=our e ps ) ;
18 B( z e r o i n d ) = zeros ( 1 , l e ngt h ( z e r oi nd ) ) ;
19 end
20 endf unct i on
Scilab Code 8.12 move sci.sci
1 / / f u n c t i o n r e s u l t = m o v e s c i ( b , n o n r e d , m a x s c i )
2 / / M o v e s m a t r i x b t o m a t r i x r e s u l t w i t h t h e
i n f o r m a t i o n o n w h e r e t o m o v e ,
3 / / d e c i d e d b y t h e i n d i c e s o f n o n r e d .
4 / / T h e m a t r i x r e s u l t w i l l h a v e a s m a n y r o w s a s b h a s
a n d m a x n u m b e r o f c o l u m n s .
5 / / b i s a u g u m e n t e d w i t h z e r o s t o h a v e n o n r e d n u m b e r o f
c o l u m n s ;
6 / / T h e c o l u m n s o f b p u t i n t o t h o s e o f r e s u l t a s
d e c i d e d b y n o n r e d .
7
8 f unc t i on r e s u l t = move s ci ( b , nonr ed , max s ci )
9 [ brows , bc ol s ] = s i z e ( b ) ;
10 b = [ b zeros ( brows , l e ngt h ( nonr ed ) bc ol s ) ] ;
154
11 r e s u l t = zeros ( brows , max s ci ) ;
12 r e s u l t ( : , nonr ed ) = b ;
13 endf unct i on
Scilab Code 8.13 polmul.sci
1 / / U p d a t e d No c h a n g e
2 / / p o l m u l
3 / / T h e c o m m a n d
4 / / [ C , d e g A ] = p o l m u l ( A , d e g A , B , d e g B )
5 / / p r o d u c e s t h e p o l y n o m i a l m a t r i x C t h a t e q u a l s t h e
p r o d u c t A
*
B o f t h e
6 / / p o l y n o m i a l m a t r i c e s A a n d B .
7 / /
8 / / H . K w a k e r n a a k , J u l y , 1 9 9 0
9
10
11 f unc t i on [ C, degC] = pol mul (A, degA , B, degB)
12 [ rA , cA] = p o l s i z e (A, degA) ;
13 [ rB , cB] = p o l s i z e ( B, degB) ;
14 i f cA = rB
15 error ( pol mul : I n c o n s i s t e n t di mens i ons of i n p u t
ma t r i c e s ) ;
16 end
17
18 degC = degA+degB ;
19 C = [ ] ;
20 f or k = 0: degA+degB
21 mi = 0;
22 i f kdegB > mi
23 mi = kdegB ;
24 end
25 ma = degA ;
26 i f k < ma
27 ma = k ;
28 end
29 Ck = zeros ( rA , cB) ;
30 f or i = mi : ma
155
31 Ck = Ck + A( : , i
*
cA+1: ( i +1)
*
cA)
*
B( : , ( ki )
*
cB
+1: ( ki +1)
*
cB) ;
32 end
33 C = [ C Ck ] ;
34 end
35 endf unct i on
Scilab Code 8.14 polsize.sci
1 / / U p d a t e d No c h a n g e
2 / / f u n c t i o n [ r Q , c Q ] = p o l s i z e ( Q , d e g Q )
3 / / F U N C T I O N p o l s i z e TO D E T E R M I N E T HE D I M E N S I O N S
4 / / OF A P O L Y N O M I A L M A T R I X
5 / /
6 / / H . K w a k e r n a a k , A u g u s t , 1 9 9 0
7
8 f unc t i on [ rQ , cQ] = p o l s i z e (Q, degQ)
9
10 [ rQ , cQ] = s i z e (Q) ; cQ = cQ/ ( degQ+1) ;
11 i f abs ( round ( cQ) cQ) > 1e6
12 error ( p o l s i z e : Degr ee of i n p u t i n c o n s i s t e n t wi t h
number of col umns ) ;
13 e l s e
14 cQ = round ( cQ) ;
15 end
16 endf unct i on
Scilab Code 8.15 polsplit3.sci
1 / / U p d a t e d ( 1 8 7 0 7 )
2 / / 9 . 1 1
3 / / f u n c t i o n [ g o o d p o l y , b a d p o l y ] = p o l s p l i t 3 ( f a c , a )
4 / / S p l i t s a s c a l a r p o l y n o m i a l o f z { 1 } i n t o g o o d a n d
b a d
5 / / f a c t o r s . I n p u t i s a p o l y n o m i a l i n i n c r e a s i n g d e g r e e
o f
6 / / z { 1 } . O p t i o n a l i n p u t i s a , w h e r e a <= 1 .
156
7 / / F a c t o r s t h a t h a v e r o o t s o u t s i d e a c i r c l e o f r a d i u s
a o r
8 / / w i t h n e g a t i v e r o o t s w i l l b e c a l l e d b a d a n d t h e r e s t
9 / / g o o d . I f a i s n o t s p e c i f i e d , i t w i l l b e a s s u m e d a s
1 .
10
11 f unc t i on [ goodpol y , badpol y ] = p o l s p l i t 3 ( f ac , a )
12 i f argn ( 2 ) == 1 , a = 1; end
13 i f a>1 error ( good pol ynomi al a l s o i s u n s t a b l e ) ; end
14 f a c 1 = pol y ( f a c ( l e ngt h ( f a c ) : 1: 1) , z , c o e f f ) ;
15 r t s = r oot s ( f a c 1 ) ;
16 r t s = r t s ( l e ngt h ( r t s ) : 1: 1) ;
17
18 / / e x t r a c t g o o d a n d b a d r o o t s
19 badi ndex = mt l b f i n d ( ( abs ( r t s )>=a 1. 0 e 5) | ( r e al ( r t s )
<0. 05) ) ;
20 badpol y = c o e f f ( pol y ( r t s ( badi ndex ) , z ) ) ;
21 goodi ndex = mt l b f i n d ( ( abs ( r t s ) <a 1. 0 e 5) &( r e al ( r t s )
>=0.05) ) ;
22 goodpol y = c o e f f ( pol y ( r t s ( goodi ndex ) , z ) ) ;
23
24 / / s c a l e b y e q u a t i n g t h e l a r g e s t t e r m s
25 [m, i ndex ] = max( abs ( f a c ) ) ;
26 goodbad = convol ( goodpol y , badpol y ) ;
27 goodbad = goodbad ( l e ngt h ( goodbad ) : 1: 1) ;
28 f a c t o r 1 = f a c ( i ndex ) / goodbad ( i ndex ) ;
29 goodpol y = goodpol y
*
f a c t o r 1 ;
30 goodpol y = goodpol y ( l e ngt h ( goodpol y ) : 1: 1) ;
31 badpol y = badpol y ( l e ngt h ( badpol y ) : 1: 1) ;
32 endf unct i on ;
Scilab Code 8.16 polyno.sci
1 / / U p d a t e d ( 1 8 0 7 )
2 / / O p e r a t i o n s :
3 / / P o l y n o m i a l d e f i n i t i o n
4 / / F l i p p i n g o f c o e f f i c i e n t s
157
5 / / V a r i a b l e p a s s e d a s i n p u t a r g u m e n t ( e i t h e r
s o r z )
6 / / B o t h n u m a n d d e n a r e u s e d m o s t l y u s e d i n s c i c o s
f i l e s ,
7 / / t o g e t r i d o f n e g a t i v e p o w e r s o f z
8
9 / / P o l y n o m i a l s w i t h p o w e r s o f s n e e d t o
10 / / b e f l i p p e d o n l y
11
12 f unc t i on [ pol ynu , pol yde ] = pol yno ( zc , a )
13 zc = cl ean ( zc ) ;
14 pol ynu = pol y ( zc ( l e ngt h ( zc ) : 1: 1) , a , c o e f f ) ;
15 i f a == z
16 pol yde = %z ( l e ngt h ( zc ) 1) ;
17 e l s e
18 pol yde = 1;
19 end
20
21 / / S c i c o s ( 4 . 1 ) F i l t e r b l o c k s h o u l d n t h a v e c o n s t a n t /
c o n s t a n t
22 i f t ype ( pol ynu ) ==1 & t ype ( pol yde ) ==1
23 i f a == z
24 pol ynu = %z ; pol yde = %z ;
25 e l s e
26 pol ynu = %s ; pol yde = %s ;
27 end ;
28 end ;
29
30 endf unct i on
Scilab Code 8.17 pp im.sci
1 / / U p d a t e d ( 2 7 7 0 7 )
2 / / 9 . 8
3 / / f u n c t i o n [ Rc , S c , T c , g a m m a , p h i t ] = p p i m ( B , A , k , p h i ,
D e l t a )
4 / / C a l c u l a t e s 2 DOF p o l e p l a c e m e n t c o n t r o l l e r .
5 / /
158
6
7 f unc t i on [ Rc , Sc , Tc , gamm] = pp i m ( B, A, k , phi , De l t a )
8
9 / / S e t t i n g u p a n d s o l v i n g A r y a b h a t t a i d e n t i t y
10 [ Ag , Ab] = p o l s p l i t 3 (A) ; dAb = l e ngt h ( Ab) 1;
11 [ Bg , Bb] = p o l s p l i t 3 ( B) ; dBb = l e ngt h ( Bb) 1;
12
13 [ zk , dzk ] = zpowk ( k ) ;
14
15 [N, dN] = pol mul ( Bb , dBb , zk , dzk ) ;
16 dDel t a = l e ngt h ( De l t a ) 1;
17 [D, dD] = pol mul ( Ab , dAb , Del t a , dDel t a ) ;
18 dphi = l e ngt h ( phi ) 1;
19
20 [ S1 , dS1 , R1 , dR1 ] = xdync (N, dN, D, dD, phi , dphi ) ;
21
22 / / D e t e r m i n a t i o n o f c o n t r o l l a w
23 Rc = convol ( Bg , convol ( R1 , De l t a ) ) ; Sc = convol ( Ag , S1 ) ;
24 Tc = Ag ; gamm = sum( phi ) / sum( Bb) ;
25 endf unct i on ;
Scilab Code 8.18 rowjoin.sci
1 / / U p d a t e d No c h a n g e
2 / / f u n c t i o n [ P , d e g P ] = r o w j o i n ( P 1 , d e g P 1 , P 2 , d e g P 2 )
3 / / MATLAB F U N C T I O N r o w j o i n TO S U P E R P O S E TWO P O L Y N O M I A L
4 / / M A T R I C E S
5
6 / / H . K w a k e r n a a k , J u l y , 1 9 9 0
7
8 f unc t i on [ P , degP ] = r owj oi n ( P1 , degP1 , P2 , degP2 )
9
10 [ rP1 , cP1 ] = p o l s i z e ( P1 , degP1 ) ;
11 [ rP2 , cP2 ] = p o l s i z e ( P2 , degP2 ) ;
12 i f cP1 = cP2
13 error ( r owj oi n : I n c o n s i s t e n t numbers of col umns ) ;
14 end
15
159
16 r P = r P1+r P2 ; cP = cP1 ;
17 i f degP1 >= degP2
18 degP = degP1 ;
19 e l s e
20 degP = degP2 ;
21 end
22
23 i f i s empt y ( P1 )
24 P = P2 ;
25 e l s e i f i s empt y ( P2 )
26 P = P1 ;
27 e l s e
28 P = zeros ( rP , ( degP+1)
*
cP ) ;
29 P ( 1 : rP1 , 1 : ( degP1 +1)
*
cP1 ) = P1 ;
30 P( r P1 +1: rP , 1 : ( degP2 +1)
*
cP2 ) = P2 ;
31 end
32 endf unct i on
Scilab Code 8.19 seshft.sci
1 / / U p d a t e d No c h a n g e
2 / / f u n c t i o n C = s e s h f t ( A , B , N )
3 / / g i v e n A a n d B m a t r i c e s , r e t u r n s C = [ < A > 0
4 / / 0 < B > ] w i t h B
s h i f t e d e a s t b y N c o l s
5
6 f unc t i on C = s e s h f t (A, B, N)
7 [ Arows , Acol s ] = s i z e (A) ;
8 [ Brows , Bcol s ] = s i z e ( B) ;
9 i f N >= 0
10 B = [ zeros ( Brows , N) B] ;
11 Bcol s = Bcol s + N;
12 e l s e i f N < 0
13 A = [ zeros ( Arows , abs (N) ) A] ;
14 Acol s = Acol s +abs (N) ;
15 end
16 i f Acol s < Bcol s
17 A = [A zeros ( Arows , Bcol s Acol s ) ] ;
160
18 e l s e i f Acol s > Bcol s
19 B = [ B zeros ( Brows , Acol s Bcol s ) ] ;
20 end
21 C = [A
22 B] ;
23 endf unct i on
Scilab Code 8.20 t1calc.sci
1 / / U p d a t e d
2 / / f u n c t i o n [ T1 , T 1 r o w s , s e l , p r ] = . . .
3 / / t 1 c a l c ( S , S r o w s , T1 , T 1 r o w s , s e l , p r , F r o w s , F b c o l s , a b a r ,
g a p )
4 / / c a l c u l a t e s t h e c o e f f i c i e n t m a t r i x T 1
5 / / r e d u n d a n t r o w i n f o r m a t i o n i s k e p t i n s e l : r e d u n d a n t
r o w s a r e m a r k e d
6 / / w i t h z e r o s . T h e u n d e l e t e d r o w s a r e m a r k e d w i t h
o n e s .
7
8 f unc t i on [ T1 , T1rows , s e l , pr ] = t 1 c a l c ( S , Srows , T1 , T1rows
, s e l , pr , Frows , Fbcol s , abar , gap )
9 b = 1; / / v e c t o r o f
p r i m a r y r e d . r o w s
10
11 whi l e ( T1rows < Frows Fbcol s ) & or ( s e l ==1) &
i s empt y ( b )
12 S = cl ean ( S) ;
13 b = i ndep ( S( mt l b l o g i c a l ( s e l ) , : ) , gap ) ; / / s e n d
s e l e c t e d r o w s o f S
14 i f i s empt y ( b )
15 b = cl ean ( b ) ;
16 b = move s ci ( b , f i nd ( s e l ) , Srows ) ;
17 j = l e ngt h ( b ) ;
18 whi l e ( b ( j ) & or ( a ba r ==j ) ) / / p i c k l a r g e s t
n o n z e r o e n t r y
19 j = j 1; / / o f c o e f f .
b e l o n g i n g t o a b a r
20 i f j
161
21 f p r i n t f ( \ nMessage from t 1 c a l c ,
c a l l e d from l e f t p r m \n\n )
22 error ( Denomi nat or i s n o n i n v e r t i b l e
)
23 end
24 end
25 i f or ( j <pr & pmodulo ( pr , Frows ) == pmodulo ( j ,
Frows ) ) / / p r ( 2 ) , p r ( 1 )
26 T1 = [ T1 ; b ] ; / / c o n d i t i o n
i s n o t v i o l a t e d
27 T1rows = T1rows +1; / / a c c e p t t h i s
v e c t o r
28 end / / e l s e d o n t
a c c e p t
29 pr = [ pr ; j ] ; / / u p d a t e
p r i m e r e d r o w i n f o
30 whi l e j <= Srows
31 s e l ( j ) = 0;
32 j = j + Frows ;
33 end
34 end
35 end
36 endf unct i on
Scilab Code 8.21 xdync.sci
1 / / U p d a t e d No c h a n g e
2 / / f u n c t i o n [ Y , d e g Y , X , d e g X , B , d e g B , A , d e g A ] = x d y n c ( N ,
d e g N , D , d e g D , C , d e g C , g a p )
3 / / g i v e n c o e f f i c i e n t m a t r i x i n T1 , p r i m a r y r e d u n d a n t
r o w i n f o r m a t i o n s e l ,
4 / / s o l v e s XD + YN = C
5
6 / / c a l l i n g o r d e r c h a n g e d o n 1 6 A p r i l 2 0 0 5 . O l d o r d e r :
7 / / f u n c t i o n [ B , d e g B , A , d e g A , Y , d e g Y , X , d e g X ] = x d y n c ( N ,
d e g N , D , d e g D , C , d e g C , g a p )
8
162
9 f unc t i on [Y, degY , X, degX , B, degB , A, degA] = xdync (N, degN ,
D, degD , C, degC , gap )
10 i f argn ( 2 ) == 6
11 gap = 1. 0 e +8;
12 end
13
14 [ F , degF ] = r owj oi n (D, degD , N, degN) ;
15
16 [ Frows , Fbc ol s ] = p o l s i z e ( F , degF ) ; / / F b c o l s = b l o c k
c o l u m n s
17
18 [ B, degB , A, degA , S , s e l , degT1 , Fbcol s ] = l e f t p r m (N, degN , D
, degD , 3 , gap ) ;
19 / / i f i s s o l n ( D , d e g D , C , d e g C , B , d e g B , A , d e g A )
20 [ Crows , Ccol s ] = s i z e ( C) ;
21 [ Srows , Sc ol s ] = s i z e ( S) ;
22 S = cl ean ( S) ;
23 S = S( mt l b l o g i c a l ( s e l ) , : ) ;
24 T2 =[ ] ;
25
26 f or i = 1: Crows ,
27 Saug = s e s h f t ( S , C( i , : ) , 0) ;
28 b = ci ndep ( Saug ) ;
29 b = move s ci ( b , f i nd ( s e l ) , Srows ) ;
30 T2 =[ T2 ; b ] ;
31 end
32
33 [X, degX , Y, degY] = c o l s p l i t ( T2 , degT1 , Fbcol s , Frows
Fbcol s ) ;
34
35 [X, degX] = c l c o e f (X, degX) ;
36 [Y, degY] = c l c o e f (Y, degY) ;
37 Y = cl ean (Y) ; X = cl ean (X) ;
38 endf unct i on
Scilab Code 8.22 zpowk.sci
1 / / U p d a t e d ( 2 6 7 0 7 )
163
2 / / 9 . 6
3 / /
4
5 f unc t i on [ zk , dzk ] = zpowk ( k )
6 zk = zeros ( 1 , k+1) ; zk ( 1 , k+1) = 1;
7 dzk = k ;
8 endf unct i on
164
Chapter 9
Implementing Internal Model
Controller for rst order systems on
a Single Board Heater System
The Aim of this experiment is to implement an Internal Model Controller for rst
order systems on a single board heater system. The target group is anyone who
has basic knowledge of Control Engineering. We have used Scilab with Xcos as
an interface for sending and receiving data. This interface is shown in Fig.9.1.Fan
speed and Heater current are the two inputs to the system. For this experiment,
the heater current is used as a control eort. The fan input could be thought of as
an external disturbance.
9.1 IMC Design for Single Board Heater System
Internal Model Controller contain explicit model of plant as its part, hence it is
name as Internal Model Controller [6]. With stable open loop transfer function
and stable controller, the closed loop system can be stabled. The IMC has been
used mainly for stable plants. Now, transfer function of the stable plant be denoted
by G
p
(z) and its model is denoted by G(z),hence
y(n) = G(z)u(n) + (n) (9.1)
where;
y(n)=plant output;
165
Figure 9.1: Xcos interface for this experiment
r
G
Q
(z)
e
G
p
(z)
u
y
G(z)
y
y
G(z)
B
nm+
A
(9.4)
B
g
= 0.01163 (9.5)
B
= 1 (9.6)
B
nm+
= 1 (9.7)
A = 1 0.9723Z
1
(9.8)
For the stable system internal model controller is give by
G
Q
=
A
B
g
B
s
B
nm+
r
G
f
(9.9)
G
Q
=
1 0.9723Z
1
0.01163
1
1 Z
1
(9.10)
Now,
G
c
=
G
Q
1 GG
Q
(9.11)
u
e
=
10.9723Z
1
0.01163
1
1Z
1
1 Z
1
0.01163
10.9723Z
1
10.9723Z
1
0.01163
1
1Z
1
(9.12)
After simplifying, We get
u
e
=
1
0.01163
1 0.9723Z
1
1 Z
1
(9.13)
u
e
= b
1 0.9723Z
1
1 Z
1
(9.14)
Where,
(9.15)
b =
1
0.01163
(9.16)
Hence,
u(n) = u(n 1) + b[e(n) 0.9723e(n 1)] (9.17)
168
Figure 9.4: Experimental Results with IMC for = 0.92
For implementing above IMCcontroller, scilab code is given in imc.sci le,listed
at the end of this document. Change the current working directory to the folder
imc controller. Execute the le ser init.sce with the appropriate com port
number and then execute the le imc.sci for loading the function. Run the xcos
le imc.xcos. Output of Xcos is shown in below g.9.4. Figure shows three
plots. First subplot shows Setpoint and output temperature prole. Second sub
plot shows control eort and third subplot shows error between setpoint and plant
output.
9.3 Experimental Results
By comparing above two graph we can say that for = 0.92 the response of the
controller is sluggish. For = 0.85 the controller starts responding quickly and
169
Figure 9.5: Experimental Results with IMC for = 0.85
170
no overshoots are seen in the temperature prole.
9.3.1 Implementing IMC controller on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained in
section 3.4. The required .sce le is imc virtual.sce. You will nd this le in
the imc controller directory under virtual folder. The necessary codes are
listed in the section 9.4
9.4 Scilab Code
Scilab Code 9.1 ser init.sce
1 / / T o l o a d t h e s e r i a l t o o l b o x a n d o p e n t h e s e r i a l p o r t
2 exec l o a d e r . s ce
3
4 handl = o p e n s e r i a l ( 6 , 9600 , n , 8 )
5
6 / / t h e o r d e r i s : p o r t n u m b e r , b a u d , p a r i t y , d a t a b i t s ,
s t o p b i t s
7 / / h e r e 9 i s t h e p o r t n u m b e r
8 / / I n t h e c a s e o f S BHS , s t o p b i t s = 0 , s o i t i s n o t
s p e c i f i e d i n t h e f u n c t i o n h e r e
9 / / L i n u x u s e r s s h o u l d g i v e t h i s a s ( / , 9 6 0 0 , n , 8 , 0 )
10
11 i f ( a s c i i ( handl ) = [ ] )
12 di s p ( COM Por t Opened ) ;
13 end
Scilab Code 9.2 imc.sci
1 mode ( 0 )
2 f unc t i on [ temp , heat , e new ] = imc ( s e t p o i n t , f an , a l pha )
3 gl obal temp h e a t i n f a n i n e t SP u new u ol d u new
e o l d e new
4
5 e new = s e t p o i n t temp ;
171
6 b=((1 a l pha ) / 0 . 0 1 1 6 3 ) ;
7 u new = u ol d + b
*
( e new ( 0. 9723
*
e o l d ) ) ;
8
9
10 i f u new>100
11 u new = 100;
12 end ;
13
14 i f u new<0
15 u new = 0
16 ;
17 end ;
18
19
20 i f f an >100
21 f an = 100;
22 end ;
23
24 i f f an <0
25 f an = 0
26 ;
27 end ;
28
29
30 he a t = u new ;
31 u ol d = u new ;
32 e o l d = e new ;
33
34
35 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / I n p u t H e a t e r ,
w r i t e s e r i a l a c c e p t s s t r i n g s ; s o c o n v e r t 2 5 4 i n t o
i t s s t r i n g e q u i v a l e n t
36 wr i t e s e r i a l ( handl , a s c i i ( he a t ) ) ;
37 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ; / / I n p u t F a n
38 wr i t e s e r i a l ( handl , a s c i i ( f an ) ) ;
39 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ; / / T o r e a d T e m p
40 s l e e p ( 100) ;
41
172
42 temp = a s c i i ( r e a d s e r i a l ( handl ) ) ; / / R e a d s e r i a l
r e t u r n s a s t r i n g , s o c o n v e r t i t t o i t s i n t e g e r (
a s c i i ) e q u i v a l e n t
43 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ; / / c o n v e r t t o t e m p w i t h
d e c i m a l p o i n t s e g : 4 0 . 7
44
45 endf unct i on ;
Scilab Code 9.3 imc virtual.sce
1 mode ( 0 ) ;
2 / / F o r s c i l a b 5 . 1 . 1 o r l o w e r v e r s i o n u s e r s , u s e s c i c o s
c o m m a n d t o o p e n s c i c o s d i a g r a m s i n s t e a d o f x c o s
3
4 gl obal f df h f d t f n c r fncw m e r r c o u n t y
5
6 f n c r = c l i e n t r e a d . s ce ;
7 f d t = mopen( f n c r ) ;
8 mseek ( 0 ) ;
9
10 e r r c o u n t = 0; / / i n i t i a l i s i n g e r r o r c o u n t f o r n e t w o r k
e r r o r
11 m =1;
12 exec ( i mc v i r t u a l . s c i ) ;
13 A = [ 0 . 1 , m, 0 , 2 5 1 ] ;
14 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
15 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
16 f i l e ( c l o s e , f df h ) ;
17 s l e e p ( 2000) ;
18 a = mget l ( f dt , 1 ) ;
19 mseek ( 0 ) ;
20 i f a = [ ] / / o p e n x c o s o n l y i f c o m m u n i c a t i o n i s
t h r o u g h ( i e r e p l y h a s c o m e f r o m s e r v e r )
21 xcos ( imc . xcos ) ;
22 e l s e
23 di s p ( NO NETWORK CONNECTION! ) ;
24 ret urn
25 end
173
Scilab Code 9.4 imc virtual.sci
1 mode ( 0 )
2
3 gl obal f an
4 f unc t i on [ temp , heat , e new , s t op ] = i mc v i r t u a l ( s e t p o i n t
, f an , a l pha )
5 gl obal temp he a t e t SP u new u ol d u new e o l d e new
f df h f d t f n c r fncw m e r r c o u n t s t op
6
7 f n c r = c l i e n t r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
8 fncw = c l i e n t wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
9
10 a = mget l ( f dt , 1 ) ;
11 b = e vs t r ( a ) ;
12 byt e = mt e l l ( f d t ) ;
13 mseek ( byt e , f dt , s e t ) ;
14
15 i f a = [ ]
16 temp = b ( 1 , $ ) ; h e a t s = b ( 1 , $2) ;
17 f a ns = b ( 1 , $1) ; y = temp ;
18
19 e new = s e t p o i n t temp ;
20
21 b=((1 a l pha ) / 0 . 0 1 1 6 3 ) ;
22 u new = u ol d + b
*
( e new ( 0. 9723
*
e o l d ) ) ;
23
24 i f u new> 39
25 u new = 39;
26 end ;
27
28 i f u new< 0
29 u new = 0;
30 end ;
174
31
32 he a t =u new ;
33 u ol d = u new ;
34 e o l d = e new ;
35
36
37
38 A = [m, m, heat , f an ] ;
39 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
40 f i l e ( l a s t , f df h )
41 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
42 f i l e ( c l o s e , f df h ) ;
43 m = m+1;
44
45 e l s e
46 y = 0;
47 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
48 i f e r r c o u n t > 300
49 di s p ( NO NETWORK COMMUNICATION! ) ;
50 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
51 end
52 / / d i s p ( s t o p )
53 end
54
55 ret urn
56 endf unct i on
175
Chapter 10
Design and Implementation of Self
Tuning PI and PID Controllers on
Single Board Heater System
10.1 Introduction
This chapter presents Design and Implementation of Self Tuning PI and PID Con-
trollers on Single Board Heater System done by Mr. Vikas Gayasen.
1
When a
plant is wired in a close loop with a PID controller, the parameters, K
c
,
i
and
d
determine the variation of the manipulated input that is given by the controller.
This, in turn, determines the variation of the controlled variable, when a set point
is given. Suitable values of these parameters can be found out when plant transfer
function is known. However, with large changes in the controlled variable, there
may be appreciable changes in the plant transfer function itself. Therefore, it is
needed to dynamically update the controller parameters according to the transfer
function.
10.1.1 Ojective
The objective of the present study was to design and implement an algorithm that
would dynamically update the values of the controller parameters that are used to
control the temperature in the Single Board Heater System (SBHS).
1
Copyright: Mr. Vikas Gayasen, student of Prof. Kannan Moudgalya, IIT Bombay for process
control course, 2010
176
10.1.2 Apparatus
1. Fig 10.1 shows the single board heater system on which this experiment will
be performed.
2. The setup consists of a heater assembly, fan, temperature sensor, microcon-
troller and associated circuitry.
3. Heater assembly consists of an iron plate placed at a distance of about 3.5
mm from the nichrome coil.
4. A 12 V computer fan positioned below this heater assembly is meant for
cooling the assembly.
5. The temperature sensed by the temperature sensor, AD 590, after suitable
processing, is fed to the microcontroller.
6. The microcontroller ATmega16 is the heart of the setup. It provides an
interface between the process and the computer.
7. The LCD display mounted above the microcontroller displays the heated
plate temperature, heater and fan inputs and also the commands communi-
cated via serial port.
8. The setup is powered by 12 V, 8 A SMPS.
10.2 Theory
10.2.1 Why a Self Tuning Controller?
The transfer function of SBHS is assumed as
T =
K
p
p
s + 1
H +
K
f
f
s + 1
F (10.1)
T: Temperature Change
F: Fan Input Change
H: Heater Input Change
177
Figure 10.1: Single Board Heater System
The values of K
p
, K
f
,
s
and
f
can be found by conducting step test exper-
iments. Using these values, the parameters (K
c
,
i
and
d
) of the PID controller
can be dened using methods like Direct Synthesis of Ziegler Nichols Tuning.
However, when the apparatus is used in over a large range of temperature, the
values of the plant parameters (K
p
, K
f
,
s
and
f
) may change. The new values
would give new values of PID controller parameters. However, in a conventional
PID controlled system, the parameters K
c
,
i
and
d
are dened beforehand and are
not changed when the system is working. Therefore, we might have a situation in
which the PID controller is working with unsuitable values that may not give the
desired performance.Therefore, it becomes necessary to change/update the values
of the PID parameters so that the plant gives the optimum performance.
178
10.2.2 The Approach Followed
Following is the Variable Discription for this project:
Manipulated Variable: Heater Input
Disturbance Variable: Fan Input
Controlled Variable: Temperature
Several open loop step test experiments were performed (giving step changes
in the heater input) and the values of K
p
and
p
were found from the results for
each experiment by tting the inverse laplace transform of the assumed transfer
function with the experimental data. These values were plotted with respect to the
corresponding average temperatures. From these plots, correlations were found
for both K
p
and
p
as functions of temperature. From correlations of K
p
and
p
,
the PID parameters could be found as functions of temperature. Thus, in the new
PID controller, the values of K
c
,
i
and
d
are calculated using the temperature of
the system. For the calculation of PID settings, two approaches: Direct Synthesis
and Ziegler-Nichols Tuning are followed.
10.2.3 Direct synthesis
Loop Circuit.jpg
Figure 10.2: Closed Loop Circuit
V(s) =
G
c
(s)G(s)
1 + G
c
(s)G(s)
(10.2)
179
Where
V(s) : Overall closed-loop transfer function
G
c
(s) : Controller transfer function
G(s) : System transfer function.
Therefore,
G
c
(s) =
1
G(s)
V(s)
1 V(s)
Let the desired closed loop transfer function be of form
V(s) =
1
(
cl
s + 1)
(10.3)
G(s) =
K
p
( ps + 1)
(10.4)
By using the equations for G(s) and V(s), we get:
G(c) = K
c
(1 +
1
s
) (10.5)
Where,
K
c
=
1
K
p
(
p
/
cl
)
i
=
p
When K
p
and
p
are known as a function of time, the values of K
c
and
i
can
be found as function of temperature as well.
180
10.3 Ziegler Nichols Tuning
For the Ziegler Nichols Tuning, we use the step response of the open loop experi-
ment.
Figure 10.3: Tangent Approach to Ziegler Nichols Tuning
K
c
i
d
P 1/RL
PI 0.9/RL 3L
PID 1.2/RL 2L .5L
Table 10.1: Ziegler Nichols PID Settings
Table 10.1 gives the PID settings. In this approach too, for every open step
test, K and are found and correlated as function of average temperature and PID
settings are then found as functions of temperature.
Note: For a First Order transfer function that we are assuming,
K
p
K
p
181
Figure 10.4: Step Responce for Heater Reading 10 to 15
10.4 Step Test Experiments and Parmeter Estima-
tion
Several Open Loop step test experiments were carried out and the values of the
open loop parameters were found by curve tting. The results are shown.
10.4.1 Step Test Experiments
10.4.1.1 Step Change in Heater Reading from 10 to 15
182
Figure 10.5: Step Response for Heater reading 10 to 15 in terms of Deviation
10.4.1.2 Step Change in Heater Reading from 20 to 25
Figure 10.6: Step Responce for Heater Reading 20 to 25
183
Figure 10.7: Step Response for Heater reading 20 to 25 in terms of Deviation
10.4.1.3 Step Change in Heater Reading from 30 to 35
Figure 10.8: Step Responce for Heater Reading 30 to 35
184
Figure 10.9: Step Response for Heater reading 30 to 35 in terms of Deviation
10.4.1.4 The Open Loop Parameters
Initial Heater Reading Final Heater Reading Average Temperature(
0
C) K
p
p
10 15 31.57 0.41 53.37
20 25 36.00 0.50 52.64
30 35 41.79 0.58 49.21
Table 10.2: Open Loop Parameters
It can be seen from the graphs that there is a lag of approximately 6 seconds
in each experiment.
10.4.2 Conventional Controller Design
1. PI Controller using Ziegler Nichols Tuning with the results of the rst step
test experiment:
Kc = 19.75
i
= 18
185
2. PID Controller using Ziegler Nichols Tuning with the results of the rst step
test experiment:
Kc = 26.327
i
= 12
d
= 3
3. PI Controller Using Direct Synthesis on the results of the second step test
experiment (
cl
is taken as
p
/2):
Kc = 4.02
i
= 52.645
10.4.3 Self Tuning Controller Design
The graphs showing the variation of Kp and
p
are shown below:
Figure 10.10: Variation of K
p
with temperature
186
Figure 10.11: Variation of
p
with temperature
1. PI Controller using Ziegler Nichols Tuning:
L = 6
R = (0.016T-0.114)/(66.90-0.415T) where T is the temperature
Kc = 0.9(66.90-0.415T)/6(0.016T-0.114)
= (60.21 - 0.3735T)/(0.096T - 0.684)
i
= 3 6 = 18
2. PID Controller using Ziegler Nichols Tuning:
L = 6
R = (0.016 T-0.114)/(66.90-0.415 T) where T is the temperature
K = 1.2(66.90-0.415T)/6(0.016T-0.114)
= (80.28 - 0.498T)/(0.096T - 0.684)
i
= 2 6 = 12
d
= 0.56 = 3
3. PI Controller using Direct Synthesis (
cl
is taken as
p
/2):
187
K = 2/(0.016T-0.114)
i
= (66.90-0.415T) where T is the temperature
10.5 Implementation
10.5.1 PI Controller
Figure 10.12: Xcos Diagram for PI Controller
The PI Controller in Continuous Time is given by:
u(t) = K
_
e(t) +
1
i
_
t
0
e(t)dt
_
On taking Laplace Transform, we obtain:
u(s) = K
_
1 +
1
i
s
_
e(s)
188
By mapping the above to discrete time interval using Backward Dierence Ap-
proximation
u(n) = K
_
1 +
T
s
i
z
z 1
_
e(n)
On Cross Multiplication, we obtain:
(z 1) u(n) = K
_
(z 1) +
T
s
i
(z)
_
e(n)
We devide by z, and using the shifting theorem, we obtain:
u(n) u(n 1) = K
_
e(n) e(n 1) +
T
s
i
e(n)
_
The PI Controller is usually written as:
u(n) = u(n 1) + s
0
e(n) + s
1
e(n 1) (10.6)
Where,
s
0
= K
_
1 +
T
s
i
_
s
1
= K
10.5.2 PID Controller
The PID Controller in Continuous Time is given by:
u(t) = K
_
e(t) +
1
i
_
t
0
e(t)dt +
d
de(t)
dt
_
On taking Laplace Transform, we obtain:
u(s) = K
_
1 +
1
i
s
+
d
s
_
e(s)
By mapping the above to discrete time interval by using the Trapezoidal Approxi-
mation for integral mode and Backward Dierence Approximation for Derivative
mode
u(n) = K
_
1 +
T
s
i
z
z 1
+
d
T
s
z 1
z
_
e(n)
189
Figure 10.13: Xcos Diagram for PID Controller
On Cross Multiplication, we obtain:
(z
2
z) u(n) = K
_
(z
2
z) +
T
s
i
(z
2
) +
d
T
s
(z 1)
2
_
e(n)
We devide by z, and using the shifting theorem, we obtain:
u(n) u(n 1) = K
_
e(n) e(n 1) +
T
s
i
e(n) +
d
T
s
{e(n) 2e(n 1) + e(n 1)}
_
The PID Controller is usually written as:
u(n) = u(n 1) + s
0
e(n) + s
1
e(n 1) + s
2
e(n 2) (10.7)
Where,
s
0
= K
_
1 +
T
s
i
+
d
T
s
_
s
1
= K
_
1 2
d
T
s
_
s
2
= K
_
d
T
s
_
190
10.5.3 Self Tuning Controller
Figure 10.14: Xcos Diagram for Self Tuning Controller
The parameters of the Controller are determined dynamically using the tem-
perature values during every sampling time. For this, the formulae derived in
section 10.4.3 are used. The formulae for the control eort are same as the con-
ventional PI and PID controllers. So the PI/PID settings are calculated for every
sampling time and the control eort is calculated thereafter using the formulae
derived for conventional controllers.
191
10.6 Set Point Tracking
The main aim of the controller is to track the set point and to reject disturbances.
When the set point of the controlled variable (temperature in this case) is changed,
the controller should work in such a manner that the actual temperature follows
the set point as close as possible.
In this project, several experiments were conducted with the self tuning and
conventional PI/PID Controllers. Table 10.3 shows the set point changes given
during the various experiments that were conducted with conventional and self
tuning controllers designed using several methods.
Conventional Controller Self Tuning Controller
Direct Synthesis PI 32
0
C to 37
0
C 32
0
C to 37
0
C
35
0
C to 45
0
C 35
0
C to 45
0
C
Ziegler Nichols PI 32
0
C to 37
0
C 32
0
C to 37
0
C
35
0
C to 45
0
C 35
0
C to 45
0
C
40
0
C to 45
0
C 35
0
C to 45
0
C
Ziegler Nichols PID 31
0
C to 45
0
C 32
0
C to 46
0
C
32
0
C to 37
0
C 32
0
C to 37
0
C
Table 10.3: Set Point Changes in experiments conducted for Set Point Tracking
192
10.6.1 PI Controller designed by Direct Synthesis
The results of the experiments carried out for the self tuning PI controller using
direct synthesis method are shown. The upper plot shows the variations of the set
point temperature (the black line) and the actual temperature (the green line) in
the SBHS. The lower plot shows the control eort.
Figure 10.15: Result for Self Tuning Controller designed using Direct Synthesis
for Set Point going from 32
0
C to 37
0
C
Although there is a small overshoot, the controller is able to make the actual
temperature follow the set point temperature quiet closely. Looking at higher
values of set point changes, the result for set point change going from 35
0
C to
45
0
C is shown.
Figure 10.16: Result for Self Tuning Controller designed using Direct Synthesis
for Set Point going from 35
0
C to 45
0
C
For a higher set point change also, the controller is able to make the tempera-
ture follow the set point closely. Notice the abrupt change in the control eort as
soon as the step change in the set point is encountered.
193
For comparison, results of experiments done with conventional PI controller de-
signed using the Direct Synthesis method are also shown.
Figure 10.17: Result for Conventional Controller designed using Direct Synthesis
for Set Point going from 32
0
C to 37
0
C
Figure 10.18: Result for Conventional Controller designed using Direct Synthesis
for Set Point going from 35
0
C to 45
0
C
As can been seen from the graph, the self tuning controller stabilised the tem-
perature faster.
194
10.6.2 PI Controller using Ziegler Nichols Tuning
The results of the of the experiments carried out for the self tuning PI controller
using Ziegler Nichols tuning method are shown. The upper plot shows the varia-
tions of the set point temperature (the black line) and the actual temperature (the
green line) in the SBHS. The lower plot shows the control eort.
Figure 10.19: Result for Self Tuning Controller designed using Ziegler Nichols
Tuning for Set Point going from 32
0
C to 37
0
C
Although there are oscillations, the temperature remains near the set point.
The result for a higher value of set point change is also shown.
Figure 10.20: Result for Self Tuning Controller designed using Ziegler Nichols
Tuning for Set Point going from 35
0
C to 45
0
C
For this experiment, the controller is able to make the temperature follow the
set point closely. The uctuations may be due to noises and the surrounding con-
ditions.The plot for result of an experiment with another value of set point change
is also shown.
In this experiment too, the controller is able to keep the temperature close to
the set point and it stabilises fast.
195
Figure 10.21: Result for Self Tuning Controller designed using Ziegler Nichols
Tuning for Set Point going from 40
0
C to 45
0
C
For comparison, results of experiments done with conventional PI controller de-
signed using the Ziegler Nichols method are also shown.
Figure 10.22: Result for Conventional Controller designed using Ziegler Nichols
Tuning for Set Point going from 32
0
C to 37
0
C
196
Figure 10.23: Result for Conventional Controller designed using Ziegler Nichols
Tuning for Set Point going from 35
0
C to 45
0
C
Figure 10.24: Result for Conventional Controller designed using Ziegler Nichols
Tuning for Set Point going from 40
0
C to 45
0
C
For set point change from 40
0
C to 45
0
C, the self tuning controller showed
small oscillations, but the conventional controller shows bigger oscillations.
197
10.6.3 PID Controller using Ziegler Nichols Tuning
The results of the of the experiments carried out for the self tuning PID controller
using Ziegler Nichols tuning method are shown. The upper plot shows the varia-
tions of the set point temperature (the black line) and the actual temperature (the
purple line) in the SBHS. The lower plot shows the control eort.
Figure 10.25: Result for Self Tuning PID Controller designed using Ziegler
Nichols Tuning for Set Point going from 32
0
C to 37
0
C
Figure 10.26: Result for Self Tuning PID Controller designed using Ziegler
Nichols Tuning for Set Point going from 32
0
C to 46
0
C
From the graph it can be seen that for both the above experiments, the self
tuning PID controller is able to keep the temperature close to the set point and
the stabilisation is also fast. For comparison, plots for experiments conducted
with conventional PID controller designed using Ziegler Nichols method are also
shown.
From the above graph we can see that the conventional PID controller is not
able to make the temperature close to the set point when the set point value is
198
Figure 10.27: Result for Conventional PID Controller designed using Ziegler
Nichols Tuning for Set Point going from 32
0
C to 37
0
C
Figure 10.28: Result for Conventional PID Controller designed using Ziegler
Nichols Tuning for Set Point going from 31
0
C to 45
0
C
45
0
C. The self tuning PID controller had successfully brought the temperature to
45
0
C.
10.6.4 Conclusion
The self tuning PI controller is able to accomplish the aim of keeping the tem-
perature as close as possible to the set point. Although it may show some initial
overshoot or oscillation for some values of set point change, the time needed for
stabilisation is low. There may also some cases where the conventional controller
shows bigger oscillations than the self tuning controller.
The PI controllers, both conventional and self tuning, show oscillations for
some values of set point change. To eliminate the oscillations, when we use the
PID Controller, the self tuning design denately seems to be a better option be-
199
cause for higher values of set point change, the self tuning PID controller shows a
better performance than the conventional controller as seen in section 10.6.3.
10.7 Disturbance Rejection
Apart from tracking the set point, the system should also be able to reject distur-
bances. There may be several factors inuencing the controlled variable and not
all of them can be manipulated. Therefore, it becomes necessary for the controller
not to let the changes in the non-manipulated vraibles to aect the controlled vari-
able. This is called Disturbance Rejection.
In this system, the disturbance variable is the fan input. Therefore, the con-
troller has to work in such a way that changes in the fan input doesnt aect the
temperature in the SBHS.
In this project, several experiments were conducted with the self tuning and
conventional PI/PID Controllers. Table 10.4 shows the fan input changes given
during the various experiments that were conducted with conventional and self
tuning controllers designed using several methods.
Conventional Controller Self Tuning Controller
Direct Synthesis PI 50 to 100 50 to 100
100 to 50 100 to 50
Ziegler Nichols PI 50 to 100 50 to 100
100 to 50 100 to 50
Ziegler Nichols PID 50 to 100 50 to 100
100 to 50 100 to 50
Table 10.4: Fan Input Changes in experiments conducted for Disturbance Rejec-
tion
10.7.1 PI Controller designed by Direct Synthesis
The results of the experiments carried out for the self tuning PI controller using
direct synthesis method are shown. The upper plot shows the variations of the set
point temperature (the black line) and the actual temperature (the green line) in
200
the SBHS. The second plot shows the control eort and the third shows the fan
input.
Figure 10.29: Results for Fan Input Change from 50 to 100 for Self Tuning PI
Controller designed using Direct Synthesis
The change in the fan input introduces a small dent in the temperature. How-
ever, the controller brings the temperature back to the set point. Notice the slight
change in the controller behaviour on encountering the fan input change. The time
taken for stabilising back is also low.
201
Here, results for fan input change from 100 to 50 are also shown.
Figure 10.30: Results for Fan Input Change from 100 to 50 for Self Tuning PI
Controller designed using Direct Synthesis
In this gure also, the temperature clearly increses a bit when the step change
in the fan input is encountered. However, it quickly stabilises back and continues
to be close to the set point.
From the above two results, it is clear that the self tuning controller designed
with direct syntheis has successfully rejected the disturbance.
For comparison, results of the disturbance change for conventional PI Con-
troller designed with direct synthesis are also shown.
202
Figure 10.31: Results for the Fan input change from 50 to 100 to Conventional PI
Controller designed using Direct Synthesis
203
Figure 10.32: Results for the Fan input change from 100 to 50 to Conventional PI
Controller designed using Direct Synthesis
10.7.2 PI Controller using Ziegler Nichols Tuning
The results of the experiments carried out for the self tuning PI controller using
Ziegler Nichols method are shown. The upper plot shows the variations of the set
point temperature (the black line) and the actual temperature (the green line) in
the SBHS. The second plot shows the control eort and the third shows the fan
input.
Even on encountering the fan input change, the temperature remains close to
the set point. Notice the change in the controller behaviour on encountering the
fan input change.
204
Figure 10.33: Results for Fan Input change from 50 to 100 given to Self Tuning
PI Controller designed using Ziegler Nichols Method
Here, result for the fan input going from 100 to 50 is also shown.
Here, a change in the control eort can be noticed. This change has been
brought by the PI Controller to keept the temperature close to the set point.
From the above two results, it is clear that the self tuning controller designed with
direct syntheis has successfully rejected the disturbance.
205
Figure 10.34: Results for Fan Input change from 100 to 50 given to Self Tuning
PI Controller designed using Ziegler Nichols Method
For comparison, correspoding results are also shown for Conventional PI Con-
trollers designed using ziegler nichols tuning.
206
Figure 10.35: Results for the Fan input change from 50 to 100 to Conventional PI
Controller designed using Ziegler Nichols Tuning
10.7.3 PID Controller using Ziegler Nichols Tuning
The results of the experiments carried out for the self tuning PID controller using
Ziegler Nichols method are shown. The upper plot shows the variations of the set
point temperature (the black line) and the actual temperature (the purple line) in
the SBHS. The second plot shows the control eort and the third shows the fan
input.
In this system also, on encountering the fan input change, the temperature
remains close to the set point. Notice the change in the control eort prole when
the change in the fan input is given.
207
Figure 10.36: Results for the Fan input change from 100 to 50 to Conventional PI
Controller designed using Ziegler Nichols Tuning
Here, result for the fan input going from 100 to 50 is also shown.
In this gure also, the temperature clearly increses a bit when the step change
in the fan input is encountered. However, it quickly stabilises back and continues
to be close to the set point.
208
Figure 10.37: Results for Fan Input change from 50 to 100 given to Self Tuning
PID Controller designed using Ziegler Nichols Method
For comparison, correspoding results are also shown for Conventional PID
Controllers designed using ziegler nichols tuning.
10.7.4 Conclusion
We see that the self tuning controller manages to keep the temperature close to
the set point temperature, even when the change in fan input is encountered. This
shows that it can reject the disturbances quite nicely.
10.8 Reproducing the Results
The following steps can be used for conducting the experiments:
1. Open and run the program for serial communication in scilab. This opens
the comm port.
2. Open and execute the sci le corresponding to the experiment that is being
done.
3. Load the scicos diagram, ensure that the parameters are correct and run the
experiment.
209
Figure 10.38: Results for Fan Input change from 100 to 50 given to Self Tuning
PID Controller designed using Ziegler Nichols Method
The various codes used for conventional and self tuning controllers are shown in
Section 10.8.3 and 10.8.5
10.8.1 Implementing Self Tuning controller on SBHS, virtually
The step by step procedure for conducting an experiment virtually is explained
in section 3.4. The required .sce le is pi bda tuned dist virtual.sce for
example if you want to run the PI Controller Fan disturbance experiment.
Under the virtual folder there are two folders Self tuning controller and
SelfTuning Vikas. You will nd this le in the PIControllerFandisturbance
directory. The necessary code is listed in the section 10.8.6 and 10.8.8
10.8.2 Serial Communication
Scilab Code 10.1 ser init.sce
1 mode( 1) ;
2 / / T o l o a d t h e s e r i a l t o o l b o x a n d o p e n t h e s e r i a l p o r t
3 exec l o a d e r . s ce
4
5 handl = o p e n s e r i a l ( 7 , 9600 , n , 8 )
210
Figure 10.39: Results for the Fan input change from 50 to 100 to Conventional
PID Controller designed using Ziegler Nichols Tuning
6
7 / / t h e o r d e r i s : p o r t n u m b e r , b a u d , p a r i t y , d a t a b i t s ,
s t o p b i t s
8 / / h e r e 9 i s t h e p o r t n u m b e r
9 / / I n t h e c a s e o f S BHS , s t o p b i t s = 0 , s o i t i s n o t
s p e c i f i e d i n t h e f u n c t i o n h e r e
10 / / L i n u x u s e r s s h o u l d g i v e t h i s a s 9 6 0 0 , n , 8 , 0
11
12 i f ( a s c i i ( handl ) = [ ] )
13 di s p ( COM Por t Opened ) ;
14 end
10.8.3 Conventional Controller, local
10.8.4 Fan Disturbance in PI Controller
Scilab Code 10.2 pi bda dist.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g b a c k w a r d d i f f e r e n c e f o r m u l a
211
Figure 10.40: Results for the Fan input change from 100 to 50 to Conventional
PID Controller designed using Ziegler Nichols Tuning
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 / / m o d e ( 1 ) ;
9 f unc t i on [ temp , C0] = p i b d a d i s t ( s e t p o i n t , d i s t u r b a n c e ,
K, Ti , Td )
10
11 gl obal temp h e a t i n f a n i n u ol d u new e o l d e new S0
S1
12
13 e new = s e t p o i n t temp ;
14
15 Ts =1;
16 S0=K
*
( 1+( ( Ts / Ti ) ) ) ;
17 S1=K;
18 u new = u ol d + S0
*
e new + S1
*
e o l d ;
19
212
20 i f u new> 39;
21 u new = 39;
22 end ;
23
24 i f u new< 0;
25 u new = 0;
26 end ;
27
28 C0=u new ;
29 h e a t i n = C0 ;
30 f a n i n = d i s t u r b a n c e ;
31 u ol d = u new ;
32 e o l d = e new ;
33
34 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
35 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
36 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
37 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
38 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
39 s l e e p ( 10) ;
40 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
41 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
42
43 endf unct i on ;
10.8.4.1 Set Point Change in PI Controller
Scilab Code 10.3 pi bda.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g b a c k w a r d d i f f e r e n c e f o r m u l a
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
213
8 f unc t i on [ temp , C0 , e new ] = pi bda ( s e t p o i n t , d i s t u r b a n c e
, K, Ti )
9
10 gl obal temp h e a t i n f a n i n C0 u ol d u new e o l d e new
11
12 e new = s e t p o i n t temp ;
13
14 Ts =1;
15 S0=K
*
( 1+( ( Ts / Ti ) ) ) ;
16 S1=K;
17 u new = u ol d +( S0
*
e new ) +( S1
*
e o l d ) ;
18
19
20 i f u new> 39
21 u new = 39;
22 end ;
23
24 i f u new< 0
25 u new = 0;
26 end ;
27
28 C0=u new ;
29 h e a t i n = C0 ;
30 f a n i n = d i s t u r b a n c e ;
31 u ol d = u new ;
32 e o l d = e new ;
33
34
35 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
36 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
37 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
38 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
39 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
40 s l e e p ( 10) ;
41 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
42 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
43
44 endf unct i on ;
214
10.8.4.2 Fan Disturbance to PID Controller
Scilab Code 10.4 pid bda dist.sci
1 mode ( 0 ) ;
2 / / P I D u s i n g B a c k w a r d d i f f e r e n c e a p p r o x i m a t i o n f o r I
a n d D
3
4 f unc t i on [ temp , CO] = p i d b d a d i s t ( s e t p o i n t , d i s t u r b a n c e
, K, Ti , Td )
5 gl obal temp h e a t i n f a n i n e t SP CO e t i u ol d u new
e o l d e new e o l d o l d
6
7 e new = s e t p o i n t temp ;
8
9 Ts =1;
10
11 S0=K
*
( 1+( Ts / Ti ) +( Td / Ts ) ) ;
12 S1=K
*
( 1 ((2
*
Td ) / Ts ) ) ;
13 S2=K
*
( Td / Ts ) ;
14
15 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
16 e t = s e t p o i n t temp ;
17 CO = u new ;
18
19 i f CO>39
20 CO = 39;
21 end ;
22
23 i f CO<0
24 CO = 0;
25 end ;
26
27 u new = CO;
28
29 u ol d = u new ;
215
30 e o l d o l d = e o l d ;
31 e o l d = e new ;
32
33
34 h e a t i n = CO;
35 f a n i n = d i s t u r b a n c e ;
36
37 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
38 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
39 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
40 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
41 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
42 s l e e p ( 1 ) ;
43 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
44 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
45
46 endf unct i on ;
10.8.4.3 Set Point Change in PID Controller
Scilab Code 10.5 pid bda.sci
1 mode ( 0 ) ;
2 / / P I D u s i n g B a c k w a r d d i f f e r e n c e a p p r o x i m a t i o n f o r I
a n d D
3
4 f unc t i on [ temp , CO, e t ] = pi d bda ( s e t p o i n t , d i s t u r b a n c e , K
, Ti , Td )
5 gl obal temp h e a t i n f a n i n e t SP CO e t i u ol d u new
e o l d e new e o l d o l d
6
7 e new = s e t p o i n t temp ;
8
9 Ts =1;
10
11 S0=K
*
( 1+( Ts / Ti ) +( Td / Ts ) ) ;
12 S1=K
*
( 1 ((2
*
Td ) / Ts ) ) ;
13 S2=K
*
( Td / Ts ) ;
216
14
15 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
16 e t = s e t p o i n t temp ;
17 CO = u new ;
18
19 i f CO>39
20 CO = 39;
21 end ;
22
23 i f CO<0
24 CO = 0;
25 end ;
26
27 u new = CO;
28
29 u ol d = u new ;
30 e o l d o l d = e o l d ;
31 e o l d = e new ;
32
33
34 h e a t i n = CO;
35 f a n i n = d i s t u r b a n c e ;
36
37 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
38 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
39 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
40 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
41 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
42 s l e e p ( 1 ) ;
43 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
44 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
45
46 endf unct i on ;
217
10.8.5 Self Tuning Controller, local
10.8.5.1 Fan Discturbance to PI Controller
Scilab Code 10.6 pi bda tuned dist.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g b a c k w a r d d i f f e r e n c e f o r m u l a
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , C0] = p i b d a t u n e d d i s t ( s e t p o i n t ,
d i s t u r b a n c e )
9
10 gl obal temp h e a t i n f a n i n C0 u ol d u new e o l d e new
11 / / L = 6 ;
12 / / R = ( 0 . 0 1 6
*
t e m p 0 . 1 1 4 ) / ( 6 6 . 9 0 0 . 4 1 5
*
t e m p ) ;
13 / / K = 0 . 9 / ( R
*
L ) ;
14 / / T i = 3
*
L ;
15
16 / / t h e a b o v e i s t h e z i e g l e r n i c h o l s p a r t
17
18
19 K = 2 / ( 0 . 0 1 6
*
temp 0. 114) ;
20 Ti = ( 66. 90 0. 415
*
temp ) ;
21
22 / / t h e a b o v e i s t h e d i r e c t s y n t h e s i s p a r t
23
24 e new = s e t p o i n t temp ;
25 Ts =1;
26 S0=K
*
( 1+( ( Ts / Ti ) ) ) ;
27 S1=K;
28 u new = u ol d +( S0
*
e new ) +( S1
*
e o l d ) ;
29
218
30
31 i f u new> 100
32 u new = 100;
33 end ;
34
35 i f u new< 0
36 u new = 0;
37 end ;
38
39 i f d i s t u r b a n c e > 100
40 d i s t u r b a n c e = 39;
41 end ;
42
43 i f d i s t u r b a n c e < 0
44 d i s t u r b a n c e = 0;
45 end ;
46
47 C0=u new ;
48 h e a t i n = C0 ;
49 f a n i n = d i s t u r b a n c e ;
50 u ol d = u new ;
51 e o l d = e new ;
52
53 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
54 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
55 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
56 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
57 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
58 s l e e p ( 10) ;
59 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
60 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
61
62 endf unct i on ;
10.8.5.2 Set Point Change to PI Controller
Scilab Code 10.7 pi bda tuned.sci
219
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g b a c k w a r d d i f f e r e n c e f o r m u l a
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , C0 , e new ] = p i b d a t u n e d ( s e t p o i n t ,
d i s t u r b a n c e )
9
10 gl obal temp h e a t i n f a n i n C0 u ol d u new e o l d e new
11 / / L = 6 ;
12 / / R = ( 0 . 0 1 6
*
t e m p 0 . 1 1 4 ) / ( 6 6 . 9 0 0 . 4 1 5
*
t e m p ) ;
13 / / K = 0 . 9 / ( R
*
L ) ;
14 / / T i = 3
*
L ;
15
16
17 / / T h e a b o v e i s t h e Z i e g l e r n i c h o l s p a r t .
18
19 K = 2 / ( 0 . 0 1 6
*
temp 0. 114) ;
20 Ti = ( 66. 90 0. 415
*
temp ) ;
21 / / T h e a b o v e i s t h e d i r e c t s y n t h e s i s p a r t
22
23 e new = s e t p o i n t temp ;
24
25 Ts =1;
26 S0=K
*
( 1+( ( Ts / Ti ) ) ) ;
27 S1=K;
28 u new = u ol d +( S0
*
e new ) +( S1
*
e o l d ) ;
29
30
31 i f u new> 39;
32 u new = 39;
33 end ;
34
35 i f u new< 0;
220
36 u new = 0;
37 end ;
38
39 C0=u new ;
40 h e a t i n = C0 ;
41 f a n i n = d i s t u r b a n c e ;
42 u ol d = u new ;
43 e o l d = e new ;
44
45 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
46 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
47 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
48 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
49 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
50 s l e e p ( 10) ;
51 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
52 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
53
54 endf unct i on ;
10.8.5.3 Fan Disturbance to PID Controller
Scilab Code 10.8 pid bda tuned dist.sci
1 mode ( 0 ) ;
2 / / P I D u s i n g B a c k w a r d d i f f e r e n c e a p p r o x i m a t i o n f o r I
a n d D
3
4 f unc t i on [ temp , CO] = p i d b d a t u n e d d i s t ( s e t p o i n t ,
d i s t u r b a n c e )
5 gl obal temp h e a t i n f a n i n e t SP CO e t i u ol d u new
e o l d e new e o l d o l d
6 L = 6;
7 R = ( 0. 016
*
temp 0. 114) / ( 66. 90 0. 415
*
temp ) ;
8 K = 1 . 2 / ( R
*
L)
9 Ti = 2
*
L;
10 Td = 0. 5
*
L;
11
221
12
13 e new = s e t p o i n t temp ;
14
15 Ts =1;
16
17 S0=K
*
( 1+( Ts / Ti ) +( Td / Ts ) ) ;
18 S1=K
*
( 1 ((2
*
Td ) / Ts ) ) ;
19 S2=K
*
( Td / Ts ) ;
20
21 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
22 e t = s e t p o i n t temp ;
23 CO = u new ;
24
25 i f CO>39
26 CO = 39;
27 end ;
28
29 i f CO<0
30 CO = 0;
31 end ;
32
33 u new = CO;
34
35 u ol d = u new ;
36 e o l d o l d = e o l d ;
37 e o l d = e new ;
38
39
40 h e a t i n = CO;
41 f a n i n = d i s t u r b a n c e ;
42
43 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
44 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
45 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
46 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
47 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
48 s l e e p ( 1 ) ;
49 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
222
50 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
51
52 endf unct i on ;
10.8.5.4 Set Point Change to PID Controller
Scilab Code 10.9 pid bda tuned.sci
1 mode ( 0 ) ;
2 / / P I D u s i n g B a c k w a r d d i f f e r e n c e a p p r o x i m a t i o n f o r I
a n d D
3
4 f unc t i on [ temp , CO, e t ] = p i d b d a t u n e d ( s e t p o i n t ,
d i s t u r b a n c e )
5 gl obal temp h e a t i n f a n i n e t SP CO e t i u ol d u new
e o l d e new e o l d o l d
6 L = 6;
7 R = ( 0. 016
*
temp 0. 114) / ( 66. 90 0. 415
*
temp ) ;
8 K = 1 . 2 / ( R
*
L)
9 Ti = 2
*
L;
10 / / K c a n d t a u i c a l c u l a t e d
11 Td = 0. 5
*
L;
12
13
14 e new = s e t p o i n t temp ;
15
16 Ts =1;
17
18 S0=K
*
( 1+( Ts / Ti ) +( Td / Ts ) ) ;
19 S1=K
*
( 1 ((2
*
Td ) / Ts ) ) ;
20 S2=K
*
( Td / Ts ) ;
21
22 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
23 e t = s e t p o i n t temp ;
24 CO = u new ;
25
26 i f CO>39
27 CO = 39;
223
28 end ;
29
30 i f CO<0
31 CO = 0;
32 end ;
33
34 u new = CO;
35
36 u ol d = u new ;
37 e o l d o l d = e o l d ;
38 e o l d = e new ;
39
40
41 h e a t i n = CO;
42 f a n i n = d i s t u r b a n c e ;
43
44 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / h e a t e r
45 wr i t e s e r i a l ( handl , a s c i i ( h e a t i n ) ) ;
46 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ;
47 wr i t e s e r i a l ( handl , a s c i i ( f a n i n ) ) ;
48 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ;
49 s l e e p ( 1 ) ;
50 temp = a s c i i ( r e a d s e r i a l ( handl , 2 ) ) ;
51 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ;
52
53 endf unct i on ;
10.8.6 Conventional Controller, virtual
10.8.7 Fan Disturbance in PI Controller
Scilab Code 10.10 pi bda dist.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g b a c k w a r d d i f f e r e n c e f o r m u l a
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
224
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 / / m o d e ( 1 ) ;
9 f unc t i on [ temp , heat , s t op ] = p i b d a d i s t ( s e t p o i n t ,
d i s t u r b a n c e , K, Ti , Td )
10 gl obal temp he a t f an u ol d u new e o l d e new S0 S1
f df h f d t f n c r fncw m e r r c o u n t s t op
11
12
13 f n c r = c l i e n t r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
14 fncw = c l i e n t wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
15
16 a = mget l ( f dt , 1 ) ;
17 b = e vs t r ( a ) ;
18 byt e = mt e l l ( f d t ) ;
19 mseek ( byt e , f dt , s e t ) ;
20
21 i f a = [ ]
22 temp = b ( 1 , $ ) ; h e a t s = b ( 1 , $2) ;
23 f a ns = b ( 1 , $1) ; y = temp ;
24
25 e new = s e t p o i n t temp ;
26
27 Ts =1;
28 S0=K
*
( 1+( ( Ts / Ti ) ) ) ;
29 S1=K;
30 u new = u ol d + S0
*
e new + S1
*
e o l d ;
31
32 i f u new> 39
33 u new = 39;
34 end ;
35
36 i f u new< 0
225
37 u new = 0;
38 end ;
39
40 he a t =u new ;
41 f an = d i s t u r b a n c e ;
42
43
44 u ol d = u new ;
45 e o l d = e new ;
46
47
48 A = [m, m, heat , f an ] ;
49 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
50 f i l e ( l a s t , f df h )
51 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
52 f i l e ( c l o s e , f df h ) ;
53 m = m+1;
54
55 e l s e
56 y = 0;
57 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
58 i f e r r c o u n t > 300
59 di s p ( NO NETWORK COMMUNICATION! ) ;
60 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
61 end
62 end
63
64 ret urn
65 endf unct i on
10.8.7.1 Set Point Change in PI Controller
Scilab Code 10.11 pi bda.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g b a c k w a r d d i f f e r e n c e f o r m u l a
226
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , heat , e new , s t op ] = pi bda ( s e t p o i n t ,
d i s t u r b a n c e , K, Ti )
9 gl obal temp he a t f an C0 u ol d u new e o l d e new f df h
f d t f n c r fncw m e r r c o u n t s t op
10
11
12 f n c r = c l i e n t r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
13 fncw = c l i e n t wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
14
15 a = mget l ( f dt , 1 ) ;
16 b = e vs t r ( a ) ;
17 byt e = mt e l l ( f d t ) ;
18 mseek ( byt e , f dt , s e t ) ;
19
20 i f a = [ ]
21 temp = b ( 1 , $ ) ; h e a t s = b ( 1 , $2) ;
22 f a ns = b ( 1 , $1) ; y = temp ;
23
24 e new = s e t p o i n t temp ;
25
26 Ts =1;
27 S0=K
*
( 1+( ( Ts / Ti ) ) ) ;
28 S1=K;
29 u new = u ol d +( S0
*
e new ) +( S1
*
e o l d ) ;
30
31 i f u new> 39
32 u new = 39;
33 end ;
34
227
35 i f u new< 0
36 u new = 0;
37 end ;
38
39 he a t =u new ;
40 f an = d i s t u r b a n c e ;
41
42
43 u ol d = u new ;
44 e o l d = e new ;
45
46
47 A = [m, m, heat , f an ] ;
48 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
49 f i l e ( l a s t , f df h )
50 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
51 f i l e ( c l o s e , f df h ) ;
52 m = m+1;
53
54 e l s e
55 y = 0;
56 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
57 i f e r r c o u n t > 300
58 di s p ( NO NETWORK COMMUNICATION! ) ;
59 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
60 end
61 end
62
63 ret urn
64 endf unct i on
10.8.7.2 Fan Disturbance to PID Controller
Scilab Code 10.12 pid bda dist.sci
1 mode ( 0 ) ;
228
2 / / P I D u s i n g B a c k w a r d d i f f e r e n c e a p p r o x i m a t i o n f o r I
a n d D
3
4 f unc t i on [ temp , heat , s t op ] = p i d b d a d i s t ( s e t p o i n t ,
d i s t u r b a n c e , K, Ti , Td )
5 gl obal temp he a t f an e t SP CO e t i u ol d u new e o l d
e new e o l d o l d f df h f d t f n c r fncw m e r r c o u n t s t op
6
7
8 f n c r = c l i e n t r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
9 fncw = c l i e n t wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
10
11 a = mget l ( f dt , 1 ) ;
12 b = e vs t r ( a ) ;
13 byt e = mt e l l ( f d t ) ;
14 mseek ( byt e , f dt , s e t ) ;
15
16 i f a = [ ]
17 temp = b ( 1 , $ ) ; h e a t s = b ( 1 , $2) ;
18 f a ns = b ( 1 , $1) ; y = temp ;
19
20 e new = s e t p o i n t temp ;
21
22 Ts =1;
23
24 S0=K
*
( 1+( Ts / Ti ) +( Td / Ts ) ) ;
25 S1=K
*
( 1 ((2
*
Td ) / Ts ) ) ;
26 S2=K
*
( Td / Ts ) ;
27
28 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
29 i f u new> 39
30 u new = 39;
31 end ;
32
33 i f u new< 0
34 u new = 0;
229
35 end ;
36
37 he a t =u new ;
38 f an = d i s t u r b a n c e ;
39
40 u ol d = u new ;
41 e o l d o l d = e o l d ;
42 e o l d = e new ;
43
44 A = [m, m, heat , f an ] ;
45 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
46 f i l e ( l a s t , f df h )
47 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
48 f i l e ( c l o s e , f df h ) ;
49 m = m+1;
50
51 e l s e
52 y = 0;
53 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
54 i f e r r c o u n t > 300
55 di s p ( NO NETWORK COMMUNICATION! ) ;
56 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
57 end
58 end
59
60 ret urn
61 endf unct i on
10.8.7.3 Set Point Change in PID Controller
Scilab Code 10.13 pid bda.sci
1 mode ( 0 ) ;
2 / / P I D u s i n g B a c k w a r d d i f f e r e n c e a p p r o x i m a t i o n f o r I
a n d D
3
230
4 f unc t i on [ temp , heat , et , s t op ] = pi d bda ( s e t p o i n t ,
d i s t u r b a n c e , K, Ti , Td )
5 gl obal temp he a t f an e t SP CO e t i u ol d u new e o l d
e new e o l d o l d f df h f d t f n c r fncw m e r r c o u n t s t op
6
7
8 f n c r = c l i e n t r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
9 fncw = c l i e n t wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
10
11 a = mget l ( f dt , 1 ) ;
12 b = e vs t r ( a ) ;
13 byt e = mt e l l ( f d t ) ;
14 mseek ( byt e , f dt , s e t ) ;
15
16 i f a = [ ]
17 temp = b ( 1 , $ ) ; h e a t s = b ( 1 , $2) ;
18 f a ns = b ( 1 , $1) ; y = temp ;
19
20 e new = s e t p o i n t temp ;
21
22 Ts =1;
23
24 S0=K
*
( 1+( Ts / Ti ) +( Td / Ts ) ) ;
25 S1=K
*
( 1 ((2
*
Td ) / Ts ) ) ;
26 S2=K
*
( Td / Ts ) ;
27
28 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
29
30 i f u new> 39
31 u new = 39;
32 end ;
33
34 i f u new< 0
35 u new = 0;
36 end ;
37
231
38 he a t =u new ;
39 f an = d i s t u r b a n c e ;
40
41 u ol d = u new ;
42 e o l d o l d = e o l d ;
43 e o l d = e new ;
44
45
46 A = [m, m, heat , f an ] ;
47 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
48 f i l e ( l a s t , f df h )
49 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
50 f i l e ( c l o s e , f df h ) ;
51 m = m+1;
52
53 e l s e
54 y = 0;
55 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
56 i f e r r c o u n t > 300
57 di s p ( NO NETWORK COMMUNICATION! ) ;
58 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
59 end
60 end
61
62 ret urn
63 endf unct i on
10.8.8 Self Tuning Controller, local
10.8.8.1 Fan Discturbance to PI Controller
Scilab Code 10.14 pi bda tuned dist.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g b a c k w a r d d i f f e r e n c e f o r m u l a
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
232
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , heat , s t op ] = p i b d a t u n e d d i s t ( s e t p o i n t
, d i s t u r b a n c e )
9 gl obal temp he a t f an C0 u ol d u new e o l d e new f df h
f d t f n c r fncw m e r r c o u n t s t op
10
11 K = 2 / ( 0 . 0 1 6
*
temp 0. 114) ;
12 Ti = ( 66. 90 0. 415
*
temp ) ;
13
14
15 f n c r = c l i e n t r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
16 fncw = c l i e n t wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
17
18 a = mget l ( f dt , 1 ) ;
19 b = e vs t r ( a ) ;
20 byt e = mt e l l ( f d t ) ;
21 mseek ( byt e , f dt , s e t ) ;
22
23 i f a = [ ]
24 temp = b ( 1 , $ ) ; h e a t s = b ( 1 , $2) ;
25 f a ns = b ( 1 , $1) ; y = temp ;
26
27 e new = s e t p o i n t temp ;
28
29 Ts =1;
30 S0=K
*
( 1+( ( Ts / Ti ) ) ) ;
31 S1=K;
32 u new = u ol d +( S0
*
e new ) +( S1
*
e o l d ) ;
33
34
35 i f u new> 39
36 u new = 39;
233
37 end ;
38
39 i f u new< 0
40 u new = 0;
41 end ;
42
43 he a t =u new ;
44 f an = d i s t u r b a n c e ;
45
46 u ol d = u new ;
47 e o l d = e new ;
48
49 A = [m, m, heat , f an ] ;
50 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
51 f i l e ( l a s t , f df h )
52 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
53 f i l e ( c l o s e , f df h ) ;
54 m = m+1;
55
56 e l s e
57 y = 0;
58 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
59 i f e r r c o u n t > 300
60 di s p ( NO NETWORK COMMUNICATION! ) ;
61 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
62 end
63 end
64
65 ret urn
66 endf unct i on
10.8.8.2 Set Point Change to PI Controller
Scilab Code 10.15 pi bda tuned.sci
1 mode ( 0 ) ;
2 / / P I C o n t r o l l e r u s i n g b a c k w a r d d i f f e r e n c e f o r m u l a
234
3 / / H e a t e r i n p u t i s p a s s e d a s i n p u t a r g u m e n t t o
i n t r o d u c e c o n t r o l e f f o r t u ( n )
4 / / F a n i n p u t i s p a s s e d a s i n p u t a r g u m e n t w h i c h i s k e p t
a t c o n s t a n t l e v e l
5 / / R a n g e o f F a n i n p u t : 2 0 t o 2 5 2
6 / / T e m p e r a t u r e i s r e a d
7
8 f unc t i on [ temp , heat , e new , s t op ] = p i b d a t u n e d (
s e t p o i n t , d i s t u r b a n c e )
9 gl obal temp he a t f an C0 u ol d u new e o l d e new f df h
f d t f n c r fncw m e r r c o u n t s t op
10
11 / / L = 6 ;
12 / / R = ( 0 . 0 1 6
*
t e m p 0 . 1 1 4 ) / ( 6 6 . 9 0 0 . 4 1 5
*
t e m p ) ;
13 / / K = 0 . 9 / ( R
*
L ) ;
14 / / T i = 3
*
L ;
15
16
17 / / T h e a b o v e i s t h e Z i e g l e r n i c h o l s p a r t .
18
19 K = 2 / ( 0 . 0 1 6
*
temp 0. 114) ;
20 Ti = ( 66. 90 0. 415
*
temp ) ;
21 / / T h e a b o v e i s t h e d i r e c t s y n t h e s i s p a r t
22
23
24 f n c r = c l i e n t r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
25 fncw = c l i e n t wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
26
27 a = mget l ( f dt , 1 ) ;
28 b = e vs t r ( a ) ;
29 byt e = mt e l l ( f d t ) ;
30 mseek ( byt e , f dt , s e t ) ;
31
32 i f a = [ ]
33 temp = b ( 1 , $ ) ; h e a t s = b ( 1 , $2) ;
34 f a ns = b ( 1 , $1) ; y = temp ;
235
35
36 e new = s e t p o i n t temp ;
37
38 Ts =1;
39 S0=K
*
( 1+( ( Ts / Ti ) ) ) ;
40 S1=K;
41 u new = u ol d +( S0
*
e new ) +( S1
*
e o l d ) ;
42
43
44 i f u new> 39
45 u new = 39;
46 end ;
47
48 i f u new< 0
49 u new = 0;
50 end ;
51
52 he a t =u new ;
53 f an = d i s t u r b a n c e ;
54
55 u ol d = u new ;
56 e o l d = e new ;
57
58 A = [m, m, heat , f an ] ;
59 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
60 f i l e ( l a s t , f df h )
61 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
62 f i l e ( c l o s e , f df h ) ;
63 m = m+1;
64
65 e l s e
66 y = 0;
67 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
68 i f e r r c o u n t > 300
69 di s p ( NO NETWORK COMMUNICATION! ) ;
70 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
71 end
236
72 end
73
74 ret urn
75 endf unct i on
10.8.8.3 Fan Disturbance to PID Controller
Scilab Code 10.16 pid bda tuned dist.sci
1 mode ( 0 ) ;
2 / / P I D u s i n g B a c k w a r d d i f f e r e n c e a p p r o x i m a t i o n f o r I
a n d D
3
4 f unc t i on [ temp , heat , s t op ] = p i d b d a t u n e d d i s t (
s e t p o i n t , d i s t u r b a n c e )
5 gl obal temp he a t f an e t SP CO e t i u ol d u new e o l d
e new e o l d o l d f df h f d t f n c r fncw m e r r c o u n t s t op
6 L = 6;
7 R = ( 0. 016
*
temp 0. 114) / ( 66. 90 0. 415
*
temp ) ;
8 K = 1 . 2 / ( R
*
L)
9 Ti = 2
*
L;
10 Td = 0. 5
*
L;
11
12
13 f n c r = c l i e n t r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
14 fncw = c l i e n t wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
15
16 a = mget l ( f dt , 1 ) ;
17 b = e vs t r ( a ) ;
18 byt e = mt e l l ( f d t ) ;
19 mseek ( byt e , f dt , s e t ) ;
20
21 i f a = [ ]
22 temp = b ( 1 , $ ) ; h e a t s = b ( 1 , $2) ;
23 f a ns = b ( 1 , $1) ; y = temp ;
24
237
25 e new = s e t p o i n t temp ;
26
27 Ts =1;
28
29 S0=K
*
( 1+( Ts / Ti ) +( Td / Ts ) ) ;
30 S1=K
*
( 1 ((2
*
Td ) / Ts ) ) ;
31 S2=K
*
( Td / Ts ) ;
32
33 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
34
35
36 i f u new> 39
37 u new = 39;
38 end ;
39
40 i f u new< 0
41 u new = 0;
42 end ;
43
44 he a t =u new ;
45 f an = d i s t u r b a n c e ;
46
47 u ol d = u new ;
48 e o l d o l d = e o l d ;
49 e o l d = e new ;
50
51 A = [m, m, heat , f an ] ;
52 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
53 f i l e ( l a s t , f df h )
54 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
55 f i l e ( c l o s e , f df h ) ;
56 m = m+1;
57
58 e l s e
59 y = 0;
60 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
61 i f e r r c o u n t > 300
238
62 di s p ( NO NETWORK COMMUNICATION! ) ;
63 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
64 end
65 end
66
67 ret urn
68 endf unct i on
10.8.8.4 Set Point Change to PID Controller
Scilab Code 10.17 pid bda tuned.sci
1 mode ( 0 ) ;
2 / / P I D u s i n g B a c k w a r d d i f f e r e n c e a p p r o x i m a t i o n f o r I
a n d D
3
4 f unc t i on [ temp , heat , et , s t op ] = p i d b d a t u n e d ( s e t p o i n t ,
d i s t u r b a n c e )
5 gl obal temp he a t f an e t SP CO e t i u ol d u new e o l d
e new e o l d o l d f df h f d t f n c r fncw m e r r c o u n t s t op
6 L = 6;
7 R = ( 0. 016
*
temp 0. 114) / ( 66. 90 0. 415
*
temp ) ;
8 K = 1 . 2 / ( R
*
L)
9 Ti = 2
*
L;
10 / / K c a n d t a u i c a l c u l a t e d
11 Td = 0. 5
*
L;
12
13
14 f n c r = c l i e n t r e a d . s ce ; / / f i l e t o b e r e a d
t e m p e r a t u r e
15 fncw = c l i e n t wr i t e . s ce ; / / f i l e t o b e w r i t t e n
h e a t e r , f a n
16
17 a = mget l ( f dt , 1 ) ;
18 b = e vs t r ( a ) ;
19 byt e = mt e l l ( f d t ) ;
20 mseek ( byt e , f dt , s e t ) ;
21
239
22 i f a = [ ]
23 temp = b ( 1 , $ ) ; h e a t s = b ( 1 , $2) ;
24 f a ns = b ( 1 , $1) ; y = temp ;
25
26 e new = s e t p o i n t temp ;
27
28 Ts =1;
29
30 S0=K
*
( 1+( Ts / Ti ) +( Td / Ts ) ) ;
31 S1=K
*
( 1 ((2
*
Td ) / Ts ) ) ;
32 S2=K
*
( Td / Ts ) ;
33
34 u new = u ol d + S0
*
e new + S1
*
e o l d + S2
*
e o l d o l d ;
35
36
37 i f u new> 39
38 u new = 39;
39 end ;
40
41 i f u new< 0
42 u new = 0;
43 end ;
44
45 he a t =u new ;
46 f an = d i s t u r b a n c e ;
47
48 u ol d = u new ;
49 e o l d o l d = e o l d ;
50 e o l d = e new ;
51
52 A = [m, m, heat , f an ] ;
53 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
54 f i l e ( l a s t , f df h )
55 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
56 f i l e ( c l o s e , f df h ) ;
57 m = m+1;
58
59 e l s e
240
60 y = 0;
61 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
62 i f e r r c o u n t > 300
63 di s p ( NO NETWORK COMMUNICATION! ) ;
64 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
65 end
66 end
67
68 ret urn
69 endf unct i on
241
Chapter 11
Model Predictive Control in Single
Board Heater System using
SCILAB
This chapter presents Model Predictive Control in Single Board Heater System
done by Mr. Pratik Behera.
1
11.1 Objective
To implement Model Predictive Control (MPC) in Single Board Heater Sys-
tem using Scilab and perform experiments using it
To perform experiments for various values of tuning parameters and study
its eect on the system
11.2 Single Board Heater System
It is a single heater system, where in, 5cm x 2cm stainless steel blade acts as a
plant, which is heated by a heating coil and cooled by a fan.
For Single Board Heater System (SBHS):
1
Copyright: Mr.Pratik Behera
242
Control variable: temperature
Manipulated variable: heater
Disturbance variable: fan
The heater element consists of Nichrome wire - of 0.7mm diameter, wound with
20 equally spaced helical turns into a coil of 5mm x 11mm. The heater element is
kept at a distance of 3.5 mm from the steel blade.
Cooling is done by a computer fan, which is placed below the stainless steel blade.
11.3 Model Predictive Control
An equivalent quadratic programming (QP) formulation for constrained DMC (as
given in LQG MPC notes by Prof Sachin Patwardhan) is given as follows
min
U
f
1
2
U
f
(k)
T
HU
f
(k) + F
T
U
f
(k) (11.1)
Subject to
AU
f
(k) b (11.2)
where
(11.3)
A =
_
I
qm
I
qm
_
b =
_
U
H
U
L
_
243
Also, we have outputs and manipulated variables related to state variables by
x(k + 1) = x(k) + (k) + w(k) (11.4)
y(k) = Cx(k) + v(k) (11.5)
(11.6)
is represented by matrix A in the code, is represented as matrix B and C is
represented as C matrix in the code.
11.4 Implementing MPC
As mentioned earlier, MPC experiments were performed on SBHS 12 remotely.
For this, the scilab codes uploaded on Moodle for Process Control SBHS assign-
ments were used. The folder containing the codes, which was used to perform
MPC experiments, have been included in the attached zip le in a folder named
codes.
11.5 Working of codes
There are three main codes, which are being used for this experiment. mpc init.sce
is the code which opens the xcos window, wherein, we have step block for the set-
point for temperature and the fan speed. Once the values have been entered into
the xcos window and the simulation is started, the scifunc block of xcos calls the
function mpc.sci after every sampling time. The mpc.sci in turn calls mpc run.sci
every time it is called by scifunc block. The mpc run.sci code optimizes manipu-
lated variable (heater) over control horizon and returns only the rst manipulated
variable (heater) value. This new heater value is then sent to the heater of the
SBHS to control the temperature at the set point.
11.6 Procedure to implement MPC on SBHS
1. Open the folder named Client-Java-latest10032011 and open VirtualLab-
Client.
2. After entering the details and connecting to the alloted SBHS, open Scilab
5.3.1
244
3. Change the current directory to this folder, where mpc codes are present.
4. Open mpc init.sce and load in scilab
5. The xcos window opens. Description of xcos window is mentioned below
in the next section.
6. Enter the required step change values, if any, in the Temperature set point
and/or fan block.
7. Enter the sampling time in the clock block as 1 second
8. Start the simulation from the xcos
9. After the experiment is over, the data les can be downloaded from the Java
client.
11.7 Description of xcos
When mpc init.sce is executed in scilab, an xcos window opens up. The xcos
window has two step input blocks. The rst step input block on the left side,
is for the Temperature set point and the second step input block is for the fan
(disturbance variable). Also the sampling time can be entered via clock block
present on the xcos.
For all the experiments done for this project, sampling time of 1 second was used
(entered via clock block of xcos).
Refer to the gure below for a clear picture of the xcos.
245
Figure 11.1: Screenshot of the xcos window with step input blocks labeled
After entering the values in the input step block, the simulation can be started.
This opens up a graph, which shows the values of Temperature-set-point, fan and
the actual temperature at each time instant during the simulation.
11.8 Code for MPC (mpc run.sci)
The MPC code, implemented by me has been mentioned below.
1 f unc t i on [ u new ] = mpc run ( T, u pr ev , Ts e t )
2 gl obal p q xk ol d
3
4 A = [ 0. 9780 0 0 0 0 0 0 0;
5 1 0 0 0 0 0 0 0;
6 0 1 0 0 0 0 0 0;
246
7 0 0 1 0 0 0 0 0;
8 0 0 0 1 0 0 0 0;
9 0 0 0 0 1 0 0 0;
10 0 0 0 0 0 1 0 0;
11 0 0 0 0 0 0 1 0 ] ;
12 B = [ 1 ; 0; 0; 0; 0; 0; 0; 0 ] ;
13 C = [ 0 0 0 0 0 0 0 0 . 0 0 7 9 ] ;
14
15 Tmax = 70; / / M a x i m u m T e m p e r a t u r e
16 We = 100
*
eye ( p , p ) ; / / E r r o r W e i g h t i n g M a t r i x , We
17 Wu = 10
*
eye ( q , q ) ; / / C o n t r o l W e i g h t i n g M a t r i x ,
Wu
18 xk=A
*
xk ol d+B
*
u pr e v ;
19
20 / / F o r m a t i o n o f S u M a t r i x f o r Q u a d t r a t i c t e r m o f
o p t i m i z a t i o n
21 f or i = 1 : 1 : p
22 f or j = 1 : 1 : q
23 i f i <= q
24 i f ( i j ) >= 0
25 Su ( i , j ) = C
*
A ( i j )
*
B;
26 e l s e
27 Su ( i , j ) = 0;
28 end
29 e l s e
30 i f j < q
31 Su ( i , j ) = C
*
A ( i j )
*
B;
32 e l s e
33 Su ( i , j ) = Su ( i 1 , j ) + C
*
A ( i j )
*
B;
34 end
35 end
36 end
37 end
38
39 du ma t r i x=ones ( q , q ) ;
40
41 / / L a m b d a M a t r i x f o r Q u a d t r a t i c t e r m o f o p t i m i z a t i o n
42 f or i = 1 : 1 : q
247
43 f or j = 1 : 1 : q
44 i f i == j
45 du ma t r i x ( i , j ) = 1;
46 i f i >1
47 du ma t r i x ( i , j 1) = 1;
48 end
49 e l s e
50 du ma t r i x ( i , j ) = 0;
51 end
52 end
53 end
54
55 du ma t r i x 0 = eye ( 1 , q ) ; / / D e c l a r a t i o n o f L a m b d a 0
v e c t o r
56
57 / / F o r m a t i o n o f S x M a t r i x f o r L i n e a r t e r m o f
o p t i m i z a t i o n
58 f or i = 1 : 1 : p
59 Sx ( i , : ) = C
*
A i ;
60 end
61
62 / / D e c l a r a t i o n o f S e t a M a t r i x
63 S e t a = ones ( 1 , p ) ;
64
65 / / D e c l a r a t i o n o f S e t P o i n t V e c t o r
66 R = ones ( 1 , p )
*
Ts e t ;
67
68 / / T e m p e r a t u r e P r e d i c t i o n u s i n g i n f o r m a t i o n t i l l
p r e v i o u s i n s t a n t
69 T pr ed = C
*
xk ;
70
71 / / M e a s u r e m e n t E r r o r
72 e t a = T T pr ed ;
73
74 / / Q u a d r a t i c T e r m f o r O p t i m i z a t i o n
75 Su t =Su ;
76 d u ma t r i x t =du mat r i x ;
77 Q=2
*
( ( Su t
*
We
*
Su ) +( d u ma t r i x t
*
Wu
*
du ma t r i x ) ) ;
248
78
79 / / L i n e a r T e r m i n O p t i m i z a t i o n
80 R t =R ;
81 S e t a t =S et a ;
82 d u ma t r i x 0 t =du ma t r i x 0 ;
83 F t e r m1 t =( R t ( Sx
*
xk ) ( S e t a t
*
e t a ) ) ;
84 F=2
*
(( F t e r m1 t
*
We
*
Su ) +( ( d u ma t r i x 0 t
*
u pr e v )
*
Wu
*
du ma t r i x ) ) ;
85
86 / / I n e q u a l i t y M a t r i c e s a n d V e c t o r s
87 A i neq = [ eye ( q , q ) ; 1
*
eye ( q , q ) ] ;
88
89 b i n e q t e r m1 1=Sx
*
xkS et a ;
90 b i n e q t e r m1 2 =(Tmax
*
ones ( 1 , p ) ) ;
91 b i ne q t e r m1 =( b i n e q t e r m1 1
*
e t a +b i n e q t e r m1 2 ) ;
92 b i n e q t e r m2 1=Sx
*
xkS et a
*
e t a ;
93 b i n e q t e r m2 2 =(Tmax
*
ones ( 1 , p ) ) ;
94 b i ne q t e r m2 =1
*
( b i n e q t e r m2 1+b i n e q t e r m2 2 ) ;
95 b i ne q = [ 40
*
ones ( 1 , q ) zeros ( 1 , q ) ] ;
96
97 me=0;
98 c i =zeros ( q , 1 ) ;
99 cs =40
*
ones ( q , 1 ) ;
100
101 [ x , i a c t , i t e r , f ]= qps ol ve (Q, F , A i neq , b i neq ) ;
102
103 u new=x ( 1 ) ;
104 xk ol d=xk ;
105 endf unct i on
11.9 Other codes used
Other codes that were used for conducting this experiment are mpc init.sce and
mpc.sci. Both these codes were originally taken from Moodle (Process controls
course for SBHS assignment). Please note that, both these codes were slightly
modied to work with our MPC.
The only changes done in the original codes are:
249
addition of global variables p,q and xk old (p is the prediction horizon, q is
the control horizon, xk old represents the last value of an internal state)
initialization of p, q and xk old
removal of some unnecessary lines (ie, lines not relevant to MPC imple-
mentation)
11.10 Experiments conducted to implement MPC
Experiments were performed as shown in table above for implementation of MPC.
We carried out experiments in which both positive and negative step changes were
given to Set point and Fan (disturbance variable) and the output response was ob-
tained by application of MPC. We also have performed several experiments to
study the eect of change in the values of q (control horizon) and tuning parame-
ters - error and manipulated variable weighting factors.
The details of the experiments mentioned in this report has been tabulated in the
table given in the next page. The rst column of the table represents the experi-
ment version (or number). For all the outputs and their gures, we have mentioned
only their experiment version (or number) to tag them. Also note that the data les
for these experiments are also named as per their experiment version number.
p and q mentioned in the table represents the prediction and control horizon re-
spectively.
Please note: For all the above experiments and graphs, we adhered to:
Scilab Version: 5.2.2
SBHS number: 12 (remotely accessed)
Sampling time: 1 second
For graphs: Until and unless mentioned, Graphic 1 represents the Temperature
set point, Graphic 2 represents the Fan and Graphic 3 represents the Temperature.
Also, please note that there are two types of graphs. The rst graph, containing
Graphic 1, Graphic 2 and Graphic 3 were directly obtained via mscope of xcos.
250
The graph following this in all the experiments is the temperature and heater value
graphs, which were obtained from the data (from the text le downloaded from
the server after each experiment).
Figure 11.2: Experiments performed
All the experiments mentioned in this report has been labeled as shown in this
table. This table is just a summary of all the parameters that was used for the
corresponding experiment. Details on the inputs and a description of the output
observed for each case has been mentioned in the corresponding section of each
experiment.
11.11 Sample run to implement MPC
11.12 Positive Step Change to Set Point and Fan
Let us consider experiment 1.1, wherein, a positive step change of 5
C (from 35
C to 40
C) was provided to set point at time t=250 s and a step change to fan was
provided at t = 500 s, from 100 to 150.
The graph obtained has been attached below:
251
Figure 11.3: Expt 1.1
252
Figure 11.4: Expt 1.1
As can, be seen above, when, the temperature set point is raised to 40 from 30,
at t=250 s, the value of the heater increases, so that it can heat up the plant upto
the required set point. Similarly, when the fan speed is increased at t=500s, the
heater value increases yet again to maintain the same constant temperature of the
SBHS blade.
253
11.13 Negative Step Change to Set Point and Fan
Let us consider experiment 2.1, wherein, a negative step change of 5
C (from
42
C to 37
C) was provided to set point at time t=250 s and a step change to fan
was provided at t = 500 s, from 150 to 100.
The graph obtained has been attached below:
254
Figure 11.5: Expt 2.1
255
Figure 11.6: Expt 2.1
As can be seen from the graphs above, when the temperature set point drops
at t=250 s, the value of the heater too falls, so that the plant (SBHS blade) can
cool down to the required set point. Similarly, when the fan speed was decreased
at t=500s, the heater value decreased yet again to maintain the same constant
temperature of the SBHS blade.
256
11.14 Eect of Tuning parameters: Weighting fac-
tors, We and Wu
We also, conducted several experiments in order the study the eect of the value of
Weighting factors (both error,We and manipulated variable,Wu). We used weight-
ing factors to be 1, 10 and 40 for both positive and negative step changes to both
set point and fan (as has been summarized in Table 1). Also, experiments were
done for dierent values of We and Wu. The results have been shown in the fol-
lowing graph.
257
11.15 For same factor of We and Wu
11.15.1 Positive Step Change and (We, Wu)=(1,1) (Expt 1.1)
Figure 11.7: Expt 1.1
258
Here we can clearly see the expected output. Providing a positive step to tempera-
ture set point at 250 seconds, increased heater value as per the control eort put in
by MPC. A positive step in fan at 500 seconds, decreased the temperature below
its set point and hence heater value increased to take the temperature close to its
setpoint.
This will be clear from the heater graph attached next.
259
Figure 11.8: Expt 1.1
As can be clearly seen, the heater graph follows the expected trend that we
talked of in the last page. Also, note that the temperature variation can be clearly
seen from this graph.
This graph shows the result for the case, where we had same weighting factors for
both error and manipulated variables (We and Wu). We will now see if changing
260
both of these is going to have any eect on the control behavior.
So, we now try an experiment with both We and Wu increased to 10.
11.15.2 Positive Step Change and (We, Wu)=(10,10) (Expt 1.2)
Figure 11.9: Expt 1.2
261
Using the same logic as has been explained in the last section, we expected to
see similar temperature and heater value proles for the positive step change in
temperature set point and the fan. (Heater graph is shown in the next page along
with the temperature on an expanded scale).
In this experiment, we increased We and Wu both to 10 from1 and wish to observe
if this changes the response of the plant.
262
Figure 11.10: Expt 1.2
The results here are almost the same as that mentioned in the last section
(where We and Wu both were 1). So, we can for the time being keep in mind
that We and Wu isnt actually much aected the output. We now will carry out
the experiment for even higher We and Wu (say 40) and see if it really does aect
the output much.
263
11.15.3 Positive Step Change and (We, Wu)=(40,40) (Expt 1.3)
Figure 11.11: Expt 1.3
264
Figure 11.12: Expt 1.3
Even the results with We and Wu as 40 doesnt show much dierence. They are
more or less similar looking as the last two experiments results.
265
11.15.4 Negative Step Change and (We,Wu)=(1,1) (Expt 2.1)
Figure 11.13: Expt 2.1
Here we expect somewhat similar results as was the case with positive step in
temperature set point.
266
Figure 11.14: Expt 2.1
We can very clearly make out that the results follow the trends as was ex-
plained for the negative step input in the section 5.2
267
11.15.5 Negative Step Change and (We, Wu)=(10,10) (Expt 2.2)
Figure 11.15: Expt 2.2
268
Figure 11.16: Expt 2.2
269
11.15.6 Negative Step Change and (We, Wu)=(40,40) (Expt 2.3)
Figure 11.17: Expt 2.3
270
Figure 11.18: Expt 2.3
11.16 For dierent We and Wu factors
We very clearly see that using the same values of We and Wu is not making much
dierence in the control response. So, will now be trying dierent values for We
and Wu.
271
11.16.1 We =100 and Wu = 2 (Expt 5.1)
Figure 11.19: Expt 5.1
Here, we have used We as 100 and Wu as 2. The response after the positive step in
temperature set point is slightly oscillatory. The temperature very well stabilzes
at the required setpoint. The settling time observed is fairly low.
272
Figure 11.20: Expt 5.1
Now having seen the results of this experiment, we would like to check the
possible eect of reversing the values of We and Wu. So, we conduct the next
experiment, in which we have We as 2 and Wu as 100.
273
11.16.2 We =2 and Wu = 100 (Expt 5.2)
Figure 11.21: Expt 5.2
With increase in Wu, we observe that the temperature stabilzes at the required ste-
point, but the settling time for reaching that setpoint increases. Also, the response
is not oscillatory. This result can be very clearly seen in the following temperature
274
and heater graph.
Figure 11.22: Expt 5.2
275
11.16.3 We =10 and Wu = 100 (Expt 5.3)
Having seen the eect of low We and high Wu (in the last section), we would like
to see what happens if We is slightly increased keeping Wu the same. For this we
increase the value of We to 10 and keep Wu at constant 100.
Figure 11.23: Expt 5.3
276
We observe that this experiments performs better than in the last section (where
We was 2). It is slighly oscillatory and also, the settling time decreased much as
compared to last experiment.
Figure 11.24: Expt 5.3
277
11.16.4 We =100 and Wu = 10 (Expt 5.4)
We now do a similar study for the case of Wu. We increase the value of Wu to 10,
keeping We constant at 100.
Figure 11.25: Expt 5.4
As is clear from the gure, we see slightly lesser oscillations compared to the
278
case when We was 100 and Wu was 2. Settling time more or less remained the
same.
Figure 11.26: Expt 5.4
279
11.17 Conclusion on Weighting factor experiments
For experiments with same values of We and Wu:
Not much dierence was seen in heater value trends and temperature value
trends for all the experiments performed above.
Reason for this will be clear from the discussion on the trends mentioned
below (for experiments with dierent values of We and Wu)
For experiments with dierent values of We and Wu:
Keeping We as large (around 100) and Wu as small (2) shows better per-
formance as compared to the case when the values are kept the other way
around.
With We very small (say around 1-2), oscillations are less, and the settling
time observed was found to be more.
With increase in We, the oscillations were observed to increase and the set-
tling time was found to reduce and hence, better control was observed.
So, with increase in We, any error is quickly dealt with, because with in-
crease in We, we are actually increasing the signicance of change of tem-
perature in deciding the control action.
With increase in Wu, oscillations reduced and the settling time was found
to increase and hence, less preferred.
So, the best performance is obtained for the cases with high We and low Wu.
11.18 Eect of Control Horizon Paramter, q
We also tried to study the eect of change of control horizon (q) on the response
of the SBHS to step change in Setpoint and disturbance variable. Generally the
value of q (control parameter) is taken somewhere between 2 to 5. So, we per-
formed our SBHS experiment for values of q as 2, 3 and 4 (as suggested by Mr
Prashant Gupta).
Both positive and negative step change experiments for temperature set point and
280
disturbance variable (fan) was performed for the sake of completeness. The re-
sults obtained thereby has been mentioned in form of graphs in this section. The
overall conclusion over these experiments has been mentioned in the conclusion
of this part.
281
11.19 For positive step change in Set point and Fan
speed
11.19.1 For q =2 (Expt 3.1)
Figure 11.27: Expt 3.1
282
Figure 11.28: Expt 3.1
283
11.19.2 For q =3 (Expt 3.2)
Figure 11.29: Expt 3.2
284
Figure 11.30: Expt 3.2
285
11.19.3 For q =4 (Expt 3.3)
Figure 11.31: Expt 3.3
286
Figure 11.32: Expt 3.3
287
11.20 For negative step change in Set point and Fan
speed
11.20.1 For q =2 (Expt 4.1)
Figure 11.33: Expt 4.1
288
Figure 11.34: Expt 4.1
289
11.20.2 For q =3 (Expt 4.2)
Figure 11.35: Expt 4.2
290
Figure 11.36: Expt 4.2
291
11.20.3 For q =4 (Expt 4.3)
Figure 11.37: Expt 4.3
292
Figure 11.38: Expt 4.3
11.21 Conclusion on the eect of Control Horizon
parameter
The eect of change in q isnt very distinct in the experiments performed.
293
While we are calculating the optimized value of manipulated variable at a
time, the number of manipulated input moves is increasing as we are in-
creasing the q value.
But, only the rst value of the optimized manipulated variable vector is used
for control.
Increase in q is only increasing the length of the manipulated variable vector
which is to be optimized.
Since, only the rst value of manipulated variable vector is used, which
itself lies in some specied range, the eect of changing q isnt very signif-
icant for SBHS.
Also, SBHS systemis a simple systemwith very fewvariables (as compared
to real life industrial systems).
Ideally, the value of q is to be maintained at 3 or 4.
11.22 Implementing Model Predictive controller on
SBHS, locally
Change the working directory of scilab to the folder where the local mpc code are
kept. Run the ser init.sce le with the appropriate com port number. Then
execute the le mpc init. Launch xcos and execute the le mpc.xcos. The code
is listed in Section 11.29
11.23 Conclusion for MPC project
The objective of this project, ie, implementing Model Predictive Control in Single
Board Heater System using Scilab was successfully achieved. Several experi-
ments were successfully performed using the developed SCILAB MPC algorithm
for both positive and negative step changes in both temperature-set-point and the
disturbance variable (fan).
In addition to the above objective, we also tried studying the eect of weight-
ing factors (tuning parameter) and control horizon parameter. We observed and
294
concluded that increase in values of We (error weighting factor), increases oscil-
lations and decreases settling time, while decrease in We leads to opposite eect.
Wu (manipulated variable weighting factor), on the other hand has an opposite ef-
fect. It decreases oscillations and increases settling time with increase in its value.
Hence, better control is obtained for high value of We and low value of Wu.
Thus, with this project, we were able to implement MPC successfully and also
were able to comment on the general preferred tuning parameters (weighting fac-
tors for error and manipulated variable).
11.24 Acknowledgement
Firstly, I would like to thank Prof Moudgalya Kannan, for giving me this opportu-
nity to undertake MPC project on SBHS. This project, which involved implement-
ing Model Predictive Control in SBHS using SCILAB, was very interesting and
provided an excellent learning opportunity. For developing the MPC algorithm,
lecture notes on Model Predictive Control by Prof Sachin Patwardhan too were ex-
tremely helpful. Also, I got to learn a lot from the speaking tutorials of SCILAB
and LaTeX, which had to be referred to for the completion of this project. Over
and above this, it was very encouraging to see the experiments working perfectly
with the developed Model Predictive Control algorithm.
I would also like to sincerely thank Mr Prashant Gupta, without whom, this project
would not have been splendidly completed. I would like to thank him for the time
he spent explaining the concepts, clearing the doubts and suggestions for the ex-
periments to implement MPC.
295
11.25 Appendix
11.26 Appendix 1: General Information on Experi-
ments for this Project
All the experiments for this project was performed remotely on SBHS 12, using
a sampling time of 1 second. Basic codes (mpc init.sce and mpc.sci) was taken
from moodle for this course. Code for implementing MPC was written in scilab
and has been mentioned in the report.
Scilab Version used: 5.2.2
SBHS number: 12 (remotely used)
Sampling time: 1 second
For graphs: Until and unless mentioned, Graphic 1 represents the Temperature
set point, Graphic 2 represents the Fan and Graphic 3 represents the Temperature.
11.27 Appendix 2: Values of State Space matrices
Initially, open loop experiment was performed, and Plant Transfer function was
obtained. For the open loop experiment, a step change in heater from 15 to 25
units at t =200 seconds was provided (sampling time 1s). The response data was
tted to a rst order transfer function with a time delay and the following was
observed:
Kp=0.37, time constant = 45s and delay = 7s.
Using the above, we obtained the plant transfer function:
G
p
=
0.37
1 + 45s
e
7s
(11.7)
Scilab Method to calculate State Space matrices
State space matrices for a transfer function can be calculated as follows using
Scilab:
296
1 s=pol y ( 0 , s ) ;
2 TFcont =s y s l i n ( c , [ kp
*
( 1 0. 5
*
D) / ( t a u
*
s +1) / ( 1+0. 5
*
D) ] ) ;
3 SScont =t f 2 s s ( TFcont ) ;
SScont (in the last line above), has the value of the required State Space matrices.
(Please note: Time delays can not be directly handled in Scilab. So, for systems
with delays, we will have to use alternate approach. Pades approximation for
time delay being one of the approach.)
The transfer function which we dervied for our SBHS was very close to the trans-
fer function derived by Mr Prashant Gupta. So, using the values of A, B and C
which were already calculated by him previously, we obtain the following exact
values:
A =
_
_
0.9780 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
_
_
B =
_
_
1
0
0
0
0
0
0
0
_
_
C =
_
0 0 0 0 0 0 0 0.0079
_
297
11.28 Appendix 3: Attachments and Contact Infor-
mation
Attachments
Folder named codes, which contains all the codes used for the experiments.
The codes in this folder should be used to reproduce MPC control experi-
ments mentioned in this report.
Folder named data les, which contains data les for all the experiments
performed
MPC Report
Contact Information
My details:
Pratik Behera (07002054)
pratik behera@iitb.ac.in
+91 99871 54061
11.29 Scilab Code
Scilab Code 11.1 mpc init.sce
1 / / F o r s c i l a b 5 . 1 . 1 o r l o w e r v e r s i o n u s e r s ,
2 / / u s e s c i c o s c o m m a n d t o o p e n s c i c o s d i a g r a m s i n s t e a d
o f x c o s
3
4 gl obal f df h f d t f n c r fncw m e r r c o u n t y p q xk ol d
5
6 p = 40; / / p r e d i c t i o n h o r i z o n
7 q = 4; / / c o n t r o l h o r i z o n
8 xk ol d = zeros ( 8 , 1 ) ;
9
10 f n c r = c l i e n t r e a d . s ce ;
11 f d t = mopen( f n c r ) ;
12 mseek ( 0 ) ;
13
298
14 e r r c o u n t = 0; / / i n i t i a l i s i n g e r r o r c o u n t f o r n e t w o r k
e r r o r
15 m =1;
16 exec ( mpc . s c i ) ;
17 exec ( mpc run . s c i ) ;
18
19 f df h = mopen( c l i e n t wr i t e . s ce ) ;
20 mseek ( 0 ) ;
21 b = mget l ( f df h , 1 ) ;
22 a = mget l ( f dt , 1 ) ;
23 mcl ose ( f df h ) ;
24
25 i f a = [ ]
26 i f b= [ ]
27 di s p ( ERROR!EMPTY THE CLIENTREAD AND CLIENTWRITE
FILES ) ;
28 ret urn
29 e l s e
30 di s p ( ERROR!EMPTY THE CLIENTREAD FILE ) ;
31 ret urn
32 end
33 e l s e
34 i f b= [ ]
35 di s p ( ERROR!EMPTY THE CLIENTWRITE FILE ) ;
36 ret urn
37 end ;
38 A = [ 0 . 1 , m, 0 , 2 5 1 ] ;
39 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
40 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
41 f i l e ( c l o s e , f df h ) ;
42 s l e e p ( 1000) ;
43 a = mget l ( f dt , 1 ) ;
44 mseek ( 0 ) ;
45 i f a = [ ] / / o p e n x c o s o n l y i f c o m m u n i c a t i o n i s
t h r o u g h
46 xcos ( mpc . xcos ) ;
47 e l s e
48 di s p ( NO NETWORK CONNECTION! ) ;
299
49 ret urn
50 end
Scilab Code 11.2 mpc.sci
1 f unc t i on [ y , s t op ] = mpc ( Tsp , f an )
2 gl obal f df h f d t f n c r fncw m e r r c o u n t s t op p q xk ol d
3
4 f n c r = c l i e n t r e a d . s ce ;
5 fncw = c l i e n t wr i t e . s ce ;
6
7 a = mget l ( f dt , 1 ) ;
8 b = e vs t r ( a ) ;
9 byt e = mt e l l ( f d t ) ;
10 mseek ( byt e , f dt , s e t ) ;
11
12 i f a = [ ]
13 t emps = b ( 1 , $ ) ; h e a t s = b ( 1 , $2) ;
14 f a ns = b ( 1 , $1) ; y = t emps ;
15
16 he a t = mpc run ( y , he a t s , Tsp ) ;
17
18 i f heat >40
19 he a t = 40;
20 e l s e i f heat <0
21 he a t = 0;
22 end ;
23
24 A = [m, m, heat , f an ] ;
25 f df h = f i l e ( open , c l i e n t wr i t e . s ce , unknown ) ;
26 f i l e ( l a s t , f df h )
27 wri t e ( f df h , A, ( 7 ( e11 . 5 , 1 x ) ) ) ;
28 f i l e ( c l o s e , f df h ) ;
29 m = m+1;
30
31 e l s e
32 y = 0;
300
33 e r r c o u n t = e r r c o u n t + 1; / / c o u n t s t h e n o o f
t i m e s n e t w o r k e r r o r o c c u r s
34 i f e r r c o u n t > 300
35 di s p ( NO NETWORK COMMUNICATION! ) ;
36 s t op = 1; / / s t a t u s s e t f o r s t o p p i n g s i m u l a t i o n
37 end
38 end
39
40 ret urn
41 endf unct i on
Scilab Code 11.3 mpc init local.sce
1 / / F o r s c i l a b 5 . 1 . 1 o r l o w e r v e r s i o n u s e r s ,
2 / / u s e s c i c o s c o m m a n d t o o p e n s c i c o s d i a g r a m s i n s t e a d
o f x c o s
3
4 gl obal e r r c o u n t y p q xk ol d Tsp h e a t s f an temp he a t
5
6 p = 40; / / p r e d i c t i o n h o r i z o n
7 q = 4; / / c o n t r o l h o r i z o n
8 xk ol d = zeros ( 8 , 1 ) ;
9 Tsp =1;
10 h e a t s =1;
11 f an =1;
12 temp =1;
13
14 exec ( mpc l oc a l . s c i ) ;
15 exec ( mpc run . s c i ) ;
Scilab Code 11.4 mpc local.sci
1
2 gl obal temp he a t h e a t s e t SP u new u ol d u new e o l d
e new e r r c o u n t s t op p q xk ol d
3
4 f unc t i on [ temp ] = mpc ( Tsp , f an )
5
301
6 u new = mpc run ( temp , he a t s , Tsp ) ;
7
8
9 i f u new>40
10 u new = 40;
11 end ;
12
13 i f u new<0
14 u new = 0
15 ;
16 end ;
17
18 he a t =u new ;
19
20 h e a t s =he a t ;
21
22
23 wr i t e s e r i a l ( handl , a s c i i ( 254) ) ; / / I n p u t H e a t e r ,
w r i t e s e r i a l a c c e p t s s t r i n g s ; s o c o n v e r t 2 5 4 i n t o
i t s s t r i n g e q u i v a l e n t
24 wr i t e s e r i a l ( handl , a s c i i ( he a t ) ) ;
25 wr i t e s e r i a l ( handl , a s c i i ( 253) ) ; / / I n p u t F a n
26 wr i t e s e r i a l ( handl , a s c i i ( f an ) ) ;
27 wr i t e s e r i a l ( handl , a s c i i ( 255) ) ; / / T o r e a d T e m p
28 s l e e p ( 100) ;
29
30 temp = a s c i i ( r e a d s e r i a l ( handl ) ) ; / / R e a d s e r i a l
r e t u r n s a s t r i n g , s o c o n v e r t i t t o i t s i n t e g e r (
a s c i i ) e q u i v a l e n t
31 temp = temp ( 1 ) + 0. 1
*
temp ( 2 ) ; / / c o n v e r t t o t e m p w i t h
d e c i m a l p o i n t s e g : 4 0 . 7
32
33 endf unct i on ;
302
Bibliography
[1] Fossee moodle. http://www.fossee.in/moodle/. Seen on 10 May 2011.
[2] Spoken tutorials. http://spoken-tutorial.org/Study Plans Scilab. Seen on 10
May 2011.
[3] K. M. Moudgalya. Introducing National Mission on Education through ICT.
http://www.spoken-tutorial.org/NMEICT-Intro, 2010.
[4] K. M. Moudgalya and Inderpreet Arora. A Virtual Laboratory for Distance
Education. In Proceedings of 2nd Int. conf. on Technology for Education,
T4E, IIT Bombay, India, 13 July 2010. IEEE.
[5] Kannan M. Moudgalya. Digital Control. John Wiley and Sons, 2009.
[6] Kannan M. Moudgalya. Identication of transfer function of a single board
heater system through step response experiments. 2009.
[7] Katsuhiko Ogata. Modern Control Engineering. Prentice-Hall of India, 2005.
[8] Dale E. Seborg, Thomas F. Edgar, and Duncan A. Mellichamp. Process Dy-
namics and Control. John Wiley and Sons, 2nd edition, 2004.
[9] Virtual labs project. Single board heater system. http://www.co-
learn.in/web sbhs. Seen on 11 May 2011.
303