62
CENG 4480 Lecture 06: PID Control Bei Yu CENG4480: feedback control V5a 1

CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

CENG4480Lecture06:PIDControl

BeiYu

CENG4480: feedback control V5a 1

Page 2: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Objectives

1) StudyDCmotors

2) Studyopen-loopandclosed-loop control

3) Controlmethods– I)Proportional feedbackcontrol

– II)PID(proportional-integral-derivative) control

CENG4480: feedback control V5a 2

Page 3: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

1)DCmotors

Forrobots

CENG4480: feedback control V5a 3

Page 4: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Motors

• DCmotors:Directcurrentmotor,easytocontrolanduse.Formakingwheeledrobots

• Servomotorsformakingrobotlegshttp://www.lynxmotion.com/

CENG4480: feedback control V5a 4

Page 5: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

SmallDirectCurrentD.C.motors

• Speed(~1200-2000rpm).• Operatesona3~5Volt,Canusegearbox(e.g.ratio58:1)toincreasetorque

• UseH-bridgecircuittoboostupcurrentfromtheTLLleveltomotordrivinglevel.

CENG4480: feedback control V5a 5

������15351 PRO����� ,picture from

http://item.taobao.com/item.htm?id=1606576457&tracelog=newcardfavirate

Page 6: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Motorcontrolchip

• H-bridgeChips

• L293D:H-bridgecircuit,up2A

• LDIR:leftmotordirection;RDIR:rightmotordirection

• LEN:leftmotorenable;REN:rightmotorenable

CENG4480: feedback control V5a 6

LEN

LDIR

REN

RDIR

2 (1A)1Y(3)

1(EN1/2)

7(2A) (2Y)6

10(3A)(3Y)11

9(EN3/4)

15(4A)(4Y)14

Left-motor

Right-motor

Page 7: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

2)open-loopandclosed-loopcontrol

FeedbackcontrolPIDtheoryandimplementation

CENG4480: feedback control V5a 7

Page 8: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Open-loopmotorcontrolanditsproblems

• Changemotorsupplypowerchangespeed• Problem:Howmuchpowerisright?

– Ans:don’tknow,dependsoninternal/externalfrictionsofindividualmotors.

• Problem:Howtomaketherobotmovestraight?• Howtocontrolpower(Ton)byISR&anMCU?

– Solution:Usefeedbackcontroltoreadactualwheel:– Slower,increasepower(+Ton)– Faster,reducepower(- Ton)

CENG4480: feedback control V5a 8

Page 9: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

PWMSignal• PulseWidthModulation• Analogresultswithdigitalmeans• asquaresignalswitchedbetweenonandoff• changingtheportionthesignalon

CENG4480: feedback control V5a 9

Page 10: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Exercise

• Whenusingtheopen-loop controlmethodwithaconstantPWMsignalforbothwheels,explainwhytherobotwouldslowdownwhenclimbinguphill.– Ans:Whenclimbinguphill,energyisusedtoactagainstgravity,hencetherobotisrunningslower.

CENG4480: feedback control V5a 10

Page 11: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Feedbackcontrol

• Therealsolutiontorealspeedcontrolisfeedbackcontrol

• Requirespeedencodertoreadbacktherealspeedofthewheelatrealtime.

CENG4480: feedback control V5a 11

Page 12: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Firstyouneedtohavespeedencoders

• Readwheelspeed.• Usephotointerrupter• Usereflectivedisktosavespace• Basedoninterrupts

CENG4480: feedback control V5a 12

Page 13: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Wheelencoder

CENG4480: feedback control V5a 13

Our motor and speed encoder

Each wheel rotation=88 on/off changes

IR receiver

IR light source

Darkenedpart blocks light

Page 14: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

CENG4480: feedback control V5a 14

Page 15: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Newspeedencoder

CENG4480: feedback control V5a 15

Demomovie

������15351 PRO����� ,picture from

