How to create a time control for loop & Communicating among multiple loops
When a loop finishes executing an iteration, it immediately begins executing the next iteration unless it reaches a stop condition. Most applications need precise control of the frequency or timing of the iteration to be maintained between successive operations of the loop. You might want to control the speed at which a process executes, such as the speed at which data values are plotted to a chart. You can use a wait function in the loop to wait an amount of time in milliseconds before the loop re-executes. LabVIEW consists of two wait functions. A wait function is placed inside a loop to allow a VI to sleep for a set amount of time. This allows your processor to address other tasks during the wait time. Wait functions use the operating system millisecond clock. They are Wait Until Next ms Multiple as shown in Figures 4.20(a) and Wait (ms) functions as shown in Figure 4.20(b).
The Wait Until Next ms Multiple function monitors a millisecond counter and waits until the millisecond counter reaches a multiple of the time you specify. This function can be used for synchronization activities. You can place this function within a loop to control the loop execution rate. For this function to be effective, your code execution time must be less than the time specified for this function. The execution rate for the first iteration of the loop is indeterminate.
The Wait (ms) function adds the wait time to the code execution time. This can cause a problem if code execution time is variable. The Wait (ms) function waits until the millisecond counter counts to an amount equal to the input you specify. This function guarantees that the loop execution rate is at least the amount of the input you specify. The Time Delay Express VI, located on the Functions>>Execution Control palette, behaves similar to the Wait (ms) function with the addition of built-in error clusters.
Example: Animate a dog running using Picture Ring Control and Wait Until Next ms Multiple function as shown in Figure.
Solution: The front panel consists of a Boolean control to ON or OFF the VI execution, a numeric control to adjust the time delay and a Picture Ring Indicator to display the pictures. To animate an action, you need a sequence of pictures with all the actions. In this example 10 different pictures consists of 10 different actions of a dog at running state is taken for animation. Following are the pictures taken for animation. If these pictures are animated, it shows as if a dog is running.
In the front panel, the Picture Ring Indicator is used to display sequence of pictures. The Picture Ring is a control by default but you have to convert it to indicator for this VI. The Picture Ring Control is available in the location Controls>>Modern>>Ring & Enum palette. Following are the steps to insert picture in the Picture Ring Control.
Step 1: Place the Picture Ring Control in the front panel and change it to the Picture Ring Indicator.
Step 2: In the front panel menu go to Edit and choose Import Picture From File …
Step 3: A file dialog appears in which you can choose the picture. Now the picture is available in Windows Clipboard.
Step 4: Right-click the Picture Ring Indicator and choose Import Picture From Clipboard. The picture available in the clipboard will be inserted in the Picture Ring Indicator.
Step 5: To insert the next picture follow steps 2 and 3. Right-click the Picture Ring Indicator and choose Add Item After. Now the new picture available in the clipboard will be inserted in the Picture Ring Indicator next to the previous picture.
Step 6: By followings steps 2, 3 and 5 you can insert all the 10 pictures. You can choose Add Item After or Add Item Before to insert pictures in appropriate order. After inserting all the pictures the order of the pictures, will be in the range of 0 to 9.
In the block diagram the value of the iteration terminal is divided by 10 because the number of pictures added is 10. The remainder of the quotient and remainder function varies from 0 to 9. The remainder value is connected to the Picture Ring Indicator. If the remainder is 0, the first picture is selected and displayed; if the remainder is 1, the second picture is selected and displayed; and if the remainder is 9, the last picture, i.e. the 10th picture will be selected and displayed. The numeric control used to provide time delay is set in the range of 0 to 200 milliseconds. By varying the value of the numeric control, we can change the speed at which the pictures are displayed which intern changes the speed at which the dog runs. The Boolean control is used to run or stop the VI. For every iteration, a picture is displayed and that produces the animation of the dog running.
COMMUNICATING AMONG MULTIPLE LOOPS
In LabVIEW, the flow of data determines the execution order of block diagram elements. Variables are block diagram elements that allow you to access or store data in another location. The actual location of the data varies depending upon the type of the variable. Local variables store data in front panel controls and indicators. Global variables and single process-shared variables store data in special repositories that you can access from multiple VIs. Functional global variables store data in While Loop shift registers. Regardless of where the variable stores data, all variables allow you to circumvent normal dataflow by passing data from one place to another without connecting the two places with a wire. For this reason, variables are useful in parallel architectures, but also have certain drawbacks, such as race conditions. You can create block diagrams that have simultaneous operations. But if you use wires to pass data between parallel block diagrams, they no longer operate in parallel. Parallel block diagrams can be two parallel loops on the same block diagram without any data flow dependency or two separate VIs that are called at the same time.