You are on page 1of 16

Lesson 11 Common Design Techniques and Patterns

Project: Temperature Weather Station


Goal
Create a state machine and practice the skills learned throughout this course.

Scenario
Complete the course project using a state machine and the state flow chart
developed earlier in the course.

The project acquires a temperature every half a second, analyzes each


temperature to determine if the temperature is too high or too low, then
alerts the user if there is a danger of heatstroke or freeze. The program logs
the data if a warning has occurred. If the user has not pressed the stop button,
the entire process repeats.

Design
You have already completed the design stage for this project. The inputs and
outputs and flowchart are repeated here.

User Interface Inputs and Outputs

Type Name Properties


Numeric Upper Limit Double Precision
Control
Numeric Lower Limit Double Precision
Control
Waveform Temperature
Chart History
String Indicator Warning Four possible values:
Heatstroke Warning, Freeze
Warning, No Warning and
Upper Limit < Lower Limit
Stop Button Stop

External Inputs and Outputs


• Inputs: Current temperature retrieved from a sensor attached to AI0 of
the data acquisition board.
Conversion formula: Voltage x 100 = Celsius
• Outputs: tab-delimited ASCII file containing current temperature,
temperature limits, and warning string for each warning that occurs.

LabVIEW Introduction Course Manual 11-12 ni.com


Lesson 11 Common Design Techniques and Patterns

State Transition Diagram

Initialize

Acquisition

Time Elapsed=TRUE
and
Stop=False
Warning=TRUE

Analysis
Datalog

Time Elapsed=FALSE
and
Stop=FALSE

Time Check

Warning=FALSE

Stop=TRUE

Figure 10-1. State Transition Diagram for the Course Project

© National Instruments Corporation 11-13 LabVIEW Introduction Course Manual


Lesson 11 Common Design Techniques and Patterns

Implementation
In the following steps, you set up the state machine in the Weather Station
UI VI.

Figure 10-2. Setting up the State Machine

1. Open the weather station project.

q Open the Weather Station.proj.

2. Open the user interface for the weather station project.

q In the Project Explorer window, double-click the Weather


Station UI.vi.

3. Switch to the block diagram.

4. Create a state machine on the block diagram using the existing


enumerate type control.

q Place a While Loop from the Structures palette on the block


diagram, as shown in Figure 10-2.

q Place a Case structure inside the While Loop.

q Place the Weather Station State enumerated type constant outside


the While Loop.

LabVIEW Introduction Course Manual 11-14 ni.com


Lesson 11 Common Design Techniques and Patterns

q Connect the Weather Station State enumerated type constant to


the case selector terminal.

q Right-click the Case structure and select Add Case for Every
Value from the shortcut menu.

q Change the enumerated type constant tunnel to a shift register.

5. Place a Wait function in the loop so that the processor can handle other
tasks when needed.

q Place a Wait (ms) function inside the While Loop, outside the
Case structure.

q Right-click the milliseconds to wait terminal and select


Create»Constant from the shortcut menu.

q Set the constant to a value of 5.

6. Pass the temperature cluster into the state machine so that it can be
accessed from any state.

q Wire the output of the Bundle function to the While Loop to


create a tunnel.

q Change the cluster tunnel to a shift register.

q Wire the shift register to the Case structure.

7. Confirm that your block diagram is similar to the one shown in


Figure 10-2.
8. Save the VI.

In the following sections, complete each state of the state machine.

© National Instruments Corporation 11-15 LabVIEW Introduction Course Manual


Lesson 11 Common Design Techniques and Patterns

Acquisition State

Figure 10-3. Acquisition State

1. Switch to the Acquisition state of the state machine.

2. Acquire a single temperature reading.

Hardware
q Place the DAQ Assistant in the Acquisition state.

q Select Analog Input»Voltage for the task type.

q Select ai0 on your DAQ device for the channel.

q Click the Finish button.

q Select Temperature from the Custom Scaling pull-down menu.

q Set the signal input range to 0 to 50 degrees Celsius.

q Set the Task Timing to 1 Sample (On Demand).

q Click the OK button.

q Skip to step 3.

LabVIEW Introduction Course Manual 11-16 ni.com


Lesson 11 Common Design Techniques and Patterns

No Hardware
q Select the Temperature Simulator VI in the Project Explorer
window and drag it into the Acquisition state. In future steps, you
wire the inputs and outputs of the Temperature Simulator VI instead of
the DAQ Assistant.

3. Display the acquired temperature on the Temperature History Waveform


Chart.

q Move the Temperature History terminal inside the Acquisition state.

q Wire the data output of the DAQ Assistant to the Temperature History
terminal. Notice that the waveform graph data type automatically
changes to match the incoming data.

4. Update the temperature cluster with the current temperature, upper limit,
and lower limit.

q Place an Unbundle by Name function inside the Acquisition state.

q Wire the temperature cluster to the Unbundle by Name function.

q Expand the Unbundle by Name function to show three elements.

q If necessary, change the contents to show the following elements in


this order: Temperature, T Upper Limit, T Lower Limit.

q Move the Upper Limit and Lower Limit terminals into the
Acquisition state.

