Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
FinnHaugen,TechTeach:IntroductiontoLabVIEWSimulationModule
Introductiontosimulationwith
ControlDesignandSimulationModule
inLabVIEW8.6
by
FinnHaugen
16.March2009
Contents:
1Preface
2Introduction
3ThecontentsoftheSimulationfunctionspalette(Video)
4Anexample:Simulatorofaliquidtank
4.1Developingthemathematicalmodelofthesystemtobesimulated
4.2TheFrontpanelandtheBlockdiagramofthesimulator(Videos)
4.3Configuringthesimulation(Videos)
5Varioustopics
5.1RepresentingstatespacemodelsusingFormulanodeandintegrators
5.2Creatingsubsystems
5.3Gettingalinearizedmodelofasubsystem
5.4Simulatingcontrolsystems
5.5ConvertingmodelsbetweenSimulationModuleandControlDesignToolkit
5.6PuttingcodeintoaWhilelooprunninginparallellwithaSimulationloop
5.7TranslatingSIMULINKmodelsintoLabVIEWSimulationmodels
1Preface
ThisdocumentgivesanintroductiontothesimulationtoolsoftheLabVIEWControlandSimulationModuleforLabVIEW8.6.Itis
assumedthatyouhavebasicskillsinLabVIEWprogramming.TherearetutorialsforLabVIEWprogrammingavailablefromtheNational
Instruments'websidehttp://ni.com,andIhavemadeonemyself(toservetheneedsinmyownteachingmorespecifically),seeFinn's
LabVIEWPage.
Thistutorialcontainsanumberofactivitiesthatyouaresupposedtoperform.Theseactivitiesareshowninblueboxes,ashere:
Activitiesareshowninblueboxesasthisone.
Mostoftheactivitiesarevideobased.Intheformertutorial,basedonLabVIEW8.5,therewerenovideos,butinsteaddetailed
explanations.Ihaveremovedtheexplanationinthepresenttutorialwherethevideosgivethesameinformation.
Ifforsomereasonthevideosdoesnotdisplaycorrectlyinthecurrentplayer,trysomeother.WindowsMediaPlayerisprobablythe
defaultvideoplayeronyourPC.AlternativeplayersareRealPlayerandQuickTime(bothcanbedownloadedfromtheInternetforfree).
MoretutorialsthatmayberelevantforyouasaLabVIEWuserareavailablefromFinn'sLabVIEWPage.
[Tableofcontents]
2Introduction
TheLabVIEWControlandSimulationModulecontainsablockdiagrambasedenvironmentforsimulationoflinearandnonlinear
continuoustimeanddiscretetimedynamicsystems.Manysimulationalgorithms(i.e.numericalmethodsforsolvingtheunderlying
differentialequations)areavailable,e.g.variousRungeKuttamethods.Themathematicalmodeltobesimulatedmustberepresentedina
simulationloop,whichinmanywaysissimilartotheordinarywhileloopinLabVIEW.Youcanmakethesimulationrunasfastasthe
computerallows,oryoucanmakeitrunwitharealorscaledtimeaxis,thussimulatingrealtimebehaviour,withthepossibilityoftheuser
tointeractwiththesimulatedprocess.ThesimulationloopcanruninparallelwithwhileloopswithinthesameVI.
[Tableofcontents]
3ThecontentsoftheSimulationfunctionspalette
http://techteach.no/labview/lv86/sim_module/
1/10
8/11/2016
FinnHaugen,TechTeach:IntroductiontoLabVIEWSimulationModule
OncetheControlandSimulationModuleisinstalled,theSimulationpaletteisavailablefromtheFunctionspalette.Thevideobelowgives
overviewoverthecontentsoftheSimulationPalette.
Playthevideosim_palette_overview(17minutes,thevideowillbeopenedinanewwindow)
togetanoverviewoverthecontentsoftheSimulationPalette.Itisassumedthatyouhavethe
blockdiagramofanyVIopened.
[Tableofcontents]
4Anexample:Simulatorofaliquidtank
Inthissectionwewillstudyandpartlydevelopasimulatorofaliquidtank.Actually,wewillplaywithapremadeexample,butnotdevelop
itfromscratch.Ithinkthisisaneffectivewaytolearnusingthetools,assumingthatyouhavebasicskillsinLabVIEWprogramming.You
aresupposedtohavebasicknowledgeaboutmodelingofdynamicsystems,asdescribedine.g.DynamicSystemsmodelling,analysisand
simulationorinanyotherbookaboutdynamicsystemstheory.
4.1Developingthemathematicalmodelofthesystemtobesimulated
Thesystemtobesimulatedisaliquidtank,seethefigurebelow.Thesimulatorwillcalculateanddisplaythelevelhatanyinstantoftime.
Thesimulationwillruninrealtime,withthepossibilityofscaledrealtime,therebygivingthefeelingofa"real"system.Theusercan
adjusttheinletbyadjustingthepumpcontrolsignal,u.
Liquidtank
Anysimulatorisbasedonamathematicalmodelofthesystemtobesimulated.Thus,westartbydevelopingamathematicalmodelofthe
tank.
Weassumethefollowing(theparametersusedintheexpressionsbelowaredefinedinthefigureabove):
Theliquiddensityisthesameintheinlet,intheoutlet,andinthetank.
Thetankhasstraight,verticalwalls.
Theliquidmassandlevelarerelatedthrough
m(t)=Ah(t)
Theinletvolumetricflowthroughthepumpisproportionaltothepumpcontrolsignal:
qin(t)=Kuu(t)
Theoutletvolumetricflowthroughthevalveisproportionaltothesquarerootofthepressuredropoverthevalve.Thispressuredrop
isassumedtobeequaltothehydrostaticpressureatthebottomofthetank(sqrtmeanssquareroot):
qout(t)=Kvsqrt[gh(t)]
Massbalance(i.e.,rateofchangeofthemassisequaltotheinflowminustheoutflow)yieldsthefollowingdifferentialequation:
dm(t)/dt=qin(t)qout(t)](1)
or,usingtheaboverelations,
d[Ah(t)]/dt=Kuu(t)Kvsqrt[gh(t)](2)
Wewillnowdrawamathematicalblockdiagramofthemodel.Thisblockdiagramwillthenbeimplementedintheblockdiagramofthe
simulatorVI.Asaproperstartingpointofdrawingthemathematicalblockdiagram,wewritethedifferentialequationasastatespace
http://techteach.no/labview/lv86/sim_module/
2/10
8/11/2016
FinnHaugen,TechTeach:IntroductiontoLabVIEWSimulationModule
model,thatis,asadifferentialequationhavingthefirstordertimederivativealoneontheleftside.ThiscanbedonebypullingandA
outsidethedifferentiation,thendividingbothsidesbyA.Theresultingdifferentialequationbecomes
d[h(t)]/dt=(1/A)*{Kuu(t)Kvsqrt[gh(t)]}(3)
Thisisadifferentialequationforh(t).Ittellshowthetimederivativedh(t)/dtcanbecalculated.h(t)iscalculated(bythesimulator)by
integratingdh(t)/dtwithrespecttotime,fromtime0totimet,withinitialvalueh(0),whichweheredenotehinit.Todrawablockdiagram
ofthemodel(3),wemaystartbyaddinganintegratortotheemptyblockdiagram.Theinputtothisintegratorisdh/dt,andtheoutputis
h(t).Thenweaddmathematicalfunctionblockstoconstructtheexpressionfordh/dt,whichistherightsideofthedifferentialequation(3).
Theresultingblockdiagramforthemodel(3)canbeasshowninthefigurebelow.
MathematicalblockdiagramofDifferentialEquation(3)
Thenumericalvaluesoftheparametersareshowninthefrontpanelpicturebelow.
Wewillassumethattherearelevelalarmlimitstobedisplayedinthesimulator.Thelimitsare
AH_h=0.9m(AlarmHigh)
AL_h=0.1m(AlarmLow)
TheblockdiagramdevelopedabovewillbeimplementedinaSimulationLoopintheBlockdiagramofoursimulationVI.
4.2TheFrontpanelandtheBlockdiagramofthesimulator
ThesubsequentfiguresshowthefrontpanelandtheblockdiagramofthecompleteVI,tanksim.vi.
http://techteach.no/labview/lv86/sim_module/
3/10
8/11/2016
FinnHaugen,TechTeach:IntroductiontoLabVIEWSimulationModule
Frontpaneloftanksim.vi.
Blockdiagramoftanksim.vi.
TheFrontpanel
http://techteach.no/labview/lv86/sim_module/
4/10
8/11/2016
FinnHaugen,TechTeach:IntroductiontoLabVIEWSimulationModule
OpentheFrontpaneloftanksim.vi.
Playthevideotanksim_frontpanel(20minutes,thevideoisopenedinanewwindow)to
TheBlockdiagram
OpentheBlockdiagramoftanksim.vi.
Playthevideo:tanksim_blockdiagram(42minutes,thevideoisopenedinanewwindow).
Note:InthebeginningofthevideoIrefertofourvideos,butIhavecompiledthesefourvideos
intoonevideo.
[Tableofcontents]
4.3Configurationofthesimulation
Playthevideotanksim_config_sim(28minutes,thevideoisopenedinanewwindow).
Note:Inthevideoafigureillustratingthetimestep(orstepsize)isshown.InthevideoIsaythat
thisfigureexistsinthetutorial,butitdoesnot(butyouseeitinthevideo).
[Tableofcontents]
5Varioustopics
(TherearenoblueactivityboxesinChapter.)
5.1RepresentingstatespacemodelsusingFormulanodeandintegrators
Astatespacemodelisasetoffirstorderdifferentialequationsconstitutingthemodelofthesystem.Statespacemodelsisastandardized
modelform.Itiscommonthatmathematicalmodelsofdynamicsystemsarewrittenasstatespacemodels.Tobealittlemorespecific,here
isageneralsecondorderstatespacemodel(thedotsrepresentstheargumentsofthefunctions):
dx1/dt=f1(x1,x2,...)
dx2/dt=f2(x1,x2,...)
y=g(x1,x2,...)
wheref1()andf2()arefunctionscontainingtherighthandpartofthefirstorderdifferentialequations.Theargumentsmaybestate
variables,inputvariables,andparameters.Thesefunctionsmaybelinearornonlinear.Theyarethetimederivativesofthestates,x1andx2,
respectively.Sometimesoneormoreoutputvariablesaredefined.Above,theoutputvariableisy,andtheoutputfunctionisg().
Toimplementtheblockdiagramofastatespacemodel,youmaystartbyaddingoneIntegratorblockforeachofthestatevariablesonthe
blockdiagram.Theoutputoftheintegratorsarethestatevariables.Theinputstotheintegratorsarethetimederivatives,andthef1()and
f2()functionsintherepresentativemodelshownarethesetimederivatives.Toimplementthefunctionsyouhavethefollowingtwooptions
(whichalsomaybecombined):
Constructingthefunctions,f1()andf2()above,usingblockfunctionsasSum,Gain,Multiplicationetc.,whichareontheSimulation
PaletteoftheFunctionsPalette.OneexampleistheBlockdiagramofthemodeloftheliquidtankshownhere.
Writingthetextualfunctionsoff1()andf2()inaFormulaNode.TheFormulanodeisontheMathematics/Scripts&Formulas
Palette(andontheStructuresPalette).TheFormulaNodeisexplainedhere(inmyIntroductiontoLabVIEW).WiththeFormula
Nodethefunctionsareeasiertomodify(itisdonebyjustingeditingtextintheFormulaNode),andtheBlockDiagrammayappear
simpler.However,itmaybedifficulttoimplementnonlinearfunctionsashysteresis,backlashetc.(therearenumeroussuchnonlinear
blocksintheNonlinearPaletteontheSimulationPalette).
HereisasimpleexampleofusingtheFormulanode.Giventhefollowingstatespacemodel:
dx1/dt=x2
dx2/dt=x1+u
y=x1
(whichisastatespacemodelofanoscillator).uistheinputvariable,andyistheoutputvariable.ssformulanode.vishownbelow
implementsasimulatorforthissystem.AFormulanodeisusedtorepresenttherightsideofthedifferentialequations.Theintegrationof
thetimederivativesareperformedbyIntegratorblocksfromtheContinuouspalette.
http://techteach.no/labview/lv86/sim_module/
5/10
8/11/2016
FinnHaugen,TechTeach:IntroductiontoLabVIEWSimulationModule
Frontpanelandblockdiagramofssformulanode.vi.
UsingFormulaNodeinsteadofblockfunctionstocalculatethetimederivativesmaygiveasimplerblockdiagram.However,ifthe
expressionsforthetimederivatives(i.e.therighthandsidesofthedifferentialequations)containsnonlinearfunctions,itmaybemore
difficulttoimplementtheseintheFormulaNodethanwithfunctionblocks.
[Tableofcontents]
5.2Creatingsubsystems
Youcancreateasubsystemofapartofasimulationdiagram.Thefirststepistoselectormarkthepartofinterest,seethefigurebelow,
whichshowstheblockdiagramoftanksim.vi.
Thefirststepincreatingasubsysteminthesimulationdiagramistoselectthepartofinterest
ThenthesubsystemiscreatedusingthemenuEdit/CreateSimulationSubsystem.Theresultingdiagramisshowninthefigurebelow.
http://techteach.no/labview/lv86/sim_module/
6/10
8/11/2016
FinnHaugen,TechTeach:IntroductiontoLabVIEWSimulationModule
Theresultingsimulationdiagram,includingthesubsystem
Notethatyoucanchangethesizeofthesubsystemiconusingthecursor.
Ifyouwantyoucanopenthefrontpanelofthesubsystembydoubleclickingthesubsystemicon,seethefigurebelow.
Thefrontpanelofthesubsystem
YoucanopentheblockdiagramofthesubsystemviatheWindow/ShowBlockDiagrammenu,seethefigurebelow.
http://techteach.no/labview/lv86/sim_module/
7/10
8/11/2016
FinnHaugen,TechTeach:IntroductiontoLabVIEWSimulationModule
Theblockdiagramofthesubsystem
[Tableofcontents]
5.3Gettingalinearizedmodelofasubsystem
LabVIEWcancreatealinearstatespacemodelfromalinearornonlinearsubsystem.(Creatingsubsystemsisdescribedintheprevious
section.)Theprocedureistoselectormarkthesubsystemofinterest,andthencreatethelinearmodelbyusingthefollowingmenu:Tools/
SimulationTools/LinearizeSubsystem.Youaregiventheoptionofsavingthelinearmodelasamodel(tobeusedbyfunctionsinthe
ControlDesignToolkit)orasaVIcontainingthestatespacemodelintheformofaclusterofcoefficientarrays.Perhapsthemostflexible
choiceisVI.
[Tableofcontents]
5.4Simulatingcontrolsystems
Simulatingcontrolsystemsisdoneinthesamewayassimulatingdynamicsystems.Youcanincludevirtuallyeverycontrolfunctionina
modelblockdiagraminsidetheSimulationLoop.InGuidelinestoPIDControlwithLabVIEWthereisanexampleofasimulatorofaPID
controlsystem.
[Tableofcontents]
5.5ConvertingmodelsbetweenSimulationModuleandControlDesignToolkit
YoucanconvertmodelsbetweentheSimulationModuleandtheControlDesignToolkitusingtheconversionfunctionsontheModel
ConversionpaletteontheControlDesignTookit.Thetwoconversionfunctionsareshowninthefigurebelow.
TheconversionfunctionsontheControlDesignToolkit/ModelConversionpalette
[Tableofcontents]
5.6PuttingcodeintoaWhilelooprunninginparallellwithaSimulationloop
ItispossibletoputalmostanyLabVIEWcodefore.g.analysisanddesignofintoasimulationdiagraminsideaSimulationloop,butdoing
somaygiveanunnecessarylargeorcomplicatedsimulationcode,andthesimulationexecutionmaybedelayed.Therefore,youshouldnot
putmorecodeinsidetheSimulationloopthanisstrictlynecessaryforrepresentingthemodeltobesimulated.Otherpartsofthetotalcode,
e.g.optimalcontroldesignfunctions(astheLQRfunction)orKamanfilter(stateestimator)designfunctions(astheKalmanGainfunction),
maybeputintooneormoreordinaryWhileloopsrunninginparallelwiththeSimulationloop.TheseWhileloopsmaybeprogrammedto
runslowerthantheSimulationloop.Datacanbeexchangedbetweentheloopsusinglocalvariables(localvariablesaredescribedin
IntroductiontoLabVIEW).
Hereisoneexample:kalmanfilter_tank.viisasimulatorofaKalmanFilterwhichestimatestheoutflowofasimulatedliquidtank.(Youcan
runthissimulatorifyouhavetheLabVIEWSimulationModuleinstalled.)ThesimulatorisimplementedwithaSimulationLoopwhich
containsamodelofthetankandtheexpressionsconstitutingtheKalmanFilteralgorithm.TheKalmangainsarecalculatedusingthe
KalmanGainfunctionoftheControlDesignToolkit.Thisfunctionisrelativelycomputationaldemanding,anditisthereforeputintoa
http://techteach.no/labview/lv86/sim_module/
8/10
8/11/2016
FinnHaugen,TechTeach:IntroductiontoLabVIEWSimulationModule
WhileloopwhichrunsinparallellwiththeSimulationloopwithacycletimeof0.5sec.TheKalmangainsaremadeavailableinsidethe
Simulationloopusinglocalvariables.
Thesimulationtimestepis0.1sec,andthePeriod(whichistheactual,realtimethatLabVIEWusedtoproceedonesimulationtimestep)is
0.025sec.(HavingthePeriodsmallerthanthesimulationtimestepmakesthesimulatorrunfasterthanrealtime.Thisisfavourablehere
sincetheprocessitselfisaslowsystem,andwedonothavetimetositwaitingforresponsestocome.)IfwehadputtheKalmanGain
functioninsidetheSimulationloop,thespecifiedPeriodof0.025seccouldnothavebeenobtainedbecauseittakesabout0.3sec(thisis
howevercomputerdependent)toexecuteKalmanGainfunction.
Belowarethefrontpanelandtheblockdiagramofkalmanfilter_tank.vi.Clickonthefigurestoseetheminfullsizes.Notehowlocal
variablesareusedtoexchangevaluesacrosstheloops.NotealsohowtheStopbuttonanditslocalvariableisusedtostopbothloops.The
MechanicalActionpropertyoftheStopbuttonmustbesettoSwitchuntilReleased.IfitissettooneoftheLatch...properties,ititwillnot
bepossibletocreatelocalvariableforit.)
Frontpanelofkalmanfilter_tank.vi(clickonthefiguretoseeitinfullsize)
http://techteach.no/labview/lv86/sim_module/
9/10
8/11/2016
FinnHaugen,TechTeach:IntroductiontoLabVIEWSimulationModule
Blockdiagramofkalmanfilter_tank.vi(clickonthefiguretoseeitinfullsize)
[Tableofcontents]
5.7TranslatingSIMULINKmodelsintoLabVIEWSimulationModulemodels
YoucantranslateSIMULINK(MathWorks)modelsintoLabVIEWsimulationmodelsusingthemenuTools/SimulationTools/
SIMULINKTranslatorinLabVIEW.
[Tableofcontents]
[Finn'sLabVIEWPage][TechTeach]
http://techteach.no/labview/lv86/sim_module/
10/10