LabVIEW功能全局變量
LabVIEW功能全局變量
功能全局變量(FGV)是一種常用的設(shè)計(jì)模式。FGV是一個(gè)非可重入VI,具有迭代一次的while循環(huán),并具有未初始化的移位寄存器。此構(gòu)造的目的是在對(duì)FGV的連續(xù)調(diào)用之間保留數(shù)據(jù)。
可以使用FGV代替全局變量,主要有兩個(gè)原因:
更改存儲(chǔ)值時(shí),可以執(zhí)行其他任務(wù)。因此,F(xiàn)GV有時(shí)也被稱為動(dòng)作引擎(AE)。
如果以不安全、意外的方式使用,正常的全局變量可能會(huì)導(dǎo)致代碼中出現(xiàn)爭用條件。FGV可以幫助預(yù)防這些。
下面是FGV常見用例的示例。該FGV可用作定時(shí)器SubVI。移位寄存器在While循環(huán)之外未初始化,這會(huì)導(dǎo)致每次循環(huán)迭代傳遞最后一個(gè)存儲(chǔ)值。在這種情況下,值僅在“初始化”案例中初始化。

當(dāng)兩段并行代碼修改同一變量時(shí),可能會(huì)出現(xiàn)爭用條件。例如,兩段代碼同時(shí)寫入變量的值。最后保存變量新值的代碼部分將覆蓋其他代碼的寫入值。這可能會(huì)導(dǎo)致稍后讀取變量時(shí)出現(xiàn)意外值。
功能全局變量可用于防止此類修改數(shù)據(jù)的操作。請(qǐng)注意,代碼的所有關(guān)鍵部分必須由不可重入的FGV作為一個(gè)整體進(jìn)行保護(hù);這可確保一次只執(zhí)行一個(gè)操作,因此沖突操作永遠(yuǎn)不會(huì)并行執(zhí)行。
舉個(gè)不好的例子,以下兩個(gè)并行操作會(huì)引起爭用條件,因?yàn)殛P(guān)鍵的讀取-修改-寫入操作是在FGV之外實(shí)現(xiàn)的:
在FGV中,代碼的關(guān)鍵讀取-修改-寫入部分已作為FGV的操作實(shí)現(xiàn)。由于FGV的非重入設(shè)置確保一次只執(zhí)行一個(gè)操作,因此兩個(gè)調(diào)用將始終相互執(zhí)行,從而防止出現(xiàn)爭用條件。
?
Functionalglobal?variable?(FGV)
Usea?functional?global?variable?(FGV)?to?communicate?between?two?parallel?processloops?contained?within?different?VIs?under?the?same?target,?and?use?a?FGV?tostop?parallel?loops?with?one?"stop"?button.?The"functional"?nature?of?the?FGV?means?that?you?can?create?additionalfunctionality?beyond?that?of?a?basic?global?variable,?e.g.,?counting?andcalculations?that?operate?on?the?stored?value.
LabVIEWRT?block?diagram?snippet:?Functional?global?variable?subVI?with?unitializedwhile-loop?shift?register?and?case?structure
Usecases
Communicatedata,?control,?and?status?between?two?or?more?parallel?process?loops?containedwithin?the?same?target?(“target-scoped”),?either?in?the?same?VI?or?in?differentVIs
Makethe?latest?value?of?a?variable?available?to?other?process?loops
Stopmultiple?parallel?loops?from?a?single?control
Performadditional?operations?(the?“functional”?aspect?of?the?FGV?acronym)?beyondmerely?storing?the?data,?e.g.,?counting?and?calculations
Features
Afunctional?global?variable?is?subVI?with?these?defining?characteristics:
Uninitializedshift?register?–?an?uninitialized?shift?register?on?a?while-loop?causes?LabVIEWto?allocate?storage?for?a?single?value?that?persists?as?long?as?the?calling?VIremains?in?memory
Single-iterationwhile-loop?–?the?while-loop?conditional?terminal?is?wired?to?a?“true”?constant,therefore?it?only?iterates?once;?the?while-loop?is?merely?a?mechanism?to?holdthe?shift?register
Casestructure?with?enumerated?control?–?the?case?structure?selects?an?operation?toperform?on?the?stored?value;?“read”?and?“write”?are?the?most?basic?operations,and?additional?cases?can?implement?additional?functions?such?as?“increment”,“decrement”,?etc.
Non-reentrantsubVI?–?this?execution?mode?ensures?that?only?one?instance?of?the?subVI?existsin?the?target;?multiple?instances?of?the?subVI?all?refer?to?the?same?storedvalue
Functionalglobal?variables?offer?two?advantages?over?local?variables?and?globalvariables:
Memoryefficiency?–?the?stored?value?exists?in?only?one?place?in?memory,?whereas?eachinstance?of?a?local/global?variable?reader?creates?its?own?copy?of?the?data
Avoidsrace?conditions?–?the?stored?value?cannot?be?written?while?it?is?being?readbecause?the?subVI?can?only?be?executed?by?one?process?at?a?time
Keepin?mind
ThesubVI?execution?mode?must?be?set?to?“non-reentrant”?(open?the?subVI,?pressCtrl+I,?select?“Execution”?category,?and?choose?“non-reentrant?execution”);?theother?two?reentrant?execution?modes?create?independent?copies?(“clones”)?of?thesubVI,?thereby?eliminating?the?global?variable?aspect?of?the?FGV
LabVIEWblock?diagram?elements
Thefunctional?global?variable?is?a?design?pattern?as?opposed?to?a?set?of?built-inVIs.?Refer?to?the?example?code?below?for?details.
Examplecode
Connectyour?Academic?RIO?Device?to?your?PC?using?USBLAN,?Ethernet,?or?Wi-Fi.?NOTE:?Notall?Academic?RIO?Devices?have?Ethernet?and?Wi-Fi?connectivity?options.
Downloadand?unpack?the?rt_functional-global-variable.zip?(for?use?with?NI?myRIO?1900)or?the?NIELVISIII-rt_functional-global-variable.zip?(for?use?with?NI?ELVIS?III)archive,?and?then?double-click?the?".lvproj"?file?to?open?theproject.?NOTE:?This?project?was?written?for?a?NI?myRIO?1900?or?NI?ELVIS?IIIconnected?by?USBLAN?at?IP?address?172.22.11.2.?If?you?are?using?a?different?IPaddress?or?another?Academic?RIO?Device?(Example:?NI?myRIO?1950?or?NI?RIOControl?Module)?do?the?following:
Ifusing?the?NI?myRIO?1950?or?NI?RIO?Control?Module?start?with?the?NI?myRIO?1900Archive.
DifferentIP?address:?Right-click?on?the?"NI?myRIO?1900"?Device,?choose"Properties",?and?then?enter?the?new?IP?address
Differentdevice:
Right-clickon?the?top?of?the?project?hierarchy,?select?"New?Targets?andDevices",?keep?the?"Existing?target?or?device"?option,?and?thenfind?and?select?your?particular?device
Selectall?of?the?components?under?the?"NI?myRIO?1900"?device:?click?thefirst?one?and?then?shift+click?the?last?one
Dragthe?selected?components?to?the?new?device
Right-clickthe?"NI?myRIO?1900"?device?and?select?"Remove?from?project"
Openthree?VIs:?“RT?Main”,?“RT?subVI-1”,?and?“RT?subVI-2”
Run“RT?Main”
The“fast?counter”?in?Process?Loop?#1?of?“VI?one”?increments?once?each?loop?cycle(100?ms)
TheBoolean?“reset”?generated?by?Process?Loop?#2?of?“VI?two”?resets?the?fastcounter
The“slow?counter”?in?Process?Loop?#2?increments?each?time?the?fast?counter?reaches10?counts;?this?condition?also?enables?the?“reset”?signal
Openthe?three?“fgv?-?”?subVIs?to?observe?the?state?of?the?three?functional?global?variables“fast?counter”,?“reset”,?and?“all?stop”
Stopall?VIs?by?clicking?the?“Stop”?button?to?set?the?“all?stop”?FGV
RTMain
Initializethe?“all?stop”?functional?global?variable?(FGV)
Runtwo?subVIs,?each?containing?FGVs
Pollthe?“Stop”?button?and?indicate?loop?activity
Setthe?“all?stop”?FGV?when?the?“Stop”?button?is?clicked
Thetwo?VIs?read?“all?stop”?FGV?for?the?loop?conditional?terminal
RTsubVI-1:?“fast?counter”
Displaysup-counter?value?as?front-panel?indicator
Writescounter?value?to?“fast?counter”?FGV
Resetscounter?to?zero?when?“reset”?FGV?is?true
RTsubVI-2:?“slow?counter”
Displaysup-counter?displayed?as?front-panel?indicator
Incrementscounter?when?the?“fast?counter”?FGV?equals?10
Activates“reset”?indicator?and?corresponding?FGV?when?the?“fast?counter”?value?equals?10
Functionalglobal?variable?VIs
Outercase?structure?implements?standard?error?behavior
While-loopstructure:
Uninitializedshift?register?allocates?persistent?storage
Loopconditional?wired?to?“T”?means?loop?runs?only?once?per?call
Innercase?structure:
Enumerateddata?type?selects?the?operation?(or?“function”)?to?be?performed?on?the?variable
“Read”?and?“Write”?are?the?most?basicoperations
Addadditional?operations?with?more?case?structure?subdiagrams
Mustuse?non-reentrant?execution?mode
需要說明的是,上述的例程和文檔,都是可以下載的,雙擊即可打開,其中壓縮文件是可以采用粘貼復(fù)制的方式,拷貝到硬盤上。這不是圖片,各位小伙伴看到后嘗試一下,這個(gè)問題就不用加微信咨詢了。有關(guān)LabVIEW編程、LabVIEW開發(fā)等相關(guān)項(xiàng)目,可聯(lián)系們。附件中的資料這里無法上傳,可去公司網(wǎng)站搜索下載。
LabVIEW程序,如下附件所示。