Top Banner
University College of Southeast Norway http://home.hit.no/~hansha System Identification and Estimation in LabVIEW Hans-Petter Halvorsen, 2016-10-19
98

University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

Feb 07, 2018

Download

Documents

vanduong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

UniversityCollegeofSoutheastNorway

http://home.hit.no/~hansha

SystemIdentification andEstimationinLabVIEW

Hans-PetterHalvorsen,2016-10-19

Page 2: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

i

PrefaceThisTutorialwillgothroughthebasicprinciplesofSystemidentificationandEstimationandhowtoimplementthesetechniquesinLabVIEWandLabVIEWMathScript.

LabVIEWisagraphicalprogramminglanguagecreatedbyNationalInstruments,whileLabVIEWMathScriptisanadd-ontoLabVIEW.LabVIEWMathScripthassimilarsyntaxas,e.g.,MATLAB.LabVIEWMathScriptmaybeusedasaseparatepart(andcanbeconsideredasaminiatureversionofMATLAB)orbeintegratedintothegraphicalLabVIEWcodeusingtheMathScriptNode.

Thefollowingmethodswillbediscussed:

StateEstimation:

• KalmanFilter• Observers

ParameterEstimation:

• LeastSquareMethod(LS)

SystemIdentification

• Sub-spacemethods/Black-Boxmethods• PolynomialModelEstimation:ARX/ARMAXmodelEstimation

Software

YouneedthefollowingsoftwareinthisTutorial:

• LabVIEW• LabVIEWControlDesignandSimulationModule• LabVIEWMathScriptRTModule(LabVIEWMathScript)

“LabVIEWControlDesignandSimulationModule”hasfunctionalityforcreatingKalmanFiltersandObservers,butitalsohasfunctionalityforSystemidentification.

Page 3: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

ii

TableofContentsPreface.......................................................................................................................................i

TableofContents......................................................................................................................ii

1 IntroductiontoLabVIEWandMathScript.........................................................................5

1.1 LabVIEW....................................................................................................................5

1.2 LabVIEWMathScript..................................................................................................6

2 LabVIEWControlandSimulationModule.........................................................................9

3 ModelCreationinLabVIEW............................................................................................11

3.1 State-spaceModels.................................................................................................12

3.2 Transferfunctions...................................................................................................15

3.2.1 commonlyusedtransferfunctions..................................................................17

4 IntroductiontoSystemIdentificationandEstimation....................................................24

5 StateEstimationwithKalmanFilter................................................................................26

5.1 State-Spacemodel...................................................................................................27

5.2 Observability...........................................................................................................29

5.3 IntroductiontotheStateEstimator........................................................................30

5.4 StateEstimation......................................................................................................37

5.5 LabVIEWKalmanFilterImplementations................................................................38

6 CreateyourownKalmanFilterfromScratch..................................................................44

6.1 TheKalmanFilterAlgorithm....................................................................................44

6.2 Examples.................................................................................................................45

7 OverviewofKalmanFilterVIs.........................................................................................49

7.1 ControlDesignpalette.............................................................................................49

7.1.1 StateFeedbackDesignsubpalette..................................................................49

7.1.2 Implementationsubpalette.............................................................................50

Page 4: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

iii TableofContents

Tutorial:SystemIdentificationandEstimationinLabVIEW

7.2 Simulationpalette...................................................................................................51

7.2.1 Estimationsubpalette.....................................................................................51

8 StateEstimationwithObserversinLabVIEW..................................................................54

8.1 State-Spacemodel...................................................................................................54

8.2 Eigenvalues..............................................................................................................56

8.3 ObserverGain..........................................................................................................57

8.4 Observability...........................................................................................................58

8.5 Examples.................................................................................................................59

9 OverviewofObserverfunctions......................................................................................64

9.1 ControlDesignpalette.............................................................................................64

9.1.1 StateFeedbackDesignsubpalette..................................................................64

9.1.2 Implementationsubpalette.............................................................................65

9.2 Simulationpalette...................................................................................................66

9.2.1 Estimationsubpalette.....................................................................................66

10 SystemIdentificationinLabVIEW................................................................................69

10.1 ParameterEstimationwithLeastSquareMethod(LS)...........................................70

10.2 SystemIdentificationusingSub-spacemethods/Black-Boxmethods.....................73

10.3 SystemIdentificationusingPolynomialModelEstimation:ARX/ARMAXmodelEstimation...........................................................................................................................74

10.4 GeneratemodelData..............................................................................................75

10.4.1 Excitationsignals.............................................................................................77

11 OverviewofSystemIdentificationfunctions..............................................................80

12 SystemIdentificationExample....................................................................................85

Page 5: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

4

PartI:Introduction

Page 6: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

5

1 IntroductiontoLabVIEWandMathScript

InthisTutorialwewilluseLabVIEWandsomeoftheadd-onmodulesavailableforLabVIEW.

• LabVIEW• LabVIEWMathScriptRTModule• LabVIEWControlDesignandSimulationModule

1.1 LabVIEWLabVIEW(shortforLaboratoryVirtualInstrumentationEngineeringWorkbench)isaplatformanddevelopmentenvironmentforavisualprogramminglanguagefromNationalInstruments.Thegraphicallanguageisnamed"G".LabVIEWiscommonlyusedfordataacquisition,instrumentcontrol,andindustrialautomation.Thecodefileshavetheextension“.vi”,whichisanabbreviationfor“VirtualInstrument”.LabVIEWofferslotsofadditionalAdd-onsandToolkits.

FormoreinformationaboutLabVIEW,pleasereadtheTutorial“IntroductiontoLabVIEW”.

IalsorecommendyouvisitmyBlog:http://home.hit.no/~hansha/andvisitNationalInstrumentsatwww.ni.com.

Page 7: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

6 IntroductiontoLabVIEWandMathScript

Tutorial:SystemIdentificationandEstimationinLabVIEW

1.2 LabVIEWMathScriptMathScriptisahigh-level,text-basedprogramminglanguage.MathScriptincludesmorethan800built-infunctionsandthesyntaxissimilartoMATLAB.Youmayalsocreatecustom-madem-filelikeyoudoinMATLAB.

MathScriptisanadd-onmoduletoLabVIEWbutyoudon’tneedtoknowLabVIEWprogramminginordertouseMathScript.

MathScriptisanadd-onmoduletoLabVIEWbutyoudon’tneedtoknowLabVIEWprogramminginordertouseMathScript.

FormoreinformationaboutMathScript,pleasereadtheTutorial“LabVIEWMathScript”.

HowdoyoustartusingMathScript?

YouneedtoinstallLabVIEWandtheLabVIEWMathScriptRTModule.Whennecessarysoftwareisinstalled,startMathScriptbyopenLabVIEW.IntheGettingStartedwindow,selectTools->MathScriptWindow...:

Page 8: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

7 IntroductiontoLabVIEWandMathScript

Tutorial:SystemIdentificationandEstimationinLabVIEW

FormoreinformationaboutMathScript,pleasereadtheTutorial“LabVIEWMathScript”.

MathScriptNode:

YoumayalsouseMathScriptCodedirectlyinsideandcombinedwithyougraphicalLabVIEWcode,forthisyouusethe“MathScriptNode”.Withthe“MathScriptNode”youcancombinegraphicalandtextualcodewithinLabVIEW.Thefigurebelowshowsthe“MathScriptNode”ontheblockdiagram,representedbythebluerectangle.Using“MathScriptNodes”,youcanenter.mfilescripttextdirectlyorimportitfromatextfile.

YoucandefinenamedinputsandoutputsontheMathScriptNodebordertospecifythedatatotransferbetweenthegraphicalLabVIEWenvironmentandthetextualMathScriptcode.

Youcanassociate.mfilescriptvariableswithLabVIEWgraphicalprogramming,bywiringNodeinputsandoutputs.Thenyoucantransferdatabetween.mfilescriptswithyourgraphicalLabVIEWprogramming.Thetextual.mfilescriptscannowaccessfeaturesfromtraditionalLabVIEWgraphicalprogramming.

TheMathScriptNodeisavailablefromLabVIEWfromtheFunctionsPalette:Mathematics→Scripts&Formulas

Page 9: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

8 IntroductiontoLabVIEWandMathScript

Tutorial:SystemIdentificationandEstimationinLabVIEW

IfyouclickCtrl+HyougethelpabouttheMathScriptNode:

Click“Detailedhelp”inordertogetmoreinformationabouttheMathScriptNode.

Page 10: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

9

2 LabVIEWControlandSimulationModule

LabVIEWhasseveraladditionalmodulesandToolkitsforControlandSimulationpurposes,e.g.,“LabVIEWControlDesignandSimulationModule”,“LabVIEWPIDandFuzzyLogicToolkit”,“LabVIEWSystemIdentificationToolkit”and“LabVIEWSimulationInterfaceToolkit”.LabVIEWMathScriptisalsousefulforControlDesignandSimulation.

