Вы находитесь на странице: 1из 18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

Live Search
Ex:Arduino,PIC....

Cytron Online
Store

PID for Embedded Design


Programming/Software

Popular Post

Robot Controller

June 22, 2012 by ober

Getting Started
On Graphical
User Interface
GUI

ByKongWaiWeng
RH2TMag,Vol.4,Mar2010
PIDcontrolsystemisoneofthemostmatureandcommonly
usedcontrolstrategiesintheindustrialfordecadesthanksto
its simple but effective algorithm. In this article, we will

XBee Series 2
Point to Point
Communication

discuss the basic concept of PID controller and how to


implementitintheembeddedsystem.
http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

1/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

Introduction
Closedloopcontrolsystemisanessentialtopicforembeddedsystems,
bringing together actuators and sensors with the control algorithm in
software.Forexample,motorspeedcanbecontrolledbyvaryingthe
PWMdutycycleusedtodrivethemotor.Thisistheopenloopcontrol.
However, due to the lack of feedback, the actual motor speed could
not be verified. This is important because driving the motor with the
same PWM duty cycle does not guarantee the motor to run at the

Moisture sensor
module (SNMOISTURE-MOD)

same speed under all circumstances. A motor will run faster in load
freeconditionthanunderloadwiththesamePWMsignal.Inorderto

Categories

control the motor speed, we need to close the loop by adding


feedbackoftheactualmotorspeedtothecontroller.
Besidesspeedcontrol,PIDcontrolsystemcanalsobeusedtocontrol

open all | close all


Buying Guides
CT ARM

otherparameterssuchasposition,temperature,waterlevel,stability,

CT UNO
Easy MP3 Shield

etc.Inthisarticle,wewilldiscusshowtoimplementaPIDcontroller

Espresso Lite V2.0

forpositioncontrolbasedonPR24.

Getting Started
Arduino
G15
LCD
PIC

TheProblemDCMotorPositionControl.
BeforewebegintodesignaPIDcontroller,weneedtounderstandthe
problem. In this example, we want to move the shaft of the motor
fromitscurrentpositiontothetargetposition.

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

Programming/Software
Raspberry Pi

Robots
Serial Communication
USB
Wireless
IoT
News
PIR
Product Guides
Projects
Rainbow Strip
rero
Seminars & Events
ThingSpeak
Uncategorized
Useful Tools / External Referen

2/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

Find Us on
Facebook

CytronTechnologie
9,287likes

LikePage

Bethefirstofyourfriendstolikethis

CytronTechnologiesS
Bhdaddedanevent
Yesterdayat06:33

Wewanttomovetheoutputshaftofthemotorfromcurrentposition
totargetposition
There are a few terms commonly used to describe the PID control
loops,suchas:
ControlVariable(CV)Thisistheoutputofthecontrolloop.In

IoTHandsOnWorkshop101

UniversitiTeknologiMalaysia(UTM)
15peopleinterested4peoplegoin

thiscase,theCVisthedutycycleofthePWMsignalthatdrives
themotor.
Process Variable (PV) This is the feedback value returned by
the system to the controller. In this example, the PV is the
currentangleofthemotorshaft.
Set Point (SP) Set point is the value that we desire for the
system. In our case, the SP is the target position of the motor
shaftinangle.
Error(E) Error refers to the difference between the set point
andtheprocessvariable.Inanotherwords,itmeanshowfarthe
currentpositionofthemotorshaftfromthetargetposition.

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

3/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

TheHardwarePR24
Note:PR24isalreadydiscontinued.Samplecodecanbedownloadhere.

The Cytrons DIY Project Set PR24 (PID Motor Controller) is the best
platformforbeginnertolearnthePIDalgorithm.Ithasthefollowing
features:
PIC16F876Aasthemaincontroller.
GearedDCmotorastheoutput.
Multiturn variable resistor connected to the motor shaft for
positionfeedback.
216CharacterLCDfortuningandtroubleshooting.
PresetsforPIDtuning.

DIYProjectSetPR24PIDMotorController
The sample source code for the PR24 (PID Motor Controller) can be
downloaded from Cytrons website under the PR24 product page
(GithubCytronTechnologies).

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

4/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

TheImplementationofPIDController
ThePIDcontroller,justlikeitsname,comprisesaproportional(P),an
integral (I) and a derivative (D) part. The controller parts are
introduced in the following sections individually and in combined
operation.

