Labview and Mydaq

March 31, 2018 | Author: kalpesh_adit1 | Category: Control Flow, Boolean Algebra, Data Type, Boolean Data Type, Input/Output
Share Embed Donate


Short Description

Download Labview and Mydaq...

Description

LabVIEW and myDAQ National Instruments NIWeek 2010 Andrew Milluzzi

What is LabVIEW? LabVIEW is a graphical programming language that implements dataflow programming. LabVIEW was first developed for virtual instrumentation. In other words, “the software is the instrument.” Each LabVIEW program is called a VI which is short for Virtual Instrument. A VI contains two windows, a “Front Panel” and a “Block Diagram.” The front panel is the user interface to the instrument. For example, imagine an oscilloscope, there is a graph, buttons, knobs, dials, etc. The front panel of a LabVIEW VI allows you to create these interfaces in software. The block diagram is like the wiring inside the oscilloscope. You can place blocks to add gain, process waveforms, etc. While LabVIEW was initially developed for instrumentation, it has been used in many different applications. For example, one of the most public applications of LabVIEW is the LEGO Mindstroms NXT. Additionally, LabVIEW has also been used in robots in the DARPA Challenges, to perform real time analysis of the Birds Nest Olympic Stadium in Beijing, and even control CERN’s Large Hadron Collider. Today, however, we will be using LabVIEW for its intended purpose and interface with the new NI myDAQ.

Getting Started – Calculator Addition Before we get to the myDAQ, let’s learn some basics about LabVIEW. After you have installed LabVIEW and the myDAQ drivers, open LabVIEW 2010. When the software finishes loading, you should see the screen pictured right. For our experimentation, click Blank VI under the New category to launch a new VI. This will launch the two windows that make up a virtual instrument. On either window, click File»Save to save the VI. Save the program under the name Calculator. For this first program we will make a simple calculator that can add, subtract, multiply and divide. Ensure the window named Front Panel is open. We first need to make Figure 1: LabVIEW Getting Started Window the interface for our program. For our calculator, we will have two controls and one indicator. A control allows the user to input data to the block diagram. Remember that the end user does not have access to the block diagram at runtime. An indicator will allow the block diagram to communicate data back to the user. Let’s place two “Numeric”1 controls and one “Numeric” indicator as show in Figure 2. The controls and indicators can be found in the Numeric Palette. In Figure 2, I have named the controls and indicators. To change the name of a control or indicator, double click the existing name, and enter a new name. Also, should you wish to include a 1

“Numeric” controls or indicators are number inputs and outputs. Other types include Boolean, String, Array, Cluster, and Graph. There are other data types, but this will be enough for this demo.

Page | 2

name or instructions, you can double click anywhere on the front panel and the text tool will allow you to create a text box. Now let’s look at the block diagram. Notice that there are already three blocks on the diagram. These blocks represent the controls and indicators you placed on the front panel. Also note the colors of the blocks. LabVIEW defaults to the double precision data type2. You can select a different data type by right clicking the block and selecting Representation. For our demo, leave the values as double precision. Let’s wire up our first simple program that performs basic addition. To make LabVIEW perform an add, select Programming»Numeric»Add from the functions palette. This will place an add block on the block diagram. Now hover your mouse over one of the terminals of the blocks.3 Note the change in cursor. The wire coming off the spool is the new point of the cursor. Wire each control to a terminal on the add block. Then wire the output of the add block to your indicator as shown in Figure 3. Note how the data will “flow” from left to right on the block diagram. Congratulations, you have written your first LabVIEW program!

Figure 2: Calculator Front Panel

Running a LabVIEW VI

Figure 3: Calculator add Block Diagram

Now that we have made our program, we need to run it. There are a few options when it comes to running a LabVIEW VI: Single Execution and Continuous Execution. Single Execution will run your program once, while Continuous Execution runs until the software encounters an error or you abort the program. In the upper left corner of both the block diagram and front panel, you will find a set of symbols as shown in Figure 4. These icons perform the following functions from left to right: Single Execution, Continuous Execution, Abort, Pause, and Highlight Execution. Figure 4: Execution Buttons Highlight Execution (sometimes called “Debug mode”) allows you to see an animation of the dataflow in your program. Little circles, representing the data, flow between blocks and green arrows appear on blocks that are currently running. This can be a great way to debug programs. Let’s enter some numbers on the front panel and click the Single Execution arrow. Notice that the indicator now shows the sum of the two numbers you entered. Try Single Execution again, but before you click the arrow, click on the Highlight Execution light bulb. The program runs slower, but if 2

