65
Simulation in LabVIEW Hans-Petter Halvorsen, M.Sc. http://home.hit.no/~hansha/?page=labview with Self-paced Step-by-Step Exercises

Simulation in LabVIEW - Overview

  • Upload
    dohanh

  • View
    290

  • Download
    17

Embed Size (px)

Citation preview

Page 1: Simulation in LabVIEW - Overview

SimulationinLabVIEW

Hans-PetterHalvorsen,M.Sc.

http://home.hit.no/~hansha/?page=labview

withSelf-pacedStep-by-StepExercises

Page 2: Simulation in LabVIEW - Overview

LabVIEWInstallation

Downloadthesoftwarehere:

http://home.hit.no/~hansha/?page=labviewThesearethemainmodulesweuseinthedifferentcoursesatTelemark UniversityCollege:

• LabVIEW (LabVIEW ProfessionalDevelopmentSystem32-Bit:English)

• NI-DAQmx(HardwareDriverforNIUSB-6008,NITC-01,etc.)• LabVIEW ControlDesignandSimulationModule• LabVIEWMathScript RTModuleNote!Thesepackagesareseparate downloads!

Note!YougettheSerialNumber fromyourTeacher,butthesoftwarecanbeused for30daysbeforeyouneed toenteravalidSerialNumber.

AllLabVIEWSoftwarecanbedownloadedhere:www.ni.com/download

Page 3: Simulation in LabVIEW - Overview

LabVIEWTraining

• LabVIEWTrainingforStudents(NationalInstruments):

http://ni.com/students/learnlabview

• LabVIEWCourse:http://home.hit.no/~hansha/?training=labview

Hereyouwillfind lotsofVideos,TutorialsandExercises

Itisrecommendedthatyouwatchsomeofthevideosbeforeyoureadfurter

LearningbyDoing!

Page 4: Simulation in LabVIEW - Overview

Contents• IntroductiontoLabVIEW• Installation• BlockDiagramSimulationbasedondifferentialEquations– SimulationLoop

• PIDControlwithbuilt-inPIDblocks/functions• CreatingandusingSimulationSubsystems• SimulationsusingaWhileLoopwithSubsystemsinside• Discrete Simulation– FormulaNode

• MathScript

Page 5: Simulation in LabVIEW - Overview

NationalInstrumentsisthevendorofLabVIEW

NationalInstrumentscreatesbothHardware andSoftware

Page 6: Simulation in LabVIEW - Overview

Hardware

cRIO

ZigBee

VisionSystem

Arduino

USB-6008

Wi-FiDAQ

TC-01

NOxSensor

Pt-100

AirHeater

WaterTank

Page 7: Simulation in LabVIEW - Overview

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 8: Simulation in LabVIEW - Overview

http://www.ni.com/pdf/manuals/376039a.pdf

Students:TrysomeoftheseShortcutsandTools

Page 9: Simulation in LabVIEW - Overview

ControlandSimulationinLabVIEWControlDesign&SimulationPaletteinLabVIEW

SimulationPaletteinLabVIEW

PIDPaletteinLabVIEW

ControlDesignPaletteinLabVIEW

Students:Checkthatyouhaveallthesepalettes.Openthedifferentsubpalettes,etc.

SimulationLoop

Page 10: Simulation in LabVIEW - Overview

LabVIEWControlandSimulationExample

Wearegoingtolearntocreatesuchasystem(andmuchmore)!

ExampleofSimulation (andcontrol)ofadynamicsystembasedontheblocksintheSimulationPalette

Page 11: Simulation in LabVIEW - Overview

ModellingofDynamicSystems

Hans-PetterHalvorsen,M.Sc.

Page 12: Simulation in LabVIEW - Overview

DynamicSystemsExamplesWaterTank:

MathematicalModels(differentialequations):

AirHeater:

Alt1(Integrator):

Alt2(Timeconstant/1.order):

Alt3(Nonlinear):h– Levelinthetank

T– Temperatureinthetube

Thesearesmall-scaleprocesseswehaveatHiT

Page 13: Simulation in LabVIEW - Overview

DynamicSystemsExamples2Tank:MathematicalModels(differentialequations):

4Tank:

Thesearesmall-scaleprocesseswehaveatHiT

Page 14: Simulation in LabVIEW - Overview

Integratorsymbol

Dynamicsystemrepresentedasadifferentialequation

DynamicSystems

