Type Definitions


Type definitions in LabVIEW are a powerful tool for defining your control and indicator data types in your program. A type definition, often called typedef for short, is a LabVIEW custom control file (.ctl), that is configured to act a "master template" for the data type of slave controls that have been linked to the typedef. We'll learn about customizing the appearance of controls in Chapter 17, "The Art of LabVIEW Programming," later on, but right here we'll focus on the data type definition functionality of custom controls.

Typedefs are among the most useful structures in the LabVIEW programmer's arsenal, and perhaps the best way to introduce them is to start with an example. Suppose you have a program that you built for controlling a motor. In your LabVIEW program, you've created a cluster called Motor State that contains information about the motor's power state (on or off), its speed, and the direction it is moving (such as the one shown in Figure 13.62).

Figure 13.62. Motor State cluster


Now let's say your application uses this cluster in several subVIs. Perhaps you have a subVI to check the motor status, another one to turn the motor on or off, and another one to switch directions. On the front panel of each subVI (shown in Figure 13.63), you're using a cluster like the one shown in Figure 13.62.

Figure 13.63. Three subVIs that use the Motor State cluster


Now let's say that after some time of your program running fine, you add a temperature sensor to the motor. You now want to monitor temperature in various places in your program, so you will need to add temperature to your Motor State cluster. Your Motor State cluster now needs to look like the one shown in Figure 13.64.

Figure 13.64. Motor State cluster, after adding a Temp numeric indicator


If you didn't know about typedefs when you wrote your original program, you would need to go and edit every single cluster that contains the motor state and "temperature." Not only is this tedious, but it can be an error-prone activity that introduces bugs into your application (and that's not good). And in some applications, you might have dozens or even hundreds of places the cluster needs to change (it will take a lot of time to do all that work). And then you might need to change the cluster contents again, later (ugh)!

The solution is to first create a typedef for the Motor State cluster and use instances of this typedef in your various VIs. Because the instance are linked to the typedef and slaves to its data type, if you ever need to change that Motor State cluster, you only have to modify the typedef, and all the VIs that use the typedef will automatically be updated to the new data type for you!

We'll guide you through the process of creating a typdef, step-by-step, in the following activity.

Activity 13-8: Creating a Typedef

In this activity you will create a type definition that can be used in several locations of your application. Each location that uses it will be updated automatically when the type definition is edited.

1.

In a new blank VI, create a cluster like the one shown earlier in Figure 13.64.

2.

Now pop up on the cluster (be sure you pop up on the cluster and not a cluster element by right-clicking on the cluster frame), and select Advanced>>Customize . . . . Alternately, you can select the cluster with the positioning tool, and from the Edit menu, select Customize Control . . . .

3.

You will now see your cluster in a new window called Control 1 Control or something similar (see Figure 13.65). This is a custom control window.

Figure 13.65. Control editor window showing the custom control type definition you will create during this activity


4.

The control window is like a front panel with no block diagram for a single control. You can edit and customize your control, but there is no functionality to program here. Go ahead and add an element to the "Motor State" cluster called "Temp."

5.

The Control window has a drop-down menu called Type Def status. By default it is set to "Control." Click on this and choose "Type Def," as shown in Figure 13.66.

Figure 13.66. Configuring the custom control as a Type Definition


When creating Type Definition, be sure and remember to set the option to "Type Def." If you leave it set to "Control" in the Control Editor window, it will not update the instances of the control in your VIs!

6.

Now save your type definition. Because a type definition is really a custom control, LabVIEW saves these as control files, with a .ctl extension. Save this as Motor State.ctl.

7.

You've just created a typedef. Now close this window. When you do so, you will get a dialog like the one in Figure 13.67, asking you if you want to replace your original control with Motor State.ctl. Say yes.

Figure 13.67. Confirmation dialog asking if you would like to replace the original front panel control with your new type definition


8.

Now look at the original VI in which you had the Motor State cluster. It should have automatically changed to include the new "Temp" element you added when you were editing the control.

9.

To see how you can now use your type definition control, open a new VI. To insert the typedef, click on "Select a Control . . ." from the Controls palette, as shown in Figure 13.68.

Figure 13.68. Choosing Select a Control . . . from the Controls palette to browse to a custom control file that you would like to add to the front panel


10.

The ensuing dialog box will let you browse for LabVIEW control files (.ctl files). Find the "Motor State.ctl" typedef you made a minute ago and select it. You will now see the typedef control on your front panel.

11.

Now if you modify the Motor State.ctl again, all the VIs that use this typedef will be updated.

12.

If you want to play with this typedef and VIs that have it on its front panel, we've provided it for you on the CD in the EVERYONE\CH12 directory. Look for the "Type Def Example" directory there.

Remember that a type definition is like a "master template." You can have any number of instances of controls linked to that typedef, so that if you modify the typedef, the controls get modified as well, automatically by default.

If you have a control on a front panel that is tied to a typedef, you can choose to pop up on the control to see some options related to the typedef (see Figure 13.69).

Figure 13.69. Disconnecting a control from its type definition "master"


These options follow:

  • Update from Type Def. This is disabled whenever "Auto-Update from Type Def." is checked, which is by default. If the auto-update option is not checked, select this to force the control to update itself from the type definition.

  • Auto-Update from Type Def. When this is checked, the control automatically updates itself any time the typedef changes.

  • Open Type Def. This will cause LabVIEW to open the typedef .ctl file so you can see it or edit it.

  • Disconnect from Type Def. This disconnects the control from the type definition. Once you do this, any changes to the type definition will have no effect on this control. You cannot "reconnect" it, either. To go back, either choose Undo (<ctrl-Z> [Windows], <command-Z> [Mac OS X], or <meta-Z> [Linux]) or delete the control and re-insert an instance of the typedef.

Type definitions are just one kind (but arguably the most important kind) of LabVIEW custom controls. The other kinds are "controls" proper and "strict type definitions." A strict type definition is like a type definition except that it also forces the appearance, and not just the data type, of its linked controls to stay the same. We'll talk more about custom controls in Chapter 17. There is also more to typedefs than we've been able to cover here, but for further reading, consult the LabVIEW documentation or some of the resources listed in Appendix E, "Resources for LabVIEW."

In general, you should develop the habit of creating typedefs in your applications whenever you think you might be re-using a control or a data structure such as clusters. It will save you hours of work and make your code easier to maintain.

Type definition instances are often placed on the front panel as controls and indicators. However, you can also place them on the block diagram as constants. You will see an example of this when we discuss the Standard State Machine design pattern later in this chapter. To open the type definition of a constant, do just as you would for a control or indicator: pop up on the constant and select Open Type Def.





LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net