delft university of technology labview introduction course course... · delft university of...

23
Delft University of Technology LabVIEW introduction course exercises document Document details Author: Bart Boshuizen Title: LabVIEW introduction course, exercises Subject: Making a data-acquisition program with LabVIEW Keywords: LabVIEW, course, exercises Version: 20100208 Author details Name: Bart Boshuizen Dept: SSC-ICT 3xO Email: [email protected]

Upload: docong

Post on 28-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology

LabVIEW introduction course exercises document Document details Author: Bart Boshuizen Title: LabVIEW introduction course, exercises Subject: Making a data-acquisition program with LabVIEW Keywords: LabVIEW, course, exercises Version: 20100208 Author details Name: Bart Boshuizen Dept: SSC-ICT 3xO Email: [email protected]

Page 2: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 1

Introduction

Today’s challenge is to create a single LabVIEW program that creates an analog signal, measures it, displays it on a graph and stores it to disk. This should be done in a concise and modular way. It should be easy to expand the number and types of signals generated and measured. The exercises in this document are the programming steps that help you to create a measurement program in a more or less general approach.. Today’s program could look something like the image below and should perform the above mentioned tasks (measure/display/store) for 2 signals each at 100 samples per second.

It is assumed that you have read the course preparation document.

Page 3: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 2

Program setup The program that you will create today should contain the following elements:

- signal measurement: measure 2 signals at 100 samples per second each - signal conversion: convert the voltage signal to the physical signal that it

represents - signal display: display the measured signal on a graph (with 2 separate Y

scales) - signal storage: write the measured data to a file (on demand)

You could also add some:

- signal generation: generate a sine shaped signal on one or two analog outputs - program optimization: split the measurement task from the other tasks - program automation: generate file names based on date and time - program documentation: document (parts of) the program

From nothing to the final program the abovementioned elements will return in the following steps:

1. Starting with a simple calculation 2. Throwing in some structures

a) the While Loop b) the For Loop

3. Creating the first working program

4. Connecting to the real world 5. Writing data to a file 6. Speeding up by changing timing

7. Finalizing

a. Speeding up by splitting tasks b. Adding an output signal c. Using the file module d. Applying scaling e. Modifying controls and indicators at startup f. Reading the scaling parameters from file g. Duplicating the Y-scale h. Starting with the About screen i. ..

All steps include an introduction and instructions. Further instructions can be found on the HOW TO.. page at the end of this document. The first 3 steps help you to familiarize with LabVIEW. The next 3 steps help you to create a working data-acquisition program. At some later time, with step 7, you could finalize your program..

Page 4: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 3

STEP 1 – Starting with a simple calculation INTRODUCTION: Sensors can often be used to convert some physical property into a voltage signal. Data-acquisition hardware can measure these voltages using analog inputs. Later on such measured inputs have to be calculated back to their corresponding physical properties. In most cases that can be done with a simple formula like:

[physical property] = A x [voltage signal] + B Let’s assume we have 2 pressure transducers with the following properties: Type Pressure range [bara] Voltage range [V] “A” “B” Low 0 .. 1 0 .. 5 0.2 0 High 1 .. 10 0 .. 5 1.8 1 This kind of calculation is a typical piece of code that you can use within other programs as a sub VI. Such other program must ’know’ what it can or even must put in and what it can expect back. The way to exchange information with a sub VI is the connector pane. TASK: Create a VI that performs the calculation. INSTRUCTIONS: Creating the code. Start with a blank VI. Place 3 numerical controls, Voltage in, A and B, and a numerical indicator, Signal out, on the Front Panel. Set the default value of A to “1”. On the Block Diagram create the function that multiplies the voltage input with factor A and then adds factor B. Connect the result to the terminal of Signal out. Test for some values. Setting up the connector pane. Go to the Front Panel and right-click on the Current VI Icon. Select the option ‘Show Connector’. Make sure it has the 4-2-2-4 Pattern. If not, then right-click the Connector and select it from the option ‘Patterns’. Set up the connector pane as indicated on the sample connector pane. SAMPLE FRONT PANEL:

SAMPLE BLOCK DIAGRAM:

Page 5: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 4

SAMPLE CONNECTOR PANE:

