SimulationinLabVIEW
Hans-PetterHalvorsen,M.Sc.
Software
• LabVIEW• LabVIEWControlDesignandSimulationModule–ThismoduleisusedforcreatingControlandSimulationapplicationswithLabVIEW.–HereyouwillfindPIDcontrollers,etc.Themoduleisavailableasapaletteonyourblockdiagram.
AllLabVIEWSoftwarecanbedownloadedhere:www.ni.com/download
Contents
• BlockDiagramSimulationbasedondifferentialEquations–UsingtheSimulationLoop
• PIDControlwithbuilt-inPIDblocks/functions• CreatingandusingSimulationSubsystems• SimulationsusingaWhileLoopwithSubsystemsinside
WhatisLabVIEW?
Hans-PetterHalvorsen,M.Sc.
LabVIEW=Fun!GraphicalProgramming:• VerydifferentfromtraditionalprogramminglikeVB,C#,Maple,
MATLAB,MathScript,etc.• Itismorelikea“drawingprogram”thanaProgrammingLanguage• Thismakesiteasytouseforthosewhoarenotprogrammers(or
dontlikeprogrammingJ)• ExcellenttoolwhenusingHardware,whenyouneedtotake
Measurements(DAQ),etc.• Itisfunandmakesyouverycreative!
LabVIEWExample
PropertyNodes
SequenceStructure WhileLoop SubVI(Function/Method)
CaseStructure(if-else)Constants
Arrays
StopButtonCondition
(Whenshalltheloopend?)
LocalVariableComment
Note!Todosomethingwithanobject– Right-clickonit
LabVIEWhasthesamethingsasotherprogramminglanguages,butinagraphical way!
LabVIEWEnvironmentFrontPanel
BlockDiagram
CreateyourUserInterface/HMIhere
CreateyourCode hereSwitchbetweenthem:Ctrl+E
Note!BoththeFrontPanelandtheBlockDiagramarestoredinonesinglefile.ThesefilesarecalledVIs (becausethefileextensionis“.vi”).VI=VirtualInstruments
Controls andFunctions Palette
AvailableonlyfromtheFrontPanel
AvailableonlyfromtheBlockDiagram
YoucreateyourUserInterface withhelpoftheseControls
Youcreateyou
rCod
ewith
helpof
theseFunctio
ns
Right-clickontheFrontPanel
Right-clicko
ntheBlockD
iagram
Youcan“pin”them!
CustomizingControlsandFunctionsPalettes
Dothisforboth theControlsPaletteandtheFunctionsPalette
DAQmxistheHardwareDriverneeded inorder tousehardwaredevices likeNIUSB-6008,NITC-01,etc.insideLabVIEW.Themoduleisavailableasapaletteonyourblockdiagram.
LabVIEWThisisthecoreLabVIEWinstallationthatinstallstheLabVIEWProgrammingEnvironment.
Thismoduleisatext-based toolthatisverysimilartoMATLAB.Thesyntaxissimilarto MATLAB,youcancreateandrunso-calledmfiles,etc.ThemoduleisavailablefromtheToolsmenuinsideLabVIEW.
LabVIEWMathScriptRTModule
LabVIEWControlDesignandSimulationModuleThismoduleisusedforcreatingControlandSimulationapplicationswithLabVIEW.HereyouwillfindPIDcontrollers,etc.Themoduleisavailableasapaletteonyourblockdiagram.
NI-DAQmx
ModellingofDynamicSystems
Hans-PetterHalvorsen,M.Sc.
DynamicSystemsExamplesWaterTank:
MathematicalModels(differentialequations):
AirHeater:
Alt1(Integrator):
Alt2(Timeconstant/1.order):
Alt3(Nonlinear):h– Levelinthetank
T– Temperatureinthetube
Thesearesmall-scaleprocesseswehaveatHiT
Integratorsymbol
Dynamicsystemrepresentedasadifferentialequation(1.ordersystem):
DynamicSystems
Dynamicsystemrepresentedasablockdiagram
Whenwehavetheblockdiagramforthesystem,wecaneasilyimplement itinLabVIEW
Note Wecan“easily”createablockdiagramfromthedifferentialequation(s)
�̇� = −𝑎𝑥 + 𝑏𝑢
SimulationinLabVIEW
Hans-PetterHalvorsen,M.Sc.
ControlandSimulationinLabVIEWControlDesign&SimulationPaletteinLabVIEW
SimulationPaletteinLabVIEW
PIDPaletteinLabVIEW
ControlDesignPaletteinLabVIEW
Checkthatyouhaveallthesepalettes.Openthedifferentsubpalettes,etc.
SimulationLoop
LabVIEWControlandSimulationExample
Wearegoingtolearntocreatesuchasystem(andmuchmore)!
ExampleofSimulation (andcontrol)ofadynamicsystembasedontheblocksintheSimulationPalette
The Simulationpalette inLabVIEWSimulationPaletteinLabVIEW
SimulationLoop:SimilartoaWhileLoop,butcustomizedforusedtogetherwiththeSimulationBlocksavailableinLabVIEW
DifferentSimulationBlocksbyCategory• ContinuousSystems• DiscreteSystems• NonlinearSystems• etc.
LabVIEWExample
Hans-PetterHalvorsen,M.Sc.
SimulationExampleIntegratorsymbol
Dynamicsystemrepresentedasadifferentialequation
�̇� = −𝑎𝑥 + 𝑏𝑢
SimulationExample- ConfigurationIntheexamplethefollowing simulationparameterscouldbeused(right-clickontheSimulationLoopborderandselect“ConfigureSimulationparameters…”):
SimulationExample- Solutions
StepResponseWeseetheStepResponseisasexpected!
Correctresults? – Checkstaticresponse:
Trywithdifferentvaluesforu
�̇� = −𝑎𝑥 + 𝑏𝑢
PIDControlinLabVIEW
Hans-PetterHalvorsen,M.Sc.
ControlSystem
PID Process
Sensor
r e u y-
Kp Ti Td
v
r– ReferenceValue,SP(Setpoint),SV(SetValue)y– MeasurementValue(MV),ProcessValue(PV)e– Errorbetweenthereferencevalueandthemeasurementvalue(e=r– y)v– Disturbance,makesitmorecomplicatedtocontroltheprocessKp,Ti,Td– PIDparameters
PIDAlgorithm:
ControlSystemimplementationinLabVIEW
PID Process
Sensor
r e u y-
Kp Ti Td
vControlSystemimplementationwith“Pen&Paper”
Thetransition from“paper”toLabVIEWiseasy,becausetheimplementation isverysimilartothe“paper”version
Herewehaveusedthe“SimulationLoop”
PID Process
Sensor
r e u y-
Controller
Kp Ti Td
v
ControlSystemimplementationinLabVIEW
ControlSystemimplementationwith“Pen&Paper”
Allthisisnormallyincluded inthecontroller
Thetransition from“paper”toLabVIEWiseasy,becausetheimplementation isverysimilartothe“paper”version
HerewehaveusedanordinaryWhileLoop(whichisrecommended!)
PIDControlinLabVIEWPIDPaletteinLabVIEW(PIDToolkit)
Note!Thefunctions“PID.vi”and“PIDAdvanced.vi”requires thatTi andTdareinminutes,whileit’snormal tousesecondsastheunitfortheseparameters.Youcanusethefollowingpieceofcodeinordertotransformthem:ThismeansweentervaluesforTi andTdinsecons ontheFrontPanelandthevaluesareconvertedtominutes inthecode.
Alternative1: Alternative2:
Thisalternativeusesseconds!
LabVIEWExample
Hans-PetterHalvorsen,M.Sc.
LabVIEWPIDExample
Wewillreplaceuinthepreviousexamplewiththebuilt-inPIDController(useAlternative2)
�̇� = −𝑎𝑥 + 𝑏𝑢
PIDExample- Solutions
FrontPanel:
�̇� = −𝑎𝑥 + 𝑏𝑢
PIDExample- Solutions
BlockDiagram:
Right-click(Configuration) onthePIDblocktoselectKp,Ti,TdasInputs(->Terminals)
�̇� = −𝑎𝑥 + 𝑏𝑢
NextStep:Continuous Simulation
AddaStopButtonanda“HaltSimulation”block
Right-clickontheSimulationLoopborderandselect“ConfigureSimulationParameters…”
Simulation in“RealTime”Inf=Infinite
PIDExample– Continuous Simulation- Solution
TheSimulationnowrunsuntilyoupresstheStopbutton
SimulationSubsystems
Hans-PetterHalvorsen,M.Sc.
LabVIEWExample
Hans-PetterHalvorsen,M.Sc.
CreateaSubsystemofthispart(yourprocessmodel)
SimulationSubsystemAWaytostructureyourcode,similartoSubVIs
WewillchangeyourcodeabovewhereyoucreateaSimulationSubSystemforyourProcess
File->New…
Thisistherecommendedwaytodo it!– Youcaneasly reuseyourSubsystemsindifferentVIsandyourcodebecomesmorestructured!
SelectFile->New...,Thenchoose“SimulationSubsystem”.Copy(ormove) theblockscontainingyourprocessintotheNewVI(SimulationSubsystem)
SimulationSubsystem- Solutions
File->New…
SimulationSubsystem2(PIDController)
CreateaSubsystemofthispart(thePIDcontroller)
WewillchangeyourcodeabovewhereyoucreateaSimulationSubSystemforthePIDControlleraswell.
SimulationSubsystem– Solutions2
Nowyourcodehasbecomereallysimpletounderstand!
Note!Youmayselectdifferenticonstyle
SimulationSubSystems
LabVIEWExampleWithWhileLoop
Hans-PetterHalvorsen,M.Sc.
SimulationsusingaWhileLoopNote!TheSimulationLoophassomedrawbacks/ismorecomplicatedtousethananordinaryWhileLoop.IfweuseSimulationSubsystems,wecanusetheminsideaWhileLoopinstead!-whichbecomesveryhandy!
Soforrealapplicationsthatinvolvesmorethanjustsimulations(suchasDAQ,FileLogging,PIDcontroloftherealprocess,etc.),IrecommendtouseaWhileLoopinsteadofaSimulationLoop.
SimulationSubsystems
WewilladdtheControllerandProcessSubsystemsinsideaWhileloopasshownabove
Hans-PetterHalvorsen,M.Sc.
UniversityCollegeofSoutheastNorwaywww.usn.no
E-mail:[email protected]:http://home.hit.no/~hansha/