Professional Documents
Culture Documents
1) Study DC motors
2) Study open-loop and closed-loop control
3) Control methods
I) Proportional feedback control
II) PID (proportional-integral-derivative) con
trol
1) DC motors
For robots
Motors
DC motors: Direct cur
rent motor, easy to c
ontrol and use. For m
aking wheeled robots
15351 PRO ,
picture from http://item.taobao.com/item.htm?
id=1606576457&tracelog=newcardfavirate
1(EN1/2)
LDIR
7(2A)
REN
RDIR
1Y(3)
(2Y)6
Left-motor
10(3A)
(3Y)11
9(EN3/4)
15(4A)
(4Y)14
Right-motor
L293D: H-bridge ci
rcuit, up 2A
LDIR: left motor di
rection; RDIR: righ
t motor direction
LEN : left motor e
nable; REN : right
motor enable
Exercise
When using the open-loop control metho
d with a constant PWM signal for both wh
eels, explain why the robot would slow do
wn when climbing up hill.
Ans: When climbing up hill, energy is used to
act against gravity, hence the robot is running
slower.
Feedback control
The real solution to real speed control is f
eedback control
Require speed encoder to read back the r
eal speed of the wheel at real time.
10
Wheel encoder
Darkened
part
blocks light
IR light source
11
12
15351 PRO
,
picture from
http://item.taobao.com/item.htm?
id=1606576457&tracelog=newcardfavir
ate
Demo
movie
CEG2400 Ch16: feedback control V3b
13
http://item.taobao.com/item.htm?spm=2013.1.0.116.KbMxbr&id=15172908917
CEG2400 Ch16: feedback control V3b
14
3) Control methods
I) Proportional feedback control
II) PID (proportional-integralderivative) control
15
leftErr
+
-
Alter PWM
for driver
L293
leftPWM
Motor
leftRPM
16
17
control method:
PID (proportional-integral-derivative) control
Required speed=
leftRPMset
leftErr
Motor
integral control
leftPWM
Igain* leftErr dt
Proportional control
Pgain*leftErr
sum
generates
PWM
for driver
L293
Derivative control
Dgain*[d(leftErr)/dt]
leftPWM
LeftRPM
leftRPM
18
IR wheel
Speed
encoder
Introduction
Good performance
Criteria depends
on users and
applications
required
time
CEG2400 Ch16: feedback control V3b
19
Steady
state
error
overshoot
Target
value
Typically
value=10%
Depends
on application
undershoot
Rise time
time
Settling time
20
Use of PID
control terms are intertwined
http://en.wikipedia.org/wiki/PID_controller
21
Rise Time
Overshoot
Settling
Time
Steady
state error
Kp (Pgain) Decrease
step1
Increase
Small
Change
Decrease
Ki (Igain)
Increase
Increase
Eliminate
step3
Decrease
step2
Decrease
Small
Change
Decrease
Kd (Dgain) Small
Change
22
Software
PIDrobotdemo.c
(at course webpage)
http://www.cse.cuhk.edu.hk/khwong/ceg2400/PIDRobotDemo.c
23
IR receiver
Speed Encoder
sensor
interrupts
_IRQ( )//1000Hz
{
:
read wheel speed
PID
:
}
24
time
Overview
//////////////main //////////////////////////////////////
Main()
{ setup()
forward (Lstep, Rstep, Lspeed, Rspeed)..
}
////////////subroutine //////////////////////////////////////////
forward (Lstep, Rstep, Lspeed, Rspeed)
{ lcount=0
if (lcount>=Lstep)
Stop left motor
same for right motor
.}
//////////timer triggered , interrupt service routine, 1000Hz///////
__irq exception// Interrupt() running at 1000HZ
{ lcount++
read wheel speeds
PID control to achieve L/Rspeed
.same for right motor.
}
CEG2400 Ch16: feedback control V3b
25
Interrupt
rate 1000Hz
_IRQ
see next slide
In our robot
88 pattern changes
in one rotation
GPIO
Parallel
interface
CPU
IR receiver
(speed
encoder
sensor)
CEG2400 Ch16: feedback control V3b
26
Exercise 3
_irq interrupt programming method for the
main PID loop, using a counter (intcount)
{
PID CORE
intcount=0;
}
}
27
28
Speed
encoder
sensor
lcount++;lold=lcur;lefttimeval++;}
time
Wheel if(lcur!=lold) {
:
Interrupts 1000 Hz
speeds
//update motor speed by PID feed back control in every Seconds
if(intcount==250) { //calculate the speed of left motor in RPM
leftRPM=lefttimeval;
:
PID core : See following slides
Part 2
PID
control
intcount=0;
lefttimeval=0;
}
Set_point
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
Error,
Accumulated error (add up all previous errors)
Derivative error (current error previous error)
PWM+=Kp* Error+
Ka*(Accumulated error)+
Kd* Derivative error ;
}
Else
30
PID core
if(intcount==250) {//for every seconds
//caculate the speed of left motor in RPM
leftRPM=lefttimeval;
P=Proportional
leftErr = leftRPMset - leftRPM; //caculate left error
I Integral
if((leftErr<DeadBand*(-1))||(leftErr>DeadBand)) //see next slide
{ //if |left error| > deadband
leftP = Pgain * leftErr;
//calculate P Proportional term
leftI = Igain * leftaccErr; //calculate I Integral term
leftD = Dgain * (leftErr - leftlastErr);//calculate D Derivative term
leftPWM += (leftP + leftI + leftD);//update left motor PWM using PID
if(leftPWM>PWM_FREQ)
leftPWM=PWM_FREQ;//prevent over range (max.=PWM_FREQ)
if(leftPWM<0) leftPWM = 0; // (min. = 0)
leftaccErr += leftErr; // accumulate error
leftlastErr = leftErr; //update left last error
D Derivative
:
: // handle right motor similarly.
current_leftRPM = leftRPM*240/88;
current_leftPWM = leftPWM;//
Pgain, Igain, Dgain are
lefttimeval = 0;
constants found by a trial and
}
control method:
PID (proportional-integral-derivative) control
Required speed=
leftRPMset
leftErr
Motor
integral control
leftPWM
Igain* leftErr dt
Proportional control
Pgain*leftErr
sum
generates
PWM
for driver
L293
Derivative control
Dgain*[d(leftErr)/dt]
leftPWM
LeftRPM
leftRPM
32
IR wheel
Speed
encoder
5) if((leftErr<DeadBand*(-1))||(leftErr>DeadBand))
:
:
7) leftP = Pgain * leftErr;
//calculate P Proportional term
8) leftI = Igain * leftaccErr;
//calculate I Integral term
9) leftD = Dgain * (leftErr - leftlastErr);//calculate D Derivative ter
m
10) leftPWM += (leftP + leftI + leftD);//update motorPWM by PID
:
:
14) leftaccErr += leftErr;
// accumulate error
33
Dead band
line5) if((leftErr<DeadBand*(-1))||(leftErr>DeadBand))
34
Exercise 4:
Discuss what will happen if dead-band is changed, say (a) 0.5 or (b) 2.
Dead band
overshoot
+/- 1
In our experiment
leftPWM=276000 at the beginning and
192800 at steady state
Steady
state
error
Target speed=
leftRPMset =10
undershoot
Typically
value=10%
Depends
on application
time
Settling time
0
Rise time
When leftRMPset=10, the real RPM is RPM*240/88=27.3., because each
rotation has 88 counts, the ISR loop is in seconds, each minutes 60 seconds.
CEG2400 Ch16: feedback control V3b
35
see line 18
Steady
state
error
overshoot
Target
value
Typically
value=10%
Depends
on application
undershoot
Rise time
time
Settling time
36
Parameter
Rise Time
Overshoot
Settling
Time
Steady
state error
(1) Kp
(Pgain)
Decrease
step1
Increase
Small
Change
Decrease
Ki (Igain)
Decrease
Increase
Increase
Eliminate
step3
Decrease
step2
Decrease
Small
Change
Kd (Dgain) Small
Change
37
(leftErr = leftRPMset-leftRPM )
overshoot
Target speed=
leftRPMset =10
8
6
leftErr=
leftRPMset
leftPRM
=10-6=4
undershoot
P is
+ve
P is
+ve
t1 t2 t3 t4
Rise time
leftP = Pgain * leftErr;
leftP= 8000* (10-6)= 8000*4 is positive
This term pushing up leftPWM
(more energy delivered to the wheel).
leftErr=
leftRPMset
leftPRM
=30-28=2
e.g.
Pgain = 8000;
Igain = 6000;
Dgain = 5000;
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
time
leftP = Pgain * leftErr;
leftP= 8000* (0-8)= 8000*2 is positive
This term pushing up leftPWM
(more energy delivered to the wheel).
38
overshoot
Target speed=
leftRPMset =10
8
6
leftErr=
leftRPMset
leftPRM
=10-6=4
undershoot
P is
+ve
P is
+ve
t1 t2 t3 t4
Rise time
leftP = Pgain * leftErr;
leftP= 8000* (10-6)= 8000*4 is positive
This term pushing up leftPWM
(more energy delivered to the wheel).
0
leftErr=
leftRPMset
leftPRM
=10-8=2
e.g.
Pgain = 8000;
Igain = 6000;
Dgain = 5000;
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
time
leftP = Pgain * leftErr;
leftP=?___________ ,is +ve or -ve?
leftPWM is increasedor decreased?__
more/less energy delivered to wheel?__
39
Target speed=
leftRPMset =10
leftErr=
leftRPMset
leftPRM
=10-13= -3
P is
-ve
overshoot
13
e.g.
Pgain = 8000;
Igain = 6000;
Dgain = 5000;
undershoot
P is
+ve
0
t1 t2 t3
Rise time
t4
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
time
40
41
dx/dt
Rise Time
Overshoot
Settling
Time
Steady
state error
Kp (Pgain) Decrease
Igain
step1
Increase
Small
Change
Decrease
Ki (Igain)
gainI
Increase
Increase
Eliminate
step3
Decrease
Kd (Dgain) Small
Change
Decrease Decrease
step2
42
Small
Change
Derivative term
Derivative control
Dgain*[d(leftErr)/dt]
d(leftErr)/dt =
=Derivative term
=current_Err - last_Err
=leftErr leftlastErr ; in our program
43
Target
Value=
seconds
overshoot
leftRPMset
=10
leftRPMset =10
leftErr=
leftRPMset
7
leftPRM
=10-7=3
leftlastErr= 3
leftRPMset
eftlastPRM
=10-3=7
0
e.g.
Pgain = 8000;
Igain = 6000;
Dgain = 5000;
undershoot
D is
-ve
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
t1 t2 t3 t4
time
Rise time
leftD = Dgain * (leftErr leftlastErr);
leftD= 5000* (3-7)= 5000*( -4) is negative
This term lowering down leftPWM (less energy delivered to the wheel).
CEG2400 Ch16: feedback control V3b
44
leftErr=
leftRPMset -leftPRM
=10-15= -5
leftPRM
15
12
leftRPMset =10
seconds
overshoot
leftlastErr=
leftRPMset -leftlastPRM
=10-12= -2
D is D is
-ve -ve
t1 t2
Rise time
e.g.
Pgain = 8000;
Igain = 6000;
Dgain = 5000;
undershoot
t3
t4
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
time
V3b
45
undershoot
D is D is
-ve -ve
t1 t2 t3 t4
time
Rise time
CEG2400 Ch16: feedback control V3b
46
leftRPMset =10
seconds
leftPRM
14
11
leftlastErr=
leftRPMset -leftlastPRM
=10-14= -4
overshoot
undershoot
D is D is
D is -ve +ve
-ve
t1 t2 t3
0
Rise time
t4
leftErr=
leftRPMset -leftPRM
=10-11= -1
e.g.
Pgain = 8000;
Igain = 6000;
Dgain = 5000;
time
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
V3b
47
seconds
leftPRM
overshoot
leftRPMset =10 9
7
undershoot
D is
leftlastErr=
D is D is +ve
+ve
leftRPMset -leftlastPRM
D is -ve
=10-9= 1
-ve
t1 t2 t3 t4
0
Rise time
leftD = Dgain * (leftErr leftlastErr);
leftD= ?___________, which is +ve or ve?____
leftPWM increased or decreased?____
More/less energy delivered to the wheel?___
leftErr=
leftRPMset -leftPRM
=10-7= 3
e.g.
Pgain = 8000;
Igain = 6000;
time
Dgain = 5000;
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
48
CEG2400 Ch16: feedback control V3b
undershoot
D is D is
+ve +ve
0
t1 t2 t3 t4
time
Rise time
CEG2400 Ch16: feedback control V3b
49
50
Rise Time
Overshoot
Settling
Time
Steady
state error
Kp (Pgain) Decrease
step1
Increase
Small
Change
Decrease
Ki (Igain)
Increase
Increase
Eliminate
step3
Decrease
step2
Decrease
Small
Change
Decrease
Kd (Dgain) Small
Change
51
control method:
PID (proportional-integral-derivative) control
Required speed=
leftRPMset
leftErr
Motor
integral control
leftPWM
Igain* leftErr dt
Proportional control
Pgain*leftErr
sum
generates
PWM
for driver
L293
Derivative control
Dgain*[d(leftErr)/dt]
At steady state, leftErr0, so
So, Pgain*leftErr=0
leftRPM
leftPWM
LeftRPM
also Dgain*[d(leftErr)/dt] =0
And if no Integral { Igain* leftErr dt} term, left PWM 0
It is a problem.
52
IR wheel
Speed
encoder
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
e.g.
leftErr = leftRPMset leftRPM=0
Pgain = 8000;
So as leftlastErr = 0
Igain = 6000;
Therefore
Dgain = 5000;
Steps
7) leftP = Pgain * leftErr//=0
//calculate P Proportional term
8) leftI = Igain * leftaccErr; //calculate I Integral term
9) leftD = Dgain * (leftErr - leftlastErr)//=0
//calculate D Derivative term
10) leftPWM += (leftP + leftI + leftD);//update left motor PWM using PID
near steady state : leftP=0
leftD=0
53
+leftErr(t=now)}
errors
:
10) leftPWM += leftI // near steady state, only leftI is valid
e.g.
Pgain = 8000;
Igain = 6000;
Dgain = 5000;
Also, because near steady state , leftaccErr will adjust itself auto
matically, see next slide
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
CEG2400 Ch16: feedback control V3b
54
Igain = 6000;
Dgain = 5000;
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
55
56
PID Tuning
(usually done by
trail and error)
Tune (adjust manually)
Motor speed
Accepted
performance
V1
(V1)/2
T1
unstable
done
CEG2400 Ch16: feedback control V3b
57
time
Application of
PID
58
Robot Turning
59
Summary
Studies PID control theory and implement
ation
60
Appendix:
New robot drive circuit ver13.3
61