Dynamicsystemrepresentedasablockdiagram

Whenwehavetheblockdiagramforthesystem,wecaneasilyimplement itinLabVIEW

Note Wecan“easily”createablockdiagramfromthedifferentialequation(s)

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 15: Simulation in LabVIEW - Overview

BlockDiagramExamples

Students:Findtheblockdiagramsfor thedifferentialequationsabove(pen&paper)

Example1:

Example2:

Page 16: Simulation in LabVIEW - Overview

BlockDiagrams- Solutions

or:

Example1:

Example2:

Page 17: Simulation in LabVIEW - Overview

BlockDiagramExamples

Students:Findtheblockdiagramfor thedifferentialequationabove(pen&paper)

HigherorderdifferentialequationsMass-Spring-DamperExample:

Page 18: Simulation in LabVIEW - Overview

BlockDiagram- SolutionsHigherorderdifferentialequationsMass-Spring-DamperExample:

Page 19: Simulation in LabVIEW - Overview

SimulationTools• MATLAB– Text-basedProgrammingTool– www.mathworks.com

• Simulink– Blockdiagram-basedSimulation,IntegratedwithMATLAB

• LabVIEW• MathScript– UsesMATLABsyntax,IntegratedwithLabVIEW

• Modelica– https://www.modelica.org

• HYSYS– http://www.aspentech.com/products/aspen-hysys.aspx

• ...

Page 20: Simulation in LabVIEW - Overview

SimulationinLabVIEW

Hans-PetterHalvorsen,M.Sc.

Page 21: Simulation in LabVIEW - Overview

SimulationinLabVIEWSimulationPaletteinLabVIEW SimulationLoop:SimilartoaWhileLoop,butcusomized forused

togetherwiththeSimulationBlocksavailableinLabVIEW

DifferentSimulationBlocksbyCategory• Continuous Systems• DiscreteSystems• NonlinearSystems• etc.

http://home.hit.no/%7Efinnh/videos/sim_module/sim_palette_overview/sim_palette_overview.html

SimulationPaletteOverviewVideo(15minutes)

Students:YoushouldwatchthisVideobeforeyougofurther!

Page 22: Simulation in LabVIEW - Overview

SimulationExample

Students: ImplementandSimulatethissysteminLabVIEW

Integratorsymbol

Dynamicsystemrepresentedasadifferentialequation

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 23: Simulation in LabVIEW - Overview

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

Page 24: Simulation in LabVIEW - Overview

SimulationExample- Solutions

StepResponseWeseetheStepResponseisasexpected!

Correctresults? – Checkstaticresponse:

Trywithdifferentvaluesforu

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 25: Simulation in LabVIEW - Overview

PIDControlinLabVIEW

Hans-PetterHalvorsen,M.Sc.

Page 26: Simulation in LabVIEW - Overview

ControlSystems

PID Process

Sensor

r e u y

-

Kp Ti Td

v

ExampleofIndustrialControllers

Students:Whatisr,e,u,v,y,Kp,Ti,Td?

TypicalBlockDiagram:

DeltaVPIDController

Page 27: Simulation in LabVIEW - Overview

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 28: Simulation in LabVIEW - Overview

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 29: Simulation in LabVIEW - Overview

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 30: Simulation in LabVIEW - Overview

PIDControlinLabVIEWPIDPaletteinLabVIEW(PIDToolkit)

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

Alternative1: Alternative2:

Thisalternativeusesseconds!

Page 31: Simulation in LabVIEW - Overview

LabVIEWPIDExample

Students:Replaceuinthepreviousexamplewiththebuilt-inPIDController (useAlternative2)

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 32: Simulation in LabVIEW - Overview

PIDExample- Solutions

FrontPanel:

Students:Findproper𝐾*,𝑇, ,𝑇- Parametersforthissystem.Use“TrialandError”oroneofthemethodsexplainedintheTextBook

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 33: Simulation in LabVIEW - Overview

PIDExample- Solutions

BlockDiagram:

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

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 34: Simulation in LabVIEW - Overview

NextStep:Continuous Simulation

Students:ChangeyourSimulationSettingsandRunyourSimulationwiththesechanges

AddaStopButtonanda“HaltSimulation”block

Right-clickontheSimulationLoopborderandselect“ConfigureSimulationParameters…”

Simulation in“RealTime”Inf=Infinite

Page 35: Simulation in LabVIEW - Overview

PIDExample– Continuous Simulation- Solution

