Timing


It's "time" for another digression. Timing functions are very important in LabVIEW and help you measure time, synchronize tasks, and allow enough idle processor time so that loops in your VI don't race too fast and hog the CPU.

You can find LabVIEW's timing functions on the Programming>>Timing palette, as shown in Figure 6.51.

Figure 6.51. Timing palette


The basic timing functions in LabVIEW are Wait (ms), Tick Count (ms), and Wait Until Next ms Multiple, located in the Programming>>Timing subpalette of the Functions palette.

Wait (ms) causes your VI to wait a specified number of milliseconds before it continues execution (see Figure 6.52).

Figure 6.52. Wait (ms)


Wait Until Next ms Multiple causes LabVIEW to wait until the internal clock equals or has passed a multiple of the millisecond multiple input number before continuing VI execution; it is useful for causing loops to execute at specified intervals and synchronizing activities (see Figure 6.53). These two functions are similar, but not identical. For example, Wait Until Next ms Multiple will probably wait less than the specified number of milliseconds in the first loop iteration, depending on the value of the clock when it starts (that is, how long it takes until the clock is at the next multiple and the VI proceeds).

Figure 6.53. Wait Until Next ms Multiple


Wait Until Next ms Multiple is commonly used for synchronizing one or more loops because its timing is periodicit waits until a specific time occurs. Wait (ms) is commonly used to create a pause between eventsit waits for a specified period of time to elapse.


Tick Count (ms) returns the value of your operating system's internal clock in milliseconds; it is commonly used to calculate elapsed time, as in the next activity (see Figure 6.54).

Figure 6.54. Tick Count (ms)


Activity 6-4: Matching Numbers

Now you'll have the opportunity to work with the Sequence Structure and one of the timing functions. You will build a VI that computes the time it takes to match an input number with a randomly generated number. Remember this algorithm if you ever need to time a LabVIEW operation. Many experienced LabVIEW developers believe that this is the only reason ever to use a multiframe Sequence Structurefor enforcing and measuring execution timing.

1.

Open a new front panel.

2.

Build the front panel shown (see Figure 6.55).



Figure 6.55. The front panel of the VI you will construct during this activity


3.

Open the diagram window and build the block diagram shown in Figure 6.56.

Figure 6.56. The block diagram of the VI you will construct during this activity


4.

Place a Flat Sequence Structure (Programming>>Structures palette) in the diagram window. It works like the For Loop and While Loop; click with the structure cursor and drag to define the boundaries you want.

You will have to build three separate frames of the Flat Sequence Structure. To create new frames, pop up on the frame border and choose Add Frame After or Add Frame Before from the pop-up menu.

5.

Build the rest of the diagram. Some new functions are described here. Make sure to use the Help window to display terminal inputs and outputs when you wire!

Tick Count Function

Tick Count (ms) function (Programming>>Timing palette). Returns the value of the internal clock.

Random Number Function

Random Number (0-1) function (Programming>>Numeric palette). Returns a random number between 0 and 1.

Multiply Function

Multiply function (Programming>>Numeric palette). Multiplies the random number by 100 so that the function returns a random number between 0.0 and 100.0.

Round to Nearest Function

Round to Nearest function (Programming>>Comparison palette). Rounds the random number between 0 and 100 to the nearest whole number.

Not Equal? Function

Not Equal? function (Programming>>Comparison palette). Compares the random number to the number specified in the front panel and returns a TRUE if the numbers are not equal; otherwise, this function returns a FALSE.

Increment Function

Increment function (Programming>>Numeric palette). Adds one to the loop count to produce the Number of Iterations value (to compensate for zero-based indexing).

In the first frame (on the left), the Tick Count (ms) function returns the value of the internal clock in milliseconds This value is wired through to the last frame. In the second frame (in the middle), the VI executes the While Loop as long as the number specified does not match the number returned by the Random Number (0-1) function. In the final frame (on the right), the Tick Count (ms) function returns a new time in milliseconds. The VI subtracts the old time from the new time to compute the time elapsed, and then divides by 1000 to convert from milliseconds to seconds.

6.

Turn on execution highlighting, which slows the VI enough to see the current generated number on the front panel.

7.

Enter a number inside the Number to Match control and run the VI. When you want to speed things up, turn off execution highlighting.

8.

Use the Save command to save the VI in your MYWORK directory as Time to Match.vi, and then close it. Good job!

Express Timing Functions

In addition to the basic timing functions, LabVIEW also provides you with two Express Timing functions: Time Delay and Elapsed Time.

Time Delay works just like Wait (ms) except that you specify the time delay in seconds (see Figure 6.57).

Figure 6.57. Time Delay Express VI and its configuration dialog


Elapsed Time lets you check whether a specified amount of time has passed. When you configure this timing function, you set how many seconds of elapsed time you want it to check for. When you call the VI, the Boolean output "Time has Elapsed" will return a TRUE if the specified amount of time has elapsed; otherwise, it returns a FALSE (see Figure 6.58).

Figure 6.58. Elapsed Time Express VI and its configuration dialog





LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net