http://item.taobao.com/item.htm?id=1606576457&tracelog=newcardfavirate

Page 16: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

3)Controlmethods

I)ProportionalfeedbackcontrolII)PID(proportional-integral-derivative)control

CENG4480: feedback control V5a 16

Page 17: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

I)Proportionalclosed-loopfeedbackcontrolsystem

• Showtheleftmotorcontrolonly

CENG4480: feedback control V5a 17

Required speed=leftRPMset

leftRPM

+

-

leftErr Motor

Alter PWM for driver L293

leftPWM

if (leftErr >deadband)leftPWM increase by (Pgain * leftErr)

Page 18: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

II)PID(proportional-integral-derivative)control

AmoreformalandprecisemethodUsedinmostmodernmachines

CENG4480: feedback control V5a 18

Page 19: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

HistoryofPID

• By NicolasMinorskyin1922• observationsofa helmsman• steeredtheshipbasedon

– thecurrentcourseerror– pasterror– thecurrentrateofchange

CENG4480: feedback control V5a 19

Page 20: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Introduction• Controlforbetterperformance• UsePID,choosewhateverresponseyouwant

CENG4480: feedback control V5a 20

Good performanceCriteria depends on users and applications

Too much overshoot/undershoot, not stable

Response too slow

time

Motor speed (w)

required

Page 21: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

ValuestoevaluateacontrolsystemExercise2:Describethetermsnthefollowing

diagrams•

CENG4480: feedback control V5a 21

Typicallyvalue=10%Depends on application

Rise time Settling time0 time

Target value

overshoot

Steady state error

undershoot

Page 22: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

PIDControl

CENG4480: feedback control V5a 22

Page 23: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

PIDControl

CENG4480: feedback control V5a 23

Page 24: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

UseofPIDcontroltermsareintertwined

http://en.wikipedia.org/wiki/PID_controller

• Kp(Pgain):ProportionalGain - LargerKptypicallymeansfasterresponse sincethelargertheerror,thelargertheProportional termcompensation.Anexcessivelylargeproportionalgainwillleadtoprocess instabilityandoscillation.

• Ki(Igain):IntegralGain - LargerKiimpliessteadystateerrorsareeliminatedquicker.Thetrade-off islargerovershoot:anynegativeerrorintegratedduringtransientresponsemustbeintegratedawaybypositiveerrorbeforewereachsteadystate.

• Kd(Dgain):DerivativeGain - LargerKddecreasesovershoot,butslowsdowntransientresponse andmayleadtoinstabilityduetosignalnoiseamplificationinthedifferentiationoftheerror.

CENG4480: feedback control V5a 25

Page 25: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Effectsofincreasingparametershttp://en.wikipedia.org/wiki/PID_controller

Parameter Rise Time Overshoot Settling Time

Steady state error

Kp (Pgain) Decreasestep1

Increase Small Change

Decrease

Ki (Igain) Decrease Increase Increase Eliminatestep3

Kd (Dgain) Small Change

Decreasestep2

Decrease Small Change

CENG4480: feedback control V5a 26

Page 26: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Software

PIDrobotdemo.c(atcoursewebpage)

http://www.cse.cuhk.edu.hk/~khwong/www2/ceng2400/PIDRobotDemo093.c

CENG4480: feedback control V5a 27

Page 27: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

controlmethod:PID(proportional-integral-derivative)control

CENG4480: feedback control V5a 28

Required speed=

leftRPMset

IR wheelSpeedencoder

LeftRPM

+

-

leftRPM

Motor

generates PWM for driver L293

leftPWM

integral control

Igain* leftErr dt

Proportional control

Pgain*leftErr

Derivative control

Dgain*[d(leftErr)/dt]

sumleftErr

leftPWM

Page 28: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

PIDcontrolalgorithmusinginterrupt

CENG4480: feedback control V5a 29

IR receiver Speed Encodersensor

interruptstime

