Jump to content
  • 0

Getting started with ULx for NI LabVIEW Part 2


Jeffrey

Question

In part one, we learned how what is the minimum amount of VIs needed to acquire one analog data sample and display it on the NI LabVIEW front panel.  In part 2 we will add to this example by including:

  • Looping,
  • Iteration Timing,
  • Error handling,
  • Display the data in a strip chart,
  • Saving the data to file.

 

Looping

Taking one sample of data each time you click on a button is good, but having it acquire data continuously is better!  To accomplish this, we will add a ‘While Loop.’

Here is where we left off:

image.png.1eb80fa09ba7e7c22f2684ad64b6ad06.png

From the Block Diagram, right click anywhere there is white space to pop up the Functions menu. Select Express >> Exec Control >> While Loop.  Your cursor will change to this image.png.36c91d07be5d91966be4c2610f168052.png.  Place the cursor between and above the two ULx VIs on the block diagram, click and hold your left mouse button down, then drag the cursor until it is to the right and below the data.VI.  Your diagram will look like this:

image.png.deee4288498b2ab6ca738d6af8395e35.png

Plus you will see a ‘Stop’ button on your Front Panel.  Feel free to resize and move this on the panel to a size and place pleasing to you.

You can click the run button and the VI will run, but the data may change too quickly to be useful. Another problem is you don’t know if the data is being updated at a uniform rate or you may be getting too much data.  Most strip chart and data logging applications require data to be collected in samples per second, per minute, or even per hour.

A basic question here is “Why do I need to use a while loop, when I can just click on the Continuous Run button of NI LabVIEW?”  The answer is you can, but there are several reasons why you might not want to.  This is a very simple example, most people will want to more or much more in their VI to get the data and manipulate it in the way needed.  With the example the way it was, each time the VI executes it is creating the task then destroying the task, and then keeps repeating the process.  That is not as efficient as creating the task then looping on getting a new data sample and doing something useful with it.  It may not sound like much of a ‘savings’ now, but when your VI’s get more complicated you will see the value.

Iteration Timing

To slow the looping down, we will add the Time Delay.VI.  It is located on the Functions Palette, select Express >> Exec Control, then click and drag it to inside the loop as shown here.

image.png.8852c3d3f5e4bb50cc1e0d7a886b398c.png

As you can see, a dialog box pops up with this VI, asking you to set the time delay.  For the purposes of this example, I set it to 0.100 seconds so that I get a new sample 10 times a second or 100 mS.

Let’s go ahead and run this from the Front Panel. You’ll see the data is now updated at a much slower rate.  To stop the execution, click on the Stop button you added and not the Abort Execution button on the NI LabVIEW menu.

“Why does this make a difference?” you may ask.  As stated above, in this example, it doesn’t make much difference, but when your VIs are running multiple loops, and high speed scans, the Abort Execution button forces the VI to terminate in potentially bad ways such as not ending a scan correctly or leaving external devices such as relays, solenoids, belts and processes still running why may be hazardous to people and equipment around you.  It is usually best to learn to do this the proper way, and as I have read in numerous NI LabVIEW books and the help, the right way to end looping is via the Stop button, not the Abort Execution.

Error Handling

In its current state, this example can run indefinitely.  That is, as long as the USB-1608G stays connected, powered, and so long as no external influence damages the device.  But what is something like that should occur?  We would want 2 things to happen.  First we’d want to stop the procedure, in this case our loop, and second, we’d want to be notified there was a problem.  To handle these matters, we add error handling.

First we’ll add a way to show there is a problem.  To do this, we add a Simple Error Handler.VI to our VI.  Hover your mouse over the Read.VI, and move it to the Error out pin.  When the cursor changes to the wiring tool, right click to pop up the menu, select Dialog & User Interface Palette, then click on Simple Error Handler.VI and drag it to the right of the Read.VI, outside the while loop. Wire the Read.VI’s error out pin to the Simple Error Handler.VI’s error in.  Next hover mouse over the Simple Error Handler.VI, locate the type of dialog pin, right click, select Create>> Constant.  It will be added as “OK message,” which is fine for this application.

