0% found this document useful (0 votes)
24 views59 pages

1st Lecture Mechatronics Application 2

This document discusses the PID controller and its application in controlling a DC motor with an encoder. It explains the closed-loop transfer function, the characteristics of proportional, integral, and derivative controls, and provides a detailed example of modeling a DC motor's speed control. Additionally, it includes MATLAB representations for both transfer function and state-space models of the motor system.

Uploaded by

pucc681
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views59 pages

1st Lecture Mechatronics Application 2

This document discusses the PID controller and its application in controlling a DC motor with an encoder. It explains the closed-loop transfer function, the characteristics of proportional, integral, and derivative controls, and provides a detailed example of modeling a DC motor's speed control. Additionally, it includes MATLAB representations for both transfer function and state-space models of the motor system.

Uploaded by

pucc681
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

2 ‫تطبيقات امليكاترونيك‬

Lecture No. 1
• PID controller
• Control of DC motor with Encoder
Dr. Eng. Essa Alghannam
Ph.D. Degree in Mechatronics
Engineering
‫ميكاترونيك – سنة رابعة‬
2023

4/30/2023 Dr. Eng. Essa Alghannam


The closed-loop transfer function
The closed-loop transfer function is measured at the
output. The output signal can be calculated from the
closed-loop transfer function and the input signal.
Signals may be waveforms, images, or other types
of data streams.
An example of a closed-loop transfer function is
shown:
The summing node and the G(s) and H(s) blocks can all be
combined into one block, which would have the following the transfer function of a closed-loop system shows a Plus ( + )
transfer function: sign in the denominator representing negative feedback.

G(s) is called feedforward transfer function,


H(s) is called feedback transfer function,

4/30/2023 Dr. Eng. Essa Alghannam


Introduction
Consider the following unity feedback system:

Plant: A system to be controlled.


Controller: Provides the excitation for the plant; Designed to control the overall system behavior
The variable (e) represents the tracking error, the difference between the desired input value (R) and the
actual output (Y).

4/30/2023 Dr. Eng. Essa Alghannam


Purpose PID
• What are the characteristics of the each of proportional (P), the integral (I), and the derivative
(D) controls:
• and how to use them to obtain a desired response.

4/30/2023 Dr. Eng. Essa Alghannam


how the PID controller works in a closed-loop system
• This error signal (e) will be sent to the PID controller, and the controller computes both the derivative and the
integral of this error signal.
The signal (u) just past the controller is equal to:
the proportional gain (Kp) times the magnitude of the error
+ the integral gain (Ki) times the integral of the error
+ the derivative gain (Kd) times the derivative of the error.

• This signal (u) will be sent to the plant, and the new
output (Y) will be obtained.
t de(t )
u (t ) = K p e(t ) + K i  e( ) d + K d
0 dt
• This new output (Y) will be sent back to the sensor again to find the new error signal (e).
• The controller takes this new error signal and computes its derivative and its integral again. This
process goes on and on.

4/30/2023 Dr. Eng. Essa Alghannam


The three-term controller - PID
The transfer function of the PID controller looks like the following:

Kp = Proportional gain
Ki K d s 2 + K p s + Ki
KI = Integral gain Kp + + Kd s =
s s
Kd = Derivative gain

4/30/2023 Dr. Eng. Essa Alghannam


Kp = 1;
Ki = 1;
Kd = 1;

s = tf('s');
C = Kp + Ki/s + Kd*s

• or

C1 = pid(Kp,Ki,Kd)
C=tf(C1)

4/30/2023 Dr. Eng. Essa Alghannam


Performance specifications

Peak time

Controlled Reference
variable

Transient State Steady State

Time

4/30/2023 Dr. Eng. Essa Alghannam


Settling time (ts) is the time required for a response to become steady. It is defined as the time
required by the response to reach and steady within specified range of 2% to 5% of its final value.

Steady-state error is defined as the difference between the input and output of a system in the limit as time
goes to infinity (i.e. when the response has reached the steady state). The steady-state error will depend on
the type of input (step, ramp, etc) as well as the system type (0, I, or II).

We define rise time as the time it takes to get to 90% of steady-state value (of a step response)

Delay time (td) is the time required to reach at 50% of its final value by a time response signal during its
first cycle of oscillation.

