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

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Tips
TIPS

ABOUT

Effects

BASICS

SearchControlTutorials

HARDWARE

INDEX

NEXT

INTRODUCTION CRUISECONTROL MOTORSPEED MOTORPOSITION SUSPENSION

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

1/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

SYSTEM
MODELING

Introduction:SimulinkControl

ANALYSIS

RelatedTutorial
Links

CONTROL
PID

Temperature
ControlActivity

ROOTLOCUS

Motor

FREQUENCY

Speed

ControlActivity
STATESPACE

Simulink
Interaction

DIGITAL

with

MATLAB
SIMULINK

RelatedExternal
Links

MODELING

Modeling
CONTROL

ChallengesVideo

Contents
Theopenloopplantmodel
ImplementingaPIDcontrollerinSimulink
Runningtheclosedloopmodel
ExtractingamodelintoMATLAB
ControllerdesignwithinSimulink
http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

2/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Theopenloopplantmodel
IntheIntroduction:SimulinkModeling page we demonstrated how Simulink can be employed to simulate a
physicalsystem.Moregenerally,Simulinkcanalsosimulatethecompletecontrolsystem,includingthecontrol
algorithminadditiontothephysicalplant.Asmentionedpreviously,Simulinkisespeciallyusefulforgenerating
the approximate solutions of mathematical models that may be prohibitively difficult to solve "by hand." For
example,considerthatyouhaveanonlinearplant.Acommonapproachistogeneratealinearapproximationof
the plant and then use the linearized model to design a controller using analytical techniques. Simulink can
then be employed to simulate the performance of your controller when applied to the full nonlinear model.
SimulinkcanbeemployedforgeneratingthelinearizedmodelandMATLABcanbeemployedfordesigningthe
controllerasdescribedintheotherIntroductionpages.VariouscontroldesignfacilitiesofMATLABcanalsobe
accesseddirectlyfromwithinSimulink.Wewilldemonstratebothapproachesinthispage.
Recall the Simulink model of the toy train system derived in the Introduction: Simulink Modeling page and
picturedbelow.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

3/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Youcangeneratethismodelyourself,oryoucandownloadthecompletedmodelhere.Assumingthatthetrain
onlytravelsinonedimension(alongthetrack),wewanttoapplycontroltothetrainenginesothatitstartsand
comestorestsmoothly,andsothatitcantrackaconstantspeedcommandwithminimalerrorinsteadystate.

ImplementingaPIDcontrollerinSimulink
LetusfirstcreatethestructureforsimulatingthetrainsysteminunityfeedbackwithaPIDcontroller.Inorderto
makeourSimulinkmodelmoreunderstandable,wewillfirstsavethetrainmodelintoitsownsubsystemblock.
Toaccomplishthis,deletethethreescopeblocksandreplaceeachonebyanOut1blockfromtheSinkslibrary.
LabeleachOut1blockwiththecorrespondingvariablename,"x1_dot","x1",and"x2".ThendeletetheSignal
Generator block and replace it with an In1 block from the Sources library. Label this input "F" for the force
generatedbetweenthetrainengineandtherailroadtrack.Yourmodelshouldnowappearasfollows.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

4/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Nextselectalloftheblocksinyourmodel(CtrlA)andselectCreateSubsystemfromtheEditmenuatthetop
ofthemodelwindow.Withalittlerearrangingandrelabeling,yourmodelwillappearasshownbelow.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

5/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Nowwecanaddacontrollertooursystem.WewillemployaPIDcontrollerwhichcanbeimplementedusinga
PID Controller block from the Continuous library. Placing this block in series with the train subsystem, your
modelwillappearasfollows.Inthefollowing,wemodelthecontrollerasgeneratingtheforce"F"directly.This
neglects the dynamics with which the train engine generates the torque applied to the wheels, and
subsequentlyneglectsthedynamicsofhowtheforceisgeneratedatthewheel/trackinterface.Thissimplified
approachistakenatthispointsinceweonlywishtointroducethebasicfunctionalityofSimulinkforcontroller
designandanalysis.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

6/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

DoubleclickingonthePIDControllerblock,wewillinitiallysettheIntegral(I)gainfieldequalto0andwillleave
theProportional(P)andDerivative(D)gainsastheirdefaultsof1and0,respectively.NextaddaSumblock
fromtheMathOperationslibrary.DoubleclickonthisblockandmodifytheListofsignsfieldto"|+".Sincewe
wishtocontrolthevelocityofthetoytrainengine,wewillfeedbacktheengine'svelocity.Thisisaccomplished
by4ringalineoffofthe"x1_dot"signalandconnectingittothenegativesignoftheSumblock.Theoutputofthe
Sum block will be the velocity error for the train engine and should be connected to the input of the PID
Controllerblock.Connectingtheblocksasdescribedandaddinglabels,yourmodelshouldappearasfollows.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

