Matlab – Background Proportional, Integral and Derivative Controllers
Characteristics of PID Controllers
Proportional Controller Kp
reduces the rise time
reduces but never eliminates steady-state error
Integral Controller Ki
eliminates steady-state error
worsens transient response
Derivative Controller Kd
increases the stability of the system
reduces overshoot
improves transient response
29 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Example Problem
Figure 3: Mass spring and damper problem
Modelling Equation
mẍ + b ẋ + kx = F (12)
30 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Example Problem
Laplace & Transfer Functions
mẍ + b ẋ + kx = F
ms 2 X (s) + bsX (s) + kX (s) = F (s) (13)
X (s) 1
= (14)
F (s) ms 2 + bs + k
31 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Matlab System Response
Assumptions
Let: m = 1[kg ], b = 10[Ns/m], k = 20[N/m]
Matlab code
1 %{ S e t up v a r i a b l e s %}
2 m=1; b =10; k =20;
3 %{ C a l c u l a t e r e s p o n s e%}
4 num=1;
5 den =[m, b , k ] ;
6 p l a n t=t f ( num , den ) ;
7 step ( plant )
32 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Matlab System Response
Figure 4: Amplitude ⇔ Displacement
33 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Problems
The steady-state error is equal to 0.95 – equation (11)
The rise time is about 1 second
The settling time is about 1.5 seconds
The PID controller should influence (reduce) all those parameters
34 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Controllers’ Characteristics
Type Rise time Overshoot Settling time S-S Error
Kp decrease increase small change decrease
Ki decrease increase increase eliminate
Kd small change decrease decrease small change
These correlations may not be exactly accurate, because Kp , Ki , and Kd
are dependent on each other. In fact, changing one of these variables can
change the effect of the other two.
35 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Proportional Controller
P Transfer Function
X (s) Kp
= 2
F (s) s + bs + (k + Kp )
Matlab code
1 %{ S e t up p r o p o r t i o n a l g a i n%}
2 Kp=300;
3 %{ C a l c u l a t e c o n t r o l l e r %}
4 s y s c t l =f e e d b a c k ( Kp∗ p l a n t , 1 ) ;
5 %{ P l o t r e s u l t s %}
6 t =0:0.01:2;
7 step ( sys ctl , t )
36 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Proportional Controller – Plot
Figure 5: Improved rise time & steady-state error
37 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Proportional Derivative Controller
PD Transfer Function
X (s) Kd s + K p
= 2
F (s) s + (b + Kd )s + (k + Kp )
Matlab code
1 %{ S e t up p r o p o r t i o n a l and d e r i v a t i v e g a i n%}
2 Kp=300; Kd=10;
3 %{ C a l c u l a t e c o n t r o l l e r %}
4 c o n t r=t f ( [ Kd , Kp ] , 1 ) ;
5 s y s c t l =f e e d b a c k ( c o n t r ∗ p l a n t , 1 ) ;
6 %{ P l o t r e s u l t s %}
7 t =0:0.01:2;
8 step ( sys ctl , t )
38 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Proportional Derivative Controller – Plot
Figure 6: Reduced over-shoot and settling time
39 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Proportional Integral Controller
PI Transfer Function
X (s) Kp s + Ki
= 3
F (s) s + bs 2 + (k + Kp )s + Ki
Matlab code
1 %{ S e t up p r o p o r t i o n a l and i n t e g r a l g a i n%}
2 Kp=30; Ki =70;
3 %{ C a l c u l a t e c o n t r o l l e r %}
4 c o n t r=t f ( [ Kp , Ki ] , [ 1 , 0 ] ) ;
5 s y s c t l =f e e d b a c k ( c o n t r ∗ p l a n t , 1 ) ;
6 %{ P l o t r e s u l t s %}
7 t =0:0.01:2;
8 step ( sys ctl , t )
40 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Proportional Integral Controller – Plot
Figure 7: Eliminated steady-state error, decreased over-shoot
41 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Proportional Integral Derivative Controller
PID Transfer Function
X (s) Kd s 2 + Kp s + Ki
= 3
F (s) s + (b + Kd )s 2 + (k + Kp )s + Ki
Matlab code
1 %{ S e t up p r o p o r t i o n a l and i n t e g r a l g a i n%}
2 Kp=350; Ki =300; Kd=50;
3 %{ C a l c u l a t e c o n t r o l l e r %}
4 c o n t r=t f ( [ Kd , Kp , Ki ] , [ 1 , 0 ] ) ;
5 s y s c t l =f e e d b a c k ( c o n t r ∗ p l a n t , 1 ) ;
6 %{ P l o t r e s u l t s %}
7 t =0:0.01:2;
8 step ( sys ctl , t )
42 / 78
Matlab – Background Proportional, Integral and Derivative Controllers
Proportional Integral Derivative Controller – Plot
Figure 8: Eliminated steady-state error, decreased over-shoot
43 / 78
Part III
Matlab – Cruise Control System
44 / 78
Matlab – Cruise Control System Design Criteria
How does Cruise Control for Poor work?
Figure 9: Forces taking part in car’s movement
Based on Carnegie Mellon University Library Control Tutorials for Matlab and Simulink
45 / 78
Matlab – Cruise Control System Design Criteria
Building the Model
Using Newton’s law we derive
F = mv̇ + bv (15)
y =v (16)
Where: m = 1200[kg ], b = 50[ Ns
m ], F = 500[N]
46 / 78
Matlab – Cruise Control System Design Criteria
Design Criteria
For the given data Vmax = 10[m/s] = 36[km/h]
The car should accelerate to Vmax within 6[s]
10% tolerance on the initial velocity
2% of a steady-state error
47 / 78
Matlab – Cruise Control System Matlab Representation
Transfer Function
System Equations:
F = mv̇ + bv
y =v
Laplace Transform:
F (s) = msV (s) + bV (s) (17)
Y (s) = V (s) (18)
Transfer Function:
Y (s) 1
= (19)
F (s) ms + b
48 / 78
Matlab – Cruise Control System Matlab Representation
Matlab Representation
Now in Matlab we need to type
Matlab code
1 m=1200;
2 b =50;
3 num = [ 1 ] ;
4 den =[m, b ] ;
5 c r u i s e=t f ( num , den ) ;
6 s t e p = (500∗ c r u i s e ) ;
49 / 78
Matlab – Cruise Control System Matlab Representation
Results
Figure 10: Car velocity diagram – mind the design criteria
50 / 78
Matlab – Cruise Control System Matlab Representation
Design criteria revisited
Our model needs over 100[s] to reach the steady-state
The design criteria mentioned 5 seconds
51 / 78
Matlab – Cruise Control System PID Controller
Feedback controller
To adjust the car speed within the limits of specification
We need the feedback controller
Figure 11: System controller
52 / 78
Matlab – Cruise Control System PID Controller
Decreasing the rise time
Proportional Controller
Y (s) Kp
= (20)
R(s) ms + (b + Kp )
Matlab code
1 Kp=100; m=1200; b =50;
2 num = [ 1 ] ; den =[m, b ] ;
3 c r u i s e=t f ( num , den ) ;
4 s y s c t l =f e e d b a c k ( Kp∗ c r u i s e , 1 ) ;
5 t =0:0.1:20;
6 s t e p (10∗ s y s c l , t )
7 a x i s ( [ 0 20 0 1 0 ] )
53 / 78
Matlab – Cruise Control System PID Controller
Under- and Overcontrol
Figure 12: Kp = 100 Figure 13: Kp = 10000
54 / 78
Matlab – Cruise Control System PID Controller
Making Rise Time Reasonable
Proportional Integral Controller
Y (s) Kp s + Ki
= (21)
R(s) ms 2 + (b + Kp )s + Ki
Matlab code
1 Kp=800; Ki =40; m=1200; b =50;
2 num = [ 1 ] ; den =[m, b ] ;
3 c r u i s e=t f ( num , den ) ;
4 c o n t r=t f ( [ Kp Ki ] , [ 1 0 ] )
5 s y s c t l =f e e d b a c k ( c o n t r ∗ c r u i s e , 1 ) ;
6 t =0:0.1:20;
7 s t e p (10∗ s y s c l , t )
8 a x i s ( [ 0 20 0 1 0 ] )
55 / 78
Matlab – Cruise Control System PID Controller
Results
Figure 14: Car velocity diagram meeting the design criteria
56 / 78
Part IV
Simulink – Cruise Control System
57 / 78
Simulink – Cruise Control System Building the Model
How does Cruise Control for Poor work?
Figure 15: Forces taking part in car’s movement
Based on Carnegie Mellon University Library Control Tutorials for Matlab and Simulink
58 / 78
Simulink – Cruise Control System Building the Model
Physical Description
Summing up all the forces acting on the mass
Forces acting on the mass
dv
F =m + bv (22)
dt
Where: m=1200[kg], b=50[ Nsec
m ], F=500[N]
59 / 78
Simulink – Cruise Control System Building the Model
Physical Description – cntd.
Integrating the acceleration to obtain the velocity
Integral of acceleration
dv dv
a= ≡ =v (23)
dt dt
60 / 78
Simulink – Cruise Control System Building the Model
Building the Model in Simulink
Figure 16: Integrator block from Continuous block library
61 / 78
Simulink – Cruise Control System Building the Model
Building the Model in Simulink
Obtaining acceleration
Acceleration
dv F − bv
a= = (24)
dt m
62 / 78
Simulink – Cruise Control System Building the Model
Building the Model in Simulink
Figure 17: Gain block from Math operators block library
63 / 78
Simulink – Cruise Control System Building the Model
Elements used in Simulink Model
Friction (Gain block)
Subtract (from Math Operators)
Input (Step block from Sources)
Output (Scope from Sinks)
64 / 78
Simulink – Cruise Control System Building the Model
Complete Model
65 / 78
Simulink – Cruise Control System Building the Model
Mapping Physical Equation to Simulink Model
66 / 78
Simulink – Cruise Control System Simulating the Model
Setting up the Variables
Now it is time to use our input values in Simulink...
F=500[N]
In Step block set: Step time = 0 and Final value = 500
...and adjust simulation parameters...
Simulation → Configuration Parameters...
Stop time = 120
...and set up variables in Matlab
1 m=1200;
2 b =50;
67 / 78
Simulink – Cruise Control System Simulating the Model
Running Simulation
Choose Simulation→Start
Double-click on the Scope block...
68 / 78
Simulink – Cruise Control System Simulating the Model
Extracting Model into Matlab
Replace the Step and Scope Blocks with In and Out Connection
Blocks
69 / 78
Simulink – Cruise Control System Simulating the Model
Verifying Extracted Model
We can convert extracted model
into set of linear equations
into transfer function
Matlab code
1 [ A , B , C , D]= l i n m o d ( ’ c c 2 m t l b ’ ) ;
2 [ num , den ]= s s 2 t f (A , B , C , D ) ;
3 s t e p ( 5 0 0 ∗ t f ( num , den ) ) ;
70 / 78
Simulink – Cruise Control System Simulating the Model
Matlab vs Simulink
Figure 18: Matlab Figure 19: Simulink
71 / 78
Simulink – Cruise Control System Implementing the PI Control
The open-loop system
In Matlab section we have designed a PI Controller
Kp = 800
Ki = 40
We will do the same in Simulik
First we need to contain our previous system in a Sybsystem block
Choose a Subsystem block from the Ports&Subsystems Library
Copy in the model we used with Matlab
72 / 78
Simulink – Cruise Control System Implementing the PI Control
Subsystem
Figure 20: Subsystem Block and its Contents
73 / 78
Simulink – Cruise Control System Implementing the PI Control
PI Controller I
Figure 21: Step: final value=10, time=0
74 / 78
Simulink – Cruise Control System Implementing the PI Control
PI Controller II
Figure 22: We use Transfer Fcn block from Continuous-Time Linear Systems
Library
75 / 78
Simulink – Cruise Control System Implementing the PI Control
Results
Runnig simulation with time set to 15[s]
76 / 78