|
For many robotics applications, you need to make decisions based on sensor or other types of inputs. Consider gathering information from a temperature sensor near the motor. Depending on the temperature value you receive, you may want to turn the fan on or off. You need to decide when to do this to prevent the motors from overheating. For example, if you take in a temperature that is less than or equal to 65 °F, you want to turn off the fan. However, if the temperature is greater than 65 °F, you want to turn on the fan. You can use case structures and select statements to make these decisions.
Text-based programmers can reference the following example:
If a happens, do b; else if c happens, do d. In text-based programs, you can accomplish this with if-else statements, case statements, switch statements, and so on. NI LabVIEW software includes these same concepts, but you implement them in a different manner.
A Case Structure, shown as follows, has two or more subdiagrams or cases.
You can find it in the Functions palette under Programming >> Structures >> Case Structures.
Only one subdiagram is visible at a time, and the structure executes only one case each time it is called. An input value determines which subdiagram executes. The Case Structure is similar to switch statements or if-then-else statements in text-based programming languages.
The case selector label at the top of the Case Structure, shown as follows, contains the name of the selector value that corresponds to the case in the center and decrement and increment arrows on each side.
Click the decrement and increment arrows to scroll through the available cases. You also can click the down arrow next to the case name and select a case from the pull-down menu.
Wire an input value, or selector, to the selector terminal, shown as follows, to determine which case executes.
You must wire a numeric, Boolean value, string, or enumerated type value to the selector terminal. You can position the selector terminal anywhere on the left border of the Case Structure. If the data type of the selector terminal is Boolean, the structure has a TRUE case and a FALSE case. If the selector terminal is an integer or string, the structure can have any number of cases.
If you do not specify a default case for the Case Structure to handle out-of-range values, you must explicitly list every possible input value. For example, if the selector is an integer and you specify cases for 1, 2, and 3, you must specify a default case to execute if the input value is 4 or any other unspecified integer value.
You can enter a single value or lists and ranges of values in the case selector label. For lists, use commas to separate values. For numeric ranges, specify a range as 10..20, meaning all numbers from 10 to 20 and all the number in between, for example 12 and 19. You also can use open-ended ranges. For example, ..100 represents all numbers less than or equal to 100, and 100.. represents all numbers greater than or equal to 100. You also can combine lists and ranges, for example ..5, 6, 7..10, 12, 13, 14. When you enter values that contain overlapping ranges in the same case selector label, the Case Structure redisplays the label in a more compact form. The previous example redisplays as ..10, 12..14. For string ranges, a range of a..c includes all of a and b, but not c. In a range of a..c, c includes the ending value of c.
When you enter string and enumerated values in a case selector label, the values display in quotation marks, for example, "red," "green," and "blue." However, you do not need to type the quotation marks when you enter the values unless the string or enumerated value contains a comma or range symbol ("," or ".."). In a string value, use special backslash codes for nonalphanumeric characters, such as \r for a carriage return, \n for a line feed, and \t for a tab.
If you change the data type of the wire connected to the selector terminal of a Case Structure, the Case Structure automatically converts the case selector values to the new data type when possible. If you convert a numeric value, for example 19, to a string, the string value is "19." If you convert a string to a numeric value, LabVIEW converts only those string values that represent a number. The other values remain strings. If you convert a number to a Boolean value, LabVIEW converts 0 to FALSE and 1 to TRUE, and all other numeric values become strings.
Note: You cannot use fixed-point numbers as case selector values. If you wire a fixed-point number to the case, the VI appears with a broken Run button.
If you enter a selector value that is not the same type as the object wired to the selector terminal, the value appears red. This indicates that the VI will not run until you delete or edit the value. Also, because of the possible round-off error inherent in floating-point arithmetic, you cannot use floating-point numbers as case selector values. If you wire a floating-point value to the case, LabVIEW rounds the value to the nearest even integer. If you type a floating-point value in the case selector label, the value appears red to indicate that you must delete or edit the value before the structure can execute.
You can create multiple input and output tunnels for a Case Structure. Inputs are available to all cases, but cases do not have to use each input. However, you must define each output tunnel for each case. When you create an output tunnel in one case, tunnels appear at the same position on the border in all the other cases. If even one output tunnel is not wired, all output tunnels on the structure appear as white squares. You can define a different data source for the same output tunnel in each case, but the data types must be compatible for each case. You also can right-click the output tunnel and select Use Default If Unwired from the shortcut menu to use the default value for the tunnel data type for all unwired tunnels.
When you wire an error cluster to the selector terminal of a Case Structure, the case selector label displays two cases—Error and No Error—and the border of the Case Structure changes color—red for Error and green for No Error. If an error occurs, the Case Structure executes the Error subdiagram.
For simple decision making, the select function is a great option. It returns the value wired to the t input or f input, depending on the value of s. If s is TRUE, this function returns the value wired to t. If s is FALSE, this function returns the value wired to f.
The connector pane displays the default data types for this polymorphic function.
There are no comments on this document