16
APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises HANDS-ON EXERCISES Table of Contents 1. Palettes 2. Examples 3. Menus 4. Templates 5. Error Codes 6. Help 7. Custom Probes Exercise files located at in Extending LabVIEW – Examples.zip Exercise #1 – Palettes 10 minutes Objective: This exercise demonstrates how to add a custom set of VIs to the palettes. In this case, we are adding a very simple library of math VIs to the User Libraries subpalette. 1. Unzip the attached “Simple Math.zip” file into your [LabVIEW]\user.lib folder. It should create a single “Simple Math” folder. 2. Launch LabVIEW. 3. Navigate to the Functions > User Libraries subpalette. Notice that the library of Math VIs is already present: 4. The auto-populated palette contents are convenient, but there are a few changes we want to make. We want rearrange the 4 basic VIs (Add, Subtract, Multiply, and Divide), and we want to put the “Combined” VI in its own utility subpalette. 1

Extending LabVIEW - Exercises

Embed Size (px)

Citation preview

Page 1: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

HANDS-ON EXERCISES

Table of Contents

1. Palettes2. Examples3. Menus4. Templates5. Error Codes6. Help7. Custom Probes

Exercise files located at in Extending LabVIEW – Examples.zip

Exercise #1 – Palettes 10 minutes

Objective: This exercise demonstrates how to add a custom set of VIs to the palettes. In this case, we are adding a very simple library of math VIs to the User Libraries subpalette.

1. Unzip the attached “Simple Math.zip” file into your [LabVIEW]\user.lib folder. It should create a single “Simple Math” folder.

2. Launch LabVIEW.

3. Navigate to the Functions > User Libraries subpalette. Notice that the library of Math VIs is already present:

4. The auto-populated palette contents are convenient, but there are a few changes we want to make. We want rearrange the 4 basic VIs (Add, Subtract, Multiply, and Divide), and we want to put the “Combined” VI in its own utility subpalette.

5. Launch Tools > Advanced > Edit Palette Set… The Edit Controls and Functions Palette Set dialog appears, along with a Controls and Functions palette.

1

Page 2: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

6. We will rearrange the contents of the Simple Math subpalette to be more intuitive. Deselect the “Synchronize with Directory” option so we can customize what appears in the palette:

7. Click the Simple Math subpalette to open its contents.

8. When VIs are added to user.lib subpalettes automatically, their library information is not preserved. We want to re-add each VI to our palette so their qualified library name is used in Palette Search and Quick Drop operations (note that these steps are not necessary if your VIs are not in a project library). Right-click each VI in the subpalette and choose “Delete Item” so the palette is empty.

9. Right-click in the palette area and choose “Insert > VIs…”, and choose the Add.vi off disk. Repeat this process for Subtract.vi, Multiply.vi, and Divide.vi:

10. Now we are going to create a “Utility” subpalette that will contain the “Combined” math VI. Right-click in the empty space in the lower-right corner of the Simple Math subpalette and choose Insert > Subpalette… The following dialog appears. In our case, we want to create a new .mnu file, so leave the first option selected and click “OK”.

11. Save the .mnu file as “utility.mnu” in the Simple Math folder.

12. Give the subpalette a name of “Simple Math Utilities”.

13. (Optional) Right-click the new Simple Math Utilities subpalette and choose “Edit Subpalette Icon”. Draw an applicable icon image and click “OK”.

2

Page 3: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

14. Click on the new “Simple Math Utilities” subpalette.

15. Right-click in empty space in the palette and choose Insert > VIs… Choose “Combined.vi” in the file dialog.

16. Click Save Changes in the Edit Controls and Functions Palette Set dialog. The following summary of your changes will appear.

Click “Continue”.

17. Your palette changes are now reflected in the Functions Palette:

18. At this point, you must now distribute the two newly created .mnu files (dir.mnu and utility.mnu) along with the other contents of the Simple Math folder in order for other users of this library to benefit from your customized palette contents.

(These files should now be found in …user.lib\Simple Math)

End of Exercise #1

3

Page 4: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

Exercise #2: Examples 10 minutes

Objective: This exercise demonstrates how to mark example VIs for inclusion in the NI Example Finder.

1. Browse the contents of the user.lib\Simple Math\_Examples folder that was created during the previous exercise. Notice there are two example VIs in this folder that illustrate how to use the Simple Math API.

2. Open LabVIEW and launch Tools > Prepare Example VIs for NI Example Finder.

3. In the “Start directory for examples” path control, browse to [LabVIEW]\user.lib\Simple Math\_Examples and choose “Current Folder”.

4. Double-click the All Individual Operations.vi in the first listbox. Notice its title is added to the “Documentation” section on the right.