4/30/2023 Dr. Eng. Essa Alghannam


The characteristics of P, I, and D controllers
• A proportional controller (Kp) will have the effect of reducing the rise time and will
reduce ,but never eliminate, the steady-state error.
• An integral control (Ki) will have the effect of eliminating the steady-state error, but it may
make the transient response worse.
• A derivative control (Kd) will have the effect of increasing the stability of the system, reducing
the overshoot, and improving the transient response.

4/30/2023 Dr. Eng. Essa Alghannam


Effects of each of controllers Kp, Kd, and Ki on a closed-loop system are
summarized in the table shown below.

CL response Rise Time Overshoot Settling time SS error

Kp Decrease Increase Small change Decrease

Decrease
Ki Increase Increase Decrease

Kd Small change Decrease Decrease Small change

4/30/2023 Dr. Eng. Essa Alghannam


Note that
these correlations may not be exactly accurate, because Kp, Ki, and Kd
are dependent of each other.

In fact, changing one of these variables can change the effect of the other
two. For this reason, the table should only be used as a reference when
you are determining the values for Ki, Kp and Kd

4/30/2023 Dr. Eng. Essa Alghannam


Example problem 1:
PID Design Method for DC Motor Speed
Control

4/30/2023 Dr. Eng. Essa Alghannam


FIRST-DC Motor Speed Modeling:

1.Physical setup and system equations:


A common actuator in control systems is the DC motor. It
directly provides rotary motion and, coupled with wheels
or drums and cables, can provide transitional motion. The
electric circuit of the armature and the free body diagram
of the rotor are shown in the following figure:

4/30/2023 Dr. Eng. Essa Alghannam


For this example, we will assume that the input of the system is the voltage source (V ) applied to the
motor's armature, while the output is the rotational speed of the shaft 𝜽· . The rotor and shaft are
assumed to be rigid.
We further assume a viscous friction model, that is, the friction torque is proportional to shaft angular
velocity.

* moment of inertia of the rotor (J) = 0.01 kg.m^2


* damping ratio of the mechanical system (b) = 0.1 Nms
* electromotive force constant (K=Ke=Kt) = 0.01 Nm/Amp
* electric resistance (R) = 1 ohm
* electric inductance (L) = 0.5 H
* input (V): Source Voltage
* output (theta): position of shaft
* The rotor and shaft are assumed to be rigid

4/30/2023 Dr. Eng. Essa Alghannam


System equations:

In general, the torque generated by a DC motor is proportional to the armature current and the strength of the
magnetic field. In this example we will assume that the magnetic field is constant and, therefore, that the motor torque
𝑇 is proportional to only the armature current 𝑖 by a constant factor 𝑘𝑡 (armature constant) as shown in the equation
below. This is referred to as an armature-controlled motor.
T = kt i
The back emf, 𝑒 , is proportional to the angular velocity of the shaft by a constant factor 𝑘𝑒 (motor
constant).
e = ke

In SI units, the motor torque and back emf constants are equal, that is, 𝑘𝑡 = 𝑘𝑒 ; therefore, we will use 𝐾 to represent
both the motor torque constant and the back emf constant.

4/30/2023 Dr. Eng. Essa Alghannam


• From the figure above we can write the following equations based on
Newton's law combined with Kirchhoff's law:

J + b = Ki
di
L + Ri = V − K
dt

4/30/2023 Dr. Eng. Essa Alghannam


2. Transfer Function
• Applying the Laplace transform, the above modeling equations can be expressed in terms of
the Laplace variable s.
( Js + b ) s ( s ) = KI ( s )
( Ls + R ) I ( s ) = V − Ks ( s )

I (s) =
( Js + b ) s ( s )
K
( Ls + R ) I ( s ) = V − Ks ( s )
( Js + b ) s ( s )
( Ls + R ) = V − Ks ( s )
K
4/30/2023 Dr. Eng. Essa Alghannam
By eliminating I(s) we can get the following open-loop transfer function, where the rotational speed is the
output and the voltage is the input

 ( Js + b ) 
 ( Ls + R ) + K  s ( s ) = V
 K 
 ( Ls + R ) ( Js + b ) + K 2  V V
  = =
 K  s ( s )  (s)
 (s) K rad / s