SAVE AS: cp conversion core.vi COMMENT: It doesn’t matter whether you start creating your program on the Front Panel or on the Block Diagram. In either case you can switch to the other window and change the items that have automatically appeared there. When you switch to the Block Diagram you can turn Highlight Execution on. If you run the program you can see how the data flows through the wires. Notice how data starts to flow at each control terminal. If you use cp conversion core.vi in another program then by setting the default value of A to “1”, Voltage in will at least be multiplied by 1 even if you keep A unconnected.

Page 6: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 5

STEP 2 – Throwing in some structures In a very simple program, data flows from one end (e.g. a control terminal) to the other (e.g. an indicator). Programming structures may alter the data flow e.g. to execute a part of code at one condition and another part of code at another condition. Often you will use structures to repeat some part of code. Such structures are called loops, which LabVIEW offers as While Loop and as For Loop. You will use them in the next two exercises. STEP 2a - The While Loop INTRODUCTION: With a While Loop a piece of code will be executed at least once until a condition is met (usually until something is, or has become, TRUE). The While Loop can be found on the Tools Palette at “Programming’/’Structures’. On many occasions you’ll put a small delay in the loop in order to lighten the CPU load. The Wait function can be found on the Tools Palette at ‘Programming’/‘Timing’. TASK: Create an ‘About’ screen. INSTRUCTIONS: Start with a blank VI. On the Block Diagram add a While Loop. Create a stop control on the Conditional Terminal. Inside the While Loop add a 100 ms delay using the Wait function. Put some decorations on the Front Panel but at least put your name there. SAMPLE BLOCK DIAGRAM:

SAMPLE FRONT PANEL:

SAVE AS: cp about.vi

Page 7: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 6

COMMENT: The effect of leaving out the Wait function can be seen with Windows Task Manager. With the Wait function there is a 50% or more CPU load whereas with the Wait function it is a mere 5 to10%. Before applying this VI as a real About screen it needs to be refined a bit. You can attend to that at step 7. STEP 2b - The For Loop INTRODUCTION: A For Loop executes a piece of code a given number of times. Data (wires) may enter or leave the loop through tunnels. An important feature of tunnels is that they can be auto indexed. For inputs this means that inputs lose 1 dimension (e.g. a 1D array will become a scalar) and outputs gain 1 dimension (e.g. a scalar will become a 1D array). A scalar is a single value (of some type such as numeric, string or Boolean), a 1D array is just an row or column of similar values. The For Loop can be found on the Tools Palette at ‘Programming’/’Structures’. TASK: Create a VI that emulates the 0-5 V signals from the data-acquisition box. INSTRUCTIONS: Start with a blank VI. On the Block Diagram add a For Loop. Create a control count on the Count Terminal. Inside the loop add both functions Random Number and Multiply. These functions can be found on the Functions Palette at ‘Programming’/’Numeric’. Wire the random value to one input of the multiplication and create a constant of 5 on the other input. Connect the output of the multiplication to the edge of the loop, which creates a tunnel with auto indexing on. On the tunnel create an indicator random values. On the Front Panel set the default value of count to “2”. SAMPLE BLOCK DIAGRAM:

SAMPLE FRONT PANEL:

SAMPLE CONNECTOR PANE:

Page 8: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 7

SAVE AS: cp random signals.vi COMMENT: This VI creates an array of “count” values. This resembles the function that you’ll use for the actual data-acquisition.

STEP 3 – Creating the first working program INTRODUCTION: This program combines some of the previously created VIs into a single program that is the true starting point of today’s application. VIs that you have made yourself can be added through ‘Select a VI’ on the Functions Palette. Since this program will add points to a graph on a regular base you will need the Wait Until Next ms Multiple function which you’ll find on the Functions Palette at ‘Programming’/’Timing’ and Waveform Chart which you’ll find on the Controls Palette at ‘Modern’/’Graph’. TASK: Create a program that displays a random signal on a graph. INSTRUCTIONS: Start with a blank VI. On the Block Diagram add a For Loop. Inside the loop put your cp conversion core.vi function. On the left of the loop put your cp random signals.vi function. On the right put the instructor made cp array reshape.vi function. Wire the random values terminal of random signal function to the Voltage in terminal of the conversion function. Notice the auto indexing on the tunnel. Wire the Signal out terminal of the conversion function to the 1D array in terminal of the reshape function. Again notice the auto indexing on the tunnel. Put a While Loop around all code and create a stop button (control) on the Conditional Terminal. Add a Wait Until Next ms Multiple function and create a constant of 1000 on its multiple milliseconds terminal for a 1000 ms delay. On the Front Panel add a Waveform Chart. Stretch the legend of the graph upward so it will accommodate 2 plots. Make sure the terminal of the Waveform Chart on the Block Diagram is inside the While Loop and at the right of the cp array