TheSimulationnowrunsuntilyoupresstheStopbutton

Students:ExtendtheexamplewithanewChartoftheControlSignal(u)andinclude thereferencesignal(r)intheexistingChart,soyoucaneasilyseethechangesinrandhowthesystemhandleit.

Page 36: Simulation in LabVIEW - Overview

SimulationSubsystems

Hans-PetterHalvorsen,M.Sc.

Page 37: Simulation in LabVIEW - Overview

CreateaSubsystemofthispart(yourprocessmodel)

SimulationSubsystemAWaytostructureyourcode,similartoSubVIs

Students:ChangeyourcodeabovewhereyoucreateaSimulationSubSystemforyourProcess

File->New…

Thisistherecommendedwaytodo it!– Youcaneasly reuseyourSubsystemsindifferentVIsandyourcodebecomesmorestructured!

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

Page 38: Simulation in LabVIEW - Overview

SimulationSubsystem- Solutions

File->New…

Page 39: Simulation in LabVIEW - Overview

SimulationSubsystem2(PIDController)

CreateaSubsystemofthispart(thePIDcontroller)

Students:ChangeyourcodeabovewhereyoucreateaSimulationSubSystemforthePIDControlleraswell.

Page 40: Simulation in LabVIEW - Overview

SimulationSubsystem– Solutions2

Nowyourcodehasbecomereallysimpletounderstand!

Note!Youmayselectdifferenticonstyle

SimulationSubSystems

Page 41: Simulation in LabVIEW - Overview

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

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

SimulationSubsystems

Students:AddyourControllerandProcessSubsystemsinsideaWhileloopasshownabove.Simulatethesystem.Doyougetthesameresults?

Page 42: Simulation in LabVIEW - Overview

DataloggingwithLabVIEW

Hans-PetterHalvorsen,M.Sc.

Page 43: Simulation in LabVIEW - Overview

SaveDatatoFile(Datalogging)

Right-click-Properties

RecommendedSettings

Page 44: Simulation in LabVIEW - Overview

DataloggingExample

Page 45: Simulation in LabVIEW - Overview

Students:(1) LogDatafromyoursystem,i.e.,theControlSignal(u)andtheoutputoftheprocess(y)toa“MeasurementFile”.(2) ThenPlottheDatafromtheFileinExcel.

DataloggingExample– BlockDiagram

Page 46: Simulation in LabVIEW - Overview

DiscreteSystems

Hans-PetterHalvorsen,M.Sc.

Page 47: Simulation in LabVIEW - Overview

ContinuousSignal

DiscretSignal Acomputercanonlydealwithdiscretesignals

k=0,1,2,3,4,....

Ts=SamplingTime

Note!Differentbooksusedifferentnotations WhenTs->0,wehaveacontinuoussignal,

butinacomputerthatisnotpossible.

Page 48: Simulation in LabVIEW - Overview

DiscretizationExample

Inorder tosimulatethissysteminLabVIEWusing theFormulaNodeweneedtofind thediscrete differentialequation.

Giventhefollowingdifferentialequation:

Wecanusee.g.,theEulerApproximation:Ts – SamplingTime

Thenweget:

Thisgivesthefollowingdiscretedifferentialequation:

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 49: Simulation in LabVIEW - Overview

FormulaNode/MathScript

Hans-PetterHalvorsen,M.Sc.

Page 50: Simulation in LabVIEW - Overview

FormulaNode&MathScriptNode

MathScriptNode:CreateanduseMathScript/MATLABcodewithinLabVIEW

FormulaNode:CreateanduseCcodewithinLabVIEW

Example:

Veryuseful formathematicalexpressionsandsimulations!

Right-clickon theborder toAdd InputorAddOutput

Page 51: Simulation in LabVIEW - Overview

FormulaNode/MathScriptNode

Students:CreatethisExampleboth inFormulaNodeandinMathScriptNode

Students:TrythisFormulaaswell(both inFormulaNodeandinMathScriptNode):

Page 52: Simulation in LabVIEW - Overview

SimulateDiscrete SystemsusingtheFormulaNodeinLabVIEW

Example

Inorder tosimulatethissysteminLabVIEWusing theFormulaNodeweneedtofind thediscrete differentialequation.

Giventhefollowingdifferentialequation:

WecanusetheEulerApproximation: Ts– SamplingTime

Thenweget:

Thisgivesthefollowingdiscretedifferentialequation:

