Академический Документы
Профессиональный Документы
Культура Документы
based on PIDRobotDemo.c
Reference : www.tic.ac.uk/micromouse/PRESENTATIONS/Heretic.ppt
http://www.cse.cuhk.edu.hk/khwong/ceg2400/PIDRobotDemo093.c
CENG4480: feedback control V5a
Objectives
1) Study DC motors
2) Study open-loop and closed-loop
control
3) Control methods
I) Proportional feedback control
II) PID (proportional-integral-derivative)
control
1) DC motors
For robots
Motors
DC motors: Direct
current motor,
easy to control and
use. For making
wheeled robots
15351 PRO ,
picture from http://item.taobao.com/item.htm?
id=1606576457&tracelog=newcardfavirate
LEN
LDIR
1(EN1/2)
7(2A)
REN
10(3A)
(3Y)11
RDIR
9(EN3/4)
15(4A)
(4Y)14
Left-motor
(2Y)6
Right-motor
L293D: H-bridge
circuit, up 2A
LDIR: left motor
direction; RDIR:
right motor
direction
LEN : left motor
enable; REN :
right motor
enable
Exercise
When using the open-loop control
method with a constant PWM signal
for both wheels, explain why the
robot would slow down 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 feedback control
Require speed encoder to read back
the real speed of the wheel at real
time.
10
11
Wheel encoder
Our motor and
speed encoder
Each wheel rotation=
88 on/off changes
IR receiver
Darkened
part
blocks light
IR light source
12
13
66mm
15351 PRO
,
picture from
http://item.taobao.com/item.htm?
id=1606576457&tracelog=newcardfavir
ate
Demo
movie
CENG4480: feedback control V5a
15
3) Control methods
I) Proportional feedback control
II) PID (proportional-integralderivative) control
16
leftErr
+
-
Alter PWM
for driver
L293
leftPWM
Motor
leftRPM
17
18
control method:
PID (proportional-integral-derivative) control
Required speed=
leftRPMset
leftErr
Motor
integral control
leftPWM
Igain* leftErr dt
Proportional control
Pgain*leftErr
sum
IR wheel
Speed
encoder
generates
PWM
for driver
L293
Derivative control
Dgain*[d(leftErr)/dt]
leftPWM
LeftRPM
leftRPM
19
Introduction
Good performance
Criteria depends
on users and
applications
required
time
CENG4480: feedback control V5a
20
Steady
state
error
overshoot
Target
value
Typically
value=10%
Depends
on application
undershoot
Rise time
Settling time
time
21
Use of PID
control terms are intertwined
http://en.wikipedia.org/wiki/PID_controller
22
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
23
Software
PIDrobotdemo.c
(at course webpage)
http://www.cse.cuhk.edu.hk/~khwong/www2/ceng2400/PIDRobotDe
mo093.c
24
IR receiver
Speed Encoder
sensor
interrupts
time
_IRQ( )//1000Hz
{
:
read wheel speed
PID
:
}
25
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.
}
CENG4480: feedback control V5a
Interrupt
rate 1000Hz
_IRQ
see next slide
26
/int0
IR transmitter
In our robot
88 pattern changes
in one rotation
GPIO
Parallel
interface
CPU
IR receiver
(speed encoder
sensor)
27
Exercise 3
_irq interrupt programming method for
the main PID loop, using a counter
(intcount)
intcount=0;
}
}
Question: If if(intcount==250) is changed to if(intcount==500)
What happens?
CENG4480: feedback control V5a
28
29
Part1
Read
Wheel
speeds
Part 2
PID
control
lefttimeval
sensor
void __irq IRQ_Exception()
{ms++;
intcount++;
//get the current wheel sensor values
lcur=IO0PIN & LWheelSen;rcur=IO0PIN & RWheelSen;
if(lcur!=lold) {
lcount++;lold=lcur;lefttimeval++;}
time
Interrupts 1000 Hz
:
//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
intcount=0;
lefttimeval=0;
}
}
CENG4480: feedback control V5a
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
Error <= dead_band, error too small do nothing
31
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
19)
20)
21)
PID core
if(intcount==250) {//for every seconds
//caculate the speed of left motor in RPM
P=Proportional
leftRPM=lefttimeval;
leftErr = leftRPMset - leftRPM;
//caculate left error
if((leftErr<DeadBand*(-1))||(leftErr>DeadBand)) //see next slide I Integral
{
//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
D Derivative
leftlastErr = leftErr;
//update left last error
:
: // handle right motor similarly.
current_leftRPM = leftRPM*240/88;
Pgain, Igain, Dgain are
current_leftPWM = leftPWM;//
constants found by a trial and
lefttimeval = 0;
error method, here we have
}
Pgain = 8000;
Igain = 6000;
Dgain = 5000;
V5a
32
control method:
PID (proportional-integral-derivative) control
Required speed=
leftRPMset
leftErr
Motor
integral control
leftPWM
Igain* leftErr dt
Proportional control
Pgain*leftErr
sum
IR wheel
Speed
encoder
generates
PWM
for driver
L293
Derivative control
Dgain*[d(leftErr)/dt]
leftPWM
LeftRPM
leftRPM
33
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 term
10) leftPWM += (leftP + leftI + leftD);//update motorPWM
by PID
:
:
14) leftaccErr += leftErr;
// accumulate error
CENG4480: feedback control V5a
34
Dead band
line5) if((leftErr<DeadBand*(-1))||
(leftErr>DeadBand))
35
Exercise 4:
Discuss what will happen if dead-band is changed, say (a) 0.5
or (b) 2.
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.
CENG4480: feedback control V5a
36
see line 18
Steady
state
error
overshoot
Target
value
Typically
value=10%
Depends
on application
undershoot
Rise time
Settling time
time
37
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
38
(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).
39
overshoot
Target speed=
leftRPMset =10
8
6
leftErr=
leftRPMset
leftPRM
=10-6=4
undershoot
P is
+ve
P is
+ve
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
t1 t2 t3 t4
time
Rise time
leftP = Pgain * leftErr;
leftP = Pgain * leftErr;
leftP= 8000* (10-6)= 8000*4 is positive leftP=?___________ ,is +ve or -ve?
leftPWM is increasedor decreased?__
This term pushing up leftPWM
(more energy delivered to the wheel). more/less energy delivered to wheel?__
0
40
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
time
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
41
42
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
Small
Change
43
Derivative term
Derivative control
Dgain*[d(leftErr)/dt]
d(leftErr)/dt =
=Derivative term
=current_Err - last_Err
=leftErr leftlastErr ; in our program
44
Target
Value=
seconds
overshoot
leftRPMset
=10
leftRPMset =10
leftErr=
leftRPMset
7
leftPRM
=10-7=3
leftlastErr= 3
leftRPMset
eftlastPRM
=10-3=7
0
undershoot
D is
-ve
e.g.
Pgain = 8000;
Igain = 6000;
Dgain = 5000;
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
To do:This term lowering down leftPWM (less energy delivered to the wheel).
CENG4480: feedback control V5a
45
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
time
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
46
undershoot
D is D is
-ve -ve
t1 t2 t3 t4
time
Rise time CENG4480: feedback control V5a
47
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
48
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
CENG4480: feedback control V5a
49
undershoot
D is D is
+ve +ve
0
t1 t2 t3 t4
time
Rise time CENG4480: feedback control V5a
50
51
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
52
control method:
PID (proportional-integral-derivative) control
Required speed=
leftRPMset
leftErr
Motor
integral control
leftPWM
Igain* leftErr dt
Proportional control
Pgain*leftErr
sum
IR wheel
Speed
encoder
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.
53
In our experiment
leftD=0
54
+leftErr(t=now)}
previous errors
e.g.
Pgain = 8000;
:
Igain = 6000;
10) leftPWM += leftI // near steady state, only leftI is valid
Dgain = 5000;
:
55
Igain = 6000;
Dgain = 5000;
In our experiment
leftPWM=276000 at the
beginning and
192800 at steady state
56
57
PID Tuning
(usually done by
trail and error)
Tune (adjust manually)
Motor speed
Accepted
performance
V1
(V1)/2
time
T1
unstable
58
Application of
PID
59
Robot Turning
(for smooth circular turns around 90-degree
corners)
http://micromouse.cannock.ac.uk/dynamics/smoothcorners.htm
60
Summary
Studies PID control theory and
implementation
61
Appendix:
New robot drive circuit ver13.3
62