reshape.vi function. Connect the 2D array out terminal of the array reshape function to the Waveform Chart terminal. SAMPLE BLOCK DIAGRAM (part inside the while loop):

Page 9: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 8

SAMPLE FRONT PANEL:

SAVE AS: cp 01.vi COMMENT: If you leave out the array reshape function, the data from the signal generation function are added to the same line (e.g. Plot 0) in the graph. With the array reshape function the data are reshaped like ‘from a single row to a single column’, which is just one way to add the data to separate lines. For now the conversion function just passes on the data, in other words the data just represents “voltage” signals.

Page 10: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 9

STEP 4 – Connecting to the real world INTRODUCTION: Of course you don’t want computer-generated values but real measurements. With NI devices you can you use the DAQmx functions that you’ll find under ‘Measurements’/’DAQmx – Data Acquisition’ in the Functions Palette. The desired data acquisition should now replace the random signal function. In this case you want to read 1 sample of 2 analog input signals for which you require the ‘DAQmx – Read.vi’ function. Before you can use any DAQmx function you need to create a data-acquisition task with ‘DAQmx – Create Virtual Channel.vi’. After the measurements are done, you should clear the data-acquisition task (and release the resources taken by this task) with ‘DAQmx – Clear Task.vi’. Short example:

Notice the text below some of the DAQmx functions. This text allows you to chose the specific instance of the ‘polymorphic’ function, where a polymorphic function contains a number of functions with similar functionality and that use the same layout of connectors. TASK: Add the data-acquisition task. INSTRUCTIONS: Continue with cp 01.vi and save it as cp 02.vi. Outside the While Loop, at the bottom left, add ‘DAQmx – Create Virtual Channel.vi’ and create a constant on the physical channels terminal. The actual name of the device (here Dev1) may be different and can be found with a separate program called ‘Measurement & Automation Explorer’. The channels that you will use are analog inputs 0 and 1, which are indicated with “ain0:1”. You can select the device and channel(s) with “Browse...”. Inside the loop add ‘DAQmx – Read.vi’ and select the instance ‘Analog 1D DBL Nchan 1Samp‘:

Delete the random signal function and wire the data terminal on the data-acquisition read function to the input tunnel on the For Loop (or to the broken wire to that tunnel).

Page 11: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 10

Outside the While Loop, at the bottom right, add ‘DAQmx – Clear Task.vi’. Wire the task and the Error Cluster as shown in the sample block diagram. SAMPLE BLOCK DIAGRAM (additional part in this step):

SAVE AS: cp 02.vi COMMENT: You now have a complete program that acquires data and displays it with a sample rate of 1 sample per second.

Page 12: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 11

STEP 5 – Writing data to a file INTRODUCTION: The simplest way to store data is to open a file at program start, convert the format of data so it can be written and write the data (probably inside a loop) and close the file at program end. The file functions (open/write/close) can be found on the Functions Palette at ‘Programming’/’File I/O’. These functions keep track of the file status with a refnum, which is short for reference number with which you refer to the file. The function to convert the data to the right format can be found on the Functions Palette at ‘Programming’/‘String’. TASK: Add the functionality to write the data to file. INSTRUCTIONS: Continue with cp 02.vi and save it as cp 03.vi. Outside the While Loop, on the left, add the file function Open/Create/Replace File. On its operation Terminal create a constant “create or replace”. Wire the refnum out terminal onto the edge of the While Loop. Inside the While Loop add the file function Write to Text File. Connect the refnum on the edge of While Loop to the file terminal of this function. Outside the While Loop, on the right, add the Close File function. Connect the refnum out terminal of the Write to Text File function to the refnum terminal of the Close File function. Notice that your VI won’t run. You need to supply data to the text terminal of the Write To Text File function. To do this put an Array to Spreadsheet function somewhere between the text terminal and the wire between the For Loop and the array reshape function. Connect that wire to the array input of the Array to Spreadsheet function and create a constant “%s“ on its format string input in order to convert it to regular strings. Connect the spreadsheet string output of the Array to Spreadsheet function to the text input of the Write to Text File function. SAMPLE BLOCK DIAGRAM (additional part in this step):