Main( ){Setup( );::}

_IRQ( )//1000Hz{:read wheel speedPID:}

1000 interrupts per second

Page 29: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Overview• //////////////main//////////////////////////////////////• Main()• {setup()• forward(Lstep,Rstep,Lspeed,Rspeed)…..• }• ////////////subroutine //////////////////////////////////////////• forward(Lstep,Rstep,Lspeed,Rspeed)• {lcount=0• if(lcount>=Lstep)• Stopleftmotor• …sameforrightmotor…• .}• //////////timertriggered,interruptserviceroutine,1000Hz///////• __irqexception//Interrupt()runningat1000HZ• {lcount++• readwheelspeeds• PIDcontroltoachieveL/Rspeed• ….sameforrightmotor….• }

CENG4480: feedback control V5a 30

_IRQ see next slide

Interrupt rate 1000Hz

Page 30: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Speedencoderinterfacing(showleftmotoronly)

• Blockdiagram

CENG4480: feedback control V5a 31

IR transmitter

IR receiver(speed encoder sensor)

GPIOParallel interface

/int0

CPU

1000Hz timer interrupt

ARM7-microcontrollerLPC2131

In our robot88 pattern changesin one rotation

Page 31: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Exercise3_irqinterruptprogrammingmethodforthemainPIDloop,usingacounter(intcount)

• __irq()exception//timer interrupt1000HZ,• {intcount++;//intcount• //increasesat1000times/sec• //readmotorspeed:• //updatethemotorspeedinevery1/4seconds• if(intcount==250)//happensatevery¼seconds• {• readwheelspeed• ControlthePWMusingPID• intcount=0;• }• }• Question:If“if(intcount==250)“ischangedto“if(intcount==500)“• Whathappens? CENG4480: feedback control V5a 32

PID CORE

Page 32: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Theinterruptserviceroutineenablesthelooptorun4timesinasecond

CENG4480: feedback control V5a 33

Loop once in ¼ seconds

Page 33: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Speedcontrolinterruptcore_irq()part1:Findmotorspeed,leftRPM

• void__irqIRQ_Exception()• {ms++; intcount++;• //getthecurrentwheelsensorvalues• lcur=IO0PIN&LWheelSen;rcur=IO0PIN&RWheelSen;• if(lcur!=lold) { lcount++;lold=lcur;lefttimeval++;}• :• //updatemotor speedbyPIDfeedbackcontrolinevery¼Seconds• if(intcount==250) {//calculatethespeedofleftmotor inRPM• leftRPM=lefttimeval;• :

• intcount=0;• lefttimeval=0;• }• }

CENG4480: feedback control V5a 34

PID core : See following slides

Speed encodersensor

timeInterrupts 1000 Hz

lefttimeval

•lcount :steps of wheel •lcur : sensor read 1 or 0•lefttimeval: time lapsed since sensor last change of state•leftRPM : left wheel RPM

Part 2PID control

Part1Read Wheelspeeds

Page 34: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Part2:AlgorithmforPIDcore