7/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

NextaddaSignalBuilderblockfromtheSourceslibrarytorepresentthevelocitycommandedtothetrain.Since
we wish to design a controller to bring the train smoothly up to speed and smoothly to rest, we will test the
systemwithavelocitycommandthatstepsupto1m/sfollowedbyastepbackdownto0m/s(recallthatour
systemisatoytrain).Togeneratethistypeofcommandsignal,doubleclickontheSignalBuilderblock.Then
chooseChangetimerangefromtheAxesmenuatthetopoftheblock'sdialogwindow.SettheMaxtimefield
to"300"seconds.Next,setthestepuptooccurat10secondsandthestepdowntooccurat150seconds.This
isaccomplishedbyclickingonthecorrespondingportionsofthesignalgraph(leftandrightverticallines)and
eitherdraggingthelinetothedesiredposition,orenteringthedesiredtimeintheTfieldatthebottomofthe
window.Whendone,yoursignalshouldappearasfollows.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

8/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Also add a Scope block from the Sinks library and use it to replace the Out1 block for the train's velocity.
Relabelingtheblocks,yourmodelwillappearasfollows.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

9/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Wearenowreadytoruntheclosedloopsimulation.Ifyouwishtoskiptheabovesteps,youmaydownloadthe
completedmodelwithcontrolhere.

Runningtheclosedloopmodel
Beforerunningthemodel,weneedtoassignnumericalvaluestoeachofthevariablesusedinthemodel.For
thetrainsystem,wewillemploythefollowingvalues.
=1kg
=0.5kg
=1N/sec
=1N
=0.02sec/m
=9.8m/s^2

Createanewmfileandenterthefollowingcommands.

M1=1;
M2=0.5;
k=1;
F=1;
mu=0.02;
g=9.8;

Execute your mfile in the MATLAB command window to define these values. Simulink will recognize these
MATLABvariablesforuseinthemodel.Nextweneedtosetthetimeforwhichoursimulationwillruntomatch
thetimerangeofthecommandfromtheSignalBuilderblock.ThisisaccomplishedbyselectingParameters
fromtheSimulationmenuatthetopofthemodelwindowandchangingtheStopTimefieldto"300".Now,run
thesimulationandopenthe"x1_dot"scopetoexaminethevelocityoutput(hitautoscale).Theresultasshown
http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

10/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

belowdemonstratesthattheclosedloopsystemisunstableforthiscontroller.

Since the performance achieved above is unsatisfactory, we need to redesign our controller. We will first
demonstrate how to extract a model from Simulink into MATLAB for analysis and design. Then we will
demonstratehowtodesignthecontrolfromdirectlywithinSimulink.

ExtractingamodelintoMATLAB
TheSimulinkControlDesigntoolboxoffersthefunctionalitytoextractamodelfromSimulinkintotheMATLAB
workspace. This is especially useful for complicated, or nonlinear simulation models. This is also useful for
generatingdiscretetime(sampled)models.Forthisexample,letusextractacontinoustimemodelofourtrain
subsystem.Firstweneedtoidentifytheinputsandoutputsofthemodelwewishtoextract.Theinputtothetrain
systemistheforce .Wecandesignatethisfactbyrightclickingonthesignalrepresenting"F"(outputofthe
PID block) and choosing Linearization Points > Input Point from the resulting menu. Likewise, we can
designate the output of the train system by rightclicking on the "x1_dot" signal and choosing Linearization
Points>OutputPointfromtheresultingmenu.Theseinputsandoutputswillnowbeindicatedbysmallarrow
http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

11/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Points>OutputPointfromtheresultingmenu.Theseinputsandoutputswillnowbeindicatedbysmallarrow
symbolsasshowninthefollowingfigure.Sincewewishtoextractamodelofthetrainbyitself,withoutcontrol,
weneedtofurtherdeletethefeedbacksignal,otherwisewewillextracttheclosedloopmodelfrom

to

Yourmodelshouldnowappearasfollows.

WecannowextractthemodelbyopeningtheLinearAnalysisTool.ThisisaccomplishedbyselectingControl
Design>LinearAnalysisfromundertheToolsmenuatthetopofthemodelwindow.Followingthesestepswill
openthewindowshownbelow.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

12/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