Double precision is a 64-bit floating point data type. In other words this allows decimal numbers to be returned to the user. Fixed point, integer (both signed and unsigned), and single point data types are also supported for numeric data. 3 When your mouse hovers over a terminal, the mouse cursor will switch to a spool of wire and a dot will appear on the block, representing the terminal.

Page | 3

you look at the block diagram during execution you can see the animation. This can be very useful! Now a calculator isn’t very useful if we keep having to hit run each time. Try clicking Continuous Execution (you should turn off Highlight Execution). You should be able to change the values in the controls and watch the indicator change as well. This is great and all, but wouldn’t it be cool to perform more than one function?

Case Statements and Subtraction Software becomes “smart” when we allow it to make decisions. In most languages this is using a structure called an “If” or “Switch” statement. LabVIEW groups these concepts into its “Case Structure”. The case structure is located on the Functions Palette under Programming»Structures»Case Structure. Let’s add one to the block diagram. Click the icon, and note how the cursor changes.4 Draw a box around the add block (and only the add block).5 You can draw the box by clicking where you want the upper left corner of the box to start and drag to where you would like the lower right corner of the Figure 5: Case Structure box to be. The case structure is shown in Figure 5. The case structure has a small block on the right edge. This is where you would wire the boolean to control which case is executed: true or false. We will use a simple front panel control to pick add or subtract. Right click on the left terminal of the decision block and select Create»Control. This is a great shortcut when working on the block diagram. If you switch to the front panel, it should look similar to Figure 6. Notice that the run arrow is broken. This means our program is not complete. We need to add something to the false case of the case structure. To switch the structure, click the arrow pointing left or right by where Figure 6: Boolean Control on Front Panel it says True. Now in the same palette as the add block, drop in a subtract block and wire up the inputs and outputs. Our calculator can now add and subtract. Run your program setting the Boolean control to add or subtract.

Multiply and Divide Adding the multiplication and division functions is similar to how we added the subtraction function. I had mentioned above that a case structure can act as a “Switch” statement. To use this functionality you need to wire up a numeric control instead of a Boolean control. To keep things simple, we can simply add a numeric control on the front panel and delete the Boolean control. Let’s wire this control up to 4

There are no blocks for structures, rather they will change the cursor and allow the user to draw the size necessary for the application at hand. 5 Drawing the case structure around more than the add block will cause the indicators only to be read when running that case. This can cause excess controls and indicators and clutter the front panel.

Page | 4

our case statement as seen in Figure 7. Notice how I set the representation of the control to U8. This means we are using an unsigned 8-bit integer. Also notice that the “false” case becomes 0 and also contains the term “Default.” In a switch statement, should a match not be found, we need to run some code and the default case is the one that is executed. We need to add 2 new cases to support multiply and divide. To add a new case, right click and select Add case after. This creates a new case in which to place the divide function. Repeat this action to create a case for the multiply function. Wire up the terminals to the input and outputs and try running your program. You have now implemented the 4 basic arithmetic Figure 7: Case Structure with Numeric Input functions. By selecting 0 through 3 on the switch statement control, you can control which function is performed. This is good and all, but what about looping?

Looping in LabVIEW We have already seen how we can loop through our code via continuous execution; however sometimes we want control in our code. Looping in the code allows us to have only sections of code loop. LabVIEW provides several types of loops. Like other programming languages, LabVIEW provides “For” and “While” Figure 8: While Loop loops. Loops are located on the functions palette in Programming»Structures. For our code, let’s use a While Loop. Like the case structure, you will need to draw the box. This time, however, draw the box around the controls, indicator, and case structure. This ensures the latest values are processed. When you have completed these steps, your code should look like Figure 8. Notice that the run arrow is still broken. This is due to two new blocks in the While loop. The While loop adds both an iterator block (the “i” inside the blue box in the lower left corner of Figure 8). This counts

Figure 9: Finished Calculator Block Diagram

Page | 5