• Forevery¼seconds– Finderror=(desired value- measuredvalue)– {If(error>deadband)

• {find– Error,– Accumulatederror(addupallpreviouserrors)– Derivativeerror(currenterror– previouserror)– PWM+=Kp*Error+– Ka*(Accumulatederror)+– Kd*Derivativeerror;

• }• Else

– Error<=dead_band,errortoosmalldonothing

CENG4480: feedback control V5a 35

In our experiment leftPWM=276000 at the beginning and 192800 at steady state

Set_point

Page 35: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

part2:PIDcore1) if(intcount==250){//forevery¼seconds2) //caculatethespeedofleftmotorinRPM3) leftRPM=lefttimeval;4) leftErr=leftRPMset- leftRPM; //caculatelefterror5) if((leftErr<DeadBand*(-1))||(leftErr>DeadBand))//seenextslide6) { //if|lefterror|>deadband7) leftP=Pgain*leftErr; //calculatePProportionalterm8) leftI=Igain*leftaccErr; //calculateIIntegralterm9) leftD=Dgain*(leftErr- leftlastErr); //calculateDDerivativeterm10) leftPWM+=(leftP+leftI+leftD);//update leftmotorPWMusingPID11) if(leftPWM>PWM_FREQ)12) leftPWM=PWM_FREQ;//preventoverrange(max.=PWM_FREQ)13) if(leftPWM<0)leftPWM=0;//(min.=0)14) leftaccErr+=leftErr; //accumulateerror15) leftlastErr=leftErr;//update leftlasterror16) :17) ://handlerightmotorsimilarly…….18) current_leftRPM=leftRPM*240/88;19) current_leftPWM=leftPWM;//20) lefttimeval=0;21) }

CENG4480: feedback control V5a 36

PID core

P=Proportional

I Integral

D Derivative

Pgain, Igain, Dgain are constants found by a trial and error method, here we havePgain = 8000; Igain = 6000; Dgain = 5000;

because each rotation has 88 counts, the ISR loop is in ¼ seconds, each minutes 60 seconds. see line 18

Page 36: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

controlmethod:PID(proportional-integral-derivative)control

CENG4480: feedback control V5a 37

Required speed=

leftRPMset

IR wheelSpeedencoder

LeftRPM

+

-

leftRPM

Motor

generates PWM for driver L293

leftPWM

integral control

Igain* leftErr dt

Proportional control

Pgain*leftErr

Derivative control

Dgain*[d(leftErr)/dt]

sumleftErr

leftPWM

Page 37: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

InsidethePIDcore,wewillstudytheselines

• 5)if((leftErr<DeadBand*(-1))||(leftErr>DeadBand))• :• :• 7)leftP=Pgain*leftErr;//calculatePProportionalterm• 8)leftI=Igain*leftaccErr;//calculateIIntegralterm• 9)leftD=Dgain*(leftErr- leftlastErr);//calculateDDerivativeterm• 10)leftPWM+=(leftP+leftI+leftD);//updatemotorPWMbyPID• :• :• 14)leftaccErr+=leftErr; //accumulateerror

CENG4480: feedback control V5a 38

Page 38: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Deadbandline5)if((leftErr<DeadBand*(-1))||(leftErr>DeadBand))

• Dead-band :ADead-band (sometimescalledaneutralzone)isanareaofasignal range orbandwherenoactionoccurs :

• onlyenablemotorwhenleftErr>asmallvalue(deadband,ie=1inourrobot)

• OtherwisemayoscillatewhenleftErrissmall

CENG4480: feedback control V5a 39

Dead-band

leftErr = leftRPM - leftRPMset; //calculate left errorif(leftErr>DeadBand ){ activate motor}

Page 39: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Exercise4:Discusswhatwillhappenifdead-bandischanged,say(a)0.5or(b)2.

Exampleofadeadband;donothing“if10-1<leftPRM<10+1

CENG4480: feedback control V5a 40

Typicallyvalue=10%Depends on application

Rise time Settling time0 time

overshoot

Steady state error

undershoot

Dead band+/- 1

Target speed=leftRPMset =10

In our experiment leftPWM=276000 at the beginning and 192800 at steady state

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.see line 18

Page 40: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Parametersforevaluatingacontrolsystem

CENG4480: feedback control V5a 41

Typicallyvalue=10%Depends on application

Rise time Settling time0 time

Target value

overshoot

Steady state error

undershoot

near steady stateSee next slide

Page 41: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

(line7)EffectsofincreasingKp(P)http://en.wikipedia.org/wiki/PID_controller

Parameter Rise Time Overshoot Settling Time

Steady state error

(1) Kp (Pgain)

Decreasestep1

Increase Small Change

Decrease

Ki (Igain) Decrease Increase Increase Eliminatestep3

