M.E. 401 Mechatronics Laboratory Two Basic Programming in LabVIEW Programming Structure Required Materials Hardware P.C. Multi-Function data acquisition board One LED Software Measurement and Automation explorer (MAX) LabVIEW Study Material Course Text LabVIEW Manual Purpose The following tutorial will continue the introduction to LabVIEW, with particular focus on loops and conditional statements. Introduction Loops and conditional statements are vital components of even the most basic programming applications. The four structures we will focus on in this tutorial are the Sequence, Case, For Loop, and While Loop. The Sequence Structure allows the user to specify an ordered set of events. This structure is created graphically as a series of frames that are executed in a desired order. Sequence structures can be either Flat or Stacked. These structures are almost identical, having film strip-like borders, and they execute the same way, from first to last in order. We will almost always use the Flat Sequence structure because all of the code is visible and it is much easier to keep track of dataflow inputs and outputs.
The Case Structure is similar to the if and else statements found in traditional textbased programming languages. The case structure is used to test if certain conditions are satisfied, most commonly using true and false statements. In addition to Boolean inputs, the case structure does allow for numeric and string inputs so that more than two frames can be utilized. This structure can be nested to test for a variety of outcomes, but remember that if you wire an output in one frame, you must wire the output from all subdiagram frames in the case structure. You can toggle between subdiagrams using the arrows at the top of the frame. The For Loop iterates a specified number of times, executing the desired commands during the process. Two details require particular attention. First, the count terminal (seen as N in the diagram window) determines the number of iterations. Once this value is available, the iterations begin at zero. For example, ten iterations are seen as N=0, 1, 2...,9. Second, the i in For Loop is the iteration terminal and returns the current iteration index. In Lab 1 we saw that the While Loop structure repeats its commands until the appropriate test condition fails. In LabVIEW, the While Loop evaluates the test condition at the end of the loop so that the loop is always executed at least one time. The one-time execution is an important characteristic to be aware of, as we do not wish to damage any hardware that we are using through exposure to an unexpected high voltage. The While Loop contains a Conditional Terminal that expects a Boolean (T/F) input. The While Loop repeats while false by default and will Stop if TRUE. (You can change the terminal to have it loop while true by selecting Continue if True from the pop-up menu). Note: Remove structures, don t just delete them! Using the delete key will delete the contents of the structure, too. Whenever you want to remove a structure be sure to use the right-click pop-up menu and select Remove While Loop or Remove For Loop. This will remove the structure, but leave all the other code on the block diagram. Part 1: Create VI Purpose: Design a virtual instrument (VI) that when powered ON, sends a series of pulses through the digital channel to the LED. Overview: A Case Structure should be used as a test to determine if the power to the instrument is ON or OFF. If the power is ON (Case is TRUE) use a While Loop to execute commands. Place a sequence in the while loop that alternately supplies and terminates the power to the LED causing the LED to pulsate. If the power to the VI is OFF (Case is FALSE) the LED should not be powered and the light will remain OFF in this case.
Lab Procedure: 1. Create a new VI with an On-Off switch in LABView as was done in Lab 1. 2. Configure a digital output channel a. Create a Digital Channel as in lab 1 using DAQmx Create Virtual Channel.vi and place on the left side of the block diagram. b. Add a control to select the appropriate line you want to control. Move to the Block Diagram and configure the internal components of your VI. 3. Create Case Structure a. Right Click in the block diagram to bring up the Functions Panel, and select Case Structure from the Structures subpalette. Click and drag cursor in the block diagram to place and size the structure. (Create Virtual Channel and Boolean Control should be outside and to the left of your case structure) b. Wire the Boolean Control to the Case Structure by attaching the T/Fbox to the case selector on the case structure frame. located c. Configure a Flat Sequence Structure i. Inside the True frame of the case structure, add a Flat Sequence Structure from the Structures subpalette of the Functions menu. The Flat Sequence Structure will be used to: ii. Right Click on the Flat Sequence border frame and select Add Frame After. You should have two movie frames inside the Case Structure. You will use these frames to set up the True-False conditions that will be used to turn the LED on and off in sequence. iii. Inside the first Flat Sequence Frame, add DAQmx Write. Right Click on the Arrow, Select Type=>Digital=>Single Channel=>Single Sample=>Boolean (1line). iv. Right Click on the DAQmx Write Data terminal and select Create Constant. Toggle Constant to TRUE in the first frame.
v. Following the same procedure, add DAQmx Write to the second frame in the sequence. Toggle the Constant to FALSE in frame 2. 4. Configure Time Constants In order to control the amount of time the LED stays on or off in each frame, we will add time constants. a. Right Click in the block diagram to bring up the Functions Panel and select Timing=>Wait (ms). Place one Wait (ms) in each frame of the Flat Sequence structure, Right Click on the wait milliseconds (left-hand) terminal and Create=>Constant. Set the constant to 500 by double clicking in each frame. Your Block Diagram should look something like this so far While Loop: executing commands until a condition is TRUE. When the power to the instrument is ON (Boolean TRUE) the VI will send a series of pulses to the digitally connected LED. To accomplish this task, we make use of a While Loop that executes the commands within its window until a condition is TRUE. The condition we will use is a certain number of iterations. 5. Configure the While Loop a. Add a While Loop and place it outside of the Case Structure. The Boolean Control should be inside the While Loop, but outside the Case Structure. 6. Add Numeric Control to the While Loop a. On the Front Panel, add a Numeric Control, rename it Number of Pulses to be Executed. b. On the Block Diagram, move Number of Pulses to be Executed inside the While Loop between the Condition and Iteration terminals of the While Loop.
c. Right Click in the block diagram and select Comparison=>Greater Than or Equal?and place it inside the While Loop next to the Number of Pulses to be Executed control. d. Configure the Numeric Control i. Wire the Loop Iteration terminal of the While Loop (i) to the x terminal (top input) of the Greater Than or Equal? function. Wire the Number of Pulses to be Executed terminal to the y terminal (bottom input) of the Greater Than or Equal? function. Wire the Greater Than or Equal? output terminal to the Conditional terminal of the While Loop. 7. Create an Iteration Incicator a. Create an indicator on the front panel that shows the number of iterations that have been executed. i. Right click on the Iteration terminal of the While Loop and select Create=>Indicator. Rename the indicator on the front panel to read Pulses Executed. 8. Wire the DAQmx Digital Channel terminals. a. Wire the Task and Error output from Create Virtual Channel to the input of DAQmx Write in frame 1 of the sequence. Similarly, wire the output of DAQmx Write in frame 1 to the input in frame 2. b. Add DAQmx Clear Task.vi to the outside-right of the While Loop in the Block Diagram. Wire the task and error output of DAQmx Write in the second frame to DAQmx Clear Task. Check your work by making sure that there are connection tunnels passing through each of the While Loop, Case Structure, and Sequence Structure. The tunnels, or data entry and exit terminals, on the right-hand side of each structure will have white centers because the FALSE case has not been wired yet. c. Toggle case structure to FALSE. Wire Create Virtual Channel task and error out terminals to Clear Task VIs task and error in terminals by spanning the empty Case Structure. You should now be ready to run your program. Set the Number of Pulses to be Executed to 5. Press the Power button to the ON position. Select the digital line you are using. Press the Run button. Change the number Pulses to be Executed, run your program and count pulses. (If you turn the highlight execution button in block diagram to on, it will take a while for the LED to response)
Questions: Did your LED pulse? How many pulses did you count, and how many iterations were executed? Explain why. Given that the iteration terminal counts iterations starting at zero, how can you modify your program to accurately reflect the desired and executed number of pulses, rather than iterations? Plot the diagram you designed, make a few explanations under your plot regarding how your code works (you may need to think about how the while loop works) Here is what your Iteration and Conditional terminal might look like in order to solve this problem, using numeric and comparison subpalettes (since the first iteration is zero, add 1 to the number of iterations, and subtract 1 from the Number of Pulses to be Executed). Here is an image of Part 1 Front Panel and Block Diagram
Your report should follow the format described below: 1. Lab title 2. Hardware and Software 3. Objective 4. Descriptions of the problems you have experienced and how you solved them 5. Solutions to all the questions (will start with a bullet in the lab transcripts). Note that brief explanations or descriptions after each plot are required. 6. Plots that need to be printed out (will start with a bullet in lab transcripts) have to be attached with lab reports. Plots should be created by selecting File - Print - Next - icon, description, panel and diagram - Print options, which will also include your lab records such as time or date, etc. 7. Summary or conclusion