the number of times the code has executed.6 The other icon in the lower right corner of the loop is the “Stop if True” icon. The loop will continue to iterate until a value of true is passed to this block.7 For our use we will wire a control to the stop condition. Your final block diagram should look like Figure 9.

Other LabVIEW Programming Tips LabVIEW programs can sometimes lead to a large mess of wires. To combat this, National Instruments provides a Block Diagram Cleanup Tool. Click the icon pictured in Figure 10 to have LabVIEW automatically clean up the block diagram. Another useful method to organize your block diagram is to clean up each wire. If you right click on Figure 10: Block a wire, you can select Clean Up Wire. This will make the wire auto route from Diagram Cleanup Icon terminal to terminal and can make your code much easier to read. Finally, adding comments to a block diagram can do wonders for code readability. Just like the front panel, double clicking the background of the block diagram creates a text box for comments. Furthermore, these comments move with the wires if you use the Block Diagram Cleanup tool. Remember to save your code early and often. One last function to note is the use of help. LabVIEW integrates two forms of help files. If you press , you will bring up the Context Help window. This window provides a brief description of the VI as well as showing required terminals in bold text. However, this help is sometimes not Figure 11: Context Help enough. You can click Detailed Help to get a help document on a given VI. The help document usually contains details about each terminal and even provides code snippets or examples.

Data Acquisition – NI myDAQ Voltage Generator Now it is time to do what LabVIEW does best: gather and display data. Everyone in this session has been provided with the new NI myDAQ. The myDAQ has 8 digital outputs, 2 analog inputs and 2 analog outputs.8 For this demo we will wire the provided motor to analog input 0 (AI0). Motors can act as generators, translating mechanical energy into electrical energy. When you turn the motor shaft, a small voltage is made between the wire leads. For fun, let’s measure this voltage.

6

The counter starts at 0. Thus your stop condition must be one less than the total number of iterations you would like. 7 Another possible symbol in this position is the “Continue while True” icon. As the name suggests, the loop will continue until a false is passed to the block. 8 For more information on the NI myDAQ, goto http://www.ni.com/mydaq

Page | 6

Open a blank VI. Let’s start with the front panel. The front panel is rather simple. All we need is a Waveform Chart and a Stop button. The stop button will come with the loop, so you don’t need to make it now. The Waveform Chart can be found on the Controls palette in the Graph sub-palette. After you add the waveform chart, your front panel should look like Figure 12. The default waveform chart will auto scale amplitude and will update with the latest data (providing the chart is placed in a loop). You can double click the name of the chart, the amplitude and the plot to rename each element. You can also right click and select “Properties” to configure line width, and other display attributes. Before we move to the block diagram we need to Figure 12: Generator Front Panel do some configuration on the myDAQ.9 To configure the myDAQ we need to open MAX.10 When launching MAX, you should get an interface close to Figure 13. Under My Computer select Devices and Interfaces. MAX searches all ports for connected devices and the myDAQ will appear in this menu. Note the name of the myDAQ. I have named mine “AndymyDAQ.” To rename the device, right click the current name and select Rename. Once you have changed the name of your device and we know it is present, we can return to LabVIEW.

Figure 13: Measurement and Automation Explorer (MAX)

On the block diagram we need to use some new blocks to set up and use the myDAQ. On the Functions palette, expand the Measurement I/O sub-palette. We will be working specifically with the NI-DAQmx palette. The first thing we will need to do is create a connection to the myDAQ. To do this we will use the

Create Virtual Channel VI. This block uses a different way to select data for the function. Select AI Voltage from the list below. We will also need to input a channel. Right click the physical channels input and select Create»Constant. Your block diagram should now look similar to Figure 14. Next we are going to need to get the data from the myDAQ. We will use the DAQmx Read VI and set it to Analog DBL 1 Chan 1 Samp. We will wire our virtual channel out of our

Figure 14: Create Channel Block

9

The myDAQ does provide some autoplay features. These include an oscilloscope, function generator, DMM, etc. We are going to use the device directly, so you can close that window. 10 MAX is short for the Measurement & Automation Explorer.

Page | 7

