Tải bản đầy đủ (.pdf) (61 trang)

Giáo trình control and simulation in LabVIEW

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.8 MB, 61 trang )

g







ControlandSimulation
inLabVIEW
Hans-PetterHalvorsen








ControlandSimulationinLabVIEW


Hans-PetterHalvorsen
Copyright©2017


E-Mail:
Web:g




g






Preface
ThisdocumentexplainsthebasicconceptsofusingLabVIEWforControlandSimulation
purposes.

FormoreinformationaboutLabVIEW,visitmyBlog:g.

Youneedthefollowingsoftware:














LabVIEW
LabVIEWControlDesignandSimulationModule

LabVIEWMathScriptRTModule
NI-DAQmx
NIMeasurement&AutomationExplorer




TableofContents
Preface......................................................................................................................................3
TableofContents.....................................................................................................................iv
1

IntroductiontoLabVIEW...................................................................................................1
1.1

Dataflowprogramming...............................................................................................1

1.2

Graphicalprogramming..............................................................................................1

1.3

Benefits.......................................................................................................................2

2

IntroductiontoControlandSimulation............................................................................3

3


IntroductiontoControlandSimulationinLabVIEW.........................................................4
3.1

3.1.1

Simulation............................................................................................................5

3.1.2

ControlDesign.....................................................................................................5

3.2

LabVIEWPIDandFuzzyLogicToolkit..........................................................................6

3.2.1

PIDControl..........................................................................................................6

3.2.2

FuzzyLogic...........................................................................................................6

3.3
4

LabVIEWControlDesignandSimulationModule.......................................................4

LabVIEWSystemIdentificationToolkit.......................................................................7


Simulation.........................................................................................................................8
4.1

SimulationinLabVIEW................................................................................................8

4.2

SimulationSubsystem...............................................................................................13

4.3

ContinuousLinearSystems.......................................................................................14

Exercises..............................................................................................................................19
5

PIDControl......................................................................................................................31
5.1

PIDControlinLabVIEW............................................................................................32
iv




v


5.2


6

SystemIdentificationinLabVIEW.............................................................................35

FuzzyLogic.......................................................................................................................36
8.1

9

ControlDesigninLabVIEW.......................................................................................34

SystemIdentification.......................................................................................................35
7.1

8

Auto-tuning...............................................................................................................33

ControlDesign.................................................................................................................34
6.1

7

TableofContents

FuzzyLogicinLabVIEW.............................................................................................36

LabVIEWMathScript.......................................................................................................38
9.1


Help...........................................................................................................................39

9.2

Examples...................................................................................................................39

9.3

Usefulcommands.....................................................................................................42

9.4

Plotting.....................................................................................................................42

10 Discretization...................................................................................................................43
10.1 Low-passFilter..........................................................................................................43
10.2 PIController..............................................................................................................46
10.2.1 PIControllerasaState-spacemodel.................................................................49
10.3 ProcessModel..........................................................................................................50


Tutorial: Control and Simulation in LabVIEW




1 IntroductiontoLabVIEW
LabVIEW(shortforLaboratoryVirtualInstrumentationEngineeringWorkbench)isa
platformanddevelopmentenvironmentforavisualprogramminglanguagefromNational

Instruments.Thegraphicallanguageisnamed"G".OriginallyreleasedfortheApple
Macintoshin1986,LabVIEWiscommonlyusedfordataacquisition,instrumentcontrol,and
industrialautomationonavarietyofplatformsincludingMicrosoftWindows,variousflavors
ofLinux,andMacOSX.VisitNationalInstrumentsatwww.ni.com.
Thecodefileshavetheextension“.vi”,whichisanabbreviationfor“VirtualInstrument”.
LabVIEWofferslotsofadditionalAdd-OnsandToolkits.

1.1 Dataflowprogramming
TheprogramminglanguageusedinLabVIEW,alsoreferredtoasG,isadataflow
programminglanguage.Executionisdeterminedbythestructureofagraphicalblock
diagram(theLV-sourcecode)onwhichtheprogrammerconnectsdifferentfunction-nodes
bydrawingwires.Thesewirespropagatevariablesandanynodecanexecuteassoonasall
itsinputdatabecomeavailable.Sincethismightbethecaseformultiplenodes
simultaneously,Gisinherentlycapableofparallelexecution.Multi-processingandmultithreadinghardwareisautomaticallyexploitedbythebuilt-inscheduler,whichmultiplexes
multipleOSthreadsoverthenodesreadyforexecution.