q Wire the data output of the DAQ Assistant to the Temperature


element of the Unbundle by Name function.

q Wire the Upper Limit terminal to the T Upper Limit element of the
Unbundle by Name function.

q Wire the Lower Limit terminal to the T Lower Limit element of the
Unbundle by Name function.

5. Set the state machine to go to the Analysis state next.

q Place a copy of the Weather Station State enumerated type constant


inside the Acquisition state.

q Select the Analysis element of the enumerated type constant.

q Wire the enumerated type constant to the output state shift register.

© National Instruments Corporation 11-17 LabVIEW Introduction Course Manual


Lesson 11 Common Design Techniques and Patterns

6. Save the VI.

Analysis State
In previous exercises, you wrote a subVI that performs the analysis required
for this state machine: Determine Warnings VI. This VI is already added to
the project file, but needs to be placed in the main VI.

Figure 10-4. Analysis State

1. Switch to the Analysis state of the state machine.

2. Place the Determine Warnings VI in the Project Explorer and place in


the Analysis state.

q Switch to the Project Explorer window.

q Select the Determine Warnings VI and drag it into the Analysis


state.

3. Pass the temperature values to the Determine Warnings VI.

q Place an Unbundle by Name function inside the Analysis state.

q Wire the temperature cluster to the Unbundle by Name function.

q Expand the Unbundle by Name function to show three elements.

q If necessary, change the contents to show the following elements in


this order: Temperature, T Upper Limit, T Lower Limit.

LabVIEW Introduction Course Manual 11-18 ni.com


Lesson 11 Common Design Techniques and Patterns

q Wire each of the Unbundle by Name elements to the appropriate


terminal of the Determine Warnings VI.

4. Display the output of the Determine Warnings VI.

q Move the Warning terminal inside the Analysis state.

q Wire the Warning Text terminal of the Determine Warnings VI to the


Warning terminal.

5. Update the warning value in the temperature cluster.

q Place a Bundle by Name function inside the Analysis state.

q Wire the temperature cluster to the input cluster terminal of the


Bundle by Name function.

q Choose the Warning element in the Bundle by Name function.

q Wire the Warning Text terminal of the Determine Warnings VI to


the Warning terminal.

q Wire the output of the Bundle by Name function to the output


temperature cluster shift register.

6. If a warning has occurred, log the data; otherwise check to see if it is


time to acquire the next piece of data.

q Place a Case structure inside the Analysis state.

q Wire the Warning? terminal of the Determine Warnings VI to the


case selector of the new Case structure.

q Switch to the True case of the Case structure.

q Place a copy of the Weather Station State enumerated type constant


inside the True case.

q Select the Data Log element of the enumerated type control.

q Wire the enumerated type control to the output state shift register.

q Switch to the False case of the Case structure.

q Place a copy of the Weather Station State enumerated type control


inside the True case.

q Select the Time Check element of the enumerated type control.

© National Instruments Corporation 11-19 LabVIEW Introduction Course Manual


Lesson 11 Common Design Techniques and Patterns

q Wire the enumerated type constant to the tunnel of the Case structure.

7. Save the VI.

Data Log State


In this state, the data is logged in a tab-delimited ASCII file. You must open
the file before the state machine executes, and close the file after the state
machine executes.

Figure 1001-5. Data Log State

1. Switch to the Data Log state of the state machine.

2. Place the Open/Create/Replace File function to the left of the state


machine.

3. Set the Open/Create/Replace File function to open or create.

q Right-click the operation terminal of the Open/Create/Replace File


function and select Create»Constant from the shortcut menu.

q Select the open or create element of the constant.

4. Pass the error cluster and file refnum into the state machine so that it can
be used by all states.

q Wire the error out terminal of the Open/Create/Replace File function


to the While Loop.

q Replace the tunnel with a shift register.

LabVIEW Introduction Course Manual 11-20 ni.com


Lesson 11 Common Design Techniques and Patterns

q Wire the refnum out terminal of the Open/Create/Replace File


function to the While Loop.

q Replace the tunnel with a shift register.

5. Prepare the temperature data for writing by combining it all in one


string.

q Place an Unbundle by Name function inside the Data Log state.

q Wire the temperature cluster to the input of the Unbundle by Name


function.

q Resize the function to show four elements.

q Select the elements so they are in the following order: Temperature, T


Upper Limit, T Lower Limit, and Warning.

q Place a Format Into String function to the right of the Unbundle by


Name function.

q Resize the function to have eight inputs.

q Place a tab constant in the Data Log state.

q Place an End of Line constant in the Data Log state.

q Wire the inputs in the following order: temperature, <tab>, T Upper


Limit, <tab>, T Lower Limit, <tab>, Warning, <end of line>.

6. Write the temperature string to the file.

q Place the Write To Text File function in the Data Log state.

q Wire the output of the Format Into String function to the text input of
the Write To Text File function.

q Wire the error clusters to and from the Write To Text File function.

q Wire the file refnum to and from the Write To Text File function.

7. Close the file.

q Place the Close File function to the right of the state machine.

q Wire the error cluster and the file refnum to the Close File function.