Kd (Dgain) Small Change

Decreasestep2

Decrease Small Change

CENG4480: feedback control V5a 42

Page 42: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Example:t0àt1,Theproportionaltermwhenthemeasurementisbelowthesetpoint(leftRPMset),

proportionalPtermis+ve•

CENG4480: feedback control V5a 43

leftP = Pgain * leftErr;leftP= 8000* (10-6)= 8000*4 is positiveThis term pushing up “leftPWM” (more energy delivered to the wheel).

leftErr=leftRPMset –leftPRM=10-6=4

Rise time0 time

overshoot

undershoot

t1 t3t2 t4

6

P is +ve

Target speed=leftRPMset =10

Usage of the P proportional termEach ¼ seconds a new left RPM (speed of wheel) is measured

leftErr=leftRPMset –leftPRM=30-28=2

leftP = Pgain * leftErr;leftP= 8000* (0-8)= 8000*2 is positiveThis term pushing up “leftPWM” (more energy delivered to the wheel).

P is +ve

e.g.Pgain = 8000; Igain = 6000; Dgain = 5000; In our experiment leftPWM=276000 at the beginning and 192800 at steady state

8

(leftErr = leftRPMset-leftRPM )

Page 43: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Exercise5:Fillin?__:t0àt1,Theproportionaltermwhenthemeasurementisbelowthesetpoint(leftRPMset),

proportionalPtermis+ve•

CENG4480: feedback control V5a 44

leftP = Pgain * leftErr;leftP= 8000* (10-6)= 8000*4 is positiveThis term pushing up “leftPWM” (more energy delivered to the wheel).

leftErr=leftRPMset –leftPRM=10-6=4

Rise time0 time

overshoot

undershoot

t1 t3t2 t4

6

P is +ve

Target speed=leftRPMset =10

Usage of the P proportional termEach ¼ seconds a new left RPM (speed of wheel) is measured

leftErr=leftRPMset –leftPRM=10-8=2

leftP = Pgain * leftErr;leftP=?___________ ,is +ve or -ve?“leftPWM” is increasedor decreased?__more/less energy delivered to wheel?__

P is +ve

e.g.Pgain = 8000; Igain = 6000; Dgain = 5000; In our experiment leftPWM=276000 at the beginning and 192800 at steady state

8

Page 44: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Example:t1àt3,Theproportionaltermwhenthemeasurementisbelowthesetpoint(leftRPMset),the

proportionalPtermis+ve•

CENG4480: feedback control V5a 45

leftP = Pgain * leftErr;leftP= 8000* (10-13)= 8000*(-3) is negativeThis term lowering down “leftPWM” (less energy delivered to the wheel).

leftErr=leftRPMset –leftPRM=10-13= -3

Rise time0 time

overshoot

undershoot

t1 t3t2 t4

13

P is +ve

Target speed=leftRPMset =10

Usage of the P proportional termEach ¼ seconds a new left RPM (speed of wheel) is measured

P is -ve

e.g.Pgain = 8000; Igain = 6000; Dgain = 5000;

In our experiment leftPWM=276000 at the beginning and 192800 at steady state

Page 45: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

UnderstandingPID–alittlesummaryforthePproportionalterm

• Whenthemeasurementisbelowthesetpoint(leftRPMset)– Themotoriscurrentlytooslow– ThePtermcalculatedis+ve,needtopushthespeedhigher

• Whenthemeasurementisabovethesetpoint(leftRPMset)– Themotorisrunningtoofast– ThePproportionaltermis-ve,loweringdownthespeed.

• IncreaseinPgain(Kp)willdecreaserisetime(meaningfastertoreachsetpoint),decreasesteadystateerror(studyitlater)– Italsoincreasesovershoot

CENG4480: feedback control V5a 46

Page 46: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

(line9)EffectsofincreasingKd(D)http://en.wikipedia.org/wiki/PID_controller