1.2 Graphicalprogramming
LabVIEWtiesthecreationofuserinterfaces(calledfrontpanels)intothedevelopmentcycle.
LabVIEWprograms/subroutinesarecalledvirtualinstruments(VIs).EachVIhasthree
components:ablockdiagram,afrontpanel,andaconnectorpanel.Thelastisusedto
representtheVIintheblockdiagramsofother,callingVIs.Controlsandindicatorsonthe
frontpanelallowanoperatortoinputdataintoorextractdatafromarunningvirtual
instrument.However,thefrontpanelcanalsoserveasaprogrammaticinterface.Thusa
virtualinstrumentcaneitherberunasaprogram,withthefrontpanelservingasauser
interface,or,whendroppedasanodeontotheblockdiagram,thefrontpaneldefinesthe
inputsandoutputsforthegivennodethroughtheconnectorpane.ThisimplieseachVIcan
beeasilytestedbeforebeingembeddedasasubroutineintoalargerprogram.
1




2



IntroductiontoLabVIEW

Thegraphicalapproachalsoallowsnon-programmerstobuildprogramssimplybydragging
anddroppingvirtualrepresentationsoflabequipmentwithwhichtheyarealreadyfamiliar.
TheLabVIEWprogrammingenvironment,withtheincludedexamplesandthe
documentation,makesitsimpletocreatesmallapplications.Thisisabenefitononeside,
butthereisalsoacertaindangerofunderestimatingtheexpertiseneededforgoodquality
"G"programming.Forcomplexalgorithmsorlarge-scalecode,itisimportantthatthe
programmerpossessanextensiveknowledgeofthespecialLabVIEWsyntaxandthe
topologyofitsmemorymanagement.ThemostadvancedLabVIEWdevelopmentsystems
offerthepossibilityofbuildingstand-aloneapplications.Furthermore,itispossibletocreate
distributedapplications,whichcommunicatebyaclient/serverscheme,andaretherefore
easiertoimplementduetotheinherentlyparallelnatureofG-code.

1.3 Benefits
OnebenefitofLabVIEWoverotherdevelopmentenvironmentsistheextensivesupportfor
accessinginstrumentationhardware.Driversandabstractionlayersformanydifferenttypes
ofinstrumentsandbusesareincludedorareavailableforinclusion.Thesepresent
themselvesasgraphicalnodes.Theabstractionlayersofferstandardsoftwareinterfacesto
communicatewithhardwaredevices.Theprovideddriverinterfacessaveprogram
developmenttime.ThesalespitchofNationalInstrumentsis,therefore,thatevenpeople
withlimitedcodingexperiencecanwriteprogramsanddeploytestsolutionsinareduced
timeframewhencomparedtomoreconventionalorcompetingsystems.Anewhardware
drivertopology(DAQmxBase),whichconsistsmainlyofG-codedcomponentswithonlya
fewregistercallsthroughNIMeasurementHardwareDDK(DriverDevelopmentKit)

functions,providesplatformindependenthardwareaccesstonumerousdataacquisition
andinstrumentationdevices.TheDAQmxBasedriverisavailableforLabVIEWonWindows,
MacOSXandLinuxplatforms.


Tutorial: Control and Simulation in LabVIEW




2 IntroductiontoControland
Simulation
Controldesignisaprocessthatinvolvesdevelopingmathematicalmodelsthatdescribea
physicalsystem,analyzingthemodelstolearnabouttheirdynamiccharacteristics,and
creatingacontrollertoachievecertaindynamiccharacteristics.
Simulationisaprocessthatinvolvesusingsoftwaretorecreateandanalyzethebehaviorof
dynamicsystems.Youusethesimulationprocesstolowerproductdevelopmentcostsby
acceleratingproductdevelopment.Youalsousethesimulationprocesstoprovideinsight
intothebehaviorofdynamicsystemsyoucannotreplicateconvenientlyinthelaboratory.
Belowweseeaclosed-loopfeedbackcontrolsystem:



3





3 ControlandSimulationin

LabVIEW
LabVIEWhasseveraladditionalmodulesandToolkitsforControlandSimulationpurposes,
e.g.,“LabVIEWControlDesignandSimulationModule”,“LabVIEWPIDandFuzzyLogic
Toolkit”,“LabVIEWSystemIdentificationToolkit”and“LabVIEWSimulationInterface
Toolkit”.LabVIEWMathScriptisalsousefulforControlDesignandSimulation.





LabVIEWControlDesignandSimulationModule
LabVIEWPIDandFuzzyLogicToolkit
LabVIEWSystemIdentificationToolkit
LabVIEWSimulationInterfaceToolkit