5. In the second listbox, double-click the “Analyzing and Processing Signals” category, then double-click the “Mathematics” subcategory. Notice this is added as a browse path for your example in the “Documentation” section on the right. You can add as many browse paths as you want for an example VI. In this exercise, however, we are only creating one.

6. Scroll the keywords list to add keywords for your example. Keywords help users who are searching the NI Example Finder for examples that cover particular concepts. Some possible keywords for this example include “mathematics” and “operations”.

7. Now that you have specified keywords, you can click the “Save Documentation” button to store this example documentation within the Example VI.

8. Since we will be using the same browse path and keyword(s) for the other example, click the “Copy” button to copy this information from the example VI we just documented.

4

Page 5: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

9. Double-click Simple Calculator.vi in the first listbox.

10. Click the “Paste” button, which will assign the correct browse path and keyword(s) to this example.

11. Click the “Save Documentation” button.

12. Switch over to the Build Data File tab.

13. For the “Data file to create” control, specify a file path of [LabVIEW]\user.lib\Simple Math\_Examples\SimpleMath.bin3.

14. For the “Directory that contains the examples” control, specify a file path of [LabVIEW]\user.lib\Simple Math\_Examples.

15. Click the “Build File” button. A “SimpleMath.bin3” file should have been created in the specified _Examples folder. Note that you will need to distribute this file, along with your examples, for the NI Example Finder incorporation of your examples to work properly:

16. Now your examples have been incorporated into the NI Example Finder. To test this, launch Help > Find Examples…

17. When the Example Finder comes up, make sure Browse according to: is set to Task, then see that the two examples appear under the Analyzing and Processing Signals > Mathematics category. You should be able to double-click each example to open its front panel.

18. Go to the Search tab. Type “operations” in the “Enter keyword(s)” control and double-click the “operations” keyword that appears in the “Double-click keyword(s)” listbox. In the search results, you should see your two examples since this keyword was assigned to them.

19. Change the ring next to the Search button to “Descriptions” and type “simple math” in the “Enter keyword(s)” control.

20. Click “Search”. In the results, you’ll see the All Individual Operations.vi and Simple Calculator.vi examples show up at the top of the list, because the phrase ‘simple math’ appears in their descriptions.

End of Example Exercise #2

5

Page 6: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

Exercise #3 – Menus 5 minutes

Objective: To understand the relationship between file locations on disk and their corresponding menu locations, and the significance of a text file to define menu contents and order.

1. Copy the attached “Test Items” folder to either the wizard, project, or help folder. The following steps assume it has been copied to the project folder.

2. Create a new VI. Drop a few controls on its panel.

3. Go to Tools > Test Items. Notice there are several menu options to choose from:

4. Select a few of the menu options to see what they do.

5. Navigate to [LabVIEW]\project\Test Items on your hard drive.

6. Notice there is a text file in this folder. Open the text file in a text editor and see the following:

test menu.vi-testmenu2.vi

End of Exercise #3

6

Page 7: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

Exercise # 4 – Templates 5 minutes

Objective: To write a new Simple Math VI starting from a template instead of a blank VI.

1. Create a Simple Math folder in [LabVIEW]\templates.

2. Place the attached Simple Math Template.vit in this new folder.

3. Launch LabVIEW. Choose File > New… to open the New dialog.

4. You should see your template VI under VI > Simple Math.

5. Select Simple Math Template.vit in the list. You’ll see an image of its diagram, along with its VI description, in the Description section:

6. Click OK to dismiss the dialog. A new VI is created based on the Simple Math template.

7. We are going to create a new VI that performs the x^y operation. Edit the icon of the VI to indicate this. Note that the icon already has the Simple Math banner applied, so you only need to add an image to the middle of the icon.

8. Notice all the necessary controls are already on the front panel (and they are also wired to the connector pane). We just need to rename the “Result” indicator. Rename this indicator to be “x^y”.

9. Delete the “Rename this indicator…” yellow comment on the front panel.

10. Switch to the block diagram.

11. Delete the wire with the “Delete this wire” label. Also delete the two yellow comments on the block diagram.

12. Use Quick Drop by pressing Ctrl-Space to drop a Power of X function on the diagram.

13. Wire the ‘x’ control to the ‘x’ input of the Power of X function. Repeat this for the ‘y’ control and the ‘x^y’ indicator.

14. Go to File > VI Properties > Documentation and give the VI a meaningful description.

7

Page 8: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

15. Save the VI. By using a template, you were able to skip a lot of the common tasks associated with creating a new VI (setting up connector pane, applying icon banner, creating an error case on the diagram, etc.)

End of Exercise #4

8

Page 9: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

Exercise # 5 – Error Codes 5 minutes

Objective: To add error information to a Simple Math VI, and define the new error in an errors text file.