SAVE AS: cp 03.vi COMMENT: The file open function requests the user for a file name. The directory supplied must exist, contrary to the file itself. Later on you will see a method where the user will not be asked for a file name. Leaving out the file close function might work or might not work, so for the best result keep it in. The important thing here is that it releases the resources (e.g. memory) taken by the refnum.

Page 13: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 12

A very simple function to write a spreadsheet file can be found in the ‘File I/O’ palette. Don’t be mistaken. This function actually opens a file, appends/writes data to it and closes it. So if you use this function inside a loop it slows down the loop, at least at higher sample rates. You could use this function outside a loop but then you need to be aware that the data must be buffered first. You now have a complete program that acquires data, displays and stores it, all at 1 sample per second. As it is, the program might even work with 100 or more samples per second for all 8 channels but probably with timing issues. In the next step you will change the program in such a way that one timing issue will disappear even when taking 100 samples per second. With even higher throughput it would be prudent to use the ‘Analog 1D DBL Nchan NSamp‘ instance of the ‘DAQmx Read.vi’ function. By using the DAQmx functions the program accommodates most of the recent data-acquisition hardware from National Instruments. But hardware from other vendors could be used as well as long as you replace the DAQmx functions with their counterparts for that hardware.

Page 14: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 13

STEP 6 – Speeding up by changing timing INTRODUCTION: Speeding up the data-acquisition consists of two steps. The first is to use the timing of the hardware to go to higher rates, which you’ll implement in this step. The second is to split the data-acquisition task from other tasks, which you could do at some later time. For DAQmx compatible devices you can set the timing of the data-acquisition task with ‘DAQmx Timing.vi’. You are obliged to set the sample rate and can select the type of measurement or sample mode e.g. “Finite Samples” or “Continuous Samples”, which is appropriate for today’s application. ‘DAQmx Timing.vi’ could also be used for more advanced timing and triggering functions. After setting the timing you might explicitly start the data-acquisition task with ‘DAQmx Start Task.vi’. Short example:

Notice there is no wait function inside the loop. The wait function/timing now is part of the data-acquisition read function. TASK: Add timing. INSTRUCTIONS: Continue with cp 03.vi and save it as cp 04.vi. Remove the wires between ‘DAQmx - Create Virtual Channel.vi’ and the While Loop. On the Functions Palette at ‘Measurements’/’DAQmx – Data Acquisition’ select ‘DAQmx Timing.vi’ and create the constants as indicated on the image above Also add ‘DAQmx – Start Task.vi’ as well. Rewire the diagram as indicated in the short example. Delete the Wait Until Next ms Multiple function and its constant. SAVE AS: cp 04.vi COMMENT: The program can now acquire data at the desired rate which is regulated by the availability of new data. With all task in one loop the data-acquisition can still be delayed by the data storage task.

Page 15: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 14

STEP 7 – Finalizing There are many things to do to finalize your program,,,, You don’t have to follow the specific order in which the following steps are presented, but if you don’t, then please do consider to use some other file names. STEP 7a – Speeding up by splitting tasks INTRODUCTION: Some tasks take too much time. Writing to file and especially starting writing to file is notoriously slow. Since this could interrupt data-acquisition it would be better to split the data display and data storage task from the data-acquisition task. The way to do this is to create a loop for each task and to pass on the data from the data-acquisition loop to the data display and data storage loop. The mechanism to transfer data is queueing. Outside the loops you will have to add the function to obtain a queue. When you do that, you will have to decide what kind of data it will transfer (here a 1D array of ‘doubles’). Inside the data-acquisition loop you will put data on that queue and on the data display and data storage loop you will retrieve data from that queue. Queue functions can be found on the Functions Palette at ‘Programming’/‘Synchronization’/’Queue Operations’. TASK: Add queueing INSTRUCTIONS: Continue with cp 04.vi and save it as cp 05a.vi. Prepare the second loop. Add a While Loop below the existing loop. From the stop terminal create a Local Variable. Set it to ‘read’ and connect it to the Conditional Terminal of the new While Loop. On the Front Panel set the mechanical action of stop button to “Switch When Pressed”. Move the data-acquisition task. On the left of the original loop delete the wires between the last data-acquisition function and the loop. On the right of the original loop delete the wires to the first data-acquisition function. Move the respective sets of data-acquisition functions alongside the new loop. Move the data-acquisition read function into the new loop. And rewire the wires for the data-acquisition task as well as the Error Cluster. Inside the old loop delete the wires for the data-acquisition task as well as the Error Cluster. Prepare the queue. Left of the original (top) loop put an Obtain Queue function. You’ll have to identify the data type of the data you would like to transfer. Since you want to transfer data from the data-acquisition read function, you can create a constant on its data wire. You then move that constant outside the loop and connect it to the element data type terminal of the Obtain Queue function. Connect the queue out terminal of the Obtain Queue function to the While loop – just create a tunnel. Read from the queue. Inside the original loop add the Dequeue Element function and wire the new tunnel on the While Loop to the queue terminal. Next wire the element terminal to the Voltage in terminal on the conversion function. Make sure to create a constant of “100” on the timeout in ms terminal of the Dequeue Element function. Write to the queue. In the new (bottom) loop you can put data on the queue using the Enqueue Element function. Put the function Enqueue Element a little to the right and above the data-acquisition read function. Connect the wire that comes from the Obtain Queue function to the queue terminal on the Enqueue Element function.