=
V ( Ls + R ) ( Js + b ) + K 2 v

4/30/2023 Dr. Eng. Essa Alghannam


State-Space

U  = V
 =  A X  +  B U 
  
X  X  =  
Y  = C  X  +  D U   i 
Y  = 

• In state-space form, the governing equations above can be expressed by choosing the rotational
speed and electric current as the state variables. Again the armature voltage is treated as the input and
the rotational speed is chosen as the output.

4/30/2023 Dr. Eng. Essa Alghannam


J + b = Ki
di
L + Ri = V − K
dt

 b K 
− 0
d    J   
   +  1 V
J
 =
dt  i   K R i   
− − L

 L 
L
 
Y  = 1 0    +  0 V
 i 

4/30/2023 Dr. Eng. Essa Alghannam


Matlab representation and open-loop response
1. Transfer Function
We can represent the above open-loop transfer function of the motor in MATLAB
by defining the parameters and transfer function as follows.

4/30/2023 Dr. Eng. Essa Alghannam


 (s) K rad / s
=
V ( Ls + R ) ( Js + b ) + K 2 v
clc
clear
J = 0.01;
b = 0.1;
K = 0.01;
P_motor =
R = 1;
L = 0.5; 0.01
num=K; ---------------------------
den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; 0.005 s^2 + 0.06 s + 0.1001
P_motor=tf(num,den)
Continuous-time transfer function.
% or
% s = tf('s');
% P_motor = K/((J*s+b)*(L*s+R)+K^2)

4/30/2023 Dr. Eng. Essa Alghannam


2. State Space
We can also represent the system using the state-space equations. The following additional MATLAB commands
create a state-space model of the motor and produce the output shown below when run in the MATLAB command
window.  b K 
− 0
clc d    J J     
clear  =  + 1 V
 
dt  i   K
−   L
R i
J = 0.01; −

 L L
b = 0.1;
 
K = 0.01; motor_ss = Y  = 1 0    +  0 V
R = 1;  i 
L = 0.5; A=
A = [-b/J K/J x1 x2 C=
-K/L -R/L]; x1 -10 1 x1 x2
B = [0 x2 -0.02 -2 y1 1 0
1/L];
C = [1 0]; B= D=
D = 0; u1 u1
motor_ss = ss(A,B,C,D) x1 0 y1 0
x2 2 Continuous-time state-space model.
4/30/2023 Dr. Eng. Essa Alghannam
Step Response for the Open Loop System

Transfer function

step(num,den,0:0.1:5) If we simulate the reference input