Now we will add a way to exit the While Loop when there is a problem.  Within the while loop, place your mouse cursor on the error out wire from the Read.VI.  When the wiring tool appears, right click, select Cluster, Class & Variant Palette.  Add an Unbundle.VI below and to the right of the Read.VI.  Wire the Read’s error out to the Unbundle’s error in.  You may need to widen the while loop to make it all fit, if so, left mouse click on the blue drag handle on the right side of the while loop and drag it to the right.  You’ll need to move the conditional terminal to the right as well.  Delete the wire between the Stop button and the conditional terminal.  You do this by left mouse clicking on the wire to select it, then press your keyboard’s "Delete" key.  We want to add an OR gate so if the Stop button is clicked OR it there is an error the loop stops.  Right mouse click on the output of the Unbundle.VI, select Boolean Palette and add an OR gate to the right of the Unbundle.VI.  If the OR gate does not already have one of its inputs wired to the Unbundle.VI, wire the other input to the Stop button, and wire the output of the OR gate to the conditional terminal.  Your Block diagram should look like this:

image.png.1378dc5e84070f23b26219d0df102238.png

Run this now, then remove the USB cable from the USB-1608G.  You should see the following error message pop up:

image.png.84cf80dd4dcea01d2656bf0d7dd7ddcd.png

When you click on Continue, notice execution of the VI has stopped.  This is exactly what we want to occur.

By now I hope you are getting the hang of this.  There are 2 more goals to this Knowledge Base article, to add a strip chart and to log the data to a file.  Because NI LabVIEW includes Express VIs for these functions, I hope you’ll find this easy to accomplish.

Display the data in a strip chart

In addition to displaying the data numerically, let’s add a Chart.  From the Front panel, right click on the unused gray area, and select Express>> Graph Indicators, click on Chart and drag it onto the Front Panel.

It should look like this:

image.png.8895069e696c6330154d62fc51c18f1a.png

You’ll need to add a wire from the data terminal (pin) of the Read.VI to the data pin on the Waveform Chart.VI.  Your Block Diagram should look like this:

image.png.71630214e70a9380246e649f562bb736.png

You can run this, but before you do, did you remember to plug the USB cable back in to your device after the error test? 

Your Front Panel should look something like this while it is acquiring data:

image.png.21b24615af4bfa805be509cc958b4151.png

Saving the data to file

 Our last goal is to save the data to a file. Again, we will use an Express.VI to keep this simple.  Right click to bring up the Functions Palette, select Express>>Output and click on Write to Measurement File.VI and place it on the Block Diagram, in the loop, to the right of the Waveform Chart.  Once placed, you will see a dialog box entitled “Configure Write to Measurement File.”  So as to keep this as straight forward as we can, change the file path to something you’ll remember.  Besides that, the only other change is Segment Headers, select ‘One header only’, and click on OK to close the dialog box and save the changes.  Only one wire to add and we’re done.  Wire the Signals input of the Write to Measurement File.VI to the data output of the Read.VI.  Your now completed block diagram VI will look like this:

image.png.241c90b4fd7185968026f4da7a4f53b2.png

Let’s run our VI!  Run it for a bit, then stop it.  If all ran correctly you should have a data file waiting for you.  Though the file is much longer that this, here is sample of contents of the file including the header:

image.png.7a84f1e6b22a4d430192f3113655de82.png

Summary

In part 1, we learned how simple it can be to acquire one sample of data using a USB-1608G, ULx and NI LabVIEW by creating a simple VI.  In part 2, we enhanced the VI we created in part 1 by adding the ability to acquire data at a steady rate, display the data in a strip chart, save the data to a log file, and notify the operator is there is an error.

In part 3, we will add scaling, alarming and a bit of digital control.

Getting started with simple Analog Input_part2.zip

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Guest
This topic is now closed to further replies.
×
×
  • Create New...