Thistutorialwillfocusonthemainaspectsinthesemodulesandtoolkits.
AllVIsrelatedtothesemodulesandtoolkitsareplacedintheControlDesignandSimulation
Toolkit:



3.1 LabVIEWControlDesignandSimulation
Module
WithLabVIEWControlDesignandSimulationModuleyoucanconstructplantandcontrol
modelsusingtransferfunction,state-space,orzero-pole-gain.Analyzesystemperformance
withtoolssuchasstepresponse,pole-zeromaps,andBodeplots.Simulatelinear,nonlinear,
anddiscretesystemswithawideoptionofsolvers.WiththeNILabVIEWControlDesignand
4




5



ControlandSimulationinLabVIEW

SimulationModule,youcananalyzeopen-loopmodelbehavior,designclosed-loop
controllers,simulateonlineandofflinesystems,andconductphysicalimplementations.

3.1.1

Simulation

TheSimulationpaletteinLabVIEW:


ThemainfeaturesintheSimulationpaletteare:





ControlandSimulationLoop-YoumustplaceallSimulationfunctionswithina
Control&SimulationLooporinasimulationsubsystem.
ContinuousLinearSystemsFunctions-UsetheContinuousLinearSystemsfunctions
torepresentcontinuouslinearsystemsofdifferentialequationsonthesimulation
diagram.
SignalArithmeticFunctions-UsetheSignalArithmeticfunctionstoperformbasic
arithmeticoperationsonsignalsinasimulationsystem.


3.1.2

ControlDesign

TheControlDesignpaletteinLabVIEW:

Tutorial: Control and Simulation in LabVIEW


6



ControlandSimulationinLabVIEW



3.2 LabVIEWPIDandFuzzyLogicToolkit
TheNILabVIEWPIDandFuzzyLogicToolkitaddcontrolalgorithmstoLabVIEW.By
combiningthePIDandfuzzylogiccontrolfunctionsinthistoolkitwiththemathandlogic
functionsinLabVIEWsoftware,youcanquicklydevelopprogramsforautomatedcontrol.
Youmayintegratethesecontroltoolswiththepowerofdataacquisition.

3.2.1

PIDControl

ThePIDpaletteinLabVIEW:




3.2.2

FuzzyLogic

TheFuzzyLogicpaletteinLabVIEW:
Tutorial: Control and Simulation in LabVIEW


7



ControlandSimulationinLabVIEW



3.3 LabVIEWSystemIdentificationToolkit
The“LabVIEWSystemIdentificationToolkit”combinesdataacquisitiontoolswithsystem
identificationalgorithmsforplantmodeling.YoucanusetheLabVIEWSystemIdentification
Toolkittofindempiricalmodelsfromrealplantstimulus-responseinformation.
TheSystemIdentificationpaletteinLabVIEW:



Tutorial: Control and Simulation in LabVIEW





4 Simulation
Simulationisaprocessthatinvolvesusingsoftwaretorecreateandanalyzethebehaviorof
dynamicsystems.Youusethesimulationprocesstolowerproductdevelopmentcostsby
acceleratingproductdevelopment.Youalsousethesimulationprocesstoprovideinsight
intothebehaviorofdynamicsystemsyoucannotreplicateconvenientlyinthelaboratory.
Forexample,simulatingajetenginesavestime,labor,andmoneycomparedtobuilding,
testing,andrebuildinganactualjetengine.YoucanusetheLabVIEWControlDesignand
SimulationModuletosimulateadynamicsystemoracomponentofadynamicsystem.For
example,youcansimulateonlytheplantwhileusinghardwareforthecontroller,actuators,
andsensors(Hardware-in-the-loopSimulation).
Adynamicsystemmodelisadifferentialordifferenceequationthatdescribesthebehavior
ofthedynamicsystem.

4.1 SimulationinLabVIEW
UsetheSimulationVIsandfunctionstocreatesimulationapplicationsinLabVIEW.Inthe
ControlDesign&SimulationpalettewehavetheSimulationSubpalette:


BelowweseetheSimulationSubpalette:

8



9



Simulation



Note!Allthe“Blocks”intheSimulationpalettearenotSubVIs,i.e.,wecannotdouble-click
onthemandopentheBlockDiagrambecausetheyhavenone.AlltheBlocksinthe
SimulationpalettemustbeusedinsidetheControlandSimulationLoop(explainedbelow).
ControlandSimulationLoop:
Inthe“Simulation”Subpalettewehavethe“ControlandSimulationLoop”whichisvery
usefulinsimulations:

