The loop body should be designed to execute the desired process task as efficiently and as quickly as possible
Place non-deterministic code in adjacent conventional loops and then use appropriate inter-process communication techniques to exchange values; in this way the timed loop process concentrates on the time-critical task (e.g., managing a high-speed PID process control loop) while the conventional loop deals with the non-time-critical tasks (e.g., logging measurements to a data file or communicating with the PC host through the network)
LabVIEW block diagram elements
Locate these elements with "Quick Drop" (press Ctrl+Space and start typing the name); click on an icon to see more sample code that uses that element:
Example code
Connect your Academic RIO Device to your PC using USBLAN, Ethernet, or Wi-Fi. NOTE: Not all Academic RIO Devices have Ethernet and Wi-Fi connectivity options.
Download and unpack the
rt_timed-loop.zip (for use with NI myRIO 1900)
or the
NIELVISIII-rt_timed-loop.zip (for use with NI ELVIS III)
archive, and then double-click the ".lvproj" file to open the project. NOTE: This project was written for a NI myRIO 1900 or NI ELVIS III connected by USBLAN at IP address 172.22.11.2.
If you are using a different IP address or another Academic RIO Device (Example: NI myRIO 1950 or NI RIO Control Module) do the following:
If using the NI myRIO 1950 or NI RIO Control Module start with the NI myRIO 1900 Archive.
Different IP address: Right-click on the "NI myRIO 1900" Device, choose "Properties", and then enter the new IP address
Different device:
Right-click on the top of the project hierarchy, select "New Targets and Devices", keep the "Existing target or device" option, and then find and select your particular device
Select all of the components under the "NI myRIO 1900" device: click the first one and then shift+click the last one
Drag the selected components to the new device
Right-click the "NI myRIO 1900" device and select "Remove from project"
Run “RT Main”; this VI contains two versions of the timed loop:
Timed Loop #1:
Calls the “Recursive File List” VI to show all subfolders and files contained in the selected folder of the RT file system; the recursive file lister illustrates how nondeterministic code can require dramatically different processing times and impact the ability of the timed loop to finish at a predictable (deterministic) time
LED0 blinks to show loop activity
The “desired time” control adjusts the intended loop time per iteration in milliseconds
The “actual time” indicator displays the measured loop time
The “finished late?” Boolean indicator displays whether or not the previous loop iteration finished on time
Timed Loop #2i (with frames):
Executes three frames, each containing a blinker (LEDs 1 through 3), with each frame staggered in time
Includes “actual time” indicators for each frame
The “finished late?” Boolean indicator applies to the entire timed loop structure
Adjust the “desired time” to observe changes in the blinker rate
Try browsing to other folders besides “/tmp” such as “/c”, “/home”, and “/u” (if you have a flash drive attached to the USB port), and observe that folders with few files do not cause the loop to finish late
Now try browsing to “/usr” that contains more than 2000 files; observe that the actual loop time triples and the “finished late?” indicator activates
Onboard LED and front-panel LED blinkers indicate loop activity
Recursive file lister runs each loop iteration – would not normally place this element inside a timed loop because its execution time is not predictable and can potentially require more time to run than what is budgeted for the iteration time
Loop time is adjustable by a front-panel control
Actual loop time is measured and reported
“Finished Late?” Boolean indicator
Timed Loop #2
Each frame is synchronized to the same timing source
Each frame may be viewed as its own timed loop
Two frames are delayed relative to the first frame
Blinkers and loop time measurement similar to Timed Loop #1
Configure Timed Loop #1
Expanded “Input Node”, “Left Data Node”, “Right Data Node”, and “Output Node” to show all available connections
Double-click “Input Node” to configure the timed loop
Loop time measurement based on “Actual End [i-1]” terminal, the number of milliseconds required for the previous iteration
Feedback node stores the previous value and subtracter compares to current value
Desired loop time is adjustable on each loop iteration by using the “dt” input of the “Right Data Node” (as opposed to the “Input Data Node” which configures the loop before it starts)
Configure Timed Loop #2 (with frames)
Right-click on the timed loop frame and select “Add frame after”
Each frame as the same loop time as specified on the input node
Each frame is synchronized to the same time source
Select the “Start” terminal of the “Right Data Node” to delay the execution of the frame
The delayed starts cannot sum to more than the “Input Node” loop time
Stop both loops from a single front-panel “Stop” button
“Stop” button resides in Loop #1
Local variable in Loop #2 reads the “Stop” button value
“Stop” button mechanical action must be changed from default “Latch When Released” to “Switch When Pressed” because latching action is not compatible with local variables
Click “Stop” to stop both loops
Unstick the “Stop” button with the “F” constant after both loops have stopped, otherwise the button remains pressed