ProportionalController
Whenthecurrentpositionofthemotorshaftisstillfarawayfromthe
target position, we want to apply more power to drive the motor
towards the target position so that we can reach there faster. When
the shaft is getting nearer to the target position, we will reduce the
power to slow it down. At the time the shaft reaches the target
position, the motor needs to be stopped. If the shaft position has
overshot,weneedtoapplynegativepowertothemotor(reversethe
motor)tobringitbacktothetargetposition.
In short, this is called proportional controller because the power we
applytothemotorisproportionaltotheerrorofthesystem.

Theblockdiagramofproportionalcontroller
Fromtheblockdiagramofproportionalcontroller,wecanseethatthe
PWM duty cycle (output) is the result of multiplying the error with a
constant,Kp.

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

5/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

Error=SetPointProcessVariable
ControlVariable=Kp*Error
Figurebelowshowstheexampleofproportionalloopimplementedin
Clanguage.

ImplementationofproportionalloopinClanguage
The value of Kp needs to be chosen carefully in order to get the
optimum system response. Lower values for Kp will tend to give
smootherbutslowerresponses.

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

6/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

SystemresponseforproportionalcontrollerwithlowKp
Higher values of Kp will yield much quicker response but may cause
overshoot,wheretheoutputoscillatesbeforesettling.

SystemresponseforproportionalcontrollerwithhighKp
Excessively high values of Kp may even throw the loop into an
unstablestatewheretheoutputoscillateswithouteversettlingatthe
setpoint.

SystemresponseforproportionalcontrollerwithexcessivelyhighKp

IntegralController

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

7/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

As can be seen from the graph of P controller, the actual position of


themotorshaft,whensettlesdownwillnotreachthetargetposition.
This is because when the current position is near to the target
position, the error becomes very small and the computed PWM duty
cycle is too small for the motor to overcome the friction and gravity.
Thesmallerrorthatexistswhenthesystemhassettleddowniscalled
thesteadystateerror.

Steadystateerrorduetothefrictionandgravity
ToovercometheproblemofsteadystateerrorforthePcontroller,I
controller is being introduced. As its name suggests, the integral is
merelyanaccumulatederrorsignalsencounteredsincestartup.
Integral=(Error)
This total is multiplied by a constant, Ki, and is added into the loop
output.UnlikethePcontroller,theIcontrollerisrarelyusedalone,but
mostly in combination with the P or PD controller. When the system
hasalreadysettleddownwithasmallsteadystateerror,theintegral
stillcontinuestoaccumulateuntiltheCVislargeenoughtobringthe
PVinlinewithSP.TheequationsforPIcontrollerareasfollow:
http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

8/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

Error=SetPointProcessVariable
Integral=Integral+Error
ControlVariable=(Kp*Error)+(Ki*Integral)
Figure below shows the example of PI controller implemented in C
language.

ImplementationofPIloopinClanguage
JustlikethePcontroller,thevalueofKineedstobechosencarefully.
Toolowthevalue,thesteadystateerroriscorrectedveryslowlytoo
highthevalue,thesystembecomesunstableandoscillates.

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

9/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

SystemresponseforPIcontrollerwithnosteadystateerror
Because the integral can grow quite large when the set point cannot
be reached, some applications stop accumulating the error when the
CVissaturated.

DerivativeController
Letssayyouaredrivingacar,andyouneedtostopyourcarexactly
100m from your current position as soon as possible. If you are
travelling at 10km/h, you would want to accelerate your car so that
you can reach the target sooner. In contrast, if you are cruising at
100km/h,youneedtostartbrakingsothatyoucanstopat100mand
will not overshoot. This is where the derivative controller comes into
play.
The derivative of any variable describes how that variable changes
overtime.InaPIDcontroller,thederivativeistherateofchangeof
theerror.Indigitalform,itcanbedescribedas:
Derivative=ErrorLastError
whereErroristhecurrenterrorvalueandLastErroristheerrorvalue
forthepreviousiteration.

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

10/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

Negative values of derivative indicate an improvement (reduction) in


theerrorsignal.Forexample,ifthelasterrorwas20andthecurrent
erroris10,thederivativewillbe10.Whenthesenegativevaluesare
multiplied with a constant, Kd, and are added to the output of the
loop,itcanslowdownthesystemwhenapproachingthetarget.
Just like the I controller, the D controller is rarely used alone, but
mostly in combination with the P or PI controller. The equations for
thePDcontrollerareasfollow:
LastError=Error
Error=SetPointProcessVariable
Derivative=ErrorLastError
ControlVariable=(Kp*Error)+(Kd*Derivative)
Figure below shows the example of PD controller implemented in C
language.

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

11/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