1. Open user.lib\Simple Math\Divide.vi.

2. Add code that checks the y input to see if it is zero. If the y value is zero, the VI needs to return an error since division by zero is not allowed.

3. We will define an error value of “5001” for this condition. Note that this error falls within the range of user-defined error codes mentioned above.

4. The Error Cluster from Error Code.vi is used to generate an error cluster from this value. The following is a screenshot illustrating what your VI should look like:

5. Now we will define this new error code with an error codes text file. Run the Error Code File Editor from Tools > Advanced > Edit Error Codes…

6. Click New to create a new error code file.

7. In the “Comments about this file” text box, type “Definitions for errors generated by the Simple Math API”.

8. Click the “Add…” button to add a new error code.

9. Type 5001 as the “New Code” value, and Unable to divide a number by zero as the “New Description”:

10. Click OK to go back to the Error Code File Editor.

11. Choose File > Save. Type “SimpleMath-errors.txt” as the filename. By default, the file should be saved in [LabVIEW]\user.lib\errors.

9

Page 10: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

12. Click Close to exit the Error Code File Editor.

13. Restart LabVIEW.

14. Create a new VI.

15. Drop Divide.vi on the diagram.

16. Create controls and indicators for the VI inputs and outputs.

17. Specify a value of 0 for the ‘y’ input.

18. Run the VI. Error 5001 should be returned.

19. Right-click the error cluster on the front panel and choose “Explain Error”:

20. The Explain Error dialog gives the proper error description for your new error:

End of Exercise #5

10

Page 11: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

Exercise #6 – Help 5 minutes

Objective: To create online help for the Simple Math VIs.

1. Copy the attached SimpleMathHelp folder to your [LabVIEW]\help folder.

2. Open user.lib\SimpleMath\Add.vi.

3. Press Ctrl-I to bring up the VI Properties Dialog.

4. Switch to the Documentation page.

5. Enter a value of <helpdir>:\SimpleMathHelp\add.htm for the Help path. Click OK.

6. Bring up the Context Help window in LabVIEW by pressing Ctrl-H.

7. Drop the Add.vi from the Simple Math subpalette on your diagram.

8. Hover over the Add.vi on the diagram. Notice there is now a Detailed Help blue link in the Context Help window:

9. Click the Detailed Help blue link in the Context Help window. A web browser should open up with the ‘add.htm’ help document.

10. The Help path has already been specified for the rest of the Simple Math VIs, so you can view their online help in a similar manner.

End of Exercise #6

11

Page 12: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

Exercise #7 – Custom Probes 5 minutes

Objective: To create a custom probe to provide a different way to visualize data while debugging.

1. Open Loop – Simple Math.vi. Note that it is simply using one of the Simple Math VIs to perform a calculation in a loop.

2. Run the VI and create a probe on the output of Subtract.vi. Note that the default numeric probe is created.

3. We are going to create a probe that displays more helpful information. Close the probe you created.

4. Right-click the same wire and choose Custom Probe > New…

5. In the resulting Create New Probe dialog, select Create a probe from an existing probe and click Next >.

6. Find “Conditional Double Probe” in the list, select it, and click Next >.

7. Specify a Filename of “Simple Math Probe”.

8. Specify a Directory to save the probe of [LabVIEW]\user.lib\_probes.

9. Click the Save button to save the probe VI. The VI will be saved and its panel will be opened.

12

Page 13: Extending LabVIEW - Exercises

APIs and Tools for Building LabVIEW Toolkits: Hands-On Exercises

10. Change the Icon of the new probe VI to have the Simple Math banner:

11. Add a new tab called “Chart” to the front panel of the probe, and add a Waveform Chart to the tab:

12. Go to the diagram and rename the “x-y” control to “Value in”.

13. Wire the “Value in” terminal directly to the new “Waveform Chart” terminal.

14. Scroll up in the diagram to find the “value string” terminal. You can wire your own value to the string to customize what appears in the “Value” column of the Probe Watch Window for this probe. In our case, we will simply generate a string of our value in Engineering format. To do this, use Quick Drop to drop a Number to Engineering String function on the diagram. Wire the “Value in” terminal to the “number” input of this function, and wire the “Engineering string” output of this function to the “value string” terminal.

15. Save and close Simple Math Probe.vi

16. Go back to Loop – Simple Math.vi, right-click the output wire from Subtract.vi, and choose Custom Probe > Simple Math Probe.vi. Your new probe should appear in the Probe Watch Window.

17. Run Loop – Simple Math.vi. You should see the value appear as an Engineering format string in the Value column. You should also see the value updating on the Data tab on the right side of the window. If you switch to the Chart tab, you can see a chart updating with the data.

End of Exercise #7

13