Parameter Rise Time Overshoot Settling Time

Steady state error

Kp (Pgain)Igain

Decreasestep1

Increase Small Change

Decrease

Ki (Igain)gainI

Decrease Increase Increase Eliminatestep3

Kd (Dgain) Small Change

Decreasestep2

Decrease Small Change

CENG4480: feedback control V5a 47

Page 47: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Derivativeterm

• Derivativecontrol– Dgain*[d(leftErr)/dt]

• d(leftErr)/dt =– =Derivativeterm– =current_Err- last_Err– =leftErr– leftlastErr;inourprogram

CENG4480: feedback control V5a 48

Page 48: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Example:time0à t1,theDerivativeterm(=current-previous)Whenthemeasurementisrising,theDerivativetermis-ve

CENG4480: feedback control V5a 49

leftD = Dgain * (leftErr – leftlastErr); leftD= 5000* (3-7)= 5000*( -4) is negativeTo do:This term lowering down “leftPWM” (less energy delivered to the wheel).

leftlastErr=leftRPMset –eftlastPRM=10-3=7

leftErr=leftRPMset –leftPRM=10-7=3

Rise time0 time

Target Value=leftRPMset=10

overshoot

undershoot

¼ seconds

t1 t3t2 t4

3

7

D is -ve

leftRPMset =10

Usage of the D Derivative termEach ¼ seconds a new left RPM (speed of wheel) ismeasured

e.g.Pgain = 8000; Igain = 6000; Dgain = 5000;

In our experiment leftPWM=276000 at the beginning and 192800 at steady state

Page 49: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Example:timet1àt2,theDerivativetermWhenthemeasurementisrising,theDerivativetermis-ve

CENG4480: feedback control V5a 50

Rise time0 time

overshoot

undershoot

leftD = Dgain * (leftErr – leftlastErr); leftD= 5000* (-5- (-2))= 5000*(-3) is negativeThis term lowering down “leftPWM” (less energy delivered to the wheel).

leftlastErr=leftRPMset -leftlastPRM=10-12= -2

¼ secondsleftErr=leftRPMset -leftPRM=10-15= -5

t1 t3t2 t4

leftRPMset =10

D is -ve

1512

leftPRM

D is -ve

Usage of the D Derivative termEach ¼ seconds a new left RPM (speed of wheel) ismeasured

e.g.Pgain = 8000; Igain = 6000; Dgain = 5000;

In our experiment leftPWM=276000 at the beginning and 192800 at steady state

Page 50: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

LittleSummary:NegativeDDerivativeterm• Whenthemeasurement(leftRPM)isrising,thechange

(change=current-previous)oferror(error=setpoint-leftRPM)is-ve=d(Err/dt)=-ve

• DDerivativeterm=Kd*d(Err/dt)is–VE• Decreaseenergytomotorà decreaseovershoot

CENG4480: feedback control V5a 51Rise time0 time

undershoot

t1 t3t2 t4

D is -ve

D is -ve

Setpoint��

overshoot

Page 51: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Example:timet2àt3,theDerivativetermWhenthemeasurementisfalling,theDerivativetermis+ve

CENG4480: feedback control V5a 52

Rise time0 time

overshoot

undershoot

leftD = Dgain * (leftErr – leftlastErr); leftD= 5000* (-1- (-4))= 5000*3 is positiveThis term pushing up “leftPWM” (more energy delivered to the wheel).

Usage of the D Derivative termEach ¼ seconds a new left RPM (speed of wheel) ismeasured

leftlastErr=leftRPMset -leftlastPRM=10-14= -4

¼ seconds

leftErr=leftRPMset -leftPRM=10-11= -1

t1 t3t2 t4

leftRPMset =10

D is -ve

1411

leftPRM

D is +ve

D is -ve

e.g.Pgain = 8000; Igain = 6000; Dgain = 5000;

In our experiment leftPWM=276000 at the beginning and 192800 at steady state