ImplementationofPDloopinClanguage
The damping effect of the D controller allows the system to have a
highervalueofKpand/orKiwithoutovershooting.Inconsequent,this
will give the system a better response time to set point changes.
However,toohighthevalueofKdwillalsohavenegativeeffect.The
Dcontrollertensetoamplifythenoiseexistsinthefeedbackloop.If
theKdistoohigh,thesystemwillbecomejerkyifthefeedbackloopis
noisy.

MainMenu
HOME

PROJECTS

CATEGORIES

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

LOGIN/REGISTER

SHOP AT CYTRON

FORUM

12/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

SystemresponseforPDcontroller

JoiningThemTogetherPIDController
ByjoiningtheP,IandDcontroller,wecantaketheadvantagesofthe
combined benefits from each controller. We have the P controller for
fastsystemresponse,Icontrollertocorrectthesteadystateerrorand
Dcontrollertodampenthesystemandreduceovershoot.

TheblockdiagramofPIDcontroller
FromtheblockdiagramofPIDcontroller,wecanseethattheoutput
oftheloopismerelythesumofoutputfromP,IandDcontroller.The
equationsforthePIDloopareillustratedbelow:
LastError=Error
MainMenu
HOME

PROJECTS
CATEGORIES
Error=SetPointProcessVariable

LOGIN/REGISTER

SHOP AT CYTRON

FORUM

Integral=Integral+Error
Derivative=ErrorLastError

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

13/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

ControlVariable=(Kp*Error)+(Ki*Integral)+(Kd*Derivative)
Figure below shows the example of PID controller implemented in C
language.

ImplementationofPIDloopinClanguage

MainMenu
HOME

PROJECTS

CATEGORIES

LOGIN/REGISTER

SHOP AT CYTRON

FORUM

SystemresponseforthecorrectlytunedPIDcontroller
http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

14/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

Summary
PID controller is a simple yet effective control system widely used in
industrial.However,toimplementthePIDcontrollerissimple,butnot
thetuning.TheprocessoftuningthePIDparameters(Kp,KiandKd)
is a continuous trial and error process. There is no exact way to
calculate the value for the parameters unless the whole system is
mathematically modeled and simulated. Experience is an important
factortogettheoptimumPIDparametersbasedontheobservationof
the system behavior during the tuning process. If you have inquiry,
feelfreetodiscussinourtechnicalforum.

References:
DennisClarkandMichaelOwings:BuildingRobotDriveTrains.
Thomas Braunl: Embedded Robotics Mobile Robot Design and
ApplicationswithEmbeddedSystems.
http://en.wikipedia.org/wiki/PID_controller
http://www.expertune.com/tutor.html
http://www.newportus.com/products/techncal/techncal.htm
http://www.dprg.org/tutorials/200310a/motorcontrol.pdf
MainMenu

Related Posts:
HOME
PROJECTS

CATEGORIES

LOGIN/REGISTER

SHOP AT CYTRON

FORUM

How RC Servo Works?

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

15/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

Line-Following Robot Using LSA08 in


Serial Mode with PID

How does Stepper Motor Works Part 1


How Does Stepper Motor Works Part 2
Embedded, PIC Microcontroller, PID Control

19 5583 views

Prev Post

Next Post

WIZnet Serial to Ethernet


Gateway with PIC

Interfacing servo motors


controllers (SC08A and SC16A)

Microcontroller

using Arduino

MainMenu
HOME

PROJECTS

CATEGORIES

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

LOGIN/REGISTER

SHOP AT CYTRON

FORUM

16/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

Related Post

Line-Following
Robot Using
LSA08 in Serial
Mode with PID
Controller

Using MDD10A
with CIKU

Getting Started
with MPLAB X
IDE

Quadrature
Encoder

2 thoughts on PID for Embedded Design


vamsi krishna says:
July 25, 2013 at 1:27 am
nice tutorial . is there any tutorial for implementing
the fuzzy logic in microcontroller for motor control

tinku jangid says:


August 24, 2014 at 5:33 pm
MainMenu
HOME

can u tell me how much we will generate in relate to


get a xed or set point frequency
PROJECTS

CATEGORIES

LOGIN/REGISTER

SHOP AT CYTRON

FORUM

Leave a Reply

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

17/18

11/10/2016

PIDforEmbeddedDesignTutorialbyCytron

Your email address will not be published. Required elds are


marked *
Comment

Name *

Email *

Website

PostComment

Notify me of follow-up comments by email.


Notify me of new posts by email.

Cytron Technologies 2016


MainMenu
HOME

Seo wordpress plugin by www.seowizard.org.


PROJECTS

CATEGORIES

http://tutorial.cytron.com.my/2012/06/22/pidforembeddeddesign/

LOGIN/REGISTER

SHOP AT CYTRON

FORUM

18/18

Вам также может понравиться