%or step(P_motor,0:0.1:5) (r) by an unit step input(input
%or step(motor_ss, 0:0.1:5) voltage of 1 Volt ), then the motor
title('Step Response for the Open Loop speed output is at 0.1 rad/sec in
System') steady state.

4/30/2023 Dr. Eng. Essa Alghannam


Step Response for the Open Loop System
J=0.01;
b=0.1;
State-Space K=0.01;
We can also represent the system R=1;
using the state-space equations. L=0.5;
Try the following commands in a A=[-b/J K/J
If we simulate the reference input
new m-file and you should get the -K/L -R/L]; (r) by an unit step input(input
same output B=[0 voltage of 1 Volt ), then the motor
speed output is at 0.1 rad/sec in
1/L];
steady state.
C=[1 0];
D=0;
figure;
step(A,B,C,D)
title('Step Response for the Open Loop
System')
4/30/2023 Dr. Eng. Essa Alghannam
Design requirements

• desired speed 1rad/sec.


• we will require that the steady-state error of the motor speed be less than 1%.
• Another performance requirement for our motor is that it must accelerate to its steady-state
speed as soon as it turns on. In this case, we want it to have a settling time less than 2
seconds.
• Also, since a speed faster than the reference may damage the equipment, we want to have a
step response with overshoot of less than 5%.

4/30/2023 Dr. Eng. Essa Alghannam


Linear System Analyzer

You can access the Linear System Analyzer also by going to the APPS
tab of the MATLAB toolstrip and clicking on the app icon under Control
System Design and Analysis, or by the following command:

linearSystemAnalyzer('step', P_motor, 0:0.1:5);

In the command below, the string 'step' passed to the function specifies
to generate a unit step response plot for the system P_motor.
The range of numbers 0:0.1:5 specify that the step response plot should
include data points for times from 0 to 5 seconds in steps of 0.1 seconds. 'Step Response for the Open Loop System'

4/30/2023 Dr. Eng. Essa Alghannam


• The resulting plot is shown in the figure below, where you can view some of the system's
characteristics by right-clicking on the figure and choosing from the Characteristics menu
such performance aspects as Settling Time and Steady State.

From the plot we see that when 1 volt is applied to the system:
• the motor can only achieve a maximum speed of 0.1 rad/sec, ten times smaller than our desired
speed.
• Also, it takes the motor 3 seconds to reach its steady-state speed; this does not satisfy our 2 seconds
settling time criterion.

4/30/2023 Dr. Eng. Essa Alghannam


You can graphically see the location of the poles (and zeros) of the
P_motor system from within the Linear System Analyzer by right-clicking
on the plot area and selecting Plot Types > Pole/Zero from the resulting
menu.
Performing this action will change the Linear System Analyzer to the
following map where the blue x's identify the locations of poles.
From the result you can see that:
• the open-loop transfer function has two real poles, one at s = -2 and
one at s = -10.
• Since both poles are real, there is no oscillation in the step response (or
overshoot) as we have already seen.
• Furthermore, since the one pole is 5 times more negative than the
other, the slower of the two poles will dominate the dynamics. That is,
the pole at s = -2 primarily determines the speed of response of the
system and the system behaves similarly to a first-order system.

4/30/2023 Dr. Eng. Essa Alghannam


• the poles of the system are indicated in the plot by an X while the zeros are indicated by a
circle or O. A pole-zero plot can represent either a continuous-time (CT) or a discrete-time
(DT) system.
• The transfer function poles are the roots of the characteristic equation, and also the
eigenvalues of the system A matrix.

4/30/2023 Dr. Eng. Essa Alghannam


SECOND-PID Design:
• From the main problem, the dynamic equations and the open-loop transfer function of the DC Motor
are:
 (s) K rad / s
=
V ( Ls + R ) ( Js + b ) + K 2 v

and the system schematic looks like:

4/30/2023 Dr. Eng. Essa Alghannam


Proportional control:

Let's first try using a proportional controller with a gain of 100. Add the following code to the end of your m-file:
Kp=100;
numa=Kp*num;
dena=den;

To determine the closed-loop transfer function, we use the cloop command. Add the following line to your m-file:

[numac,denac]=cloop(numa,dena);

Note that [numac,denac] are the numerator and the denominator of the overall closed-loop transfer function.

4/30/2023 Dr. Eng. Essa Alghannam


Proportional control:
• Now let's see how the step response looks, add the following to the end of your m-
file, and run it in the command window:

t=0:0.01:5;
figure
step(numac,denac,t)
title('Step response with Proportion Control')

You should get the following plot:

4/30/2023 Dr. Eng. Essa Alghannam


Proportional control:

4/30/2023 Dr. Eng. Essa Alghannam


PID control
• From the plot above we see that both the steady-state error and the overshoot are too large.
• Let's try a PID controller with small Ki and Kd. Change your m-file so it looks like the following.
Running this new m-file gives you the following plot.

4/30/2023 Dr. Eng. Essa Alghannam


PID control
Kp=100;
Ki=1;
J=0.01; Kd=1;
b=0.1; numc=[Kd, Kp, Ki];
K=0.01; denc=[1 0];
R=1; numa=conv(num,numc);
L=0.5; dena=conv(den,denc);
num=K; [numac,denac]=cloop(numa,dena);
den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)]; Figure;
step(numac,denac)
title('PID Control with small Ki and Kd')

4/30/2023 Dr. Eng. Essa Alghannam


PID control

4/30/2023 Dr. Eng. Essa Alghannam


Tuning the gains
• Now the settling time is too long. Let's increase Ki to reduce the settling time. Go back to your
m-file and change Ki to 200. Rerun the file and you should get the plot like this:

4/30/2023 Dr. Eng. Essa Alghannam