• LabVIEWControlDesignandSimulationModule• LabVIEWPIDandFuzzyLogicToolkit• LabVIEWSystemIdentificationToolkit• LabVIEWSimulationInterfaceToolkit

BelowweseetheControlDesign&SimulationpaletteinLabVIEW:

InthisTutorialwewillfocusontheVIsusedforParameterandStateestimationandespeciallytheuseofObserversandKalmanFilterforStateestimation.

IfyouwanttolearnmoreaboutSimulation,SimulationLoop,blockdiagramsandPIDcontrol,etc.,IrefertotheTutorial“ControlandSimulationinLabVIEW”ThisTutorialisavailablefromhttp://home.hit.no/~hansha/.

InthisTutorialwewillneedthefollowingsubpalettesintheControlDesignandSimulationpalette:

• ControlDesign• SystemIdentification• Simulation

BelowweseetheControlDesignpalette:

Page 11: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

10 LabVIEWControlandSimulationModule

Tutorial:SystemIdentificationandEstimationinLabVIEW

BelowweseetheSystemIdentificationpalette:

BelowweseetheSimulationpalette:

Inthenextchapterswewillgoindetailanddescribethedifferentsubpalettesinthesepalettesandexplainthefunctions/SubVIswewillneedforSystemidentificationandEstimation.

Page 12: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

11

3 ModelCreationinLabVIEWWhenyouhavefoundthemathematicalmodelforyoursystem,thefirststepistodefine/orcreateyourmodelinLabVIEW.YourmodelcanbeaTransferfunctionoraState-spacemodel.

InLabVIEWandthe“LabVIEWControlDesignandSimulationModule”youcancreatedifferentmodels,suchasState-spacemodelsandtransferfunctions,etc.

IntheControlDesignpalette,wehaveseveralsubpalettesthatdealswithmodels,theseare:

• ModelConstruction• ModelInformation• ModelConversion• ModelInterconnection

BelowwegothroughthedifferentsubpalettesandthemostusedVIsinthesepalettes.

“ModelConstruction”Subpalette:

InthispalettewehaveVIsforcreatingstate-spacemodelsandtransferfunctions.

Page 13: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

12 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

→UsetheModelConstructionVIstocreatelinearsystemmodelsandmodifythepropertiesofasystemmodel.YoualsocanusetheModelConstructionVIstosaveasystemmodeltoafile,readasystemmodelfromafile,orobtainavisualrepresentationofamodel.

SomeofthemostusedVIswouldbe:

CDConstructState-SpaceModel.vi

CDConstructTransferFunctionModel.vi

TheseVIsandsomeothersareexplainedbelow.

3.1 State-spaceModelsGiventhefollowingState-spacemodel:

𝑥 = 𝐴𝑥 + 𝐵𝑢

𝑦 = 𝐶𝑥 + 𝐷𝑢

InLabVIEWweusethe“CDConstructState-SpaceModel.vi”tocreateaState-spacemodel:

Page 14: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

13 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

YoumayusenumericvaluesinthematricesA,B,CandDorsymbolicvaluesbyselectingether“Numeric”or“Symbolic”:

Numeric Symbolic

Example:CreateState-Spacemodel

BlockDiagram:

FrontPanel:

Page 15: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

14 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

The“CDDrawState-SpaceEquation.vi”canbeusedtoseeagraphicalrepresentationoftheState-spacemodel.

Example:CreateSISO/MIMOState-Spacemodels

SISOModel(SingleInput,SingleOutput):

SIMOModel(SingleInput,MultipleOutput):

MISOModel(MultipleInput,SingleOutput):

MIMOModel(MultipleInput,MultipleOutput):

Page 16: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

15 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

[EndofExample]

3.2 TransferfunctionsGiventhefollowingTransferfunction:

𝐻 𝑠 =𝑛𝑢𝑚𝑒𝑟𝑎𝑡𝑜𝑟𝑑𝑒𝑛𝑜𝑚𝑖𝑛𝑎𝑡𝑜𝑟 =

𝑏6 + 𝑏7𝑠 + 𝑏8𝑠8 +⋯𝑎6 + 𝑎7𝑠 + 𝑎8𝑠8 +⋯

InLabVIEWweusethe“CDConstructTransferFunctionModel.vi”tocreateaTransferFunction:

Example:TransferFunction

BlockDiagram:

Page 17: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

16 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

FrontPanel:

[EndofExample]

Example:TransferFunctionwithSymbolicvalues

BlockDiagram:

FrontPanel:

Page 18: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

17 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

[EndofExample]

3.2.1 commonlyusedtransferfunctions

Forcommonlyusedtransferfunctionswecanusethe“CDConstructSpecialTFModel.vi”:

1.ordersystem:

Thetransferfunctionfora1.ordersystemisasfollows:

𝐻 𝑠 =𝐾

𝑇𝑠 + 1 𝑒>?@

Where

𝐾 isthegain

TistheTimeconstant

𝜏 istheTimedelay

Selectthe polymorphicinstanceonthe“CDConstructSpecialTFModel.vi”:

Page 19: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

18 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

2.ordersystem:

Thetransferfunctionfora2.ordersystemisasfollows:

𝐻 𝑠 =𝐾𝜔68

𝑠8 + 2𝜁𝜔6𝑠 + 𝜔68=

𝐾𝑠𝜔6

8+ 2𝜁 𝑠

𝜔6+ 1

Where

• 𝐾 isthegain• 𝜁 zetaistherelativedampingfactor• 𝜔6[rad/s]istheundampedresonancefrequency.

Selectthe polymorphicinstanceonthe“CDConstructSpecialTFModel.vi”:

TimedelayasaPade’approximation:

Time-delaysareverycommonincontrolsystems.TheTransferfunctionofatime-delayis:

𝐻 𝑠 = 𝑒>?@

Page 20: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

19 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

Insomesituationsitisnecessarytosubstitute 𝑒>?@ withanapproximation,e.g.,thePadé-approximation:

𝑒>?@ ≈1 − 𝑘7𝑠 + 𝑘8𝑠8 + ⋯± 𝑘I𝑠I

1 + 𝑘7𝑠 + 𝑘8𝑠8 + ⋯+ 𝑘I𝑠I

Selectthe polymorphicinstanceonthe“CDConstructSpecialTFModel.vi”:

“ModelInformation”Subpalette:

→UsetheModelInformationVIstoobtainorsetparameters,data,andnamesofasystemmodel.Modelinformationincludespropertiessuchasthesystemdelay,systemdimensions,samplingtime,andnamesofinputs,outputs,andstates.

“ModelConversion”Subpalette:

Page 21: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

20 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

→UsetheModelConversionVIstoconvertasystemmodelfromonerepresentationtoanother,fromacontinuous-timetoadiscrete-timemodel,orfromadiscrete-timetoacontinuous-timemodel.YoualsocanusetheModelConversionVIstoconvertacontroldesignmodelintoasimulationmodelorasimulationmodelintoacontroldesignmodel.

SomeofthemostusedVIsinthe“ModelConversion”subpaletteare:

CDConverttoState-SpaceModel.vi

CDConverttoTransferFunctionModel.vi

CDConvertContinuoustoDiscrete.vi

TheseVIsandsomeothersareexplainedbelow.

ConverttoState-SpaceModels:

Example:ConvertfromTransferfunctiontoState-Spacemodel

BlockDiagram:

Page 22: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

21 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

FrontPanel:

[EndofExample]

ConverttoTransferFunctions:

Example:ConvertfromState-SpacemodeltoTransferFunction

BlockDiagram:

FrontPanel:

Page 23: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

22 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

[EndofExample]

ConvertContinuoustoDiscreteModel:

Example:ConvertfromContinuousState-SpacemodeltoDiscreteState-Spacemodel

BlockDiagram:

FrontPanel:

Page 24: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

23 ModelCreationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

[EndofExample]

“ModelInterconnection”Subpalette:

→UsetheModelInterconnectionVIstoperformdifferenttypesoflinearsysteminterconnections.Youcanbuildalargesystemmodelbyconnectingsmallersystemmodelstogether.

Page 25: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

24

4 IntroductiontoSystemIdentificationandEstimation

ThisTutorialwillgothroughthebasicprinciplesofSystemidentificationandEstimationandhowtoimplementthesetechniquesinLabVIEW.

Thefollowingmethodswillbediscussedinthenextchapters:

StateEstimation:

• KalmanFilter• Observers

SystemIdentification:

• ParameterEstimationandtheLeastSquareMethod(LS)• Sub-spacemethods/Black-Boxmethods• PolynomialModelEstimation:ARX/ARMAXmodelEstimation

ThenextchapterswillgothroughthebasictheoryandshowhowitcouldbeimplementedinLabVIEWandMathScript.

Page 26: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

25

PartII:Estimation