ThistoolgeneratesanLTIobjectfroma(possiblynonlinear)Simulinkmodelandallowsyoutospecifythepoint
aboutwhichthelinearizationisperformed.SinceourSimulinkmodelisalreadylinear,ourchoiceofoperating
pointwillhavenoeffectandwecanleaveitasthedefaultModelInitialCondition.Inordertogeneratethe
linearizedmodel,selecttheLinearizebuttonintheabovefigure,whichisindicatedbythegreentriangle.The
LinearAnalysisToolwindowshouldnowappearasshownbelow.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

13/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Inspectingtheabove,thestepresponseofthelinearizedmodelwasautomaticallygenerated.Comparingthis
stepresponsetotheonegeneratedbythesimulationoftheopenlooptrainsystemintheIntroduction:Simulink
Modelingpage,youcanseethattheresponsesareidentical.Thismakessensesincethesimulationmodelwas
already linear. Additionally, the linearization process generated the object linsys1 shown in the Linear
AnalysisWorkspaceabove.ThisLTIobjectcanbeexportedforusewithinMATLABbysimplydraggingthe
objectintotheMATLABWorkspacewindow.
Havingextractedthismodel,wecannowemployallofthefacilitiesthatMATLABoffersforcontrollerdesign.For
example,letusemploythefollowingcommandstogenerateandanalyzetheclosedloopsystemreflectingthe
Simulinkmodelcreatedabove.
http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

14/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

sys_cl=feedback(linsys1,1);
pole(sys_cl)

ans=

1.5261
0.0000
0.0670+1.1977i
0.06701.1977i

Examinationoftheabovedemonstratesthattheclosedloopsysteminitscurrentstatehaspoleswithpositive
realpartand,therefore,isunstable.Thisagreeswiththeresultofourclosedloopsimulationfromabove.We
can then employ MATLAB to design a new controller. Instead, we will demonstrate how to access some of
MATLAB'sfunctionalityfromdirectlywithinSimulink.

ControllerdesignwithinSimulink
Rather than performing the controller design in MATLAB, we can also launch interactive tools to tune our
controllerfromwithinSimulink.OnemannerinwhichthiscanbedoneistodoubleclickonthePIDControllerin
themodelandselecttheTunebuttontolaunchthePIDTunerGUI.Ratherthandothis,willlaunchthemore
general Simulink Control Design GUI by selecting Linear Analysis > Compensator Design from under the
Toolsmenulocatedatthetopofthemodelwindow.FollowingthesestepswillopentheControlandEstimation
ToolsManagerwindowshownbelow.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

15/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Thefirstthingthatneedstobedoneistoidentifythecontrollerblockthatistobetuned.Thisisaccomplishedby
firstclickingontheSelectBlocksbutton,andthenselectingthePIDControllerblockfromtheresultingwindow
asshownbelow.NextclicktheOKbutton.Notethatcontrollersrepresentedinothertypesofblocks(Transfer
Function,StateSpace,etc.)canalsobetuned.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

16/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Beforeweproceedtotuneourcontroller,wemustfirstidentifytheinputsandoutputsoftheclosedloopsystem
we wish to analyze. This is done in the same manner we did when extracting a model into MATLAB.
Specifically, rightclick on the velocity command signal (output of the Signal Builder block) and choose
Linearization Points > Input Point from the resulting menu to identify the input of our closedloop system.
Similarly,rightclickonthetrainenginevelocitysignal("x1_dot")andselectLinearizationPoints>OutputPoint
fromthemenutochoosetheoutputofoursystem.Yourmodelshouldnowappearasfollowswherethesmall
arrowsymbolsidentifytheinputandoutputofthemodel.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

17/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Nowthatwehaveidentifiedtheblocktotuneandourinputandoutputsignals,wecannowcommencewith
tuning the controller. SelecttheTuneBlocks button in the Control and Estimation Tools Manager window.
ThiswillopentheDesignConfigurationWindow shown below which provides some introduction on how to
employtheinteractivedesigntool.Inessence,thisGUIistheSISODesignTool that is available from within
MATLABaswell.

ClickingtheNextbutton,wewillchoosethedesignplotswewishtoemployfordesigningourcontroller.Inthis
http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

18/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

example,wewillemployarootlocusdesignapproachandhencewillchooseaPlotTypeofRootLocus for
Plot1asshownbelow.Sincetherootlocusapproachtodesignemploysaplotfromtheopenloopsystemfor
placingtheclosedlooppoles,wewillleavethechoiceofOpen/ClosedLoopsasOpenLoop1(thisisouronly
choice!).