YoumustplaceallSimulationfunctionswithinaControl&SimulationLooporinasimulation
subsystem.YoualsocanplacesimulationsubsystemswithinaControl&SimulationLoopor
anothersimulationsubsystem,oryoucanplacesimulationsubsystemsonablockdiagram
outsideaControl&SimulationLooporrunthesimulationsubsystemsasstand-aloneVIs.

Tutorial: Control and Simulation in LabVIEW


10



Simulation


TheControl&SimulationLoophasanInputNode(upperleftcorner)andanOutputNode
(upperrightcorner).UsetheInputNodetoconfiguresimulationparameters
programmatically.YoualsocanconfiguretheseparametersinteractivelyusingtheConfigure
SimulationParametersdialogbox.Accessthisdialogboxbydouble-clickingtheInputNode
orbyright-clickingtheborderandselectingConfigureSimulationParametersfromthe
shortcutmenu.

Configuration:
Whenyouplacetheseblocksonthediagramyoumaydouble-clickorright-clickandthen
select“Configuration…”
Example:ConfigurationDialogbox
Forthe“TransferFunction”(Simulation→ContinuousLinearSystems)blockwehave
thefollowingConfigurationwindow:

Tutorial: Control and Simulation in LabVIEW


11



Simulation


AllthedifferentblockshavetheirowndifferentConfigurationwindow.


IntheParametersourceyoumayselectbetween:



ConfigurationDialogBox
Terminal

Ifyouselect“ConfigurationDialogBox”youentertheconfigurationintheConfiguration
windowlikeweseeabove,whileifyouselect“Terminal”thatspecificconfigurationisset
fromtheBlockDiagramlikethis:



IconStyle:
Whenyouplacetheblockontheblockdiagramyoumayselecthowthatshouldappear.
Right-clickontheblock/iconandselect“IconStyle”:

Tutorial: Control and Simulation in LabVIEW


12



Simulation


Example:IconStyle
Forthe“TransferFunction”(Simulation→ContinuousLinearSystems)blockwehave
thefollowingdifferenticonstyles:
Static:

Dynamic:


TextOnly:


Express:



WeseefortheDynamicandExpressstylesthattheappearancechangesaccordingto
configurationparametersweset.

Tutorial: Control and Simulation in LabVIEW


13



Simulation

Ipersonallypreferthe“static”iconstylebecauseitdoesnotrequirelotsofspaceonthe
diagram.

4.2 SimulationSubsystem
YoumaycreateaSimulationSubsystem(File→New…):


TheSimulationSubsystemisveryusefulwhendealingwithlargersimulationsystemsin
ordertocreateamorestructuredcode.Irecommendthatyou(always)usethisfeature.
TheSimulationSubsystemisalmostequaltoanormalLabVIEWBlockDiagrambutnoticethe
backgroundcolorisslightlydarker.
Note!InordertoopentheSimulationSubsystem,right-clickandselect“OpenSubsystem”.
TheSimulationSubsystemmayalsoberepresentedbydifferenticons.Ifyouselect
“dynamic”iconstyle,youwillseea“miniature”versionofthesubsystemlikethis:

Tutorial: Control and Simulation in LabVIEW



14



Simulation


Youmaydraginthecornerinordertoincreaseordecreasethedynamicicon.
Ifyouselect“static”iconstyleyouseetheiconyoucreatedwiththeIconEditor.


Likethis:





4.3 ContinuousLinearSystems
Inthe“ContinuousLinearSystems”Subpalettewewanttocreateasimulationmodel:


ThemostusedblocksprobablyareIntegrator,TransportDelay,State-SpaceandTransfer
Function.
Tutorial: Control and Simulation in LabVIEW


15




Simulation

Whenyouplacetheseblocksonthediagramyoumaydouble-clickorright-clickandthen
select“Configuration…”
Integrator-Integratesacontinuousinputsignalusingtheordinarydifferential
equation(ODE)solveryouspecifyforthesimulation.
TheConfigurationwindowfortheIntegratorblocklookslikethis:


TransportDelay-Delaystheinputsignalbytheamountoftimeyouspecify.
TheConfigurationwindowfortheTransportDelayblocklookslikethis:



TransferFunction-Implementsasystemmodelintransferfunctionform.Youdefine
thesystemmodelbyspecifyingtheNumeratorandDenominatorofthetransferfunction
equation.
Tutorial: Control and Simulation in LabVIEW


16



Simulation

TheConfigurationwindowfortheTransferFunctionblocklookslikethis:


State-Space-Implementsasystemmodelinstate-spaceform.Youdefinethesystem

modelbyspecifyingtheinput,output,state,anddirecttransmissionmatrices.
TheConfigurationwindowfortheState-Spaceblocklookslikethis:



SignalArithmetic:
The“SignalArithmetic”Subpaletteisalsousefulwhencreatingasimulationmodel:

Tutorial: Control and Simulation in LabVIEW


17



Simulation



Example:SimulationModel
BelowweseeanexampleofasimulationmodelcreatedinLabVIEW.



Example:Simulation
BelowweseeanexampleofasimulationmodelusingtheControlandSimulationLoop.


Noticethefollowing:
Tutorial: Control and Simulation in LabVIEW



18



Simulation

Clickontheborderofthesimulationloopandselect“ConfigureSimulationParameters…”


Thefollowingwindowappears(ConfigureSimulationParameters):


InthiswindowyousetsomeParametersregardingthesimulation,someimportantare:



FinalTime(s)–sethowlongthesimulationshouldlast.Foraninfinitetimeset“Inf”.
EnableSynchronizedTiming-Specifiesthatyouwanttosynchronizethetimingof
theControl&SimulationLooptoatimingsource.Toenablesynchronization,placea
Tutorial: Control and Simulation in LabVIEW




19




Simulation

checkmarkinthischeckboxandthenchooseatimingsourcefromtheSourcetype
listbox.
ClicktheHelpbuttonformoredetails.
YoumayalsosetsomeoftheseParametersintheBlockDiagram:


YoumayusethemousetoincreasethenumbersofParametersandright-clickandselect
“SelectInput”.

Exercises
Exercise:Simulationofaspring-massdampersystem
Inthisexerciseyouwillconstructasimulationdiagramthatrepresentsthebehaviorofa
dynamicsystem.Youwillsimulateaspring-massdampersystem.
𝐹(𝑡) − 𝑐𝑥(𝑡) − 𝑘𝑥(𝑡) = 𝑚𝑥(𝑡)
wheretisthesimulationtime,F(t)isanexternalforceappliedtothesystem,cisthe
dampingconstantofthespring,kisthestiffnessofthespring,misamass,andx(t)isthe
positionofthemass. 𝑥 isthefirstderivativeoftheposition,whichequalsthevelocityof
themass. 𝑥 isthesecondderivativeoftheposition,whichequalstheaccelerationofthe
mass.
Thefollowingfigureshowsthisdynamicsystem.



Tutorial: Control and Simulation in LabVIEW


20




Simulation


Thegoalistoviewthepositionx(t)ofthemassmwithrespecttotimet.Youcancalculate
thepositionbyintegratingthevelocityofthemass.Youcancalculatethevelocityby
integratingtheaccelerationofthemass.Ifyouknowtheforceandmass,youcancalculate
thisaccelerationbyusingNewton'sSecondLawofMotion,givenbythefollowingequation:
Force=Mass×Acceleration
Therefore,
Acceleration=Force/Mass
Substitutingtermsfromthedifferentialequationaboveyieldsthefollowingequation:
𝑥=

1
(𝐹 − 𝑐𝑥 − 𝑘𝑥)
𝑚

Youwillconstructasimulationdiagramthatiteratesthefollowingstepsoveraperiodof
time.
CreatingtheSimulationDiagram
YoucreateasimulationdiagrambyplacingaControl&SimulationLoopontheLabVIEW
blockdiagram.
1. LaunchLabVIEWandselectFile»NewVItocreateanew,blankVI.
2. SelectWindow»ShowBlockDiagramtoviewtheblockdiagram.Youalsocanpress
the<Ctrl-E>keystoviewtheblockdiagram.
3. IfyouarenotalreadyviewingtheFunctionspalette,selectView»FunctionsPaletteto
displaythispalette.
4. SelectControlDesign&Simulation»SimulationtoviewtheSimulationpalette.

5. ClicktheControl&SimulationLoopicon.
6. Movethecursorovertheblockdiagram.Clicktoplacethetopleftcorneroftheloop,
dragthecursordiagonallytoestablishthesizeoftheloop,andclickagaintoplace
theloopontheblockdiagram.
ThesimulationdiagramistheareaenclosedbytheControl&SimulationLoop.Noticethe
simulationdiagramhasapaleyellowbackgroundtodistinguishitfromtherestoftheblock
diagram.YoucanresizetheControl&SimulationLoopbydraggingitsborders.
ConfiguringSimulationParameters
TheControl&SimulationLoopcontainstheparametersthatdefinehowthesimulation
executes.Completethefollowingstepstoviewandconfigurethesesimulationparameters.
1. Double-clicktheInputNode,attachedtotheleftsideoftheControl&Simulation
Loop,todisplaytheConfigureSimulationParametersdialogbox.YoualsocanrightTutorial: Control and Simulation in LabVIEW


×