a quick tutorial on labview data acquisition

Upload: mino3089389

Post on 03-Apr-2018

240 views

Category:

Documents


2 download

TRANSCRIPT

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    1/12

    A Quick Tutorial on LabVIEW Data AcquisitionR. B. Darling Autumn 2010

    This is a very fast tutorial that can be followed to create a bare-bones Virtual Instrument (VI) in

    LabVIEW for simply acquiring data from a Data Acquisition (DAQ) board. The resulting VI is not aprofessional product, nor is this the way that a power LabVIEW user would do it, but it is the shortest

    distance between two points for getting something that works. Lots of embellishment can then be added

    to make it prettier and better documented.

    This tutorial will walk through the steps to create a brand new VI for displaying data from an

    analog input channel, first as a voltmeter, then as an oscilloscope, and finally as a frequency counter.

    This tutorial is done using LabVIEW 2009, but the steps are nearly identical for other versions of

    LabVIEW from 2007 onward.

    Part 1 - VoltmeterFirst, launch LabVIEW. You should get the main LabVIEW console window which looks like

    this, (for LabVIEW 2009):

    There are a few useful things to note first. The Find Examples in the lower right will bring up

    a window that allows you to browse through a very large number of example VIs. This is a very good

    place to go to find how similar problems can be solved in LabVIEW and how their VIs can beconstructed. Sometimes, just grabbing one of these examples and modifying it slightly is all that you

    need to do. There are quite a few examples which offer solutions similar to what will be constructed here,

    but the point of this tutorial is to show how to build up a basic one from scratch.

    To start, select the New > Blank VI item. This will bring up two new windows: Untitled 1 Front

    Panel, and Untitled 1 Block Diagram. You may also see a palette window and a Tools window,

    depending upon how your default settings are set. In LabVIEW, the Front Panel window is the interface

    that the user will see while the VI is running. The Block Diagram is the programming guts which runs

    behind the interface. When you are working inside the Front Panel, the palette window shows Controls.

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    2/12

    When you are working inside the Block Diagram, the palette window shows Functions. Within the Front

    Panel, inputs are known as Controls, and outputs are known as Indicators.

    Start building the Front Panel by adding an indicator which will be the voltmeter output. To do

    this, click on the Modern tab of the Controls palette (the Untitled 1 Front Panel window must be the

    active one), and click on the upper left box (which contains the thermometer) to open a selection of

    numerical controls. Click on the second one from the left, the Numeric Indicator, and then move the

    mouse over to the Untitled 1 Front Panel window and click about where you want to place the indicator.

    Immediately after clicking to place the indicator, the label above the indicator will allow you to rename it,

    so just type Measured Voltage and click somewhere else in the Untitled 1 Front Panel window to finish

    placing the indicator. An icon for the indicator will also appear in the Untitled 1 Block Diagram.

    To increase the font size of the indicator, select both the indicator and its label, and use Ctrl+= to

    progressively increase the font size. To decrease the font size, use Ctrl+- in the same way. You can

    stretch the width of the indicator to show more digits by grabbing the blue handle and dragging itsideways with the mouse.

    To set how the indicator will display the measured voltage reading, select the indicator, right

    click, and select Properties, which will bring up the Numeric Properties: Measured Voltage window.

    Select the Display Format tab, and select Floating Point, 4 Digits, Digits of Precision, and deselect Hidetrailing zeros. This will create an indicator which looks and acts more like the display from a DMM.

    Click on OK, and the indicator in the Front Panel should now show a displayed reading of 0.0000.

    The screen shot shown below shows where things stand at this point.

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    3/12

    Use the File > Save dialog to name the VI and save it. Pick a directory which is not too deep,

    since LabVIEW will need to access other VIs in its libraries, and if the path is too complicated, LabVIEWwill encounter difficulty later on in linking to those library VIs. It is often a good practice to put the

    current VI under development on the desktop, and only archive away old versions or VIs which are not

    being actively used. When a VI is saved, both the Front Panel and the Block Diagram are simultaneously

    saved as part of that file. In this case, the VI is saved under the name EE436_Voltmeter1.vi.

    Next switch to the Block Diagram, and from the Functions pallette, click on a While Loop from

    the Programming > Structures group. Move the mouse into the Block Diagram window, and use a

    diagonal click and drag to place and size the While Loop. Make this While Loop a big one, since it needs

    to enclose everything within the Block Diagram. Move things around so that the Measured Voltage

    indicator is inside the While Loop.

    Switch back to the Front Panel, and from the Controls palette, select the STOP button from the

    Modern > Boolean group. Move the mouse over into the Front Panel and click to place the STOP button.The button label already says stop, so you do not need to rename it. Use the blue handles to make the

    button larger, if desired. Select the STOP button, and use a right click to select the Properties. In the

    Appearance tab, deselect the Label Visible checkbox, and then click on OK. The STOP button already

    says STOP, so the extra label is not needed.

    Switch back to the Block Diagram, and reposition the icon for the STOP button close to the red

    stop sign in the lower right of the While Loop. Use the mouse to wire the STOP button to the red stop

    sign. When the mouse is near the proper location on the icon to make a connection, it will turn into a wire

    bobbin symbol. At that point just click to make the connection and drag the wire to the desired

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    4/12

    connection point on the next icon. Click it again when you have it there, and the wiring connection

    should be made. At this point, the Block Diagram should look like that shown below. In general, one

    should always put the entire VI inside a While Loop. This allows you to create the STOP button which

    will gracefully close down the VI when you are done with it. This is generally good LabVIEW practice.

    The previous steps are fairly common to all LabVIEW VIs. Next, the data acquisition part will

    be added to create the voltmeter. There are many ways to accomplish this, but the simplest is to use the

    DAQ Assistant. The more professional way to do this is to use the root level DAQmx VIs and chainthese together to create the desired DAQ function and behavior. This requires knowing rather precisely

    what one wants and how to do it. It is the better way to do it when more control and flexibility is

    required, but it takes a little more skill at LabVIEW to do it this way. For simplicity, the method of using

    the DAQ Assistant will be shown here.

    Before starting the DAQ Assistant, or trying to call any of the DAQmx VIs for that matter, the

    DAQ hardware needs to be connected and powered up. For the computers in the EEB-137 electronics

    laboratory, the internal DAQ and GPIB cards inside each computer should fire up as soon as the computer

    is started, so this should already be taken care of. If developing a VI for some DAQ hardware that might

    be portable with your computer, such as a USB-6009 or a MyDAQ, make sure that the drivers are

    installed, and that the hardware is connected and powered up before starting to add any DAQ functions to

    the VI.

    With the DAQ hardware live, make the Block Diagram active, and select the DAQ Assistant from

    the Functions palette within the Express > Input group. Move the mouse over into the While Loop, and

    click to place the DAQ Assistant. After you have clicked to place it, the Create New Express Task

    window will open. In the right pane, expand Acquire Signals, and expand Analog Input, and select

    Voltage. The Physical tab on the next page will then show you a selection of possible analog inputs thatare available to you. This is the reason why you want to have everything powered up beforehand. If the

    hardware is not live, you will not see it in the selection box. Pick the analog input channel of choice. For

    this example, I have picked analog input channel ai0 from the cDAQ1Mod2 (2nd

    module of the 1st

    cDAQ

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    5/12

    chassis), which happens to be a National Instruments model 9229 A/D input module. Click the Finish

    button and the DAQ Assistant then opens. Clicking on the Connection Diagram tab will give you an

    illustration of how the selected channel needs to be connected:

    Click on the Express Task tab to return to the main configuration elements. Under the first tab

    for Configuration, change the range of the input voltage to match to your purpose. In this case, I have

    changed the voltage range to run from 10 V to +10 V. Under Timing Settings near the bottom, the

    Acquisition Mode is one of the most important settings to get configured. For this simple situation where

    we just want the voltmeter to return samples on each call from the VI, select N Samples from the drop list

    box. In theory, the number of samples should be just 1, and in some cases this can be done using the

    setting of 1 Sample (On Demand), but this does not work for all hardware. The setting of N Samples with

    a small number of samples, e.g. 2, usually works the most consistently with all hardware types. In this

    case, the drop list box has been set to N Samples, the Samples to Read set to 2, and the Sampling Rate set

    to its default value of 25 kHz. The DAQ Assistant window should then look like that shown below. Forthis simple situation, the default settings under the Triggering, Advanced Timing, and Logging tabs are

    fine and do not require any modifications.

    Once all of the settings for the DAQ hardware have been made, click on OK. The DAQ Assistant

    will then automatically construct a VI to perform the desired task. All of this will be done in the

    background, and after a short status message Building VI , you will be returned to the Block Diagram

    where the completed Express VI has been placed. In this case for analog input, the light blue colored

    DAQ Assistant block will have been expanded downward and there should be showing a data box from

    which you can then wire the acquired voltage signal to where it needs to go next.

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    6/12

    It would seem logical to simply wire the output of the data connection on the DAQ Assistant to

    the input of the Measured Voltage front panel indicator. Unfortunately, the data types are not the same,

    and a conversion must be made first. From the Functions palette, select Convert From Dynamic Data in

    the Express > Signal Manipulation group. Move the mouse into the Block Diagram, and click to place

    the Convert From Dynamic Data near the right side of the DAQ Assistant. Once you do, a new Configure

    Convert From Dynamic Data window will open. Select Single scalar as the Resulting data type, and click

    on OK. The Convert From Dynamic Data block will now have been configured and is ready to be

    connected. Wire the output of the DAQ Assistant data box to the input of the Convert From Dynamic

    Data, and then wire the output of the Convert From Dynamic Data to the input of the front panel

    Measured Voltage indicator. You will notice that the dynamic data type (thick dark blue wire) is thereby

    converted into a double precision floating point type (orange wire).

    The last element that is required is a timing control which will determine how often the function

    elements inside the While Loop get executed. From the Functions palette, select the Wait Until Next ms

    Multiple (the metronome icon) from the Programming > Timing group, then move the mouse inside the

    While Loop, and click to place the timer. On the left side of the timer, hover the mouse over the

    connector so that it looks like the wiring bobbin, and then right click and select Create > Constant. This

    will automatically create an integer constant whose value you can just fill in. Since we want the

    voltmeter to sample at a 2 Hz rate, enter a value of 500 for the number of milliseconds, and then click

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    7/12

    somewhere else in the block diagram to complete this entry. At this point, the Block Diagram should

    look like that shown below:

    Save the VI, and switch over to the Front Panel window. Click on the Run button (located just

    below the Edit item of the Menu), and the VI should start running, providing voltmeter samples every 500

    ms, as shown below. Any DC voltage applied to the input terminals of the DAQ should now show up as

    a measured voltmeter reading on the VI when it is running. Use the STOP button under the Measured

    Voltage indicator to stop the VI.

    Thats it for creating the voltmeter.

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    8/12

    Part 2 - OscilloscopeCreating an oscilloscope output is very similar to the voltmeter; the only main differences are that

    the output of the DAQ Assistant are sent to a waveform graph instead of a front panel indicator, and the

    sampling frequency is higher so that a smoothly flowing graph is created. In this case, we will start with

    the voltmeter VI and just modify it to turn it into an oscilloscope output.

    Open the EE436_Voltmeter1.vi, if it is not already open, and start a File > Save As dialog.Select the radio button options for Copy (create copy on disk), and Substitute copy for original. Although

    this sounds strange, this the LabVIEW nomenclature for keeping the original VI on disk and opening up a

    new copy in memory which will be given a new name. Click on Continue, rename the copy as

    EE436_Oscilloscope1.vi, and click OK.

    First, delete the front panel Measured Voltage indicator from either the Front Panel or the Block

    Diagram. Next, delete the Convert From Dynamic Data function from the Block Diagram. An easy way

    to then clean up all of the broken wires is to use the LabVIEW hot key Ctrl+B.

    Switch to the Front Panel and from the Controls palette, select the Waveform Chart from the

    Modern > Graph group. Move the mouse over to the Front Panel and click to place the chart. Position

    and size it using the blue handles. Select the Waveform Chart, right click, and select Properties to bring

    up its properties window. Under the Appearance tab, select Sweep Chart from the Update mode. Mostbiopotentials are slowly varying, so displaying them as a sweep chart is usually better for the eye. This

    mimics the classic ECG above the hospital bed type of display. From the Display Format tab, select the

    Time (X-Axis) from the drop list, and select Custom Time Format, HH:MM:SS, and Date unused to

    simplify the time axis legend. From the Scales tab, select the Time (X-Axis) from the drop list, deselect

    Autoscale, and enter Minimum = 0 and Maximum = 5, which will force the time axis to always display 5

    seconds of recorded data. Click on OK to save these settings for the waveform chart. Depending upon

    the application, the vertical Y-Axis can be similarly formatted.

    Switch to the Block Diagram, and use the mouse to connect a wire between the data output of the

    DAQ Assistant and the input to the Waveform Chart. This will be a thick dark blue wire, indicating that

    it is a dynamic data type, but the Waveform Chart is intelligent enough to know how to deal with this

    without any conversions, so this is all that has to be done in this situation.

    Double click on the DAQ Assistant to bring up its configuration window. Change the

    Acquisition Mode to Continuous Samples, change the Samples to Read to 1, and change the Rate (Hz) to

    100. Click on OK, and the DAQ Assistant VI will be rebuilt.

    In this situation, we are letting the DAQ hardware itself drive the instrument timing. All that we

    need the VI to do is accept the samples coming from the DAQ at their 100 Hz rate, and add them into the

    Waveform Chart. However, the VI does not need to wait during this process. Switch to the Block

    Diagram and delete the 500 ms Wait Until Next ms Multiple timer. From the Functions palette, select the

    Wait (ms) timer (looks like a wristwatch) in the Programming > Timing group. Move the mouse over to

    the Block Diagram and click to place this timer. Select the timer with the mouse, hover the mouse over

    the left side, and when the connection terminal is located by the cursor changing to the wiring bobbin,

    right click, and select Create > Constant. Simply click away from the entry field, and this will set the

    constant to a value of zero. This zero length wait timer is needed to make the VI run efficiently and keepthe overall timing coming from the DAQ hardware itself.

    The Waveform Chart uses a history buffer which must be sized suitably large for the amount of

    data it needs to show. In the present case, we are sampling at 100 Hz and wish to show 5 seconds of data

    at a time. The history buffer therefore needs to be sized to at least 500 samples. The default value of1000 is thus sufficient, but in many cases this may need to be increased. To do so, select the Waveform

    Chart, right click and select the Chart History Length . Enter the new value of samples, and click on

    OK.

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    9/12

    Save the VI, whose Block Diagram should now look that that shown below:

    Click on Run, and the Front Panel should then look like the following, with the sweep moving

    across from left to right as the samples come in from the DAQ:

    And thats it for the oscilloscope, which is in some ways a little easier than the voltmeter. For

    more specific applications, the vertical axis on the Waveform Chart can be formatted more precisely, andthe legends and labels made more suitable, too. The Waveform Charts and Graphs have many additional

    features such as cursors, grids, scale menus, and color controls, which are too numerous in their options to

    describe here. Refer to the LabVIEW documentation and on-line tutorials to learn more about these.

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    10/12

    Part 3 Frequency CounterA frequency counter, especially one for slowly varying signals, can be one of the more

    challenging instruments to create. The reason for this is that the acquired signal has to be analyzed as a

    time series of samples; the frequency cannot be determined from any one sample, or any small number of

    samples. In general, that means that the acquired data has to be buffered differently than in the case of the

    voltmeter and oscilloscope, both of which can be configured to produce nearly real-time outputs. For afrequency counter, many samples must be acquired, and then these analyzed to compute the frequency of

    the signal. Also, if any degree of accuracy is desired on the frequency measurement, several cycles of the

    acquired waveform are needed. This is because the normal sampling rate of the waveform may be slow

    compared to the rate which is needed to produce accurate timings of when the waveform crosses a

    threshold level. Most frequency measurements computationally use a histogram method, which normally

    requires several cycles of the waveform to be averaged over in order to compute the histogram. All that

    said, it is fairly challenging to create a frequency counter whose output appears real-time. Usually the

    frequency counter output must lag the data by a few seconds.

    LabVIEW does offer a few functions which make the task of frequency counting easier. One

    fairly simple method will be explained here. If it is not already open, open the EE436_Oscilloscope1.vi

    that was just created, and then perform a Save As to save it as EE436_FrequencyCounter1.vi. In this

    tutorial design, we will keep the oscilloscope function intact to illustrate the acquired data bufferingissues. Switch to the Block Diagram, and from the Functions palette select the Timing and Transitions

    Measurements from the Express > Signal Analysis group. Move the cursor over to the Block Diagram,

    and click inside the While Loop to place the Timing and Transitions Measurements VI. After you

    complete the click to place it, the Configure Timing and Transitions Measurements window will open.Select the first check box for Frequency, and then click on OK to close this window. The VI is now

    configured to measure the frequency of its input signal.

    Move the mouse over the output of the Frequency box until the cursor changes into the wiring

    bobbin. Right click and select Create > Indicator. This will create a properly wired indicator for

    Frequency on the Front Panel. Switch to the Front Panel, select the new indicator for Frequency, and use

    the Ctrl+= and Ctrl+- key combinations to adjust the font size. Use the blue handles to stretch out the

    indicator to show enough digits. Select the indicator and right click to select Properties. Under the

    Display Format tab, select Floating Point type, 4 Digits, Digits of precision as the Precision Type, and

    deselect the checkbox for Hide trailing zeros. Click on OK to accept these changes.

    Switch to the Block Diagram and wire the Signals input of the Timing and Transition

    Measurements VI to the output from the DAQ Assistant, just connecting to the same wire which is

    already going to the oscilloscope Waveform Chart.

    This would seem to be sufficient to make the system work, but this will produce several errors if

    you try to run it as is. The problem is that the oscilloscope VI was set up to take only one sample at a

    time and send these to the Waveform Chart. One sample is utterly insufficient for the Timing and

    Transition Measurement VI to compute a Frequency from. To fix this, double click on the DAQAssistant VI to open its configuration window. Increase the Samples to Read value from 1 to 5k. This

    sets the size of the buffer that is used to hold the acquired values. At a sample rate of 100 Hz, this

    corresponds to 50 seconds worth of data, but in practice not all of that is needed. The algorithm which isused to compute the Frequency output usually needs about 10 past cycles of the waveform to create the

    histogram, which at the slowest anticipated frequency of 0.5 Hz would be 20 seconds of data acquisition.

    At a 100 Hz sampling rate, that gives a minimum of about 2k samples which are required. Click on OK

    to reassemble the DAQ Assistant VI with these settings. Note that the oscilloscope Chart History Length

    buffer is not the same as the data acquisition buffer which has just been set up within the DAQ Assistant.

    These are two different buffers with two different purposes.

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    11/12

    The frequency of biomedical signals are usually expressed in beats per minute (bpm) rather than

    Hz, so the last step is to just pretty-up the front panel indicator. Switch to the Block Diagram, and delete

    the wire connecting the Timing and Transition Measurements VI to the Front Panel indicator. From the

    Functions palette, select the Multiply function from the Programming > Numeric group. Move the mouse

    cursor over into the While Loop of the Block Diagram, and click to place the Multiply function between

    the Timing and Transition Measurements VI and the Frequency front panel indicator. Wire the output of

    the Timing and Transition Measurements VI Frequency box to one input of the Multiply function. Hoverthe mouse over the other input until it turns into the wiring bobbin, right click, and select Create >

    Constant. In the newly created constant box, type 60, and click away from the box. Finally, wire the

    output of the Multiply function to the input of the Frequency front panel indicator. The Block Diagram

    should now look like the view shown below. Note that LabVIEW automatically does a type conversion

    within the Multiply function. The type conversion is denoted by the small red coercion dot.

    Switch to the Front Panel and select the Frequency indicator. Right click and select Properties to

    open its properties window. In the Appearance tab, change the Label to be Frequency (bpm). In the

    Display Format tab, change the Digits to 2, and click on OK to accept these changes.

    Click on Run to start the Frequency Counter VI. The Front Panel should look similar to that

    shown below when the input to the DAQ is a ~1.0 Hz sine wave. In spite of the rather large seeming

    buffer for the acquired data, the update rate on the Frequency (bpm) indicator is only a few seconds once

    the buffer has been initially filled. However, it is fairly obvious that the oscilloscope Waveform Chart

    has completely lost its real-time nature when it, too, now updates only every few seconds. This illustrates

    the basic data buffering trade off in combining a frequency counter with any real-time measurement.

  • 7/29/2019 A Quick Tutorial on LabVIEW Data Acquisition

    12/12

    One solution to this problem is to do some of the frequency counter operations in circuit

    hardware, rather than inside LabVIEW. By using a comparator with a properly adjusted threshold, the

    analog input can also be converted into a digital input whose transition times match to the threshold

    crossings of the analog signal. This digital input can then be used with one of the fairly accurate

    timer/counter modules that are often part of a multi-function DAQ card. The frequency can then beobtained fairly easily from the timer/counter function by just setting up the DAQ Assistant for that new

    digital input channel. In that way, the analog input could still be sampled continuously with 1 Sample to

    Read per acquisition, as needed to produce the real-time Waveform Chart, while the digital input would

    automatically be buffered by the timer/counter module. For higher performance systems which require

    the real-time display, this is usually the way to go. There are some more sophisticated methods for databuffering within LabVIEW to get around this problem using only one analog input, but they are quite a

    bit more complicated to set up properly.

    The above method is one fairly easy way to implement the frequency counter and should prove

    adequate for most applications.