Page 27: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

26

5 StateEstimationwithKalmanFilterKalmanFilterisacommonlyusedmethodtoestimatethevaluesofstatevariablesofadynamicsystemthatisexcitedbystochastic(random)disturbancesandstochastic(random)measurementnoise.

TheKalmanFilterisastateestimatorwhichproducesanoptimalestimateinthesensethatthemeanvalueofthesumoftheestimationerrorsgetsaminimalvalue.

BelowweseeasketchofhowaKalmanFilterisworking:

Theestimator(modelofthesystem)runsinparallelwiththesystem(realsystemormodel).Themeasurement(s)isusedtoupdatetheestimator.

LabVIEWControlDesignandSimulationModulehavelotsoffunctionalityforStateEstimationusingKalmanFilters.Thefunctionalitywillbeexplainedindetailinthenextchapters.

BelowweseetheDiscreteKalmanFilterimplementationinLabVIEW:

TheKalmanFilterfornonlinearmodelsiscalledthe“ExtendedKalmanFilter”.

Page 28: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

27 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

5.1 State-SpacemodelGiventhecontinuouslinearstatespace-model:

𝑥 = 𝐴𝑥 + 𝐵𝑢

𝑦 = 𝐶𝑥 + 𝐷𝑢

Orgiventhediscretelinearstatespace-model

𝑥JK7 = 𝐴𝑥J + 𝐵𝑢J

𝑦J = 𝐶𝑥J + 𝐷𝑢J

LabVIEW:

InLabVIEWwemayusethe“CDConstructState-SpaceModel.vi”tocreateaState-spacemodel:

Note!IfyouspecifyadiscreteState-spacemodelyouhavetospecifytheSamplingTime.

LabVIEWExample:CreateaState-spacemodel

BlockDiagram:

Thematrices𝐴, 𝐵, 𝐶 and 𝐷 maybedefinedontheFrontPanellikethis:

Page 29: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

28 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

[EndofExample]

Discretization:

Ifyouhaveacontinuousmodelandwanttoconvertittothediscretemodel,youmayusetheVI“CDConvertContinuoustoDiscrete.vi”inLabVIEW:

LabVIEWFunctionsPalette:ControlDesign&Simulation→ControlDesign→ModelConversion→CDConvertContinuoustoDiscrete.vi

LabVIEWExample:ConvertfromContinuoustoDiscretemodel

Note!WehavetospecifytheSamplingTime.

[EndofExample]

MathScript:

UsethessfunctioninMathScripttodefineyourmodel(ortfifyouhaveatransferfunction).Usethec_to_dfunctiontoconvertacontinuousmodeltoadiscretemodel.

MathScriptExample:

Page 30: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

29 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

GiventhefollowingState-spacemodel:

𝑥7𝑥8

=0 1

−𝑘𝑚 −

𝑐𝑚

N

𝑥7𝑥8 +

01𝑚O

𝑢

𝑦 = 1 0P

𝑥7𝑥8

ThefollowingMathScriptCodecreatesthismodel:

c=1; m=1; k=1; A = [0 1; -k/m -c/m]; B = [0; 1/m]; C = [1 0]; ssmodel = ss(A, B, C)

Ifyouwanttofindthediscretemodelusethec_to_dfunction:

Ts=0.1 % Sampling Time discretemodel = c_to_d(ssmodel, Ts)

[EndofExample]

5.2 ObservabilityAnecessaryconditionfortheKalmanFiltertoworkcorrectlyisthatthesystemforwhichthestatesaretobeestimated,isobservable.Therefore,youshouldcheckforObservabilitybeforeapplyingtheKalmanFilter.

TheObservabilitymatrixisdefinedas:

𝑂 =𝐶𝐶𝐴⋮

𝐶𝐴I>7

Wherenisthesystemorder(numberofstatesintheState-spacemodel).

→Asystemofordernisobservableif 𝑶 isfullrank,meaningtherankof 𝑶 isequalton.

LabVIEW:

TheLabVIEWControlDesignandSimulationModulehaveaVI(ObservabilityMatrix.vi)forfindingtheObservabilitymatrixandcheckifastates-pacemodelisObservable.

Page 31: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

30 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

LabVIEWFunctionsPalette:ControlDesign&Simulation→ControlDesign→State-SpaceModelAnalysis→CDObservabilityMatrix.vi

Note!InLabVIEW 𝑁 isusedasasymbolfortheObservabilitymatrix.

LabVIEWExample:CheckforObservability

[EndofExample]

MathScript:

InMathScriptyoumayusetheobsvmxfunctiontofindtheObservabilitymatrix.YoumaythenusetherankfunctioninordertofindtherankoftheObservabilitymatrix.

MathScriptExample:

ThefollowingMathScriptCodecheckforObservability:

% Check for Observability: O = obsvmx (discretemodel) r = rank(O)

[EndofExample]

5.3 IntroductiontotheStateEstimator

Page 32: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

31 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

ContinuousModel:

Giventhecontinuouslinearstatespacemodel:

𝑥 = 𝐴𝑥 + 𝐵𝑢 + 𝐺𝑤

𝑦 = 𝐶𝑥 + 𝐷𝑢 + 𝐻𝑣

oringeneral:

𝑥 = 𝑓(𝑥, 𝑢) + 𝐺𝑤

𝑦 = 𝑔(𝑥, 𝑢) + 𝐻𝑣

DiscreteModel:

Giventhediscretelinearstatespacemodel:

𝑥JK7 = 𝐴𝑥J + 𝐵𝑢J + 𝐺𝑤J

𝑦J = 𝐶𝑥J + 𝐷𝑢J + 𝐻𝑣J

oringeneral:

𝑥JK7 = 𝑓(𝑥J, 𝑢J) + 𝐺𝑤J

𝑦J = 𝑔(𝑥J, 𝑢J) + 𝐻𝑣J

Where 𝑣 isuncorrelatedwhiteprocessnoisewithzeromeanandcovariancematrix 𝑄 andwisuncorrelatedwhitemeasurementsnoisewithzeromeanandcovariancematrix ,i.e.suchthat

𝑄 = 𝐸{𝑤𝑤`}

𝑅 = 𝐸{𝑣𝑣`}

𝐸{𝑤} istheexpectedvalueormeanoftheprocessnoisevector.

𝐸{𝑣}istheexpectedvalueormeanofthemeasurementnoisevector.

Itisnormaltolet 𝑄 and 𝑅 bediagonalmatrices:

𝑄 =

𝑞77 0 0 00 𝑞88 0 00 0 ⋱ 00 0 0 𝑞II

, 𝑅 =

𝑟77 0 0 00 𝑟88 0 00 0 ⋱ 00 0 0 𝑟ee

Page 33: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

32 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

𝐺 istheprocessnoisegainmatrix,andyounormallyset 𝐺 equaltotheIdentitymatrix 𝐼:

𝐺 =

1 0 0 00 1 0 00 0 ⋱ 00 0 0 1

𝐻 isthemeasurementnoisegainmatrix,andyounormallyset 𝐻 = 0

StateEstimator:

Thestateestimatorisgivenby:

𝑥JK7 = 𝐴𝑥J + 𝐵𝑢J + 𝐾(𝑦 − 𝑦)

𝑦J = 𝐶𝑥J

Where 𝐾 istheKalmanFilterGain

Itcanbefoundthat:

