6.1 State Machines

   

Alan Turing first proposed the state machine in 1936. He envisaged a box with a paper tape input (input alphabet), a set of states (and start states), and a mechanism for mapping the input to the next state.

Definition ” A state machine is a conceptual machine with a given number of states; it will only be in one of the states at any given time. State transitions are changes in state caused by input events. In response to an input event the system may transition to the same or a different state, while an output event may be optionally generated.

For our purposes Enumerated Types and other assorted inputs replace the paper tape input. The mechanism for setting the start states is an initialization function, and the mechanism for mapping an input to the next state is the case structure. If the state machine needs to manage its own data you will need to include a While Loop structure and a shift register.

If you are beginning to think this is a little archaic, you could always call your state machine an object, package, module, component, or whatever.

So let's try out a simple and reasonably useless example before diving into the good stuff.

6.1.1 State Machine Example ”Washing Machine

graphics/washingmachine_icon.gif

The standard example given to describe a state machine is the washing machine, so who are we to buck a trend. Figure 6.1 shows the states available to a washing machine.

Figure 6.1. Washing machine state machine.

graphics/06fig01.gif

Select your start state and run. It's best to view the diagram with Highlight Execution selected. If the subVIs are set up to run properly, you will find that the display light emitting diodes (LEDs) will run in order, displaying the current state of the washing machine.

Figure 6.2 is the hierarchy of subVIs, each one can be selected and its output condition chosen .

Figure 6.2. Washing machine state subVIs.

graphics/06fig02.gif

So looking at the block diagram in Figure 6.3 the start condition "Door Closed" is input into the shift register and then is passed to the case statement. The door state is returned (this could be from a switch reading) and then if the door is closed, the next state "Fill with Water" is called and so on.

Figure 6.3. Washing machine block diagram.

graphics/06fig03.gif

The other states are modeled in Figure 6.4.

Figure 6.4. Washing machine states.

graphics/06fig04.jpg

If you want to experiment with the actual state responses, pop up on the relevant subVI and change the enumerated settings control.

The corresponding state transition diagram is shown in Figure 6.5. The states are the boxes and the transitions are the arrows.

Figure 6.5. Washing machine state transition diagram.

graphics/06fig05.gif

So why are state machines better than flowing it in the normal LabVIEW way?

The strictly sequential way in Figure 6.6 looks okay from a diagrammatic point of view, nice and simple, but what if we want to add a new state? Or change the order of states? Both of these would involve deleting the source code and moving VIs around. You don't have the options and control of flow that you get with the state machine structure.

Figure 6.6. Sequential washing machine block diagram.

graphics/06fig06.gif

The error handling is also more vague. If you model the error conditions as valid states of the system you will know what error was called, and you would also know that only the actions you control would be called on an error.

State machines are an excellent method of reducing complexity in your software. They allow your code to be flexible, readable, and maintainable .


   
Top


A Software Engineering Approach to LabVIEW
A Software Engineering Approach to LabVIEW
ISBN: 0130093653
EAN: 2147483647
Year: 2003
Pages: 66

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