Students:SimulateandPlotthediscretesystemaboveusingaFormulaNodeandaForLoopinLabVIEW Ts=0.1s

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 53: Simulation in LabVIEW - Overview

FormulaNodeinLabVIEW- Solutions

Asexpected,wegetthesameresultsasinthepreviousexampleusingablockdiagramsimulation

Students: Improveyour solutionbycreatingaSubVI ofyourmodel

xk1=(1-a*Ts)*xk + Ts*b*uk;

Discretemodel:

Page 54: Simulation in LabVIEW - Overview

FormulaNodeinLabVIEW– Solutions2

SubVI

MainVI

AdvantagesusingSubVI:• OurMainprogramhasbecomesimplerandeasierto

understandandmaintain• Wecanreuseourmodel inotherapplications• Ifweneedtodochangesinthemodel,weonlydoitonceandin

oneplace!

Page 55: Simulation in LabVIEW - Overview

MathScriptLabVIEWMathScriptRTModule

Add-onModule forLabVIEWwherewecandotext-basedprogrammingandsimulations– verypowerful!

Page 56: Simulation in LabVIEW - Overview

MathScriptSimulationExample

% Simulation of discrete modelclear, clc

% Model Parametersa = 0.25;b = 2;

% Simulation ParametersTs = 0.1; %sTstop = 20; %suk = 1; % Step Responsex(1) = 0;

% Simulationfor k=1:(Tstop/Ts)

x(k+1) = (1-a*Ts).*x(k) + Ts*b*uk;end

% Plot the Simulation Resultsk=0:Ts:Tstop;plot(k,x)grid on

Students:CreateandtesttheMathScriptcode.YoushouldgetthesameresultsasintheLabVIEWExamples

CreatethefollowingcodeintheMathScript“ScriptEditor”

�̇� = −𝑎𝑥 + 𝑏𝑢

Page 57: Simulation in LabVIEW - Overview

MathScriptNodeStudents:TrythesameexampleinsideLabVIEWusingtheMathScriptNode

Justcopyandpastethecodefromthepreviousexample

Page 58: Simulation in LabVIEW - Overview

Hans-PetterHalvorsen,M.Sc.

DoyouneedmorePractice?- SelectaChallenge

Page 59: Simulation in LabVIEW - Overview

Students:SimulatethissystemusingLabVIEW.Plottheposition, speedandtheaccelleration.Testwithdifferentvaluesonm,kandd.

Mass-Spring-DamperSystem

http://www.techteach.no/simview/mass_spring_damper/index.php

Page 60: Simulation in LabVIEW - Overview

AsketchoftheAirHeaterSystem:

AirHeaterOverview

TemperatureMeasurement

AirIn

ControlSignal

Heater

AirOut

TT

AirtubeAirflow

TC

RealAirHeater

Page 61: Simulation in LabVIEW - Overview

InthisassignmentwearegoingtosimulateandcontrolamathematicalmodelofanAirHeatersystem.

Amathematicalmodelofthesystemcouldbe:

Usethefollowing valuesinthesimulations:

Where:

AirHeaterSimulation

Note!YoufindanexampleofhowtodoitinLabVIEWonthenextslides– buttrytosolveitbyyourself first!

Page 62: Simulation in LabVIEW - Overview

ImplementtheHeatermodel inaSimulationSubSystem,asshownbelow:

SeenextslideforFrontPanelexample

AirHeaterSimulationExample

Page 63: Simulation in LabVIEW - Overview

FrontPanelExample:

AirHeaterSimulation

FindProperPI(D)Parameters

Page 64: Simulation in LabVIEW - Overview

AirHeaterSimulationwithMathScript % Simulation of discrete model

clear, clc

% Model Parametersa = 0.25;b = 2;

% Simulation ParametersTs = 0.1; %sTstop = 20; %suk = 1;x(1) = 0;

% Simulationfor k=1:(Tstop/Ts)

x(k+1) = (1-a*Ts).*x(k) + Ts*b*uk;end

% Plot the Simulation Resultsk=0:Ts:Tstop;plot (k, x)grid on

AreyouabletosimulatetheAirHeatermodelusingMathScriptor/andMathScriptNode?

MathScriptSimulationExample

Page 65: Simulation in LabVIEW - Overview

Hans-PetterHalvorsen,M.Sc.

UniversityCollegeofSoutheastNorwaywww.usn.no

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