[推薦][教學] Real-Time Applications with the Timed Loop

這裡有「Real-Time Applications with the Timed Loop」的英文教學,請大家前往觀看。

網址:http://www.ni.com/swf/presentation/us/timedloop/

 

我訪照這個網路研討會,做了一個中文版的研討會,歡迎有興趣的人可以參考一下:

  1. http://www.labview.com.tw/webevent/timed_loop/player.html
  2. http://www.labview.com.tw/webevent/timed_loop2/index.htm
1個讚

有人知道投影片第10項的Tracing Time Loop Execution裡的Trace tool是什麼嗎? 是要購買的toolkit還是7.1內就有了,我好像都找不到耶

那是要另外購買的,叫做「NI LabVIEW Execution Trace Toolkit」,這裡有網頁可以看看他們的說明。Trace tool在LabVIEW 7.1的研討會中有出現過,工程師有展示他的功能,請見:LabVIEW 7.1新版發表及資料擷取應用技術研討會 [有範例程式以及投影片]

![](upload://7YAW2dxkhlKehpNA7FO3adsKCDJ.jpeg)
Welcome to the Rea-Time Applications with the Timed Loop online tutorial. The LabVIEW Timed Loop is a new programming structure that was introduced with version 7.1 of the LabVIEW Development system. This powerful new feature allows you to develop multirate real-time applications using a high-level programming interface. Each timed loop corresponds to a real-time task that runs in an independent thread. You can assign a unique priority to a maximum of 128 timed loop.

Timed Loops have numbers features that make programming real-time applications intuitive. Each timed loop provides instant real-time feedback to verify deterministic performance and dynaminally adapt to changing conditions. Additionally, the timed loop gives you more options for synchronizing software execution with hardware events.

Timed LoopLabVIEW7.1版的新功能,Timed Loop讓您可以使用高階語言來開發多功、平行處理的Real-Time應用系統。每一個Timed Loop分別被分配到獨立、隔離的執行序(Task),以確保程式可以進行多功處理。在一個LabVIEW程式中,您最多可以使用128個各自獨立的Timed Loop

Time Loop最大的特色就是讓您很直覺化地撰寫Real-Time程式。使用Feedback的功能可以在一個Timed-Loop執行後,是否逾時,以便在程式內部由程式本身自行動態調整Timed-Loop設定值。此外,Timed-Loop也可以藉由硬體觸發來達到程式內部Timed-Loop的同步。

 

![](upload://9L2Wp1M3eTxroKgl3M5EGv8mvUi.jpeg)
The LabVIEW Timed Loop is a feature available in LabVIEW7.1 for Windows. The feature is also available and ideally suited for the LabVIEW Real-Time Module. The LabVIEW Real-Time Module is an add-on to the LabVIEW development system that extends the capability of LabVIEW to create complied code that executes on dedicated hardware targets. This feature can be used on all LabVIEW Real-Time ETS targets, including Compact FiledPoint, CompactRIO, PXI, Compact Vision System, and Desktop that have been converted into a real-time target.

Timed-LoopLabVIEW7.1 for Windows版本才有的新功能。Timed-Loop也可以適用於LabVIEW Real-Time模組。LabVIEW Real-Time模組是LabVIEW的附加工具,需要另外安裝,Real-Time模組的主要功能就是讓LabVIEW可以建立專門用於「決定性硬體控制」的程式,通常這樣的程式需要具備反應速度快的特色。Timed-Loop可以用於LabVIEW Real-Time ETS targets,包含:Compact FieldPoint, CompactRIO, PXI, Compact Vision System以及轉換為Real-Time target的桌上型電腦。

 

![](upload://xESDWSGvMgKfvfLQLMfuGDwb3oy.jpeg)
To use a timed loop in your LabVIEW diagram, go to the Structures subpalette of the All Functions palette. Within the structures palette, find the Timed Loop subpalette. You can click and drag on  your block diagram to place a timed loop just as if you were placing any other LabVIEW loop. Double clicking the configuration node will bring up the loop configuration dialog. This is where you can set the timing parameters for the code that is placed within the loop.

在使用Timed-Loop之前,首先要將Timed-Loop拖曳到Block Diagram上。Tiked-Loop的位置是在:All Functions > structures > Timed Loop裡面。您可以將Timed-Loop拖曳到Block Diagram上。在Timed-Loop的左邊有一個突出的區域,叫做「Configuration Node」,在Configuration Node用滑鼠左鍵點兩下,螢幕就會呈現這個Timed-Loop的組態設定對話框,這個對話框可以用來設定Timed-Loop的參數。

 

![](upload://3NziNmOkWbK5pynkDiogPKmaF5z.jpeg)
The Loop Configuration Dialog has serval parameters that determine the timing behavior of each timed loop. Let's look at each of these parameters in more detail.

Each timed loop must have a timing source that servers as the time base of the execution of the loop. There are two build-in timing sources, a 1kHz clock and a 1MHz clock. The 1 kHz clock is available for all supported platforms. The 1 MHz clock is available for most real-time targets. This clock is based off the processor of the real-time targets, giving you the ability tp precisely time software with microsecond resolution. You can select to use the terminal if you wish to supply an external timing source using data acquisition modules. We will learn more ablout this option later in the toturial.

The period of the timed loop is set using the units if the selected timing source. If the timing source is the 1MHz clock, for example, the period will be in microseconds. If an external timing source is used, you set the period in ticks. The offset parameter let you shiht the phase of a loop relative to other loops. The parameter will determine how LabVIEW will schedule this particular loop. You can choose a number between 0 and 2 billion for the priority. The priority number is relative to other timed loops; a higher number will be higher priority. It is important to note that all timed loops will execute between the high and time-critical priorty of the LabVIEW VI priporities. The loop can be assigned a name in order to control the loop programmatically.

If the code within a timed loop does not finish execution within the specified period a late iteration occurs. You can configure how the timed loop handles these, selecting whether to discard missed periods and/or maintain the original timing phase. Finally, you can set a timeout that will wake up the loop if a timing source soes not wake it up beforehand.

Timed-Loop的組態設定對話框可以設定許多決定Timed-Loop的參數,我門線在就來實際了解一下,Timed-Loop的組態設定對話框可以設定哪些參數。

<?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

每一個Timed-Loop都要有一個參考時間源(Timing source)來作為參考時間的依據,在Timed-Loop裡面有兩個內建的Timing Source,分別是:1KHz以及<?:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />1MHz1KHz可以支援所有的作業系統平台,但是1MHz只能支援Real-Time Target的平台,1MHztiming source可以提供準確到1us的時間解析度。您也可以選擇使用外部的DAQ所提供的訊號作為timing source

 

Timed-loop的週期的單位取決於timing source的選定。舉例來說,如果timing source選擇的是1MHz,那麼週期的單位就是us。如果是選擇使用外部的timing source,那麼單位就是ticks。「offset」就是設定這個timed-loop跟其他timed-loop的相對時間差。「priority」就是優先權,在優先權裡面您可以設定介於0到兩百萬的數字,優先權的數字是相對性的,較高的數字表示具有較高的優先權。另外需要注意的是,所有Timed-loopVI程式裡面都會被視為優先權介於hightime-critical之間。你有可以為timed-loop取一個好記的名子。

 

如果在Timed-Loop裡面的程式沒有在指定的時間內完成的話,您也可以設定當遇到這種情況時的處理方式。您可以選擇忽略或是維持原來的時間相位。最後,您可以設定一個作業逾時的時間(timeout),以防止Timed-loop被乎卻而沒有執行。

 

![](upload://paHQr7riDv2VNWEv0tRc2OtDqjc.jpeg)
Once the timed loop has beenconfigured, you program the real-time task by adding code with the loop. The timed loop has additional features that let you determine the timing characteristic of each task. The left data node of the timed loop provides feedback about the execution of the loop.

The Finished Late[i-1] terminal will ouput a ture or false value depending on whether or not the code within the loop finished executing within the configured period. This is vital in determining whether the task is executing deterministically.

The Expected end[i-1] terminal will tell you what time value the last iteration of the loop was expected to end at, while the Actual end[i-1] terminal tells you when the code within the loop actually  ended. this can help you determine what percentage of the loop period is being used by the code within.

The Expected start and Actual start terminals are helpful in determining when the current iteration of the loop actually started if the previous iteration missed it's deadline.

The Wakeup Reason terminal tells you whether the task has been aborted programmatically, has timed out, or is executing normally.

Use this feedback to modify the configuration dynamically while your program executes. You can change the period, priority, offset, and mode of the timed loop programmatically using the right data node of the timed loop.

在設定完timed-loop的組態後,接下來就可以把程式放進timed-loop裡面,就像我們以前在使用while loop一樣。Timed-loop另外提供了可以讓您得知每次迴圈的執行狀況的功能。在timed-loop左邊的資料節點就可以feedback每次迴圈執行的狀況。

在這裡,左邊從上開始數的第一個接點就做:「Finished Late」,他會送回True或是False的信號,讓我們知道這個timed-loop裡面的程式執行的總時間有沒有超過timed-loop所設定的迴圈時間。比如說:如果我們將timed-loop的時間設定為10ms,但是實際timed-loop裡面的程式需要15ms的執行時間,這時timed-loop就會讓程式內部執行15ms,但是下次進入這個迴圈時,「Finished Late」就會送出「True」的信號,意思就是:上一個迴圈有逾時的現象發生。

 

Expected end接點告訴您告訴您這次timed-loop完成的預估時間。Actual end接點告訴您這次timed-loop完成的實際時間。這兩個功能可以幫助您這個timed-loop實際用於執行程式的佔用時間百分比。

 

Expected startActual start分別為這個timed-loop的預估開始時間和實際開始時間,這兩個功能可以幫助您檢視上個週期是否有逾時以及得知逾時了多少時間。

 

Wakeup Reason接點則告訴您這個timed-loop被終止的原因,究竟是:被程是終止,還是作業逾時(time out)

 

使用以上的這些feedback接點可以協助您在程式執行過程中,動態調整timed-loop的參數,使timed-loop的參數變的更恰當。

 

![](upload://r8e4ucBfCw6swfdcmjJUx4vBQaE.jpeg)
The stop Timed Loop Function allows you ti programmatically stop a timed loop by provising the assigned name of the loop. Additionally, you can dynamically elevate the priority of the aborted loop to preemep otherand run shutdown code faster. When the loop ia aborted, it will executeone more iteration. The Wakeup Reason feedback terminal of the loop will be equal to "Aborted". Use this feedback with a case structurein LabVIEW to executed clean up code.

This diagram shows an example of a shutdown loop that wakes up whena digital edge is received on a counter. The loop responds to this event and proceeds to shutdown two other time loops. Notice how the priority of the loop named "PWM" is elevated so it can preempt other tasks to shutdown sooner.

Stop Timed Loop」的功能讓你可以從程式的任何地方去關閉Timed-loop,您可以從Timed-loop的內部去關閉它,也可以從Timed-loop的外部去關閉這個Timed-loop。此外,您可以在提升那個要被關閉的timed-loop的優先權,讓那個Timed-loop可以更快的被關閉。Timed-loop左邊的「Wakeup Reason」接點可以被視為剛剛所說的「關閉Timed-Loop」。

 

接下來這個範例顯示,當這個Timed-loop被外部的DAQ訊號觸發時,會開始執行,並關閉量外兩個Timed-loop,分別為「PWM」以及「AI Loop」。請注意,在關閉PWM時,同時也將PWM的優先權提高了,這意味著名為PWMTimed-loop會被更快速地被關閉。

 

![](upload://p9fSi27XFRUlLdFKsrmNp4lSBQW.jpeg)
Timed Loop can be synchronized using the Synchronize Timed loop Starts VI. This can force a group of timed loops to dtart at the same time by not allowing any loop to run until all loops are ready to run. This is useful when data flow may cause a timed loop to be delayed before starting. The behaviar of this functions is similar to the LabVIEW Rendezvous functions.

如果我們要讓同一個程式的多個timed-loop進行同步,可以使用一個簡單的工具,就是:「Synchronize Timed Loop Starts VI」。這個VI可以讓您指定多個timed-loop,以同時開始執行。執行的過程中,先完成的timed-loop會閒置,等待需要花費較長時間的timed-loop,直到所有被指定的timed-loop都執行完畢後,才會再開始進行下一次的同步執行。這個功能與LabVIEW裡面的「Rendezvous」相似。

 

![](upload://rRfgTNe6a7ZhsFSFKKz3kYHpQUU.jpeg)
Often times real-time applications require synchronization whth hardware, which can be difficult to do with traditional real-time development tools. The LabVIEW Timed Loop and NI-DAQmz data acquisitions driver provide powerful features for synchronization. With NI-DAQmx you can create timing sources for timed loops from a variety of hardware events using the DAQmx Create Timing source VI. Rhis function has several options.

Frequency - This creates a hardware counter pulse train that serves as the timebase for the loop. It is supported by M-Series devices, E-Series devices, and TIO Counter devices.

Digital Edge (Counter) - This allows you to time a loop based off an input signal to a counter. This is especially useful if you want to synchronize the loop with pulse generated by an encoder.

Digital Change Detection - This wakes up the loop when digital lines change states. This is supported by industrial digital devices that support change detection.

Signal from DAQmx Task - This will wake up the loop when a previously configured NI-DAQmz task generates an event. For example, an acquisition task will generate an event whenever a sample has been acquired.

通常一個Real-time的應用程式需要與外部的硬體進行同步,如果您使用傳統的Real-time開發工具,可能很難做到這一點。然而,LabVIEWtimed-loopNI-DAQmx卻提供了方便的工具,讓您可以讓您的Real-time應用程式與外部的DAQ進行同步。究竟是如何做到的呢?

使用DAQmx裡面內建的一個工具:「DAQmx Create Timing source VI」,您可以建立一個供timed-loop使用的timing source。「DAQmx Create Timing source VI」有以下幾種功能:

 

頻率:

此功能會建立由DAQ硬體所產生的脈波信號,供Timed-loop作為timing source。支援此功能的硬體為:M系列的DAQ卡、E系列的DAQ卡以及TIO計數器。

 

數位邊緣:

此功能可以讓你從外部訊號來啟動程式中的某一個timed-loop

 

數位改變:

此功能可以讓你從外部訊號來啟動程式中的某一個timed-loop。支援此功能的硬體為:DIO

 

DAQmx task(程序)產生的信號:

DAQmx task產生的信號也可以啟動程式中的某一個timed-loop

 

![](upload://7Fr4HsA75jbjX9RobkepcZ4q0SA.jpeg)
With the LabVIEW Timed Loop you can implement custom scheduling in order to guarantee deteministic performance of multirate system. For example. assume that you have two tasks your program must execute deterministically. Task A must execute ever 10 milliseconds. The processing within A requires 3 milliseconds of CPU time. Task B must execute every 1 millisecond. The processing within Task B requires helf a millisecond of CPU time to complete.

By putting Task A in a timed loop with a period of 10 milliseconds ans Task B in another timed loop with a period of 1 millisecond you can guarantee both tasks will execute deterministically. Assign Timed Loop B a higher priority than Timed Loop A and the LabVIEW Timed Loop Scheduler will schedule Task A to run when Task B is idle. This assures that Task B always completes it's job every millisecond, while Task A also completes it's job brfore the 10 millisecond deadline. This si an example of Rate Monotonic Scheduling, a scheduling system in which the highest rate task is assigned the highest priority.

In the next slide we will see how this execution can be verified.

使用Timed-Loop,您可以確保一個多執行緒系統的整體效能與時間排程。舉例來說,假設您的LabVIEW程式需要同時執行兩個迴圈,或者說,您的程式會進行兩個執行緒,它們分別為Task A以及Task BTask A每隔10ms會執行一次,而每次會佔用3msCPU時間;Task B則是每隔1ms執行一次,每次執行時會佔用0.5msCPU時間。

 

我們可以把Task A放在一個週期為10msTimed-loop中,而將Task B放在另一個週期為1msTimed-loop中,這就是一個平行處理的結構,是不是很容易呢?

 

接著我們將Task ATask BTimed-loop的優先權做一些調整,將Task B的優先權調整為比Task A還高,這表示當CPU處與忙碌的狀態時,會先執行Task B,只有當Task B閒置時,才會執行task A

 

這樣的設定可以確保Task B一定會每秒執行一次,因為Task B有較高的優先權。而Task A也會每10秒執行一次,只是當CPU繁忙時,會暫停Task A,先執行Task B。在下一張投影片中,我們將會使用一個新工具:「Execution Trace Toolkit」來證明程式的真實執行狀況。

 

![](upload://iGMMwTTNEix5u9Yv1LRYPU71qr4.jpeg)
In real-time applications it is helpful to validate the execution of software. With the LabVIEW Execution Trace Toolkit, you can visualize the operation of timed loops and verify expected behavior. Instrument your code by adding a Trace Tool Start Trace VI and a Trace Tool Dtop Trace and send VI before and after the code of the program. This will log events registered by the LabVIEW VI running on a real-time target, including when each timed loop started and ended a iteration. It will alse log when a timed loop was preempted by a higher priority timed loop. When Finished executing, the log is send back to a host PC for analysis. You can view the log file with the Execution Trace Tool.

The screenshot shows the trace log of the previously shown VI. Here we can see each thread that is executing on the real-time target. Notice that each timed loop has it's own trace because each timed loop executes in a separate thread. Because the timed loops were assigned names, these alse show up in the trace.

Let's study the execution in detail. The yellow flags indicate that a timed loop is in queue and ready to run, so at the beginning of the trace, both Task A and Task B are ready to executes first because it has a higher priority, notice that the priorities are in the brackets next to each timed loop trace. When Task B is finished with it's work a blue flag indicates that the loop has finished iterating. At this point, Task A begins executing. Although Task A has 3 milliseconds worth of word to do, it only completes helf a millisecond, before it is preempted by Task B. This preemption is indicated by a red flag.

This execution pattern will continue until Task A has finished all of it's work for the first iteration. Subsequently, between periods of task B, the CPU is idle, indicated by the null thread trace which is blue. Once 10 milliseconds has elapsed from the last iteration, Task A begins to execute again.

在一個Real-time的環境中,如果有個工具可以將整個程式執行的過程幫我們抽絲剝繭出來,以直覺化的介面展現在螢幕上,那麼對於工程師將會是很有幫助的。幸好,LabVIEW的一個Toolkit可以做到這一點,那就是Execution Trace Toolkit。它會紀錄程式執行過程中,在Real-time target所發生的事件,包括Timed-loop何時開始的,何時結束;也會紀錄CPU處理優先順序不同的兩個timed-loop的全部過程。在程式結束後,記錄檔會從Real-time target傳送回host PC,然後我們就可以在host PC上觀看程式執行的過程紀錄檔。

 

這個畫面是我們在上一張投影片討論的情況,在Real-time target實際執行的結果。請注意,每一個timed-loop都有其專屬的軌跡,因為每一個timed-loop都在不同的執行緒上執行。我們可以在 Timed-loop的「組態設定對話框」裡面設定每一個Timed-Loop的名稱,這個名稱就會顯示在trace紀錄檔上,作為辨別不同Timed-loop的標誌。

 

我們來深入抽絲剝繭吧!黃色的小旗子表示這個timed-loop正在閒置而且隨時可以執行,所以在程式的一開始,Task ATask B都準備開始執行,因為Task B的優先權較高,所以Task B先佔用CPU時間,開始執行。請注意,每個timed-Loop的優先權也會顯示在trace的結果上。當Task B執行完畢後,會出現一個藍色的小旗子,表示Task B已經執行完畢了。皆下來,Task A才開始執行。雖然Task A需要3msCPU時間才可以執行完畢,但是由於Task B的優先權高於Task A,所以Task A只執行了0.5ms就被Task B中斷了。被中斷的地方就用紅色的小旗子表示。

 

Task A在接下來的執行過程中會持續被Task B中斷,直到Task A執行完畢為止。隨後,Task B的執行週期中有0.5msCPU閒置時間,這裡用藍色來表示CPU的閒置時間。等到10ms的週期開始重複,Task A又會繼續開始執行了。

 

![](upload://7LBHZ1NlDSwOZfadzadCtL7Ahyh.jpeg)
When integrating your timed loops within a real-time program, it is best to assign a higher priority to those loops that handle tasks such as closed-loop control, event response, and deterministic I/O. Assign lower priorities to loop that handle tasks sucj as networking, file I/O, and communication with a host PC.

Use Real-Time FIFOs to pass data between timed loop as shown in the diagram. This prevents timing issues that can arise from resource contention.

Timed-loop整合到您的應用程式時,建議您將「閉迴路控制」、「事件回應」、「確定性的I/O」賦予較高的優先權。而建議您將「網路功能」、「檔案讀寫」、「對外通訊」給予較低的優先權。

 

就像這張圖片一樣,您可以使用Real-timeFIFO功能,將資料從一個Timed-Loop傳遞到另一個Timed-Loop。使用FIFO的好處是可以避免因為優先權的競爭而造成資料漏失。

 

![](upload://4nrR1of8Xt8Hi2fnmUbSUFEKQYE.jpeg)
Use the timed loops to implement independent timing for each real-time tasl. Remember to assign priorities to the timed loops appropriately. Faster rate loops should have higher priorities, while lower rate loops should have lower prioritoes. Use the feedback terminals from each loop to keep trace of execution.

使用timed-loop,您可以執行一個具備多執行緒的程式。記得要為各個不同功能的Timed-Loop賦予適當的優先權唷。須要快速執行的timed-loop請給予較高的優先權;慢速執行也沒有關係的timed-loop請給予較低的優先權。您可以使用「Execution Trace Toolkit」來監看程式執行的狀況,也可以使用feedback接點來判斷是否有逾時的現象發生。

 

![](upload://iCTfnzjyRJKSeETFOQi7P8JrXUc.jpeg)
Congratulations, you have completed the Real-Time Applications with the Timed Loop Tutorial. Tale the next step tp learn more about LabVIEW7.1 and the LabVIEW Real-Time Module by clicking on the link.

恭喜您,您已經學會Timed-Loop的基本操作,在Real-time的應用中,Timed-loop真是不可或缺的好幫手。

 

 

 

我錄製了中文的研討會內容,歡迎有興趣的網友可以參考看看。

http://www.labview.com.tw/webevent/timed_loop/player.html

哇!!!感謝站長大大....看完英文版的本來還有些疑問,這樣一來學習就沒障礙了!造福咱們這些英文殘障人士~~

事後聽一聽,發現音量不夠,原因是我這次用了另一支麥克風。過幾天我再重錄一次。

昨天意外發現,原來NI的網路研討會是使用Macromedia的Breeze所錄製的。Breeze的網址是:http://www.macromedia.com/tw/software/breeze/

下一期的網路研討會,如果沒有意外的話,應該就會使用Breeze來錄製了,敬請期待唷。

2023年這些網址好像都不能看了QQ