Page 16: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 15

Finally connect the data terminal of the data-acquisition read function to the element terminal of the Enqueue Element function. SAMPLE BLOCK DIAGRAM (bottom loop only):

SAVE AS: cp 05a.vi COMMENT: Using a queue to transfer data is unlike data flow. The wire between the queue functions doesn’t represent the data but it represents or rather it is the reference to the queue. The program now should handle 100 samples per second without timing issues. Yet there is much to improve. STEP 7b – Adding an output signal INTRODUCTION: Measuring just noise isn’t fun. With the data-acquisition box you could also generate analog signals. You can do this with some DAQmx functions. Since you will be ‘writing’ data you require the ‘DAQmx – Read.vi’ function. To make this work you can create an additional task in a separate loop. The sine function can be found on the Functions Palette at ‘Mathematics’/’Elementary & Special Functions’/’Trigonometric Functions’. The increment and the divide functions that you will use, can be found on the Functions Palette at ‘Programming’/‘Numeric’. TASK: Add a sine wave signal on one analog output. INSTRUCTIONS: Continue with cp 05a.vi and save it as cp 05b.vi. Add a while loop above the existing loops. Create an extra local variable of the stop terminal and move the local variable to the new loop. Set it to ‘read’ and connect it to the conditional terminal. On the left of the new loop add ‘DAQmx – Create Virtual Channel.vi’ and select the instance ‘Analog Output’/’Voltage’. Create the constant “Dev1/ao0” on the physical channels terminal. Set the maximum value and minimum value to 5 and 0 respectively. To the right of this function add ‘DAQmx – Start Task.vi’. Inside the loop add ‘DAQmx Write.vi’ and keep the default instance ‘Analog DBL 1chan 1Samp‘. On the right of the new loop add ‘DAQmx Clear Task.vi’. Wire the task and error cluster as indicated in the block diagram.

Page 17: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 16

Notice that your VI won’t run. This is because you have to connect the data terminal on the write function. Move the iteration counter to the top part of the loop. To the right of it add the functions divide, sine, increment and divide. Wire these function together with their constants as indicated in the Block Diagram. SAMPLE BLOCK DIAGRAM (the new loop only):

SAVE AS: cp 05b.vi COMMENT: The sine function generates values between -1 and +1. The output voltage can only be positive. That is why you increment it, which results in values between 0 to +2. By dividing it by 2 it obviously becomes 0 to +1. You could as well multiply by 2.5 to get the full 0 to 5 range. STEP 7c - The File module INTRODUCTION: You can replace the file functions with the file module. The file module allows you to stop and start writing during the measurement. It won’t ask for a file name but uses the current date and time to generate a file name. TASK: Use the file module instead of file functions INSTRUCTIONS: Continue with cp 05b.vi and save it as cp 05c.vi. Identify and replace the 3 file functions and replace each of them with the instructor-made file module cp file module.vi. Set the task from left to right to respectively “init”, “add data” and “terminate”. Remove the broken wires and make sure the 3 instances of the module are connected through the Error Cluster. Inside the loop remove the Array to Spreadsheet String function and its “%s” constant. Connect the broken data wire to the data terminal of the “add data” instance of the file module. On the same instance create a control on the write? terminal. On the “init” instance create a control on the path (=directory) terminal. On the Front Panel stretch the path control so it can accommodate the complete path. SAMPLE BLOCK DIAGRAM (changed part only):

