Giáo trình control and simulation in LabVIEW

61 21 0
  • Loading ...
1/61 trang

Thông tin tài liệu

Ngày đăng: 19/11/2019, 00:38

https://www.halvorsen.blog ControlandSimulation inLabVIEW Hans-PetterHalvorsen ControlandSimulationinLabVIEW Hans-PetterHalvorsen Copyright©2017 E-Mail:hans.p.halvorsen@usn.no Web:https://www.halvorsen.blog https://www.halvorsen.blog Preface ThisdocumentexplainsthebasicconceptsofusingLabVIEWforControlandSimulation purposes FormoreinformationaboutLabVIEW,visitmyBlog:https://www.halvorsen.blog Youneedthefollowingsoftware: • • • • • LabVIEW LabVIEWControlDesignandSimulationModule LabVIEWMathScriptRTModule NI-DAQmx NIMeasurement&AutomationExplorer TableofContents Preface 3 TableofContents iv IntroductiontoLabVIEW 1 1.1 Dataflowprogramming 1.2 Graphicalprogramming 1 1.3 Benefits 2 IntroductiontoControlandSimulation 3 IntroductiontoControlandSimulationinLabVIEW 4 3.1 3.1.1 Simulation 3.1.2 ControlDesign 3.2 LabVIEWPIDandFuzzyLogicToolkit 3.2.1 PIDControl 3.2.2 FuzzyLogic 6 3.3 LabVIEWControlDesignandSimulationModule LabVIEWSystemIdentificationToolkit Simulation 8 4.1 SimulationinLabVIEW 4.2 SimulationSubsystem 13 4.3 ContinuousLinearSystems 14 Exercises 19 PIDControl 31 5.1 PIDControlinLabVIEW 32 iv v 5.2 SystemIdentificationinLabVIEW 35 FuzzyLogic 36 8.1 ControlDesigninLabVIEW 34 SystemIdentification 35 7.1 Auto-tuning 33 ControlDesign 34 6.1 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 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 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 IntroductiontoControland Simulation Controldesignisaprocessthatinvolvesdevelopingmathematicalmodelsthatdescribea physicalsystem,analyzingthemodelstolearnabouttheirdynamiccharacteristics,and creatingacontrollertoachievecertaindynamiccharacteristics Simulationisaprocessthatinvolvesusingsoftwaretorecreateandanalyzethebehaviorof dynamicsystems Youusethesimulationprocesstolowerproductdevelopmentcostsby acceleratingproductdevelopment Youalsousethesimulationprocesstoprovideinsight intothebehaviorofdynamicsystemsyoucannotreplicateconvenientlyinthelaboratory Belowweseeaclosed-loopfeedbackcontrolsystem: 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 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 42 LabVIEWMathScript 9.3 Usefulcommands Herearesomeusefulcommands: Command eye(x), eye(x,y) ones(x), ones(x,y) zeros(x), zeros(x,y) diag([x y z]) size(A) A’ Description Identitymatrixoforderx Amatrixwithonlyones Amatrixwithonlyzeros Diagonalmatrix DimensionofmatrixA InverseofmatrixA 9.4 Plotting ThischapterexplainsthebasicconceptsofcreatingplotsinMathScript Topics: • BasicPlotcommands Example:Plotting Functionplotcanbeusedtoproduceagraphfromtwovectorsxandy Thecode: x = 0:pi/100:2*pi; y = sin(x); plot(x,y) Tutorial: Control and Simulation in LabVIEW 10 Discretization Often,weneedtodevelopdiscretealgorithmsofourprocess Inaddition,wemightneedto createourowndiscretePI(D)controller Adiscretelow-passfilterisalsogoodtohave Thereexistslotsofdifferentdiscretizationmethodslikethe“ZeroOrderHold”(ZOH) method,Tustin’smethodandEuler’smethods(ForwardandBackward) Wewillfocuson Eulersmethodsinthisdocument,becausetheyareveryeasytouse EulerForwarddiscretizationmethod: 𝒙≈ 𝒙𝒌=𝟏 − 𝒙𝒌 𝑻𝒔 EulerBackwarddiscretizationmethod: 𝒙≈ 𝒙𝒌 − 𝒙𝒌A𝟏 𝑻𝒔 𝑇C istheSamplingTime 10.1 Low-passFilter Thetransferfunctionforafirst-orderlow-passfiltermaybewritten: 𝑯 𝒔 = 𝒚𝒇 (𝒔) 𝟏 = 𝒚(𝒔) 𝑻𝒇 𝒔 + 𝟏 Where 𝑇G isthetime-constantofthefilter, 𝑦(𝑠) isthefilterinputand 𝑦G 𝑠 isthefilter output Discreteversion: Itcanbeshownthatadiscreteversioncanbestatedas: 𝒚𝒇,𝒌 = 𝟏 − 𝒂 𝒚𝒇,𝒌A𝟏 + 𝒂𝒚𝒌 Where 43 44 𝒂= Discretization 𝑻𝒔 𝑻𝒇 + 𝑻𝒔 Where 𝑇C istheSamplingTime Itisagoldenrulethat 𝑇C ≪ 𝑇G andinpracticeweshouldusethefollowingrule: 𝑇C ≤ 𝑇G Example: Wewillimplementthediscretelow-passfilteralgorithmbelowusingaFormulaNodein LabVIEW: 𝑦G,N = − 𝑎 𝑦G,NAO + 𝑎𝑦N Where 𝑎= 𝑇C 𝑇G + 𝑇C TheBlockDiagrambecomes: TheFrontPanel: Tutorial: Control and Simulation in LabVIEW 45 Discretization ItisagoodideatobuildthisasaSubVIs,andthenwecaneasilyreusetheLow-passfilterin allourapplications Wewilltestthediscretelow-passfilter,tomakesureitworksasexpected: Wecreateasimpletestapplicationwhereweaddsomerandomwhitenoisetoasinesignal Wewillplottheunfilteredandthefilteredsignaltoseeifthelow-passfilterisableto removethenoisefromthesinesignal Wegetthefollowingresults: Tutorial: Control and Simulation in LabVIEW 46 Discretization Weseethatthefilterworksfine Theredlineistheunfilteredsinesignalwithwhitenoise, whiletheredlineisthefilteredresults [EndofExample] 10.2 PIController APIcontrollermaybewritten: 𝒖 𝒕 = 𝒖𝟎 + 𝑲𝒑 𝒆 𝒕 + 𝑲𝒑 𝑻𝒊 𝒕 𝒆𝒅𝝉 𝟎 Where 𝑢 isthecontrolleroutputand 𝑒isthecontrolerror: 𝑒 𝑡 = 𝑟 𝑡 − 𝑦(𝑡) Laplaceversion: 𝑢 𝑠 = 𝐾[ 𝑒 𝑠 + 𝐾[ 𝑒 𝑠 𝑇\ 𝑠 Discreteversion: Westartwith: Tutorial: Control and Simulation in LabVIEW 47 𝑢 𝑡 = 𝑢] + 𝐾[ 𝑒 𝑡 + Discretization 𝐾[ 𝑇\ ` 𝑒𝑑𝜏 ] Inordertomakeadiscreteversionusing,e.g.,Euler,wecanderivebothsidesofthe equation: 𝑢 = 𝑢] + 𝐾[ 𝑒 + 𝐾[ 𝑒 𝑇\ IfweuseEulerForwardweget: 𝑢N − 𝑢NAO 𝑢],N − 𝑢],NAO 𝑒N − 𝑒NAO 𝐾[ = + 𝐾[ + 𝑒 𝑇C 𝑇C 𝑇C 𝑇\ N Thenweget: 𝒖𝒌 = 𝒖𝒌A𝟏 + 𝒖𝟎,𝒌 − 𝒖𝟎,𝒌A𝟏 + 𝑲𝒑 𝒆𝒌 − 𝒆𝒌A𝟏 + 𝑲𝒑 𝑻𝒆 𝑻𝒊 𝒔 𝒌 Where 𝑒N = 𝑟N − 𝑦N Wecanalsosplittheequationabovein2differentparsbysetting: ∆𝑢N = 𝑢N − 𝑢NAO ThisgivesthefollowingPIcontrolalgorithm: 𝒆𝒌 = 𝒓𝒌 − 𝒚𝒌 ∆𝒖𝒌 = 𝒖𝟎,𝒌 − 𝒖𝟎,𝒌A𝟏 + 𝑲𝒑 𝒆𝒌 − 𝒆𝒌A𝟏 + 𝑲𝒑 𝑻𝒆 𝑻𝒊 𝒔 𝒌 𝒖𝒌 = 𝒖𝒌A𝟏 + ∆𝒖𝒌 ThisalgorithmcaneasilybeimplementedinLabVIEWorotherlanguagessuchas,e.g.,C#or MATLAB FormoredetailsabouthowtoimplementthisinC#,seetheTutorial“DataAcquisitionin C#”,availablefromhttps://www.halvorsen.blog LabVIEWExample: BelowwehaveimplementedthediscretePIcontrollerusingaFormulaNodeinLabVIEW: Tutorial: Control and Simulation in LabVIEW 48 Discretization ThePIcontrollerisimplementedasaSubVI,soitiseasytoreusethealgorithminallour applications WetestourdiscretePIcontrollerwiththefollowingapplication: Tutorial: Control and Simulation in LabVIEW 49 Discretization BlockDiagram: [EndofExample] 10.2.1 PIControllerasaState-spacemodel Tutorial: Control and Simulation in LabVIEW 50 Discretization O Weset 𝑧 = 𝑒 ⇒ 𝑠𝑧 = 𝑒 ⇒ 𝑧 = 𝑒 C Thisgives: 𝑧 = 𝑒 𝑢 = 𝐾[ 𝑒 + 𝐾[ 𝑧 𝑇\ Where 𝑒 = 𝑟 − 𝑦 Discreteversion: UsingEuler: 𝑧≈ 𝑧N=O − 𝑧N 𝑇C Where 𝑇C istheSamplingTime Thisgives: 𝑧N=O − 𝑧N = 𝑒N 𝑇C 𝑢N = 𝐾[ 𝑒N + 𝐾[ 𝑧 𝑇\ N Finally: 𝒆𝒌 = 𝒓𝒌 − 𝒚𝒌 𝒖𝒌 = 𝑲𝒑 𝒆𝒌 + 𝑲𝒑 𝒛 𝑻𝒊 𝒌 𝒛𝒌=𝟏 = 𝒛𝒌 + 𝑻𝒔 𝒆𝒌 ThisalgorithmcaneasilybeimplementedinLabVIEWorotherlanguagessuchas,e.g.,C#or MATLAB FormoredetailsabouthowtoimplementthisinC#,seetheTutorial“DataAcquisitionin C#”,availablefromhttps://www.halvorsen.blog 10.3 ProcessModel Tutorial: Control and Simulation in LabVIEW 51 Discretization Wewilluseasimplewatertanktoillustratehowtocreateadiscreteversionofa mathematicalprocessmodel Belowweseeanillustration: Averysimple(linear)modelofthewatertankisasfollows: 𝐴` ℎ = 𝐾[ 𝑢−𝐹fg` or ℎ= 𝐾 𝑢−𝐹fg` 𝐴` [ Where: • • • • ℎ [cm]isthelevelinthewatertank 𝑢 [V]isthepumpcontrolsignaltothepump 𝐴` [cm2]isthecross-sectionalareainthetank 𝐾[ [(cm3/s)/V]isthepumpgain • 𝐹fg` [cm3/s]istheoutflowthroughthevalve(thisoutflowcanbemodeledmore accuratelytakingintoaccountthevalvecharacteristicexpressingtherelation betweenpressuredropacrossthevalveandtheflowthroughthevalve) WecanusetheEulerForwarddiscretizationmethodinordertocreateadiscretemodel: 𝑥≈ 𝑥N=O − 𝑥N 𝑇C Thenweget: ℎN=O − ℎN = 𝐾 𝑢 −𝐹 𝑇C 𝐴` [ N fg` Tutorial: Control and Simulation in LabVIEW 52 Discretization Finally: 𝒉𝒌=𝟏 = 𝒉𝒌 + 𝑻𝒔 𝑲 𝒖 −𝑭 𝑨𝒕 𝒑 𝒌 𝒐𝒖𝒕 Thismodelcaneasilybeimplementedinacomputerusing,e.g.,MATLAB,LabVIEWorC# FormoredetailsforhowtodothisinC#,seetheTutorial“DataAcquisitioninC#” InLabVIEWthiscan,e.g.,beimplementedinaFormulaNodeorMathScriptNode Example: InthisexamplewewillsimulateaBacteriaPopulation InthisexamplewewilluseLabVIEWandtheLabVIEWControlDesignandSimulation Moduletosimulateasimplemodelofabacteriapopulationinajar Themodelisasfollows: birthrate=bx deathrate=px2 Thenthetotalrateofchangeofbacteriapopulationis: 𝑥 = 𝑏𝑥 − 𝑝𝑥 w Wesetb=1/hourandp=0.5bacteria-hourinourexample Wewillsimulatethenumberofbacteriainthejarafter1hour,assumingthatinitiallythere are100bacteriapresent WewillsimulatethesystemusingaForLoopinLabVIEWandimplementthediscretemodel inaFormulaNode Step1:Westartbycreatingthediscretemodel IfweuseEulerForwarddifferentiationmethod: 𝑥≈ 𝑥N=O − 𝑥N 𝑇C Where 𝑇C istheSamplingTime Weget: 𝑥N=O − 𝑥N = 𝑏𝑥N − 𝑝𝑥Nw 𝑇C Tutorial: Control and Simulation in LabVIEW 53 Discretization Thisgives: 𝑥N=O = 𝑥N + 𝑇C (𝑏𝑥N − 𝑝𝑥Nw ) Step2:WeimplementthemodelintheFormulaNodeandcreateaSubVI Step3:WecreatethesimulationprogramusingaForLoop Wegetthefollowingresults(notetheScalingFactorssetintheGraphProperties): Tutorial: Control and Simulation in LabVIEW 54 Discretization [EndofExample] Example: Giventhefollowingmathematicalmodel(nonlinear): 𝑥 = −𝐾O 𝑥 + 𝐾w 𝑢 WewillcreateanewapplicationinLabVIEWwherewesimulatethismodelusingaFormula Nodetoimplementthediscretemodel WewillusetheEulerForwardmethod(becausethisisanonlinearequation): 𝑥≈ 𝑥N=O − 𝑥N 𝑇C Thisgives: 𝑥N=O − 𝑥N = −𝐾O 𝑥 + 𝐾w 𝑢 𝑇C 𝑥N = 𝑥NAO + 𝑇C −𝐾O 𝑥NAO + 𝐾w 𝑢NAO BlockDiagram: Tutorial: Control and Simulation in LabVIEW 55 Discretization FrontPanel: [EndofExample] Tutorial: Control and Simulation in LabVIEW ControlandSimulationinLabVIEW Hans-PetterHalvorsen Copyright©2017 E-Mail:hans.p.halvorsen@usn.no Web:https://www.halvorsen.blog https://www.halvorsen.blog ... arithmeticoperationsonsignals in a simulation system 3.1.2 Control Design The Control Designpalette in LabVIEW: Tutorial: Control and Simulation in LabVIEW Control and Simulation in LabVIEW 3.2 LabVIEW PID and FuzzyLogicToolkit... Belowweseeaclosed-loopfeedback control system: Control and Simulation in LabVIEW LabVIEWhasseveraladditionalmodules and Toolkitsfor Control and Simulation purposes, e.g., LabVIEW Control Design and Simulation Module”, LabVIEW PID and FuzzyLogic... functions,providesplatformindependenthardwareaccesstonumerousdataacquisition and instrumentationdevices TheDAQmxBasedriverisavailablefor LabVIEW onWindows, MacOSX and Linuxplatforms Tutorial: Control and Simulation in LabVIEW Introductionto Control and Simulation
- Xem thêm -

Xem thêm: Giáo trình control and simulation in LabVIEW , Giáo trình control and simulation in LabVIEW

Gợi ý tài liệu liên quan cho bạn