4/30/2023 Dr. Eng. Essa Alghannam
• Now we see that the response is much faster than before, but the large
Ki has worsened the transient response (big overshoot). Let's increase
Kd to reduce the overshoot. Go back to the m-file and change Kd to 10.
Rerun it and you should get this plot:

4/30/2023 Dr. Eng. Essa Alghannam


4/30/2023 Dr. Eng. Essa Alghannam
ALTERNATIVE CODING:
J = 0.01;
b = 0.1;
K = 0.01;
R = 1;
L = 0.5;
s = tf('s');
P_motor = K/((J*s+b)*(L*s+R)+K^2);
Kp = 100;
C = pid(Kp);
sys_cl = feedback(C*P_motor,1);
t = 0:0.01:5;
step(sys_cl,t)
grid
title('Step Response with Proportional Control')

4/30/2023 Dr. Eng. Essa Alghannam


ALTERNATIVE CODING:
J = 0.01;
b = 0.1;
K = 0.01;
R = 1;
L = 0.5;
s = tf('s');
P_motor = K/((J*s+b)*(L*s+R)+K^2);
Kp = 75; %%Kp = 100;
Ki = 1;%% Ki = 200;
Kd = 1;%%Kd = 10;
C = pid(Kp,Ki,Kd);
sys_cl = feedback(C*P_motor,1);
step(sys_cl,[0:1:200])
title('PID Control with Small Ki and Small Kd')

4/30/2023 Dr. Eng. Essa Alghannam


you can employ the Control System Designer by
entering the
command: controlSystemDesigner(P_motor)
or by going to the APPS tab and clicking on the app
icon under Control System Design and
Analysis and then opening a closed-loop step
response plot from the New Plot tab of the Control
System Designer window as shown:

4/30/2023 Dr. Eng. Essa Alghannam


4/30/2023 Dr. Eng. Essa Alghannam
‫تطبيق ‪:‬‬
‫‪5v‬‬ ‫‪12v‬‬

‫قراءة إشارة مشفر (انكودر)‬


‫دوراني مربوط على محور املحرك‬
‫املستمر‬

‫‪4/30/2023‬‬ ‫‪Dr. Eng. Essa Alghannam‬‬


‫‪CW‬‬ ‫‪CCW‬‬
‫لحظة حدوث جبهة صاعدة على القناة األولى نفحص القناة الثانية قيما اذا كانت ‪ HIGH‬او ‪LOW‬‬

‫‪4/30/2023‬‬ ‫‪Dr. Eng. Essa Alghannam‬‬


‫تطبيق ‪:‬‬

‫قياس زاوية دوران محرك‬


‫تيار مستمر‬

‫‪4/30/2023‬‬ ‫‪Dr. Eng. Essa Alghannam‬‬


4/30/2023 Dr. Eng. Essa Alghannam
‫تحكم باتجاه دوران املحرك وقياس زاوية الدوران‬

‫‪4/30/2023‬‬ ‫‪Dr. Eng. Essa Alghannam‬‬


‫تطبيق ‪:‬‬

‫تطبيق ‪ :‬قراءة إشارة ‪Encoder‬‬


‫للتعرف على سرعة محرك مستمر‬

‫‪4/30/2023‬‬ ‫‪Dr. Eng. Essa Alghannam‬‬


‫تطبيق ‪ :‬قراءة إشارة ‪ Encoder‬للتعرف على سرعة محرك مستمر‬

‫‪4/30/2023‬‬ ‫‪Dr. Eng. Essa Alghannam‬‬


: ‫تطبيق‬

4/30/2023 Dr. Eng. Essa Alghannam


‫التحكم التناسبي التكامل التفاضلي بزاوية دوران محرك تيار مستمر‬

‫‪t‬‬ ‫) ‪de(t‬‬
‫‪u (t ) = K p e(t ) + K i  e( ) d + K d‬‬
‫‪0‬‬ ‫‪dt‬‬

‫‪4/30/2023‬‬ ‫‪Dr. Eng. Essa Alghannam‬‬


4/30/2023 Dr. Eng. Essa Alghannam
4/30/2023 Dr. Eng. Essa Alghannam
4/30/2023 Dr. Eng. Essa Alghannam
‫انتهت املحاضرة‬

4/30/2023 Dr. Eng. Essa Alghannam

You might also like