Page 18: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 17

SAVE AS: cp 05b.vi COMMENT: A module – or style II global or functional global – is basically a case structure within a while loop that will be executed only once. The task performed depends on the case selector. Between consecutive calls a module can store data using ‘uninitialized shift registers’. You can open the VI and see how it works. STEP 7d - Applying scaling INTRODUCTION: The conversion VI requires both an A and B factor which haven’t been supplied yet and which is why the shown values are between 0 and 5. You could supply the factors through a table such as:

You would have to place the table outside the for loop, either inside or outside the middle. Inside the for loop you would have to split the row that you get with Index Array:

A more general approach would be to group the conversion functionality in a separate module. The supplied module takes care of the conversion but what is more important, it can be expanded without affecting the main program (too much). TASK: Use the conversion module INSTRUCTIONS: Continue with cp 05c.vi and save it as cp 05d.vi. Put an instance of the instructor-made cp conversion module.vi left of the left-most file module. On the action terminal create a constant and set it to “init”. On the conversion factors terminal create a constant. This is an empty 2D array. Extend and fill it as indicated on the before-mentioned table. Wire the error out cluster to the error in cluster on the file module. Delete the for loop with the basic conversion function in it. In its place put another instance of the conversion module which by default will execute the “use” action. Wire the element terminal of Dequeue Element to voltages in and wire signals out to the broken wire(s),

Page 19: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 18

SAMPLE BLOCK DIAGRAM (left of and inside the while loop respectively)

COMMENT: Using a module is a way to add functionality to a program without affecting the structure. As a matter of fact it improves overall readability. STEP 7e - Modifying controls and indicators at star tup INTRODUCTION: You may have noticed that some controls don’t have the desired value at program re-run. Most notably the stop button that has been pressed to stop the previous run and which must be set back manually. One method of changing the value of a control (or indicator) is by using a property node. In general nodes are locations on the Block Diagram where wires start or end e.g. terminals and functions. A property node is a node that allows you to change one or more properties of a control or an indicator. TASK: Set the controls and the chart to their default values at start-up INSTRUCTION: Create a property node on the terminals of write?, stop and Waveform Chart. Place them left of DAQmx Create Channel (for reading signals). Change them all to Write (through the right-click pop-up menu). Select the desired property (again through the right-click poop-up menu) which are Value, Value and History respectively. Create a constant for each property node, the defaults are okay. Wire the Error Clusters and wire the last Error Cluster to the DAQmx function. SAMPLE BLOCK DIAGRAM (additional part):

SAVE AS: cp 05e.vi

Page 20: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 19

COMMENT: Instead of the value property you could use a local variable. But in general using the value property is a better method because you can wire the Error Clusters and thus have control on the execution order. In this case just one property has been changed but you could as well change other properties by expanding the property node. The order of changes is top-down. STEP 7f - Reading the scaling parameters from file INTRODUCTION: Using constants in a program isn’t always practical. The original programmer may know how to change them but even that isn’t certain after half a year or so. The occasional user of the program may want to change the value without the intervention of a programmer. So this calls for a better implementation. In this case the desired values, conversion factors, can be placed in a separate text file. TASK: Read the conversion factors from a file INSTRUCTIONS: Create a tab-separated file conversion factors.txt with the following content: 0.2 0 1.8 1 On the block diagram create the code to read a spreadsheet file. The starting point is the path to the current vi which can be found on the functions palette at ‘File I/O’/’File Constants’. The name of the vi can be stripped of with the Strip Path function which can also be found on the ‘File I/O’ palette. The name of the file with the conversions factors can then be added with the Build Path function, which can be found near the Strip Path function. You create a constant “conversion factors.txt” on the name or relative path terminal of the Build Path function. Next you can read the spreadsheet (type of) file with ‘Read From Spreadsheet File.vi’, which again can be found on the ‘File I/O’ palette. Delete the table constant from the block diagram and replace it with the new code. Wire as indicated on the block diagram image. BLOCK DIAGRAM (new part only):

