Presentation is loading. Please wait.

Presentation is loading. Please wait.

TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion.

Similar presentations


Presentation on theme: "TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion."— Presentation transcript:

1 TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

2 S TATE M ACHINES You use the state machine design pattern to implement algorithms that can be explicitly described by a state diagram or flow chart. A state machine consists of a set of states and a transition function that maps to the next state.

3 S TATE M ACHINE I NFRASTRUCTURE 1. While Loop: The follow of the state transition diagram is implemented by the while loop. The while loop allows to execute the various states continually. 2. Case Structure: the individual states are represented by cases in the case structure. It contains a case for each state and the code to execute for each state. 3. Shift Register: A shift register on the while loop keeps track of the current state to the Case structure input 4. State Functionality Code: it is used to implement the function of the state(it is located in the case structure). 5. Transition Code: the transition code determines the next state in the sequence, we usually use the “enum” constant to determine the next state

4 You use enumerated type constants as case selectors to control the initialization and transition of state machines. The most common obstacle when implementing state machines with enumerated type constants is that if a user attempts to add or delete a state from the enumerated type constant, the remaining wires connected to the copies of this enumerated type constant break. One solution to this problem is to type define the enumerated constant. Creating a type defined enumerated type constant causes all the enumerated type constant copies to automatically update if you add or remove a state. Enumerated Type Constant C ONTROLLING THE S TATE M ACHINE

5 State Machines A state machine relies on user input or in-state calculation to determine which state to go to next. Many applications require an initialization state and a default state, where many different actions can be performed. State machines are commonly used to create user interfaces where different user actions send the user interface into different processing segments. Each processing segment acts as a state in the state machine. Each segment leads to another segment for further processing or waits for another user action. Click on each button to see how different actions can lead to different states!

6 T RANSITION C ODE There are several common design patterns used to develop the transition code. You choose the transition code design pattern based on the number of states you need to transition between: Transitioning to another state (Default Transition) Transition between two possible states Transition among two or more possible states

7 D EFAULT T RANSITION For the default transition, no code is needed to determine the next state, as there is only one possible choice.

8 T RANSITION B ETWEEN T WO S TATES There are several patterns commonly used to accomplish transition between two states. This method (in the figure) works well if you know that the individual state always moves between two states. But, If you need to modify the state to transition among more than two states, this solution would not work and would require a major modification of the transition. code.

9 T RANSITION A MONG T WO OR M ORE S TATES You can create a more scalable architecture by using one of the following methods for transition among multiple states: 1. Case Structure: Each case in a Case structure corresponds to an item in the enumerated type control, making it easy to read and understand the code. A Case structure is also scalable and you can add more transitions to a particular state by adding more cases to the Case structure. However, due to the nature of the Case structure, you cannot see the complete functionality of the transition code at a glance.

10 T RANSITION A MONG T WO OR M ORE S TATES : C ASE S TRUCTURE E XPLICATION This case structure means, In the “Analyzer Case”: If the string “Analysis”= “One” the next state will be “Start” If the string “Analysis”=“Two” the next state will be “Analyzer” If the string “Analysis”=“Three” the nest state will be “Error Handler. Case 1 Case2 Case 3

11 T RANSITION A MONG T WO OR M ORE S TATES 2. Transition Array: If you need more of the code to be visible than a Case structure allows, you can create a transition array for all the transitions that can take place in the transition code for a state.

12 S TATE M ACHINE : C ONCLUSION A state machine in LabVIEW consists of a While Loop, a Case structure, a type defined enum constant and a shift register: Each state of the state machine is a separate case in the Case structure. You place VIs and other code that the state should execute within the appropriate case. A shift register stores the state (indicated by the enum constant)that should execute upon the next iteration of the loop. The next state will be either Shut Down or Idle

13 S TATE M ACHINE : C ONCLUSION For the displayed VI, the possible states are Startup, Idle, Event 1, Event 2, and Shutdown. An enumerated constant stores the states. Each state has its own case in the Case structure. The outcome of one case determines the case that next executes. The shift register stores a value that determines which case runs next.

14 S TATE M ACHINE D ESIGN P ATTERN An advantage of the state machine architecture is that each case determines the next state, unlike Sequence structures that cannot skip a frame. The state machine design can make the block diagram much smaller, and therefore easier to read and debug.  A disadvantage of the state machine design pattern is that it makes it possible to skip states. If two states in the structure are called at the same time, this model handles only one state, and the other state does not execute.


Download ppt "TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion."

Similar presentations


Ads by Google