q Place a Simple Error Handler to the right of the Close File function.

© National Instruments Corporation 11-21 LabVIEW Introduction Course Manual


Lesson 11 Common Design Techniques and Patterns

q Wire the error out terminal from the Close File function to the
Simple Error Handler.

8. Place a copy of the Weather Station State enumerated type control inside
the Data Log state.

9. Select the Time Check element of the constant.

10. Wire the constant to the tunnel of the Case structure.

11. Save the VI.

Time Check State


In this state, determine whether half a second has passed. If it has, acquire
the next piece of data.

Figure 10-6. Time Check State

1. Switch to the Time Check state of the state machine.

2. Move the Stop terminal inside the Time Check state.

3. Determine when half a second has passed since the last data acquisition.

q Place the Elapsed Time Express VI on the block diagram inside the
While Loop, but outside the Case structure.

q Configure the Elapsed Time to 0.5 seconds

LabVIEW Introduction Course Manual 11-22 ni.com


Lesson 11 Common Design Techniques and Patterns

q Remove the checkmark from the Automatically reset after time


target checkbox.

q Click the OK button.

4. Navigate to the Acquisition state if the time has elapsed.

q Place a Case structure inside the Time Check state.

q Wire the Time has Elapsed terminal from the Elapsed Time
Express VI to the case selector.

q Switch to the True case.

q Place a copy of the Weather Station State constant inside the True
case.

q Select the Acquisition element of the constant .

q Wire the constant to the state tunnel of the Case structure.

5. Navigate to the Time Check state if the time has not elapsed.

q Switch to the False case.

q Place a copy of the Weather Station State constant inside the False
case.

q Select the Time Check element of the constant.

q Wire the constant to the state tunnel of the Case structure.

6. Save the VI.

Finish the State Machine


1. Wire the temperature data cluster, the error cluster and the file refnum
through all states.

2. Determine when to stop the state machine. Stop when an error occurs or
the user has clicked the stop button, but only after a full sequence has
occurred (Acquisition, Analysis, Data Log).

q Place an Unbundle by Name function inside the While Loop to the


right of the Case structure.

q Wire the error cluster to the Unbundle by Name function.

q Place the Or function next to the loop condition terminal.

© National Instruments Corporation 11-23 LabVIEW Introduction Course Manual


Lesson 11 Common Design Techniques and Patterns

q Wire the status element of the error cluster to the top input of the Or
function.

q From the Time Check state, wire the Stop button to the bottom input
of the Or function.

q Wire the output of the Or function to the loop condition terminal.

q Switch to the Acquisition state.

q Right-click the tunnel attached to the bottom input of the Or function


and select Create»Constant from the shortcut menu to wire a False
constant to the Boolean tunnel.

q Switch to the Analysis state.

q Right-click the tunnel attached to the bottom input of the Or function


and select Create»Constant from the shortcut menu to wire a False
constant to the Boolean tunnel.

q Switch to the Data Log State.

q Right-click the tunnel attached to the bottom input of the Or function


and select Create»Constant from the shortcut menu to wire a False
constant to the Boolean tunnel.

3. Set the Elapsed Time Express VI to start counting when the VI begins.

q Place a True constant outside the While Loop, to the left of the
Elapsed Time Express VI.

q Wire the constant to the Reset terminal of the Elapsed Time Express
VI.

q Replace the Reset tunnel with a shift register.

4. Document the block diagram.

5. Figures 1001-7 through 1001-10 show an example of the final block


diagram for each state. Complete any block diagram wiring that is still
necessary. Pay particular attention to the Booleans in the lower right of
each case. These Booleans determine when the state machine stops, and
when the Elapsed Time Express VI count is reset.

LabVIEW Introduction Course Manual 11-24 ni.com


Lesson 11 Common Design Techniques and Patterns

Figure 10-7. Complete Acquisition State

Figure 10-8. Complete Analysis State

© National Instruments Corporation 11-25 LabVIEW Introduction Course Manual


Lesson 11 Common Design Techniques and Patterns

Figure 10-9. Complete Data Log State

Figure 10-10. Complete Time Check State

6. Switch to the front panel.

7. Save the VI.

8. Save the project.

Testing
1. Run the VI.

2. Enter 30 in the Upper Limit.

LabVIEW Introduction Course Manual 11-26 ni.com


Lesson 11 Common Design Techniques and Patterns

3. Enter 20 to the Lower Limit.

4. When prompted, provide a name and a location for the file.

5. Modify the Lower Limit until it is higher than the current temperature
reading.

6. The Freeze Warning should appear.

7. Set the Lower Limit back to 20.

8. Modify the Upper Limit until it is lower than the current temperature
reading.

9. The Heatstroke Warning should appear.

10. Set the Upper Limit to 15.

11. The Upper Limit < Lower Limit Warning should appear.

12. Stop the VI.

13. Open Microsoft Excel.

14. Select File»Open.

15. Navigate to the file created by the VI.

16. Select Open.

17. Examine the file created by the VI.

18. Close the file when you are finished.

19. Close the VI.

End of Exercise 11-1

© National Instruments Corporation 11-27 LabVIEW Introduction Course Manual

You might also like