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 clusterNow 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 TypedefIn 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.
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:
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. |