Create Virtual Channels VI to the channel/task input of the Read VI. Next wire the data out to the waveform chart. We can now read samples from the myDAQ. Since we are going to want multiple samples, we need to put the read block inside a while loop, like before. Create a control for the stop and your block diagram should look like the one pictured in Figure 15. We are almost ready to gather some data, but we need to close communication with the myDAQ. To close communication we need to use the DAQmx Clear VI. This will free the myDAQ to interface Figure 15: Read Loop on a different channel or allow you to run another function of the myDAQ. Wire the task out from the Read block to the channel/task in of the Clear block. Also note that colored squares appear on the While loop. These are called data tunnels. Data tunnels can take several forms, from indexing data to shift registers, to just passing data in and out. In this case we will just allow the data to pass in and out. Your final block diagram should like the one pictured in Figure 16. Now we can gather some data. On the front panel select single execution and begin to spin the motor. This will generate a potential difference. Keep spinning the Figure16: Completed block diagram motor and gather some data. This rather simple VI is generating data that can then be processed to relate angular speed of the motor shaft to the voltage generated at the wires or how the direction of rotation can relate to the sign of the voltage generated. Figure 17 shows some sample data I generated. Remember to use the stop button to finish the data collection. If you use the abort button, you will leave the channel open and will need to restart LabVIEW.

LED Dimmer Digital motor control and lighting can be controlled through a digital waveform process called pulse width modulation. The NI myDAQ provides a National Instruments timing chip. This device will allow for Figure 17: Sample data from motor generator

Page | 8

precise timing on select DIO channels11. Our first example will show the limitations without time enabled. Later, we will revise our code to leverage the power of this chip and see improved dimming. This example is going to use a few new features including shift registers and comparisons. First we are going to need to open a digital channel. This is similar to the analog channel we used in the previous example. Open a new VI and place a DAQmx Create Virtual Channel VI. Select Digital Output from the drop down box and create a new channel constant. The LED is wired between DIO0 and ground. Next we will add a DAQmx Write block. Select Digital Bool 1Line 1Point in the drop down menu. When Figure 18: Open Channel and DAQmx Write blocks you are done, your block diagram should look like Figure 18. Next we will want to clean up the channel. First let’s add another Write block to set the channel to off. To make it turn off the light, wire a false Boolean constant to the data in channel of the block. Then we can use the DAQmx Clear block, as before.

Figure 19: LED DAQ Blocks

Now comes time for the logic. Let’s first start by placing a While loop around the first Write VI and wire up a control to the stop, as shown in Figure 19. Next we will need to place a Case statement. Our loop will use a counter to keep time in our loop. This can be done with a case statement and some comparison blocks. First, create a numeric constant outside the loop. To

make a numeric constant you can go to the Functions palette and select Programming»Numeric»Constant. Set the value of the constant to 1. Next create another constant inside the loop. Set this constant value to 4.12 We have now made our comparison value (the constant in the loop) and the index (the constant outside the loop). Now we need to add the Case statement. Make a small box inside the loop. For the True case we will place another constant and set its value to 1 as shown in Figure 20. Next 11

Figure 20: Constants and Case Structure

The timing functions are called “Counters” in the DAQ Assistant/DAQmx. See the myDAQ technical specifications regarding which DIO ports support timing. 12 As mention before LabVIEW passes data, and therefore does not often use variables. Thus we use constants to create the data stream and then modify these values. This is similar to writing int example = 42; in C and then modifying example later.

Page | 9

we will want to wire up the False case. The false case is simply an increment block. This can be found on the Functions palette under Programming»Numeric»Increment. Wire the constant outside the While loop to the input of the increment block. Note that a tunnel is created. We are going to want to replace this tunnel with a shift register. This can be done by right clicking and selecting Replace with Shift Register.13 Click on the right side of the loop to place the second terminal of the shift register. Wire the output of the increment block to the shift register. Also remember to wire out the True case. 14 Figure 21 shows the proper wiring of these nodes. Now we need to add some comparisons. We want the value of our counter to reset once Figure 21: Shift Register we reach a value of 4. To implement this comparison, go to the Functions palette and select Programming»Comparisons. In this case we will want to use the “Equals” block. Add this to the block diagram and connect the constant containing the value 4 to one input and connect the numeric wire leading to the case structure to the other input. Wire the output of this comparison block to the Boolean selector block on the case structure. This will cause our counter to reset when the desired value is reached. Next we are going to want to compare our counter to a user input value. For this we will use a “Less than or Equal to” block from the Comparison palette. Again connect the wire coming from the shift register to the top terminal of the comparison block. Next right click on the lower terminal and create a control. Your code should now look like Figure 22.15 This completes the block diagram. Now Figure 22: Completed Block Diagram let’s go to the front panel and select a different control interface. On the front panel, click the default numeric control and select Replace to locate a fitting control. I used the slider, but any control will do. Now run you program and vary your control from 1 to 4. Notice how the LED blinks at faster rates and can give the appearance of dimming. This is all fine and dandy, but it is not perfect. Let’s explore some more precise timing.