Page 52: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Exercise6:Fillin?_timet2àt3,theDerivativetermWhenthemeasurementisfalling,theDerivativetermis+ve

CENG4480: feedback control V5a53

Rise time0 time

overshoot

undershoot

leftD = Dgain * (leftErr – leftlastErr); leftD= ?___________, which is +ve or –ve?____“leftPWM” increased or decreased?____More/less energy delivered to the wheel?___

Usage of the D Derivative termEach ¼ seconds a new left RPM (speed of wheel) ismeasured

leftlastErr=leftRPMset -leftlastPRM=10-9= 1

¼ seconds

leftErr=leftRPMset -leftPRM=10-7= 3

t1 t3t2 t4

leftRPMset =10

D is -ve

97

leftPRM

D is +ve

D is -ve

D is +ve

e.g.Pgain = 8000; Igain = 6000; Dgain = 5000;

In our experiment leftPWM=276000 at the beginning and 192800 at steady state

Page 53: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

LittleSummary:PositiveDDerivativeterm• Whenthemeasurement(leftRPM)isfalling,thechange

(change=current-previous)oferror(error=setpoint-leftRPM)is+ve

• DDerivativeterm=Kd*d(Err/dt)is+VE• Increaseenergytomotorà decreaseundershoot

CENG4480: feedback control V5a 54Rise time0 timet1 t3t2 t4

D is +ve

D is +ve

Setpoint��

overshoot

undershoot

Page 54: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

UnderstandingPID–alittlesummaryfortheDderivativeterm

• Whenthemeasurement(leftRPM) isrising,– Themotor isgainingspeed– TheDderivativetermis–ve,solowering themotorspeedà decrease

overshoot

• Whenthemeasurement(leftRPM) isfalling,– Themotor isreducingspeed– TheDerivativetermis+ve,sopushing themotorspeedhigherà decrease

undershoot– Inconclusion,thegradientoftheerror(Err)determinesthe

adjustment.Dependsonwhetherd(Err)/dtis+veor–ve.• IncreaseinDgain(Kd)willdecreaseovershoot/undershootand

settlingtime(systemmorestable)

CENG4480: feedback control V5a 55

Page 55: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

(line8)EffectsofincreasingKi(I)http://en.wikipedia.org/wiki/PID_controller

Parameter Rise Time Overshoot Settling Time

Steady state error

Kp (Pgain) Decreasestep1

Increase Small Change

Decrease

Ki (Igain) Decrease Increase Increase Eliminatestep3

Kd (Dgain) Small Change

Decreasestep2

Decrease Small Change

CENG4480: feedback control V5a 56

Page 56: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

controlmethod:PID(proportional-integral-derivative)control

CENG4480: feedback control V5a 57

Required speed=

leftRPMset

IR wheelSpeedencoder

LeftRPM

+

-

leftRPM

Motor

generates PWM for driver L293

leftPWM

integral control

Igain* leftErr dt

Proportional control

Pgain*leftErr

Derivative control

Dgain*[d(leftErr)/dt]

sumleftErr

leftPWM

At steady state, leftErrà0, soSo, Pgain*leftErr=0

also Dgain*[d(leftErr)/dt] =0

And if no Integral { Igain* leftErr dt} term, left PWM à0

It is a problem.

Page 57: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Timeà nearsteadystateleftRPMset=leftRPM

henceleftErr=0,leftlastErr=0à leftP=0,leftD=0

• leftErr=leftRPMset– leftRPM=0• SoasleftlastErr=0• Therefore• Steps• 7)leftP=Pgain*leftErr//=0 //calculatePProportional term• 8)leftI=Igain*leftaccErr; //calculateIIntegralterm• 9)leftD=Dgain*(leftErr - leftlastErr)//=0 //calculateDDerivativeterm• 10)leftPWM+=(leftP+leftI+leftD);//update leftmotorPWMusingPID

Theonlyvalidtermistheintegralterm“leftI”• leftPWM+=leftI• Themainideaistocreateasmallterm(leftI)tomaintaintheleftPWMvalue

CENG4480: feedback control V5a 58

e.g.Pgain = 8000; Igain = 6000; Dgain = 5000;

In our experiment leftPWM=276000 at the beginning and 192800 at steady state

near steady state : leftP=0 leftD=0

Page 58: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Theintegraltermisfoundbyaddingallpreviouserrors

sameasleftaccErr=sum{leftErr(t=0)+leftErr(t=1)+.. +leftErr(t=now)}• 14)leftaccErr+=leftErr;//LeftaccErristhesummationofallpreviouserrors• 8)leftI=Igain*leftaccErr//integralterm,• :• 10)leftPWM+=leftI//nearsteadystate,onlyleftIisvalid• :

• Don’tworryitwillnot becomeinfinitive,onemeasuretosafeguardthisis:• 11)if(leftPWM>PWM_FREQ);// PWM_FREQ=maximumPWMallowed• 12)leftPWM=PWM_FREQ;//preventoverrange• (max.=PWM_FREQ)

• Also,becausenearsteadystate,leftaccErrwilladjustitselfautomatically,seenextslide

CENG4480: feedback control V5a 59

e.g.Pgain = 8000; Igain = 6000; Dgain = 5000;

In our experiment leftPWM=276000 at the beginning and 192800 at steady state

Page 59: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Howtheintegraltermadjustsitselfautomaticallynearsteadystate

• Nearsteadystate– 8)leftI=Igain*leftacceErr– 10)leftPWM+=leftI

• Ifmeasuredspeed(leftRPM)>setpoint– leftErris-ve– leftaccErr(Acculumation)decreases,

hencereducingleftaccErratasuitablevaluetomaintainleftPWM

• Ifmeasuredspeed(leftRPM)<setpoint– leftErris+ve– leftaccErr(Acculumation)increases,

henceincreasingleftaccErratasuitablevaluetomaintainleftPWM

CENG4480: feedback control V5a 60

Pgain = 8000; Igain = 6000; Dgain = 5000;

In our experiment leftPWM=276000 at the beginning and 192800 at steady state

Page 60: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

UnderstandingPID–alittlesummaryfortheIIntegralterm

• Whenthemeasurementisbelowthesetpoint(leftRPMset)– Themotorisslow– TheIIntergraltermis+ve,pushingthespeedhigher

• Whenthemeasurementisabovethesetpoint(leftRPMset)– Themotorisrunningtoofast– TheIintergraltermis-ve,loweringdownthespeed.

• IncreaseinIgain(Ki)willresultin– ItissimilartothePterm(seeabovetwopoints)– Soincreaseovershoot, settlingtime– anddecreaserisetime– Note:themainfunction ofIntegralcontrolistoreducesteadystateerror

CENG4480: feedback control V5a 61

Page 61: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

PIDTuning(usuallydonebytrailanderror)

• Tune(adjustmanually)– step1)Pgainproportional_gain(Kp),– step2)Dgainderivative_gain(Kd),– step3)Igainintegral_gain(Ki)

• SetconstantspeedV1forawhile(5seconds)andreducedto(V1)/2atT1

– RecordthespeedbythecomputerafterT1andseeiftheperformanceisokornot• Yes(acceptKp,Ki,Kd)• No(tuneKp,Ki,Kd again)

CENG4480: feedback control V5a 62

Motor speed

V1

(V1)/2

Accepted performance

unstable

done

T1 time

Page 62: CENG 4480 Lecture 06: PID Controlbyu/CENG4480/2016Fall/L06_pid.pdf · 2016-11-01 · Discuss what will happen if dead -band is changed, say (a) 0.5 or (b) 2. Example of a dead band

Summary

• StudiesPIDcontroltheoryandimplementation

CENG4480: feedback control V5a 63