Because of the preemptive nature of the real-time operating system (RTOS) on RT targets, deterministic tasks can monopolize the processor on the target. A deterministic task might use all processor resources and not allow non-deterministic tasks in the application to execute. After separating the deterministic tasks from non-deterministic tasks, you can use timing methods to ensure deterministic performance for deterministic tasks while allowing non-deterministic tasks to execute as needed.
You can use the Real-Time Timing VIs, timed structures, or external timing sources to time and control loop execution rates.
|Note Some RT targets automatically disable the millisecond timing engine. For more information, refer to the RT target device documentation.|
|Note The millisecond timing engine in versions prior to version 8.0 of the Real-Time Module had a 0.07% error. If you included code in your application to account for the 0.07% error, you must remove the code or enable the pre-Real-Time Module 8.0 behavior. Refer to the KnowledgeBase for more information on this task.|
Use the Real-Time Timing VIs, Wait and Wait Until Next Multiple, to time loops on RT targets. With the Real-Time Timing VIs, you can use the millisecond or microsecond timer of the RTOS running on an RT target to control the resolution of loops.
The Wait VI causes a VI to sleep for the specified amount of time. For example, you can use the Wait VI with Counter Unit set to milliseconds in a VI running on the RT target to provide loop rates up to 1 kHz. If the millisecond timer value of the RTOS is 112 ms when the Wait VI executes and the Count input equals 10, then the VI sleeps and does not return until the millisecond timer value equals 122 ms.
The following illustration shows the timing of a real-time application using the Wait VI. The application executes Function A, executes Function B, and then uses the Wait VI to sleep 10 ms after the execution of Function B.
The Wait Until Next Multiple VI causes a thread to sleep until the value of the RTOS millisecond or microsecond timer equals a multiple of the Count input. For example, if the Wait Until Next Multiple VI executes with a Count input of 10 ms and the RTOS millisecond timer value is 112 ms, the VI that calls the Wait Until Next Multiple VI sleeps until the millisecond timer value equals 120 ms because 120 ms is the first multiple of 10 ms after the execution of the Wait Until Next Multiple VI.
The following illustration shows the timing of a real-time application using the Wait Until Next Multiple VI. The application executes Function A, executes Function B, and then sleeps until the value of the RTOS millisecond timer equals a multiple of 20 ms, which is the time specified by the Count input of the Wait Until Next Multiple VI.
In the preceding illustration, the time of execution for the Wait Until Next Multiple VI varies depending on the execution of Function A and B. When Function A and B finish executing, the Wait Until Next Multiple VI executes until the operating system timer reaches the next multiple of 20 ms.
Avoid using the Wait VI or Wait Until Next Multiple VI in parallel with other LabVIEW code, which can lead to unexpected timing behavior. Within a timed structure or a VI set to time-critical priority, only one thread is available, so the timing VI executes in sequence with the rest of the LabVIEW code despite being placed in parallel. In this case, LabVIEW serializes the parallel code paths while compiling the VI, and the execution order of the timing VI is subject to change from one compile to the next. On the other hand, when the code is not inside a timed structure or a time-critical VI, LabVIEW executes the timing VIs in parallel with the other LabVIEW code, which changes the effect of the timing VI.
To ensure well-defined timing behavior, use a Sequence structure to force a specific execution order. In the following block diagram, the Wait Until Next Multiple VI causes the VI to sleep until the millisecond timer reaches a multiple of 100 ms. When the VI returns from sleep, the VI begins to execute the next frame of the Sequence structure.
A timed structure executes a subdiagram in a specific order with time bounds at a priority you specify. The priority setting of a timed structure specifies the priority of that timed structure relative to other timed structures executing on the target.
|Caution LabVIEW uses two separate but related priority schemes: VI priorities and timed structure priorities. Timed structure priorities are numeric, and a higher value represents a higher priority relative to other timed structures executing on the target. However, all timed structure priorities fall between the high and time-critical VI priority levels. To prevent undesired behavior, National Instruments recommends using only one priority scheme in your application. If the application uses timed structures, keep all VIs at normal priority.|
A Timed Loop executes a subdiagram, or frame, each iteration of the loop at the period you specify. Use the Timed Loop when you want to develop VIs with multirate timing capabilities, feedback on loop execution, timing characteristics that change dynamically, or several levels of execution priority.
Because of the preemptive nature of Timed Loops, they can monopolize processor resources. A Timed Loop might use all of the processor resources, not allowing other tasks on the block diagram to execute. You must configure the highest priority Timed Loop with a period large enough to perform the deterministic task and have idle time during every iteration to allow lower priority loops to execute. The following Timed Loop contains a subVI that performs a data acquisition for 50 ms.
The Timed Loop has a period of 100 ms that allows the loop to remain idle for 50 ms during each iteration. During the time when the Timed Loop remains idle, LabVIEW can execute lower priority tasks on the block diagram.
A Timed Sequence consists of one or more task subdiagrams, or frames, that execute sequentially and can be timed with an internal or external timing source. Use the Timed Sequence when you want to develop VIs with execution feedback, timing characteristics that change dynamically, or several levels of execution priority.
You can add frames to a Timed Loop to execute multiple subdiagrams in sequence. A Timed Loop with frames behaves like a regular Timed Loop with an embedded Sequence structure.
The National Instruments drivers that run on RT targets support VIs or functions that can cause sleep in the current LabVIEW thread and then return when the driver detects a specific event. For example, you can use NI-DAQmx and NI data acquisition hardware to time real-time applications. Refer to the specific NI driver documentation for information about VIs or functions that you can use to sleep and wait for driver events.
You can use NI data acquisition hardware with NI-DAQmx to match loop rates to match the rate of the hardware clock. With NI-DAQmx, you can use the following methods to time real-time applications:
Refer to the NI-DAQmx Help for more information about timing control loops using NI data acquisition hardware and NI-DAQmx software. Select Start»All Programs»National Instruments»NI-DAQ»NI-DAQmx Help to display the NI-DAQmx help.