13

A shift register in LabVIEW does not actually shift bits, but rather stores values on loop iterations. Failure to wire out both cases of a case structure can lead to a broken run arrow. 15 Note the red dot on my comparison. This is called a coercion dot; in other words, a dynamic type case. This is due to the fact thatI created my input before I made the comparison. Your code should not have this red dot. 14

Page | 10

LED Dimmer – Timing Timing can be a tricky. Most computers and operating systems execute code as fast as possible. However, in most DAQ applications, we need to gather data at known intervals. There are two solutions to this problem: Real-Time Operating Systems (RTOS) or a DAQ timing chip. National Instruments has actually developed a RTOS called LabVIEW Real-Time. This operating system can be embedded such as in an NI Compact RIO or run on a computer like your desktop or an NI PXI system. Not all applications require the complexity of a RTOS. National Figure 23: DAQ Assistant Instruments has developed a computer chip that performs this timing on simpler systems, such as a myDAQ. To access this power we will need to use a DAQ task. Since tasks can become complex and are a more advanced topic, we will use the NI DAQ Assistant. The DAQ Assistant is a wizard that will create complex DAQ functions and tasks. It removes the coding form the user to speed up development. It also hides all the complex code behind a DAQ Assistant block. To make a precise dimmer, let’s create a blank VI and drag over a DAQ Assistant. The DAQ Assistant can be found in the DAQmx palette. When you drag over the block, a window will appear. This window will look like Figure 23. For this example we will want to generate some signals. Under Generate Signals, select Counter Output»Pulse Output. When you click on “Pulse Output” you will be taken to a window similar to Figure 24. You should now be able to select ct0 and click Finish. 16 The DAQ Assistant will open up a new window that will allow us to configure some details about the signal that we wish to generate. First thing we will want to do is generate several signals. Under Generation Mode in the bottom of the screen, select N Pulses. The default value is fine. Next, under Pulse Output Setup we can set our high time and our low time. I will leave them at the default value,

Figure 24: Physical Channels

16

It should also be noted that the process is similar for acquiring signals. The wizard will walk you through the steps and generate the code for you.

Page | 11

but feel free to explore. After you finish your window should look similar to Figure 25. Click OK and the DAQ Assistant will generate the code, leaving a simple block on the screen. Now let’s make our code loop to keep the light on until the user requests a stop. Add a While loop around the DAQ Assistant block and create a stop control. Also wire the stop control to the stop of the DAQ Assistant. Now run your code! You have successfully used the timing chip in the myDAQ and the DAQ Assistant to create a precision timed dimmer. Play around with the dimmer and see what else you can create!

What’s Next? This concludes this myDAQ hands on tutorial. You have a very powerful NI device at your finger tips. To find ideas for cool things to do with NI myDAQ or to upload your own applications, visit http://ni.com/mydaqzone. Also see http://ni.com/mydaq/go for more help getting started with the device.

Real Engineering As mentioned before, LabVIEW and NI DAQ are Figure 25: DAQ Assistant Timing Window widely used in industry. For example, mechanical and electrical engineers at Los Alamos National Labs use NI DAQ systems with LabVIEW to aid in the development of renewable energy. Another cool example is Discovery Channel’s Mythbusters, who use NI devices to bust some of the coolest myths, including determining if a soda thrown out a car window can be lethal. LabVIEW can even interface with MatLAB, .NET, and C libraries meaning the possibilities are endless. Have fun with your NI myDAQ and LabVIEW, and don’t forget to tell us what interesting things you are doing with NI myDAQ by visiting http://ni.com/mydaqzone.

Page | 12

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF