47
Simulation in LabVIEW Hans-Petter Halvorsen, M.Sc.

Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

SimulationinLabVIEW

Hans-PetterHalvorsen,M.Sc.

Page 2: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

Software

• LabVIEW• LabVIEWControlDesignandSimulationModule–ThismoduleisusedforcreatingControlandSimulationapplicationswithLabVIEW.–HereyouwillfindPIDcontrollers,etc.Themoduleisavailableasapaletteonyourblockdiagram.

AllLabVIEWSoftwarecanbedownloadedhere:www.ni.com/download

Page 3: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

Contents

• BlockDiagramSimulationbasedondifferentialEquations–UsingtheSimulationLoop

• PIDControlwithbuilt-inPIDblocks/functions• CreatingandusingSimulationSubsystems• SimulationsusingaWhileLoopwithSubsystemsinside

Page 4: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

WhatisLabVIEW?

Hans-PetterHalvorsen,M.Sc.

Page 5: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

LabVIEW=Fun!GraphicalProgramming:• VerydifferentfromtraditionalprogramminglikeVB,C#,Maple,

MATLAB,MathScript,etc.• Itismorelikea“drawingprogram”thanaProgrammingLanguage• Thismakesiteasytouseforthosewhoarenotprogrammers(or

dontlikeprogrammingJ)• ExcellenttoolwhenusingHardware,whenyouneedtotake

Measurements(DAQ),etc.• Itisfunandmakesyouverycreative!

Page 6: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

LabVIEWExample

PropertyNodes

SequenceStructure WhileLoop SubVI(Function/Method)

CaseStructure(if-else)Constants

Arrays

StopButtonCondition

(Whenshalltheloopend?)

LocalVariableComment

Note!Todosomethingwithanobject– Right-clickonit

LabVIEWhasthesamethingsasotherprogramminglanguages,butinagraphical way!

Page 7: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

LabVIEWEnvironmentFrontPanel

BlockDiagram

CreateyourUserInterface/HMIhere

CreateyourCode hereSwitchbetweenthem:Ctrl+E

Note!BoththeFrontPanelandtheBlockDiagramarestoredinonesinglefile.ThesefilesarecalledVIs (becausethefileextensionis“.vi”).VI=VirtualInstruments

Page 8: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

Controls andFunctions Palette

AvailableonlyfromtheFrontPanel

AvailableonlyfromtheBlockDiagram

YoucreateyourUserInterface withhelpoftheseControls

Youcreateyou

rCod

ewith

helpof

theseFunctio

ns

Right-clickontheFrontPanel

Right-clicko

ntheBlockD

iagram

Youcan“pin”them!

Page 9: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

CustomizingControlsandFunctionsPalettes

Dothisforboth theControlsPaletteandtheFunctionsPalette

Page 10: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

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

Page 11: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

ModellingofDynamicSystems

Hans-PetterHalvorsen,M.Sc.

Page 12: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

DynamicSystemsExamplesWaterTank:

MathematicalModels(differentialequations):

AirHeater:

Alt1(Integrator):

Alt2(Timeconstant/1.order):

Alt3(Nonlinear):h– Levelinthetank

T– Temperatureinthetube

Thesearesmall-scaleprocesseswehaveatHiT

Page 13: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

Integratorsymbol

Dynamicsystemrepresentedasadifferentialequation(1.ordersystem):

DynamicSystems

Dynamicsystemrepresentedasablockdiagram

Whenwehavetheblockdiagramforthesystem,wecaneasilyimplement itinLabVIEW

Note Wecan“easily”createablockdiagramfromthedifferentialequation(s)

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 14: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

SimulationinLabVIEW

Hans-PetterHalvorsen,M.Sc.

Page 15: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

ControlandSimulationinLabVIEWControlDesign&SimulationPaletteinLabVIEW

SimulationPaletteinLabVIEW

PIDPaletteinLabVIEW

ControlDesignPaletteinLabVIEW

Checkthatyouhaveallthesepalettes.Openthedifferentsubpalettes,etc.

SimulationLoop

Page 16: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

LabVIEWControlandSimulationExample

Wearegoingtolearntocreatesuchasystem(andmuchmore)!

ExampleofSimulation (andcontrol)ofadynamicsystembasedontheblocksintheSimulationPalette

Page 17: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

The Simulationpalette inLabVIEWSimulationPaletteinLabVIEW

SimulationLoop:SimilartoaWhileLoop,butcustomizedforusedtogetherwiththeSimulationBlocksavailableinLabVIEW

DifferentSimulationBlocksbyCategory• ContinuousSystems• DiscreteSystems• NonlinearSystems• etc.

Page 18: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

LabVIEWExample

Hans-PetterHalvorsen,M.Sc.

Page 19: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

SimulationExampleIntegratorsymbol