ClickingtheNext button again will allow us to choose our analysis plots. We use the step response plot to
assesshowwellweareabletomeetourgoalofbringingthetrainuptospeedsmoothlywithminimalsteady
stateerrortoaconstantspeedcommand.Therefore,wewillchooseaPlotTypeofStepfromthedropdown
menuunderPlot1asshownbelow.WewillalsoselectPlot1underthePlotContentsportionofthewindowfor
theonlysystemthatisdefined.Thereisonlyonesystemavailablebecausewehavedefinedonlyasingleinput
andsingleoutputforoursystem.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

19/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

SelectingtheNextButtonwillthenopentheSISODesignToolwitharootlocusplotandastepresponseplot.
Therootlocusplotshownbelowdisplaystheclosedlooppolelocationsofthetrainsystemplantundersimple
proportionalcontrol.Examiningtheplot,onecanseethatmanyvaluesofloopgainwillplaceclosedlooppoles
intherighthalfplaneleadingtoanunstableresponse.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

20/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Ifwedecreasetheloopgainsufficiently,wecanmovetheclosedlooppolesintothelefthalfplaneandwecan
stabilizeoursystem.Thiscanbeaccomplishedgraphicallyby"grabbing"thepinkboxesmarkingtheclosed
loop pole locations and dragging them toward the openloop pole locations (marked by x's). A loop gain of
approximately 0.1 will stabilize the system. Examining the corresponding step response, which will change
automaticallyinresponsetothegainchangeiftheRealTimeUpdateboxischeckedintheLTIViewerwindow,
youcanseethatwhiletheresponseisstableitssteadystateerrorisquitelarge.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

21/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Recallthataddingintegralcontrolisonewaytoreducethesteadystateerrorforasystem.Inthiscase,adding
anintegratorviathecontrollerwillmakethesystemtype1,wheretype1systemscantrackstepreferenceswith
zerosteadystateerror.RecallthefollowingformofaPIcontroller.
(1)

Therefore, a PI controller will add an integrator and a zero to our openloop system. The integrator can be
addedtothesystembyrightclickinginthefieldoftherootlocusplotandselectingAddPole/Zero>Integrator
fromtheresultingmenu.Similarly,thezerocanbeaddedbyrightclickingontherootlocusplotandselecting
AddPole/Zero>Realzerofromtheresultingmenu.Thenclickwherealongtherealaxisyouwishtoplacethe
zero.Wewillplacethezerojusttotherightoftheplantpoleontherealaxis.Youcanmovethezerobyclicking
onitanddraggingittoanewlocation.Onceyouhaveplacedthezero,thengrabthepinkboxesrepresenting
the closedloop poles and attempt to line the three dominant poles up so that they have the same real part.
Therealsowillbearealclosedlooppoletotheleftthatis"faster"thantherest,andaclosedlooppoleatthe
originthatiscancelledbyaclosedloopzeroattheorigin.Theresultingrootlocusplotisshownbelow.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

22/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Thecompensatorcanalsobeeditedbydirectlytypinginpoleandzerolocations.Thiscanbedonebychoosing
EditCompensatorfromtheDesignmenulocatedatthetopoftheSISODesignTaskwindow.Thewindowthat
opensisshownbelow.Wewillmorepreciselyplacethezeroat0.15andwillchoosealoopgainequalto0.01.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

23/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Theresultingclosedloopstepresponseplotisshownbelowdemonstratingthatthetrainengineisbroughtto
speedsmoothlyandwithzerosteadystateerrorforaconstantspeedcommand.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

24/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

The control gains that have been chosen can then be applied to the Simulink model by clicking the Update
SimulinkBlockParametersbuttonundertheCompensatorEditortaboftheControl and Estimation Tools
Managerwindow(seeabove).Thesimulationcanthenberunwiththisnewlytunedcontroller.Clickingonthe
Scopeblockforthetrainengine'svelocityandselectingautoscalewillproduceaplotliketheoneshownbelow.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

25/26

20/01/2016

ControlTutorialsforMATLABandSimulinkIntroduction:SimulinkControl

Overall this response seems to meet our goals of bringing the train up to speed and to rest smoothly, while
maintainingminimalsteadystateerror.ThisresponsematchestheresultgeneratedwiththeSISODesignTool
abovebecausethatanalysisandtheSimulinkmodelusedtheexactsamelinearmodel.

PublishedwithMATLAB7.14

AllcontentslicensedunderaCreativeCommonsAttributionShareAlike4.0InternationalLicense.

http://ctms.engin.umich.edu/CTMS/index.php?example=Introduction&section=SimulinkControl

26/26