𝐾 = 𝑋𝐶`𝑄>7

Where 𝑋 isthesolutiontotheRiccatiequation.ItiscommontousethesteadystatesolutionoftheRiccatiequation(AlgebraicRiccatiEquation),i.e., 𝑋 = 0.

Note! 𝑄 and 𝑅 isusedastuning/weightingmatriceswhendesigningtheKalmanFilterGain 𝐾.

BelowweseeaBlockDiagramofa(discrete)KalmanFilter/StateEstimator:

Page 34: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

33 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

LabVIEW:

InLabVIEWDesignandSimulationModuleweusethe“CDKalmanGain.vi”inordertofindK:

LabVIEWFunctionsPalette:ControlDesign&Simulation→ControlDesign→StateFeedbackDesign→CDKalmanGain.vi

Note!InLabVIEW 𝐿 isusedasasymbolfortheKalmanFilterGainmatrix.

Note!The“KalmanFilterGain.vi”ispolymorphic,dependingonwhatkindofmodel(deterministic/stochasticorcontinuous/discrete)youwiretothisVI,theinputschangesautomaticallyoryoumayusethepolymorphicselectorbelowtheVI:

Page 35: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

34 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

DeterministicandContinuous:

StochasticandContinuous:

DeterministicandDiscrete:

StochasticandDiscrete:

LabVIEWExample:FindtheKalmanGain

BlockDiagram:

FrontPanel:

Page 36: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

35 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

[EndofExample]

MathScript:

Usethefunctionskalman,kalman_dorlqetofindtheKalmangainmatrix.

Function Description Example

kalman Calculatestheoptimalsteady-stateKalmangainKthatminimizesthecovarianceofthestateestimationerror.YoucanusethisfunctiontocalculateKforcontinuousanddiscretesystemmodels.

>>[SysKal, K]=kalman(ssmodel, Q, R)

kalman_d Calculatestheoptimalsteady-stateKalmangainKthatminimizesthecovarianceofthestateestimationerror.Theinputsystemandnoisecovariancearebasedonacontinuoussystem.Alloutputsarebasedonadiscretizedsystem,whichisbasedonthesamplerateTs.

[SysKalDisc, K]=kalman_d(ssmodel, Q, R, Ts)

lqe Calculatestheoptimalsteady-stateestimatorgainmatrixKforacontinuousstate-spacemodeldefinedbymatricesAandC.

K=lqe(A,G,C,Q,R)

MathScriptExample:FindKalmanGainusingtheKalmanfunction

Page 37: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

36 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

MathScriptCodeforfindingthesteadystateKalmanGain:

% Define the State-space model: c=1; m=1; k=1; A = [0 1; -k/m -c/m]; B = [0; 1/m]; C = [1 0]; ssmodel = ss(A, B, C); % Discrete model: Ts=0.1; % Sampling Time discretemodel = c_to_d(ssmodel, Ts); % Check for Observability: O = obsvmx(discretemodel); r = rank(O); % Find Kalman Gain Q=[0.01 0; 0 0.01]; R=[0.01]; [SysKal, K]=kalman(discretemodel, Q, R); K

TheOutputis:

K = 0.64435 0.10759

[EndofExample]

MathScriptExample:FindKalmanGainusingtheKalmanfunction

MathScriptCodeforfindingthesteadystateKalmanGain:

% Define the State-space model: c=1; m=1; k=1; A = [0 1; -k/m -c/m]; B = [0; 1/m]; G=[1 0 ; 0 1]; C = [1 0]; % Find Kalman Gain Q=[0.01 0; 0 0.01]; R=[0.01]; K=lqe(A,G,C,Q,R)

TheOutputis:

K = 0.86121 -0.12916

Page 38: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

37 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

[EndofExample]

5.4 StateEstimationLabVIEWControlDesignandSimulationModulehavebuilt-infunctionalityforStateEstimationusingtheKalmanFilter.

InthenextsectionwewillcreateourownKalmanFilterStateEstimationalgorithm.

LabVIEW:

TherearedifferentfunctionsandVIsforfindingtheStateEstimationusingtheKalmanFilter.

CDStateEstimator.vi:

LabVIEWFunctionsPalette:ControlDesign&Simulation→ControlDesign→StateFeedbackDesign→CDStateEstimator.vi

BelowweshowanexampleofhowtousetheCDStateEstimator.viinLabVIEW.

LabVIEWExample:StateEstimatorSimulation

Giventhefollowingmodel:

𝑥7𝑥8

= −0.2 0.50 −0.1

N

𝑥7𝑥8 + 0

1O

𝑢

𝑦 = 1 0P

𝑥7𝑥8 + 0

k𝑢

Wewillusethe“CDStateEstimator.vi”inLabVIEW.

BlockDiagrambecomesasfollows:

Page 39: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

38 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

Note!Wehaveused“CDInitialResponse.vi”forplottingtheresponse.TheVIislocatedintheLabVIEWFunctionsPalette:ControlDesign&Simulation→ControlDesign→TimeResponse→CDInitialResponse.vi

Theresultbecomesasfollows:

Weseetheestimatesaregood.

MathScript:

InMathScriptwemayusethebuilt-inestimatorfunction.

5.5 LabVIEWKalmanFilterImplementationsLabVIEWDesignandSimulationModulehaveseveralbuilt-inversionsoftheKalmanFilter;herewewillinvestigatesomeofthem.

TheControlDesign→ImplementationpaletteinLabVIEW:

Page 40: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

39 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

Herewehavethe“CDDiscreteKalmanFilter”.

Simulation→EstimationpaletteinLabVIEW:

Herewehaveimplementationsfor:

• ContinuousKalmanFilter• ContinuousExtendedKalmanFilter• DiscreteKalmanFilter• DiscreteExtendedKalmanFilter

Wewillgothroughthe“DiscreteKalmanFilter”indetailandshowsomeexamples.

DiscreteKalmanFilter:

LabVIEWFunctionsPalette:ControlDesign&Simulation→Simulation→Estimation→DiscreteKalmanFilter

Page 41: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

40 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

Bydefaultyouneedtowiretheinput(𝑢)andoutput(𝑦)vectors:

InordertoConfiguretheblockyouright-clickonitandselect“Configuration…”

IntheConfigurationwindowyoucanenteryourmodelparameters:

Page 42: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

41 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

Ifyouselect“Terminal”inthe“Parametersource”youmaycreateyourmodelinLabVIEWcodelikethis:

LabVIEWExample:DiscreteKalmanFilter

Giventhefollowinglinearstate-spacemodelofawatertank:

𝑥7𝑥8

= 0 −100 0

N

𝑥7𝑥8 + 0.02

0O

𝑢

Page 43: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

42 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

𝑦 = 1 0P

𝑥7𝑥8 + 0

k𝑢

Where 𝑥7 isthelevelinthetank,while 𝑥8 istheoutflowofthetank.Onlythelevel 𝑥7 ismeasured.

Step1:Firstwecreatethemodel:

WhereA,B,DandDisdefinedaccordingtothestate-spacemodelabove:

Note!TheDiscreteKalmanFilterfunctioninLabVIEWrequiresastochasticstate-spacemodel,sowehavetocreateastochasticstate-spacemodelorconvertourstate-spacemodelintoastochasticstate-spacemodelasdoneintheLabVIEWcodeabove.

Step2:ThenweusetheDiscreteKalmanFilterfunctioninLabVIEWonourmodel:

Page 44: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

43 StateEstimationwithKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

TheDiscreteKalmanFilterfunctionalsorequiresaNoisemodel,sowecreateanoisemodelfromour 𝑄 and 𝑅 matricesasdoneintheLabVIEWcodeabove.

Theresultsareasfollows:

Weseetheresultisverygood.

[EndofExample]

Page 45: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

44

6 CreateyourownKalmanFilterfromScratch

InthischapterwewillcreateourownKalmanFilterAlgorithmfromscratch.

6.1 TheKalmanFilterAlgorithmLabVIEWDesignandSimulationModulehaveseveralbuilt-inversionsoftheKalmanFilter,butinthischapterwewillcreateourownKalmanFilteralgorithm.

HereisastepbystepKalmanFilteralgorithmwhichcanbedirectlyimplementedinaprogramminglanguage,suchasLabVIEW.Youmay,e.g.,implementitinstandardLabVIEWcodeoraFormulaNodeinLabVIEW.

PreStep:FindthesteadystateKalmanGainK

Kistime-varying,butyounormallyimplementthesteadystateversionofKalmanGainK.Usethe“CDKalmanGain.vi”inLabVIEWoroneofthefunctionskalman,kalman_dorlqeinMathScript.

InitStep:SettheinitialApriori(Predicted)stateestimate

𝑥6 = 𝑥6

Step1:FindMeasurementmodelupdate

𝑦J = 𝑔(𝑥J, 𝑢J)

ForLinearState-spacemodel:

𝑦J = 𝐶𝑥J + 𝐷𝑢J

Step2:FindtheEstimatorError

𝑒J = 𝑦J − 𝑦J

Step3:FindtheAposteriori(Corrected)stateestimate

𝑥J = 𝑥J + 𝐾𝑒J

WhereKistheKalmanFilterGain.UsethesteadystateKalmanGainorcalculatethetime-varyingKalmanGain.

Step4:FindtheApriori(Predicted)stateestimateupdate

𝑥JK7 = 𝑓(𝑥J, 𝑢J)

Page 46: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

45 CreateyourownKalmanFilterfromScratch

Tutorial:SystemIdentificationandEstimationinLabVIEW

ForLinearState-spacemodel:

𝑥JK7 = 𝐴𝑥J + 𝐵𝑢J

Step1-4goesinsidealoopinyourprogram.

Thisisthealgorithmwewillimplementintheexamplebelow.

6.2 ExamplesLabVIEWExample:KalmanFilteralgorithm

Giventhefollowinglinearstate-spacemodelofawatertank:

𝑥7𝑥8

= 0 −100 0

N

𝑥7𝑥8 + 0.02

0O

𝑢

𝑦 = 1 0P

𝑥7𝑥8 + 0

k𝑢

Where 𝑥7 isthelevelinthetank,while 𝑥8 istheoutflowofthetank.Onlythelevel 𝑥7 ismeasured.

FirstwehavetofindthesteadystateKalmanFilterGainandcheckforObservability:

Thenweruntherealprocess(orsimulatedmodel)inparallelwiththeKalmanFilterinordertofindestimatesfor 𝑥7 and 𝑥8:

Page 47: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

46 CreateyourownKalmanFilterfromScratch

Tutorial:SystemIdentificationandEstimationinLabVIEW

InthiscasewehaveusedaSimulationLoop,butaWhileLoopwilldothesame.

Blocks/SubVIs:

Realprocess/Simulatedprocess:

Hereweeitherhaveamodelofthesystemorread/writedatafromtherealprocessusingaDAQcard,e.g.,USB-6008fromNationalInstruments.

ImplementationoftheKalmanFilterAlgorithm:

TheBlockDiagramisasfollows:

Page 48: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

47 CreateyourownKalmanFilterfromScratch

Tutorial:SystemIdentificationandEstimationinLabVIEW

Thisisageneralimplementationandwillworkforalllineardiscretesystems.

Theresultsareasfollows:

Page 49: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

48 CreateyourownKalmanFilterfromScratch

Tutorial:SystemIdentificationandEstimationinLabVIEW

[EndofExample]

Page 50: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

49

7 OverviewofKalmanFilterVIsInLabVIEWthereareseveralVIsandfunctionsusedforKalmanFilterimplementations.

7.1 ControlDesignpaletteInthe“ControlDesign”palettewefindsubpalettesfor“StateFeedbackDesign”and“Implementation”:

7.1.1 StateFeedbackDesignsubpalette

Inthe“StateFeedbackDesign”subpalettewefindVIsforcalculationtheKalmanGain,etc.

→UsetheStateFeedbackDesignVIstocalculatecontrollerandobservergainsforclosed-loopstatefeedbackcontrolortoestimateastate-spacemodel.YoualsocanuseState

Page 51: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

50 OverviewofKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

FeedbackDesignVIstoconfigureandteststate-spacecontrollersandstateestimatorsintimedomains.

KalmanFilterGainVI:

7.1.2 Implementationsubpalette

Inthe“Implementation”subpalettewefindVIsforimplementingadiscreteObserverandadiscreteKalmanFilter.

→UsetheImplementationVIsandfunctionstosimulatethedynamicresponseofadiscretesystemmodel,deployadiscretemodeltoareal-timetarget,implementadiscreteKalmanfilter,andimplementcurrentandpredictiveobservers.

DiscreteKalmanFilter:

Page 52: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

51 OverviewofKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

7.2 SimulationpaletteInthe“Simulation”palettewefindthe“Estimation“subpalette:

7.2.1 Estimationsubpalette

Inthe“Estimation”palettewefindVIsforimplementingacontinuous/discreteKalmanFilter.

Page 53: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

52 OverviewofKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

→UsetheEstimationfunctionstoestimatethestatesofastate-spacesystem.Thestate-spacesystemcanbedeterministicorstochastic,continuousordiscrete,linearornonlinear,andcompletelyorpartiallyobservable.

ContinuousKalmanFilterVIs:

Page 54: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

53 OverviewofKalmanFilter

Tutorial:SystemIdentificationandEstimationinLabVIEW

DiscreteKalmanFilterVIs:

Page 55: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

54

8 StateEstimationwithObserversinLabVIEW

ObserversareanalternativetotheKalmanFilter.AnObserverisanalgorithmforestimatingthestatevariablesinasystembasedonamodelofthesystem.ObservershavethesamestructureasaKalmanFilter.

InObserversyouspecifyhowfastandstableyouwanttheestimatestoconvergetotherealvalues,i.e.,youspecifytheeigenvaluesofthesystem.BasedontheeigenvaluesyouwillfindtheObservergainKthatisusedtoupdatetheestimates.

OnesimplewaytofindtheeigenvaluesistousetheButterwortheigenvaluesfromtheButterworthpolynomial.WhenwehavefoundtheeigenvalueswecanthenusetheAckermaninordertofindtheObservergain.

LabVIEWControlDesignandSimulationModulehavelotsoffunctionalityforStateEstimationusingObservers.Thefunctionalitywillbeexplainedindetailinthenextchapters.

8.1 State-SpacemodelGiventhecontinuouslinearstatespace-model:

𝑥 = 𝐴𝑥 + 𝐵𝑢

𝑦 = 𝐶𝑥 + 𝐷𝑢

Orgiventhediscretelinearstatespace-model

𝑥JK7 = 𝐴𝑥J + 𝐵𝑢J

𝑦J = 𝐶𝑥J + 𝐷𝑢J

Page 56: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

55 StateEstimationwithObserversinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

LabVIEW:

InLabVIEWwemayusethe“CDConstructState-SpaceModel.vi”tocreateaState-spacemodel:

Note!IfyouspecifyadiscreteState-spacemodelyouhavetospecifytheSamplingTime.

LabVIEWExample:CreateaState-spacemodel

BlockDiagram:

Thematrices𝐴, 𝐵, 𝐶 and 𝐷 maybedefinedontheFrontPanellikethis:

[EndofExample]

Page 57: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

56 StateEstimationwithObserversinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

8.2 EigenvaluesOnesimplewaytofindtheeigenvaluesistousetheButterwortheigenvaluesfromtheButterworthpolynomial.

ButterwortPolynomial:

TheButterworthPolynomialisdefinedas:

𝐵I 𝑠 = 𝑎I𝑠I +…+ 𝑎8𝑠8 + 𝑎7𝑠 + 1

where 𝑎6 = 1, 𝑎7, 𝑎8, … , 𝑎I arethecoefficientsintheButterworthPolynomial.

Herewewillusea2.orderButterworthPolynomial,whichisdefinedas:

𝐵8 𝑠 = 𝑎8𝑠8 + 𝑎7𝑠 + 1

where 𝑎6 = 1, 𝑎7 = 2𝑇, 𝑎8 = 𝑇8.

Thisgives:

𝐵8 𝑠 = 𝑇8𝑠8 + 2𝑇𝑠 + 1

wheretheparameter 𝑇 isusedtodefinedthespeedoftheresponseaccordingto:

𝑇e ≈ 𝑛𝑇

where 𝑇e isdefinedastheObserverresponsetimewherethestepresponsereach63%ofthesteadystatevalueoftheresponse.

→Sowewilluse 𝑇e asthetuningparameterfortheObserver.

LabVIEW:

InLabVIEWwecanusethe“PolynomialRoots.vi”tofindtherootsbasedontheButterworthPolynomial

LabVIEWFunctionsPalette:Mathematics→Polynomial→PolynomialRoots.vi

Page 58: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

57 StateEstimationwithObserversinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

BelowweseetheMathematicsandthePolynomialpalettesinLabVIEW.

MathScript:

InMathScriptwecanusetherootsfunctioninordertofindtheeigenvaluesbasedonagivenpolynomial.

8.3 ObserverGainLabVIEW:

InLabVIEWwecanusethe“CDAckerman.vi”tofindtheObservergainbasedonsomegiveneigenvalues(foundfromtheButterwortPolynomial).

LabVIEWFunctionsPalette:ControlDesign&Simulation→ControlDesign→StateFeedbackDesign→CDAckerman.vi

Page 59: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

58 StateEstimationwithObserversinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

MathScript:

InMathScriptwecanusetheackerfunctioninordertofindtheObservergainbasedonsomegiveneigenvalues(foundfromtheButterwortPolynomial).

8.4 ObservabilityAnecessaryconditionfortheObservertoworkcorrectlyisthatthesystemforwhichthestatesaretobeestimated,isobservable.Therefore,youshouldcheckforObservabilitybeforeapplyingtheObserver.

TheObservabilitymatrixisdefinedas:

𝑂 =𝐶𝐶𝐴⋮

𝐶𝐴I>7

Wherenisthesystemorder(numberofstatesintheState-spacemodel).

→Asystemofordernisobservableif 𝑶 isfullrank,meaningtherankof 𝑶 isequalton.

LabVIEW:

TheLabVIEWControlDesignandSimulationModulehaveaVI(ObservabilityMatrix.vi)forfindingtheObservabilitymatrixandcheckifastates-pacemodelisObservable.

LabVIEWFunctionsPalette:ControlDesign&Simulation→ControlDesign→State-SpaceModelAnalysis→CDObservabilityMatrix.vi

Page 60: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

59 StateEstimationwithObserversinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

Note!InLabVIEW 𝑁 isusedasasymbolfortheObservabilitymatrix.

LabVIEWExample:CheckforObservability

[EndofExample]

MathScript:

InMathScriptyoumayusetheobsvmxfunctiontofindtheObservabilitymatrix.YoumaythenusetherankfunctioninordertofindtherankoftheObservabilitymatrix.

MathScriptExample:

ThefollowingMathScriptCodecheckforObservability:

% Check for Observability: O = obsvmx (discretemodel) r = rank(O)

[EndofExample]

8.5 ExamplesHerewewillshowimplementationsofanObserverinLabVIEWandMathScript.

Giventhefollowinglinearstate-spacemodelofawatertank:

𝑥7𝑥8

= 0 −100 0

N

𝑥7𝑥8 + 0.02

0O

𝑢

𝑦 = 1 0P

𝑥7𝑥8 + 0

k𝑢

Where 𝑥7 isthelevelinthetank,while 𝑥8 istheoutflowofthetank.Onlythelevel 𝑥7 ismeasured.

Page 61: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

60 StateEstimationwithObserversinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

LabVIEWExample:ObserverGain

BelowweseetheBlockDiagraminLabVIEWforcalculatingtheObservergain:

Weusedthe“PolynomialRoots.vi”inordertofindthepolesasspecifiedintheButterworthPolynomial.

Weusea2.orderButterworthPolynomial:

𝐵8 𝑠 = 𝑇8𝑠8 + 2𝑇𝑠 + 1

wheretheparameter 𝑇 isusedtodefinedthespeedoftheresponseaccordingto:

𝑇e ≈ 𝑛𝑇 ↔ 𝑇 =𝑇e𝑛

Intheexampleweset 𝑇e = 2𝑠 and 𝑛 = 2 intheexample.

Thisgives:

𝐵8 𝑠 = 𝑠8 + 1.41𝑠 + 1

Sothecoefficientsinthepolynomialareasfollows:

Thenwehaveusedthe“CDAkerman.vi”tofindtheObservergain.

Theresultbecomes:

Page 62: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

61 StateEstimationwithObserversinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

[EndofExample]

LabVIEWExample:ObserverEstimator

LabVIEWhaveseveralbuilt-inObserverfunctions,e.g.,the“CDContinuousObserver.vi”wewilluseinthisexample.BelowweseetheBlockDiagramfortheObserver:

Theresultisasfollows:

Page 63: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

62 StateEstimationwithObserversinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

[EndofExample]

MathScriptExample:ObserverGain

HerewewilluseMathScriptinordertofindtheObservergainforthesamesystemasabove.

TheCodeisasfollows:

% Define the State-space model: A = [0 -10; 0 0]; B = [0.02; 0]; C = [1 0]; D=[0]; ssmodel = ss(A, B, C,D); % Check for Observability: O = obsvmx (ssmodel); r = rank(O); %Butterwort Polynomial: B2=[1, 1.41, 1]; p=roots(B2); % Find Observer Gain K = ackermann(ssmodel, p, 'L')

Theresultis:

Page 64: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

63 StateEstimationwithObserversinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

K = 1.41 -0.1

[EndofExample]

Page 65: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

64

9 OverviewofObserverfunctionsObserversareverysimilartoKalmanfilters.Inobserverstheestimatorgainiscalculatedfromspecifiedeigenvaluesorpolesoftheestimatorerrordynamics(inotherwords:howfastyouwanttheestimationerrortoconvergetorealstates).

InLabVIEWthereareseveralVIsandfunctionsusedforObserverimplementations.

9.1 ControlDesignpaletteInthe“ControlDesign”palettewefindsubpalettesfor“StateFeedbackDesign”and“Implementation”:

9.1.1 StateFeedbackDesignsubpalette

Inthe“StateFeedbackDesign”subpalettewefindVIsforcalculationtheObserverGain,etc.

Page 66: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

65 OverviewofObserverfunctions

Tutorial:SystemIdentificationandEstimationinLabVIEW

→UsetheStateFeedbackDesignVIstocalculatecontrollerandobservergainsforclosed-loopstatefeedbackcontrolortoestimateastate-spacemodel.YoualsocanuseStateFeedbackDesignVIstoconfigureandteststate-spacecontrollersandstateestimatorsintimedomains.

AckermannVI:

9.1.2 Implementationsubpalette

Inthe“Implementation”subpalettewefindVIsforimplementingadiscreteObserverandadiscreteKalmanFilter.

→UsetheImplementationVIsandfunctionstosimulatethedynamicresponseofadiscretesystemmodel,deployadiscretemodeltoareal-timetarget,implementadiscreteKalmanfilter,andimplementcurrentandpredictiveobservers.

DiscreteObserver:

Page 67: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

66 OverviewofObserverfunctions

Tutorial:SystemIdentificationandEstimationinLabVIEW

9.2 SimulationpaletteInthe“Simulation”palettewefindthe“Estimation“sub-palette:

9.2.1 Estimationsubpalette

Inthe“Estimation”palettewefindVIsforimplementingcontinuous/discreteObserversandKalmanFilter.

Page 68: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

67 OverviewofObserverfunctions

Tutorial:SystemIdentificationandEstimationinLabVIEW

→UsetheEstimationfunctionstoestimatethestatesofastate-spacesystem.Thestate-spacesystemcanbedeterministicorstochastic,continuousordiscrete,linearornonlinear,andcompletelyorpartiallyobservable.

ContinuousObserverVI:

DiscreteObserverVI:

Page 69: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

68

PartIII:SystemIdentification

Page 70: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

69

10 SystemIdentificationinLabVIEWThemodelcanbeinformofdifferentialequationsdevelopedfromphysicalprinciplesorfromtransferfunctionmodels,whichcanberegardedas“black-box”-modelswhichexpressestheinput-outputpropertyofthesystem.Someoftheparametersofthemodelcanhaveunknownoruncertainvalues,forexampleaheattransfercoefficientinathermalprocessorthetime-constantinatransferfunctionmodel.Wecantrytoestimatesuchparametersfrommeasurementstakenduringexperimentsonthesystem.

Herewewilldiscuss:

• ParameterEstimationandtheLeastSquareMethod(LS)• Sub-spacemethods/Black-Boxmethods• PolynomialModelEstimation:ARX/ARMAXmodelEstimation

InLabVIEWwecanusethe“SystemIdentificationPalette”.

The“SystemIdentification”paletteinLabVIEW:

InthenextchapterswewillusethedifferentfunctionalityavailableintheSystemIdentificationToolkit.

Page 71: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

70 SystemIdentificationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

10.1 ParameterEstimationwithLeastSquareMethod(LS)

ParameterEstimationusingtheLeastSquareMethod(LS)isusedtofindamodelwithunknownphysicalparametersinamathematicalmodel.

TheLeastsquaremethodcanbewrittenas:

𝑌 = Φ𝜃

Where

𝜽 istheunknownparametervector

𝑌 istheknownmeasurementvector

Φ istheknownregressionmatrix

Thesolutionfor 𝜃 maybefoundas:

𝜃 = Φ>7𝑌

Itcanbefoundthattheleastsquaresolutionfor 𝑌 = Φ𝑌 is:

𝜃st = (ΦuΦ)>7ΦuY

ImplementationinMathScript/MATLAB:

theta=inv(phi’*phi)* phi’*Y

orsimply:

theta=phi\Y

InLabVIEWwecanusetheblocks(“AxB.vi”,“TransposeMatrix.vi”,“InverseMatrix.vi”)inthe“LinearAlgebra”(locatedintheMathematicspalette)paletteinordertofintheLeastSquaresolution:

Page 72: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

71 SystemIdentificationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

Wecanalsousethe“SolveLinearEquations.vi”:

Example:

Giventhefollowingmodel:

𝑦 𝑢 = 𝑎𝑢 + 𝑏

Thefollowingvaluesarefoundfromexperiments:

𝑦 1 = 0.8

𝑦 2 = 3.0

𝑦 3 = 4.0

Wewillfindtheunknowns 𝑎 and 𝑏 usingtheLeastSquare(LS)methodinMathScript/LabVIEW.

Wehavethat:

𝑌 = Φ𝜃

Where

𝜃 istheunknownparametervector

𝑌 istheknownmeasurementvector

Page 73: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

72 SystemIdentificationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

Φ istheknownregressionmatrix

Thesolutionfor 𝜃 maybefoundas(if Φ isaquadraticmatrix):

𝜃 = Φ>7𝑌

Itcanbefoundthattheleastsquaresolutionfor 𝑌 = Φ𝜃 is:

𝜃st = (ΦuΦ)>7ΦuY

Weget:

0.8 = 𝑎 ∙ 1 + 𝑏

3.0 = 𝑎 ∙ 2 + 𝑏

4.0 = 𝑎 ∙ 3 + 𝑏

Thisbecomes:

0.83.04.0z

=1 12 13 1{

𝑎𝑏|

MathScript:

Wedefine 𝑌 and Φ inMathScriptandfind 𝜃 by:

phi = [1 1; 2 1; 3 1]; Y = [0.8 3.0 4.0]'; theta = inv(phi'*phi)* phi'*Y %or simply by theta=phi\Y

Theanswerbecomes:

theta = 1.6

-0.6

i.e.:

𝑎 = 1.6

𝑏 = −0.6

Thisgives:

Page 74: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

73 SystemIdentificationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

𝑦 𝑢 = 1.6𝑢 − 0.6

LabVIEW:

BlockDiagram:

FrontPanel:

Wecanalsousethe“SolveLinearEquations.vi”directly:

[EndofExample]

10.2 SystemIdentificationusingSub-spacemethods/Black-Boxmethods

Sub-spacemethods/Black-Boxmethodsisusedtofindamodelwithnon-physicalparameters.

Asub-spacemethods/Black-BoxmethodestimatesalineardiscreteState-spacemodelontheform:

𝑥JK7 = 𝐴𝑥J + 𝐵𝑢J

𝑦J = 𝐶𝑥J + 𝐷𝑢J

Page 75: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

74 SystemIdentificationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

LabVIEWoffersfunctionalityforthis.Inthe“ParametricModelEstimation”palettewefindthe“SIEstimateState-SpaceModel.vi”whichcanbeusedforsub-spaceidentification.

“ParametricModelEstimation”palette:

ThisVIestimatestheparametersofastate-spacemodelforanunknownsystem.

10.3 SystemIdentificationusingPolynomialModelEstimation:ARX/ARMAXmodelEstimation

LabVIEWoffersVIsforARX/ARMAXmodelestimationinthe“ParametricModelEstimation”palette.

Page 76: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

75 SystemIdentificationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

ForARXmodelswecanuse“SIEstimateARXmodel”:

ForARMAXmodelswecanuse“SIEstimateARMAXmodel”:

10.4 GeneratemodelDataInordertofindamodelweneedtogeneratedatabasedontherealprocess.Thestimulus(exitation)signalandtheresponsesignalwillthenbeinputtothefunctions/VIs(algorithms)inLabVIEWthatyouwillusetomodelyourprocess.

BelowweexplainhowwedothisinLabVIEW.

Datalogging:

Page 77: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

76 SystemIdentificationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

UseLabVIEWforexcitingtheprocessandloggingsignals.Useopen-loopexperiments(nofeedbackcontrolsystem).YoucanusetheWritetoMeasurementFilefunctionontheFileI/OpaletteinLabVIEWforwritingdatatotextfiles(usetheLVMdatafileformat,nottheTDMSfileformatwhichgivebinaryfiles).

IntheFileI/OpaletteinLabVIEWwehavelotsoffunctionalityforwritingandreadingfiles.

Belowweseethe“FileI/O”paletteinLabVIEW:

InthisTutorialwewillfocusonthe“WriteToMeasurementFile”and“ReadFromMeasurementFile”.

The“WriteToMeasurementFile”and“ReadFromMeasurementFile”isso-called“ExpressVIs”.WhenyoudragtheseVI’stotheBlockDiagram,aconfigurationdialogpops-upimmediately,likethis:

Page 78: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

77 SystemIdentificationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

Inthisconfigurationdialogyousetfilename,filetype,etc.

Notethatthese“ExpressVIs”havenoBlockDiagram.

10.4.1 Excitationsignals

Itisimportanttohaveagoodexcitationsignal,youcanusedifferentexcitationsignals,suchas:

• APRBSsignal(PseudoRandomBinarySignal) • AChirpSignal• AUp-downsignal

LabVIEW:

InLabVIEWyoucanusesomeofthefunctionsintheSignalGenerationpalette:

Page 79: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

78 SystemIdentificationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

LabVIEWFunctionsPalette:SignalProcessing→SignalGeneration

PRBSSignalAPRBSsignallookslikethis:

LabVIEW:

InLabVIEWyoucanusethe“SIGeneratePseudo-RandomBinarySequence.vi”function.

LabVIEWFunctionsPalette:ControlDesign&Simulation→Systemidentification→Utilities→SIGeneratePseudo-RandomBinarySequence.vi

Page 80: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

79 SystemIdentificationinLabVIEW

Tutorial:SystemIdentificationandEstimationinLabVIEW

ChirpSignalAChirpsignallookslikethis:

LabVIEW:

InLabVIEWyoucanusethe“ChirpPattern.vi”function.

LabVIEWFunctionsPalette:SignalProcessing→SignalGeneration→ChirpPattern.vi

Page 81: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

80

11 OverviewofSystemIdentificationfunctions

InLabVIEWwecanusetheSystemIdentificationToolkit.

The“SystemIdentification”paletteinLabVIEW:

→UsetheSystemIdentificationVIstocreateandestimatemathematicalmodelsofdynamicsystems.YoucanusetheVIstoestimateaccuratemodelsofsystemsbasedonobservedinput-outputdata.

The“SystemIdentification”paletteinLabVIEWhasthefollowingsubpalettes:

Icon Name Description

Preprocessing

DataPreprocessing

UsetheDataPreprocessingVIstopreprocesstherawdatathatyouacquiredfromanunknownsystem.

Page 82: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

81 OverviewofSystemIdentificationfunctions

Tutorial:SystemIdentificationandEstimationinLabVIEW

Parametric

ParametricModelEstimation

UsetheParametricModelEstimationVIstoestimateaparametricmathematicalmodelforanunknown,linear,time-invariantsystem.

Frequency

Frequency-DomainModelEstimation

UsetheFrequency-DomainModelEstimationVIstoestimatethefrequencyresponsefunction(FRF)andtoidentifyatransferfunction(TF)orastate-space(SS)modelofanunknownsystem.

Grey-Box

PartiallyKnownModelEstimation

UsethePartiallyKnownModelEstimationVIstocreateandestimatepartiallyknownmodelsfortheplantinasystem.

Recursive

RecursiveModelEstimation

UsetheRecursiveModelEstimationVIstorecursivelyestimatetheparametricmathematicalmodelforanunknownsystem.

Nonparametric

NonparametricModelEstimation

UsetheNonparametricModelEstimationVIstoestimatetheimpulseresponseorfrequencyresponseofanunknown,linear,time-invariantsystemfromaninputandcorrespondingoutputsignal.

Validation

ModelValidation

UsetheModelValidationVIstoanalyzeandvalidateasystemmodel.

Analysis

ModelAnalysis

UsetheModelAnalysisVIstoperformaBode,Nyquist,orpole-zeroanalysisofasystemmodelandtocomputethestandarddeviationoftheresults.

Conversion

ModelConversion

UsetheModelConversionVIstoconvertmodelscreatedintheLabVIEWSystemIdentificationToolkitintomodelsyoucanusewiththeLabVIEWControlDesignandSimulationModule.YoucanconvertanAR,ARX,ARMAX,output-error,Box-Jenkins,general-linear,orstate-spacemodelintoatransferfunction,zero-pole-gain,orstate-spacemodel.Youalsocanconverta

Page 83: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

82 OverviewofSystemIdentificationfunctions

Tutorial:SystemIdentificationandEstimationinLabVIEW

continuousmodeltoadiscretemodelorconvertadiscretemodeltoacontinuousmodel.

Management

ModelManagement

UsetheModelManagementVIstoaccessinformationaboutthesystemmodel.Modelinformationincludespropertiessuchasthesystemtype,samplingrate,systemdimensions,noisecovariance,andsoon.

Utilities

Utilities

UsetheUtilitiesVIstoperformmiscellaneoustasksondataorthesystemmodel,includingproducingdatasamples,displayingmodelequations,mergingmodels,andsoon.

The“DataPreprocessing”paletteinLabVIEW:

Someimportantfunctionsinthe“DataPreprocessing”paletteare:

Page 84: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

83 OverviewofSystemIdentificationfunctions

Tutorial:SystemIdentificationandEstimationinLabVIEW

The“ParametricModelEstimation”paletteinLabVIEW:

Someimportantfunctionsinthe“ParametricModelEstimation”paletteare:

The“ParametricModelEstimation”paletteinLabVIEWhassubpalettefor“PolynomialModelEstimation”:

Page 85: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

84 OverviewofSystemIdentificationfunctions

Tutorial:SystemIdentificationandEstimationinLabVIEW

→UsethePolynomialModelEstimationVIstoestimateanAR,ARX,ARMAX,Box-Jenkins,oroutput-errormodelforanunknown,linear,time-invariantsystem.

Someimportantfunctionsinthe“PolynomialModelEstimation”paletteare:

Page 86: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

85

12 SystemIdentificationExampleWewanttoidentifythemodelofagivensystem.

Wehavefoundthemodeltobe:

𝑥 = −1𝑇 𝑥 + 𝐾𝑢(𝑡 − 𝜏)

where

𝑇 isthetimeconstant

𝐾 isthesystemgain,e.g.pumpgain

𝜏 isthetime-delay

→Wewanttofindthemodelparameters 𝑇, 𝐾, 𝜏 usingtheLeastSquaremethod.WewilluseLabVIEWandMathScript.

Setthesystemontheform 𝒚 = 𝛗𝜽

Solutions:

Weget:

𝑥�= 𝑥 𝑢(𝑡 − 𝜏)

−1𝑇𝐾|

i.e.

𝜃 = −1𝑇𝐾

Inordertofind 𝜃 usingtheLeastSquaremethodweneedtologinputandoutputdata.Thismeansweneedtodiscretizethesystem.

WeuseasimpleEulerforwardmethod:

𝑥 ≈𝑥JK7 − 𝑥J

𝑇@

𝑇@ isthesamplingtime.

Page 87: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

86 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

Thisgives:

𝑥JK7 − 𝑥J𝑇@�

= 𝑥J 𝑢J> ?�̀

−1𝑇𝐾|

Let’sassume 𝜏 = 3𝑠 (whichcanbefoundfromasimplestepresponseontherealsystem),wethenget(withsamplingtime 𝑇@ = 0.1):

𝑥JK7 − 𝑥J𝑇@�

= 𝑥J 𝑢J>�6�

−1𝑇𝐾|

Note!In3secondswelog30pointswithdatausingsamplingtime 𝑻𝒔 = 𝟎. 𝟏!!!

Giventhefollowingloggingdata(thedataisjustforillustrationandnotrealistic):

𝒌 𝒖 𝒚

1 0.9 3

2 1.0 4

3 1.1 5

4 1.2 6

5 1.3 7

6 1.4 8

7 1.5 9

Weusethefollowingsamplingtime: 𝑻𝒔 = 𝟏𝒔

Fromasimplestepresponse,wehavefoundthetime-delaytobe: 𝜏 = 3𝑠.

→Setthesystemontheform 𝒀 = 𝚽𝛉

Solutions:

Withtime-delay 𝜏 = 3𝑠and 𝑇@ = 1𝑠 weget:

Page 88: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

87 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

𝑥JK7 − 𝑥J𝑇@�

= 𝑥J 𝑢J>��

−1𝑇𝐾|

Usingthegivendatasetwecansetontheform 𝑌 = Φθ:

7 − 68 − 79 − 8

=6 0.97 1.08 1.1

−1𝑇𝐾

i.e.:

111z

=6 0.97 1.08 1.1

{

−1𝑇𝐾|

Note!Weneedtomakesurethedimensionsarecorrect.

→Wefindthemodelparameters(𝛉)usingMathScript

MathScriptgives:

clear, clc Y = [1, 1, 1]'; phi = [6, 0.9; 7, 1.0; 8, 1.1]; theta = phi\Y %or theta = inv(phi'*phi)*phi'*Y T = -1/theta(1) K = theta(2)

MathScriptrespondswiththefollowinganswers:

theta =

-0.3333

3.3333

T =

3

K =

3.3333

Page 89: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

88 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

i.e.,themodelparametersbecome:

𝑇 = 3, 𝐾 =103 , 𝜏 = 3

Whichgivesthefollowingmodell:

𝑥 = −1𝑇 𝑥 + 𝐾𝑢(𝑡 − 𝜏)

Withvalues:

𝑥 = −13𝑥 +

103 (𝑡 − 3)

ImplementthemodelinLabVIEW.Use 𝑻 = 𝟓, 𝑲 = 𝟐, 𝝉 = 𝟑 andsimulatethesystem.Plotthestepresponseforthesystem.

Model:

𝑥 = −1𝑇 𝑥 + 𝐾𝑢(𝑡 − 𝜏)

Where 𝑇 = 5, 𝐾 = 2, 𝜏 = 3

Solutions:

Weimplementthemodelusinga“SimulationSubsystem”andusetheavailableblocksintheControlandDesignModule.

Themodelmaybeimplementedasfollows:

Page 90: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

89 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

Wesimulatethesystemusingthefollowingprogram:

BlockDiagram:

FrontPanel:

Wedoasimplestepresponse:

Page 91: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

90 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

Findthetransferfunctionforthesystem:

𝐻 𝑠 =𝑥(𝑠)𝑢(𝑠)

Model:

𝑥 = −1𝑇 𝑥 + 𝐾𝑢(𝑡 − 𝜏)

PlotthestepresponseforthetransferfunctioninMathScript.Compareanddiscusstheresultsfromprevioustask.

Solutions:

Weusethedifferentialequation:

𝑥 = −1𝑇 𝑥 + 𝐾𝑢(𝑡 − 𝜏)

Laplacetransformationgives:

Page 92: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

91 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

𝑠𝑥(𝑠) = −1𝑇 𝑥(𝑠) + 𝐾𝑢(𝑠)𝑒

>?@

Note!WeusethefollowingLaplacetransformation:

𝐹 𝑠 𝑒>?@ ⟺ 𝑓(𝑡 − 𝜏)

𝑠𝐹(𝑠) ⟺ 𝑓(𝑡)

Thenweget:

𝑠𝑥 𝑠 +1𝑇 𝑥 𝑠 = 𝐾𝑢(𝑠)𝑒>?@

and:

𝑥 𝑠 𝑠 +1𝑇 = 𝐾𝑢(𝑠)𝑒>?@

and:

𝑥 𝑠𝑢(𝑠) =

𝐾

𝑠 + 1𝑇𝑒>?@

Finally:

𝐻 𝑠 =𝑥 𝑠𝑢(𝑠) =

𝐾𝑇𝑇𝑠 + 1 𝑒

>?@ =𝐾���𝑇𝑠 + 1 𝑒

>?@

Withvalues(𝑇 = 5, 𝐾 = 2, 𝜏 = 3):

𝐻 𝑠 =𝑥 𝑠𝑢(𝑠) =

105𝑠 + 1 𝑒

>�@

MathScript:

WeimplementthetransferfunctioninMathScriptandperformastepresponse.Weusethestep()function.

clear, clc s=tf('s'); K=2; T=5; H1=tf(K*T/(T*s+1)); delay=3; H2=set(H1,'inputdelay',delay); step(H2)

Page 93: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

92 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

% You may also use: figure(2) H = sys_order1(K*T, T, delay) step(H)

Thestepresponsebecomes:

→Weseethatthestepresponseisthesameasintheprevioustask.

Loginputandoutputdatabasedonthemodel.

Model:

𝑥 = −1𝑇 𝑥 + 𝐾𝑢(𝑡 − 𝜏)

Where 𝑇 = 5, 𝐾 = 2, 𝜏 = 3

Solutions:

Wesavethedatausing“WriteToMeasurementFile”inLabVIEW.

Basedonasimplestepresponsewecanfindthetime-delay 𝜏.

Weusethesameapplicationasinaprevioustask:

Page 94: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

93 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

Findthemodelparameters (𝑻 og 𝑲)usingLeastSquareinLabVIEWbasedontheloggeddata.

Note!Theanswersshouldbe 𝑇 ≈ 5 and 𝐾 ≈ 2.

Model:

𝑥 = −1𝑇 𝑥 + 𝐾𝑢(𝑡 − 𝜏)

Where 𝑇 = 5, 𝐾 = 2, 𝜏 = 3

Solutions:

Itisagoodideatosplityourprogramintodifferentlogicalpartsusing,e.g.,SubVIsinLabVIEW.

Thedifferentparts/stepscould,e.g.,be:

1. GetLoggedDatafromFilea. Input:FileNameb. Outputs: 𝑢 and 𝑦 (𝑇���)

2. Transformthedataandstackdatainto 𝑌 and Φ

Page 95: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

94 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

a. Inputs: 𝑢 and 𝑦 (𝑇���)b. Outputs: 𝑌 and Φ

3. FindtheLeastSquaresolution 𝜃st = (ΦuΦ)>7ΦuYa. Input: 𝑌 and Φb. Output: 𝜃(𝜃�, 𝐾�)

LabVIEWcode:

BlockDiagram:

FrontPanel:

Page 96: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

95 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

→Asyouseetheresultis 𝑇 = 5 and 𝐾 = 2 (asexpected).

ThedifferentSubVI’sdothefollowing:

1.“OpenMeasurementDatafromFile.vi”

ThisSubVIopenstheloggeddatafromfile(createdinaprevioustask).

BlockDiagram:

Page 97: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

96 SystemIdentificationExample

Tutorial:SystemIdentificationandEstimationinLabVIEW

2.“CreateLSMatricesfromLoggedDatawithTimeDelay.vi”

ThisSubVI“stack”dataontheform:

𝑥JK7 − 𝑥J𝑇@�

= 𝑥J 𝑢J> ?�̀

−1𝑇𝐾|

BlockDiagram:

3.“FindLSSolution.vi”

ThisSubVIfindtheLSsolution:

𝜃st = (ΦuΦ)>7ΦuY

BlockDiagram:

Page 98: University College of Southeast Norwayhome.hit.no/~hansha/documents/labview/training/System... · University College of Southeast Norway hansha System Identification and Estimation

Hans-PetterHalvorsen,M.Sc.

E-mail:[email protected]

Blog:http://home.hit.no/~hansha/

UniversityCollegeofSoutheastNorway

www.usn.no