Dynamicsystemrepresentedasadifferentialequation

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 20: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

SimulationExample- ConfigurationIntheexamplethefollowing simulationparameterscouldbeused(right-clickontheSimulationLoopborderandselect“ConfigureSimulationparameters…”):

Page 21: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

SimulationExample- Solutions

StepResponseWeseetheStepResponseisasexpected!

Correctresults? – Checkstaticresponse:

Trywithdifferentvaluesforu

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 22: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we
Page 23: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

PIDControlinLabVIEW

Hans-PetterHalvorsen,M.Sc.

Page 24: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

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:

Page 25: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

ControlSystemimplementationinLabVIEW

PID Process

Sensor

r e u y-

Kp Ti Td

vControlSystemimplementationwith“Pen&Paper”

Thetransition from“paper”toLabVIEWiseasy,becausetheimplementation isverysimilartothe“paper”version

Herewehaveusedthe“SimulationLoop”

Page 26: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

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!)

Page 27: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

PIDControlinLabVIEWPIDPaletteinLabVIEW(PIDToolkit)

Note!Thefunctions“PID.vi”and“PIDAdvanced.vi”requires thatTi andTdareinminutes,whileit’snormal tousesecondsastheunitfortheseparameters.Youcanusethefollowingpieceofcodeinordertotransformthem:ThismeansweentervaluesforTi andTdinsecons ontheFrontPanelandthevaluesareconvertedtominutes inthecode.

Alternative1: Alternative2:

Thisalternativeusesseconds!

Page 28: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

LabVIEWExample

Hans-PetterHalvorsen,M.Sc.

Page 29: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

LabVIEWPIDExample

Wewillreplaceuinthepreviousexamplewiththebuilt-inPIDController(useAlternative2)

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 30: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

PIDExample- Solutions

FrontPanel:

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 31: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

PIDExample- Solutions

BlockDiagram:

Right-click(Configuration) onthePIDblocktoselectKp,Ti,TdasInputs(->Terminals)

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 32: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we
Page 33: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

NextStep:Continuous Simulation

AddaStopButtonanda“HaltSimulation”block

Right-clickontheSimulationLoopborderandselect“ConfigureSimulationParameters…”

Simulation in“RealTime”Inf=Infinite

Page 34: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

PIDExample– Continuous Simulation- Solution

TheSimulationnowrunsuntilyoupresstheStopbutton

Page 35: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we
Page 36: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

SimulationSubsystems

Hans-PetterHalvorsen,M.Sc.

Page 37: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

LabVIEWExample

Hans-PetterHalvorsen,M.Sc.

Page 38: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

CreateaSubsystemofthispart(yourprocessmodel)

SimulationSubsystemAWaytostructureyourcode,similartoSubVIs

WewillchangeyourcodeabovewhereyoucreateaSimulationSubSystemforyourProcess

File->New…

Thisistherecommendedwaytodo it!– Youcaneasly reuseyourSubsystemsindifferentVIsandyourcodebecomesmorestructured!

SelectFile->New...,Thenchoose“SimulationSubsystem”.Copy(ormove) theblockscontainingyourprocessintotheNewVI(SimulationSubsystem)

Page 39: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

SimulationSubsystem- Solutions

File->New…

Page 40: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we
Page 41: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

SimulationSubsystem2(PIDController)

CreateaSubsystemofthispart(thePIDcontroller)

WewillchangeyourcodeabovewhereyoucreateaSimulationSubSystemforthePIDControlleraswell.

Page 42: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

SimulationSubsystem– Solutions2

Nowyourcodehasbecomereallysimpletounderstand!

Note!Youmayselectdifferenticonstyle

SimulationSubSystems

Page 43: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we
Page 44: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

LabVIEWExampleWithWhileLoop

Hans-PetterHalvorsen,M.Sc.

Page 45: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

SimulationsusingaWhileLoopNote!TheSimulationLoophassomedrawbacks/ismorecomplicatedtousethananordinaryWhileLoop.IfweuseSimulationSubsystems,wecanusetheminsideaWhileLoopinstead!-whichbecomesveryhandy!

Soforrealapplicationsthatinvolvesmorethanjustsimulations(suchasDAQ,FileLogging,PIDcontroloftherealprocess,etc.),IrecommendtouseaWhileLoopinsteadofaSimulationLoop.

SimulationSubsystems

WewilladdtheControllerandProcessSubsystemsinsideaWhileloopasshownabove

Page 46: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we
Page 47: Simulation in LabVIEW Video - halvorsen.blog · The Simulation Loop has some drawbacks/is more complicated to use than an ordinary While Loop. If we use Simulation Subsystems, we

Hans-PetterHalvorsen,M.Sc.

UniversityCollegeofSoutheastNorwaywww.usn.no

E-mail:[email protected]:http://home.hit.no/~hansha/