SAVE AS: cp 05f.vi COMMENT: Using the Current VI’s Path function allows you to move the entire program directory without having to change some text in the program.

Page 21: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 20

Using ‘Read From Spreadsheet.vi’ is a very convenient way of reading data from a file. Especially in this case where data is being read only once and before any other operations. STEP 7g - Duplicating the Y-scale INTRODUCTION: Both signals are on the same scale. But they aren’t of the same scale. You could use separated graphs for separate data but sometimes it is better to add an scale to the graph. One reason to use is a single graph might be there are space (screen) restraints, another reason might be you want to see both graphs together on the same time scale. TASK: Add a second Y-scale INSTRUCTIONS: Go to the front panel. Right-click on the Y-scale and select Duplicate Scale. Next right-click on the new scale and select Swap Sides. Select the Text tool and change the text of the respective Y-axes to “low press (bara)” and “high press (bara)” respectively. Use the Text tool to change the text on the legends to “low pressure” and “high pressure” respectively. Right-click on the graphs in the legend and then select Y-scale and the appropriate scale to use. Right-click on an empty space of the chart and select Visible Items. Remove the mark for Label. The X-scale says “Time” but actually it is the number of the points. You can correct this as follows: Right-click on an empty space on the graph and select properties. Go to the Scales tab and select the Time (X-Axis). Change the Name to “time (s)”. Since the time between points is 0.01 sec change the Multiplier to “0.01”. SAMPLE FRONT PANEL (waveform chart only):

Page 22: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 21

SAVE AS: cp 5g.vi COMMENT: These may seem just cosmetic changes yet the average user wants a screen layout that has been made with care and where what you see corresponds with the measurement.. STEP 7h - Starting with the About screen INTRODUCTION: An About screen is often a good way to show some program information such as version number and author name. TASK: Create an About screen as pop-up at program-start INSTRUCTIONS: Open your cp about.vi. On the front panel add an Error In control and an Error Out indicator, which you’ll find on the tools palette at ‘Array, Matrix & Cluster’. Wire them on the block diagram. Put Error In and Error Out on the bottom left and right of the connector pane respectively. Resize the front panel so you don’t see the error cluster anymore. Change the appearance of the window. From the File menu select VI Properties. In the Category Window Appearance select “Dialog” furthermore change the title of the VI to “About the LabVIEW course” or whatever suits you best.. Save and close the VI. Open your latest project VI. On the block diagram add cp about.vi. Wire its Error Out terminal to the Error In terminal of the write? property node. SAMPLE FRONT PANEL (during runtime):

SAVE AS: cp 5h.vi COMMENT: There are still many things that you could change. It’s up to you! STEP 7i - ..

Page 23: Delft University of Technology LabVIEW introduction course course... · Delft University of Technology LabVIEW introduction course 2 Program setup The program that you will create

Delft University of Technology LabVIEW introduction course 22

HOW TO.. SET THE..

..cluster size Right-click on the Array to Cluster node. From the pop-up menu select ‘Cluster Size…’ and set the desired size. ..default value of a control Go to the control (on the Front Panel). Set the desired value. Right-click and select ‘Data Operations’/’Make Current Value Default’. Don’t forget to save the VI after this operation.

INSERT A.. ..function into a wire

Right-click on the wire into which you need to insert some function. Choose ‘Insert’ and choose to search the desired function either on the suggested Palette or on all Palettes.

CREATE A.. ..Constant/Control/Indicator

On constants, terminals and tunnels you can create either a Constant, a Control or an Indicator. Right-click on the item (constant, terminal or tunnel). From the pop-up select ’Create’ and what you want to create. ..Local Variable/Reference/Property Node/Invoke Nod e On a terminal (control or indicator) you can also create either a Local Variable, a Reference, a Property Node or an Invoke Node. Right-click on the terminal. From the pop-up select ’Create’ and what you want to create.

REMOVE.. ..broken wires If you want to remove ALL broken wires then press <CTR><B>

SWITCH BETWEEN.. .. the Front Panel and the Block Diagram

You can use <CTRL><E> but you can also use the menu and select ‘Window’/’Show…’. If both windows are open you can also just click on the other window. .. tools With autoselect tool off you can quickly select another tool (and mouse pointer) yourself. To switch between the most common tools use <SPACE>. Use <TAB> to switch between a larger set of common tools.. If this doesn’t work select the desired tool from the Tools Palette.