Control Systems with Scilab Aditya Sengupta Indian Institute of Technology Bombay apsengupta@iitb.ac.in December 1, 2010, Mumbai A simple first order system // D e f i n i n g a f i r s t o r d e r s y s t e m : s = %s // The q u i c k e r a l t e r n a t i v e t o u s i n g s = poly (0 , ’ s ’ ) K = 1, T = 1 // G a i n and t i m e c o n s t a n t SimpleSys = s y s l i n ( ’ c ’ , K /(1+ T * s ) ) Aditya Sengupta, EE, IITB CACSD with Scilab Simulating the system- Step test t =0:0.01:15; y1 = c s i m ( ’ s t e p ’ , t , SimpleSys ) ; // s t e p r e s p o n s e p l o t ( t , y1 ) Aditya Sengupta, EE, IITB CACSD with Scilab Simulating the system- Sine test u2=s i n ( t ) ; y2 = c s i m ( u2 , t , SimpleSys ) ; p l o t ( t , [ u2 ; y2 ] ’ ) Aditya Sengupta, EE, IITB // s i n e r e s p o n s e CACSD with Scilab Simulating the system- Sine test u3=s i n ( 5 * t ) ; y3 = c s i m ( u3 , t , SimpleSys ) ; frequency p l o t ( t , [ u3 ; y3 ] ’ ) Aditya Sengupta, EE, IITB // s i n e r e s p o n s e a t d i f f e r e n t CACSD with Scilab Root Locus e v a n s ( SimpleSys ) Aditya Sengupta, EE, IITB CACSD with Scilab Bode Plot fMin = 0 . 0 1 , fMax=10 bode ( SimpleSys , fMin , fMax ) Aditya Sengupta, EE, IITB CACSD with Scilab Second Order Systems- Overdamped p=sˆ2+9 * s+9 O v e rd a mp e d S y s t e m= s y s l i n ( ’ c ’ , 9/ p ) roots (p) Aditya Sengupta, EE, IITB CACSD with Scilab Second Order Systems- Overdamped p=sˆ2+9 * s+9 O v e rd a mp e d S y s t e m= s y s l i n ( ’ c ’ , 9/ p ) roots (p) Aditya Sengupta, EE, IITB CACSD with Scilab Second Order Systems- Overdamped y4 = c s i m ( ’ s t e p ’ , t , Ov e rd a mp ed S ys t em ) ; p l o t ( t , y4 ) Aditya Sengupta, EE, IITB CACSD with Scilab Second Order Systems- Overdamped bode ( OverdampedSystem , fMin , fMax ) Aditya Sengupta, EE, IITB CACSD with Scilab Second Order Systems- Underdamped q=sˆ2+2 * s+9 U n d er d am p e d S y s t e m = s y s l i n ( ’ c ’ , 9/ q ) roots (q) Aditya Sengupta, EE, IITB CACSD with Scilab Second Order Systems- Underdamped y5 = c s i m ( ’ s t e p ’ , t , U nd e r d am p e d Sy s t e m ) ; p l o t ( t , y5 ) Aditya Sengupta, EE, IITB CACSD with Scilab Second Order Systems- Undamped r = sˆ2+9 Unda mpedS ys te m = s y s l i n ( ’ c ’ , 9/ r ) roots (r) Aditya Sengupta, EE, IITB CACSD with Scilab Second Order Systems- Undamped y6 = c s i m ( ’ s t e p ’ , t , Un damp edSys tem ) ; p l o t ( t , y6 ) Aditya Sengupta, EE, IITB CACSD with Scilab Second Order Systems- Critically damped m = sˆ2+6 * s+9 C r i t i c a l l y D a m p e d S y s t e m = s y s l i n ( ’ c ’ , 9/ m ) roots (m) Aditya Sengupta, EE, IITB CACSD with Scilab Second Order Systems- Critically damped y7 = c s i m ( ’ s t e p ’ , t , C r i t i c a l l y D a m p e d S y s t e m ) ; p l o t ( t , y7 ) Aditya Sengupta, EE, IITB CACSD with Scilab Discrete time systems z = %z a = 0.1 DTSystem = s y s l i n ( ’ d ’ , a * z / ( z − (1−a ) ) ) Aditya Sengupta, EE, IITB CACSD with Scilab Discrete time systems u = ones (1 , 50) ; y = f l t s ( u , DTSystem ) ; p l o t ( y ) // C l o s e t h i s when done Aditya Sengupta, EE, IITB CACSD with Scilab Discrete time systems bode ( DTSystem , 0 . 0 0 1 , 1 ) Aditya Sengupta, EE, IITB CACSD with Scilab Discrete time systems e v a n s ( DTSystem ) Aditya Sengupta, EE, IITB CACSD with Scilab State space- representation A B C D = = = = [ 0 , 1 ; −1, −0. 5] [ 0 ; 1] [1 , 0] [0] x0 = [ 1 ; 0 ] // The i n i t i a l state SSsys = s y s l i n ( ’ c ’ , A , B , C , D , x0 ) Aditya Sengupta, EE, IITB CACSD with Scilab State space- simulation t = [0: 0.1: 50]; u = 0.5* ones (1 , l e n g t h ( t ) ) ; [ y , x ] = c s i m ( u , t , SSsys ) ; Aditya Sengupta, EE, IITB CACSD with Scilab State space- simulation scf ( 1 ) ; p l o t ( t , y ) Aditya Sengupta, EE, IITB CACSD with Scilab State space- simulation scf ( 2 ) ; p l o t ( t , x ) Aditya Sengupta, EE, IITB CACSD with Scilab State space e v a n s ( SSsys ) // zoom i n // C o n v e r s i o n from s t a t e s p a c e t o t r a n s f e r f u n c t i o n : s s 2 t f ( SSsys ) r o o t s ( denom ( a n s ) ) spec (A) Try this: obtain the step response of the converted transfer function. Then compare this with the step response of the state space representation (remember to set the initial state (x0) and step size (u) correctly. Aditya Sengupta, EE, IITB CACSD with Scilab State space e v a n s ( SSsys ) // zoom i n // C o n v e r s i o n from s t a t e s p a c e t o t r a n s f e r f u n c t i o n : s s 2 t f ( SSsys ) r o o t s ( denom ( a n s ) ) spec (A) Try this: obtain the step response of the converted transfer function. Then compare this with the step response of the state space representation (remember to set the initial state (x0) and step size (u) correctly. Aditya Sengupta, EE, IITB CACSD with Scilab Discretizing continuous time systems Simp leSys Di sc r = s s 2 t f ( d s c r ( SimpleSys , 0 . 1 ) ) // S i n c e d s c r ( ) r e t u r n s a s t a t e s p a c e model Aditya Sengupta, EE, IITB CACSD with Scilab Discretizing continuous time systems t = [0: 0.1: 10]; u = ones ( t ) ; y = f l t s ( u , Si mp le SysDi scr ) ; plot (t , y) Aditya Sengupta, EE, IITB CACSD with Scilab Multiple subsystems SubSys1 = s y s l i n ( ’ c ’ , 1/ s ) SubSys2 = 2 // System w i t h g a i n 2 Series = SubSys1 * SubSys2 Parallel = SubSys1+SubSys2 Feedback = SubSys1 / . SubSys2 // Note s l a s h −dot , n o t dot−s l a s h // A l s o t r y t h e a b o v e s t e p u s i n g 2 i n s t e a d o f S u b s y s 2 Hint: put a space after the dot and then try. Aditya Sengupta, EE, IITB CACSD with Scilab Multiple subsystems SubSys1 = s y s l i n ( ’ c ’ , 1/ s ) SubSys2 = 2 // System w i t h g a i n 2 Series = SubSys1 * SubSys2 Parallel = SubSys1+SubSys2 Feedback = SubSys1 / . SubSys2 // Note s l a s h −dot , n o t dot−s l a s h // A l s o t r y t h e a b o v e s t e p u s i n g 2 i n s t e a d o f S u b s y s 2 Hint: put a space after the dot and then try. Aditya Sengupta, EE, IITB CACSD with Scilab Nyquist Plot G = s y s l i n ( ’ c ’ , ( s −2) / ( s+1) ) ; H = 1 n y q u i s t ( G*H ) Aditya Sengupta, EE, IITB CACSD with Scilab An Example Aditya Sengupta, EE, IITB CACSD with Scilab Modelling the system mẍ(t) + b ẋ(t) + kx(t) = F (t) Taking the Laplace transform: ms 2 X (x) + bsX (s) + kX (s) = F (s) X (s) 1 = F (s) ms 2 + bs + k We will use a scaling factor of k and represent the system as: G (s) = ms 2 Aditya Sengupta, EE, IITB k + bs + k CACSD with Scilab Modelling the system in Scilab m b k s = = = = 1 // Mass : 10 // Damper : 20 // S p r i n g : %s kg Ns/m N/m // System : System = k / ( m * s ˆ2 + b * s + k ) // We u s e k a s o u r s c a l i n g f a c t o r . Aditya Sengupta, EE, IITB CACSD with Scilab A second order model Comparing the system: ms 2 k + bs + k with the standard representation of a second order model: ωn2 s 2 + 2ζωn s + ωn2 We have r ωn = and ζ= Aditya Sengupta, EE, IITB k m b/m 2ωn CACSD with Scilab Understanding the system wn = s q r t ( k / m ) zeta = ( b / m ) / ( 2 * wn ) We note that this is an overdamped system since ζ > 1 Aditya Sengupta, EE, IITB CACSD with Scilab // S t e p r e s p o n s e ( open l o o p t r a n s f e r f u n c t i o n ) : t = 0:0.01:3; y = c s i m ( ’ s t e p ’ , t , System ) ; plot (t , y) Aditya Sengupta, EE, IITB CACSD with Scilab // S t e p r e s p o n s e o f t h e s y s t e m w i t h u n i t y f e e d b a c k : t = 0:0.01:3; yFeedback = c s i m ( ’ s t e p ’ , t , System / . 1 ) ; p l o t ( t , yFeedback ) Aditya Sengupta, EE, IITB CACSD with Scilab // Comparing t h e open l o o p t r a n s f e r f u n c t i o n and c l o s e d l o o p transfer function : p l o t ( t , [ y ; yFeedback ] ) Aditya Sengupta, EE, IITB CACSD with Scilab Steady State error From the final value theorem: lim f (t) = lim sF (s) t→∞ s→0 Rs = 1/ s Gs = System // The s t e a d y s t a t e v a l u e o f t h e s y s t e m i s : css = h o r n e r ( s * System * Rs , 0 ) After adding the feedback loop: css = h o r n e r ( s * ( System / . 1 ) * Rs , 0 ) Aditya Sengupta, EE, IITB CACSD with Scilab Steady State error From the final value theorem: lim f (t) = lim sF (s) t→∞ s→0 Rs = 1/ s Gs = System // The s t e a d y s t a t e v a l u e o f t h e s y s t e m i s : css = h o r n e r ( s * System * Rs , 0 ) After adding the feedback loop: css = h o r n e r ( s * ( System / . 1 ) * Rs , 0 ) Aditya Sengupta, EE, IITB CACSD with Scilab Lets take a look at the root locus e v a n s ( System ) Aditya Sengupta, EE, IITB CACSD with Scilab Finding the gain at a point on the root locus Say we have the root locus in front of us. Now we wish to calculate some system parameters at some particular point along the root locus. How do we find the position of this point? Aditya Sengupta, EE, IITB CACSD with Scilab Finding the gain at a point on the root locus The root locus is the plot of the location of the poles of: KG (s) 1 + KG (s) as k varies. That is to say, we need to find the solution of the denominator going to zero: 1 + KG (s) = 0 or K= Aditya Sengupta, EE, IITB −1 G (s) CACSD with Scilab Finding the gain at a point on the root locus We can find the location of a given point on the root locus using the locate() command. We then need to multiply the [x; y] coordinates returned by this command with [ 1, % i ] so that we obtain the position in the complex plane as x + iy We then simply evaluate -1/G(s) at the given position using horner() Aditya Sengupta, EE, IITB CACSD with Scilab Try this now: e v a n s ( System ) Kp = −1/ r e a l ( h o r n e r ( System , [ 1 %i ] * l o c a t e ( 1 ) ) ) // C l i c k on a p o i n t on t h e r o o t l o c u s css = h o r n e r ( s * ( Kp * System / . 1 ) * Rs , 0 ) // T h i s i s t h e s t e a d y s t a t e v a l u e o f t h e c l o s e d l o o p s y s t e m // w i t h g a i n Kp . (Choose any point on the root locus right now, we will shortly see how to decide which point to choose) Aditya Sengupta, EE, IITB CACSD with Scilab Achieving specific parameters- a proportional controller Lets say we wish to achieve the following parameters: OS = 0 . 3 0 tr = 0 . 0 8 // O v e r s h o o t // R i s e t i m e We know from theory, for a second order system, − ln OS ζ=p π2 ωn = 1 tr p 1 − ζ2 + (ln OS)2 ! p 2 1 − ζ π − tan−1 ζ In Scilab: zeta = −l o g ( OS ) / s q r t ( %pi ˆ2 + l o g ( OS ) ˆ 2 ) wn = ( 1 / ( tr * s q r t ( 1 − zeta ˆ 2 ) ) ) * ( %pi − a t a n ( s q r t ( 1 − zeta ˆ 2 ) ) / zeta ) We use these values of ζ and ωn to decide which point to choose on the root locus. Aditya Sengupta, EE, IITB CACSD with Scilab Achieving specific parameters- a proportional controller Lets say we wish to achieve the following parameters: OS = 0 . 3 0 tr = 0 . 0 8 // O v e r s h o o t // R i s e t i m e We know from theory, for a second order system, − ln OS ζ=p π2 ωn = 1 tr p 1 − ζ2 + (ln OS)2 ! p 2 1 − ζ π − tan−1 ζ In Scilab: zeta = −l o g ( OS ) / s q r t ( %pi ˆ2 + l o g ( OS ) ˆ 2 ) wn = ( 1 / ( tr * s q r t ( 1 − zeta ˆ 2 ) ) ) * ( %pi − a t a n ( s q r t ( 1 − zeta ˆ 2 ) ) / zeta ) We use these values of ζ and ωn to decide which point to choose on the root locus. Aditya Sengupta, EE, IITB CACSD with Scilab Achieving specific parameters- a proportional controller Lets say we wish to achieve the following parameters: OS = 0 . 3 0 tr = 0 . 0 8 // O v e r s h o o t // R i s e t i m e We know from theory, for a second order system, − ln OS ζ=p π2 ωn = 1 tr p 1 − ζ2 + (ln OS)2 ! p 2 1 − ζ π − tan−1 ζ In Scilab: zeta = −l o g ( OS ) / s q r t ( %pi ˆ2 + l o g ( OS ) ˆ 2 ) wn = ( 1 / ( tr * s q r t ( 1 − zeta ˆ 2 ) ) ) * ( %pi − a t a n ( s q r t ( 1 − zeta ˆ 2 ) ) / zeta ) We use these values of ζ and ωn to decide which point to choose on the root locus. Aditya Sengupta, EE, IITB CACSD with Scilab Achieving specific parameters- a proportional controller Lets say we wish to achieve the following parameters: OS = 0 . 3 0 tr = 0 . 0 8 // O v e r s h o o t // R i s e t i m e We know from theory, for a second order system, − ln OS ζ=p π2 ωn = 1 tr p 1 − ζ2 + (ln OS)2 ! p 2 1 − ζ π − tan−1 ζ In Scilab: zeta = −l o g ( OS ) / s q r t ( %pi ˆ2 + l o g ( OS ) ˆ 2 ) wn = ( 1 / ( tr * s q r t ( 1 − zeta ˆ 2 ) ) ) * ( %pi − a t a n ( s q r t ( 1 − zeta ˆ 2 ) ) / zeta ) We use these values of ζ and ωn to decide which point to choose on the root locus. Aditya Sengupta, EE, IITB CACSD with Scilab Achieving specific parameters- a proportional controller e v a n s ( System ) s g r i d ( zeta , wn ) Aditya Sengupta, EE, IITB CACSD with Scilab Achieving specific parameters- a proportional controller Find the value of proportional gain at some point on the root locus: Kp = −1/ r e a l ( h o r n e r ( System , [ 1 %i ] * l o c a t e ( 1 ) ) ) // C l i c k n e a r t h e i n t e r s e c t i o n Aditya Sengupta, EE, IITB CACSD with Scilab Achieving specific parameters- a proportional controller PropSystem = Kp * System / . 1 yProp = c s i m ( ’ s t e p ’ , t , PropSystem ) ; p l o t ( t , yProp ) p l o t ( t , o n e s ( t ) , ’ r ’ ) , // Compare w i t h s t e p Aditya Sengupta, EE, IITB CACSD with Scilab A PI controller Note the steady state error and overshoot. In order to eliminate the steady state error, we need to add an integrator- that is to say, we add a pole at origin. In order to have the root locus pass through the same point as before (and thus achieve a similar transient performance), we add a zero near the origin Aditya Sengupta, EE, IITB CACSD with Scilab A PI controller PI = ( s+1)/ s e v a n s ( PI * System ) s g r i d ( zeta , wn ) // These v a l u e s a r e from t h e P r o p o r t i o n a l controller Kpi = −1/ r e a l ( h o r n e r ( PI * System , [ 1 %i ] * l o c a t e ( 1 ) ) ) Aditya Sengupta, EE, IITB CACSD with Scilab A PI controller PISystem = Kpi * PI * System / . 1 yPI = c s i m ( ’ s t e p ’ , t , PISystem ) ; p l o t ( t , yPI ) p l o t ( t , o n e s ( t ) , ’ r ’ ) , // Compare w i t h s t e p Aditya Sengupta, EE, IITB CACSD with Scilab A PD controller We wish to achieve the following parameters: OS = 0 . 0 5 Ts = 0 . 5 From theory, we know the corresponding values of ζ and ωn are: s (log OS)2 ζ= (log OS)2 + π/2 ωn = 4 ζTs zeta = s q r t ( ( l o g ( OS ) ) ˆ 2 / ( ( l o g ( OS ) ) ˆ2 + %pi ˆ 2 ) ) wn = 4 / ( zeta * Ts ) Aditya Sengupta, EE, IITB CACSD with Scilab A PD controller PD = s + 20 e v a n s ( PD * System ) s g r i d ( zeta , wn ) // Zoom f i r s t , t h e n e x e c u t e n e x t l i n e : Kpd = −1/ r e a l ( h o r n e r ( PD * System , [ 1 %i ] * l o c a t e ( 1 ) ) ) Aditya Sengupta, EE, IITB CACSD with Scilab A PD controller PDSystem = Kpd * PD * System / . 1 yPD = c s i m ( ’ s t e p ’ , t , PDSystem ) ; p l o t ( t , yPD ) p l o t ( t , o n e s ( t ) , ’ r ’ ) , // Compare w i t h s t e p Aditya Sengupta, EE, IITB CACSD with Scilab A PD controller Note the improved transient performance, but the degraded steady state error: Aditya Sengupta, EE, IITB CACSD with Scilab A PID Controller We design our PID controller by eliminating the steady state error from the PD controlled system: PID = ( s + 2 0 ) * ( s+1)/ s e v a n s ( PID * System ) s g r i d ( zeta , wn ) // These v a l u e s a r e from t h e PD s y s t e m Aditya Sengupta, EE, IITB CACSD with Scilab A PID Controller Kpid = −1/ r e a l ( h o r n e r ( PID * System , [ 1 %i ] * l o c a t e ( 1 ) ) ) PIDSystem = Kpid * PID * System / . 1 yPID = c s i m ( ’ s t e p ’ , t , PIDSystem ) ; p l o t ( t , yPID ) p l o t ( t , o n e s ( t ) , ’ r ’ ) , // Compare w i t h s t e p Aditya Sengupta, EE, IITB CACSD with Scilab A Comparison Aditya Sengupta, EE, IITB CACSD with Scilab References Control Systems Engineering, Norman Nise Modern Control Engineering, Katsuhiko Ogata Digital Control of Dynamic Systems, Franklin and Powell Master Scilab by Finn Haugen. http://home.hit.no/f̃innh/scilab scicos/scilab/index.htm Mass, damper, spring image: released into the public domain by Ilmari Karonen. http://commons.wikimedia.org/wiki/File:Mass-SpringDamper.png Aditya Sengupta, EE, IITB CACSD with Scilab