State machines are one of several programming methods used to create functional logic. The state machine interface offers a diagramatic approach to perform work as you move between states using transitions.
- State - represents the current state the machine or process is in. When moving in or out of a state Actions can be fired. The Initial State is automatically run at bootup after Screen.screenready fires. All of the initial states fire asynchronously which means you cannot prioritize one over another.
- Transition - Called by a CCM action from an external event to move from one state to another or from within a state in the State Machine. Examples of external events can be a button press in a menu page or any user created event or system native event. An action can also be called from the transition that will be fired when the transition completes.
- Conditional Transition - A transition may have a condition attached to it and is called by an event just like a normal transition to move to a different state only when a condition is met. This is done using a logic expression for the transition, if the condition evaluates to true, the transition will complete and move to the next state. If it evaluates to false no action is taken. To utilize this feature the name of the condition is called rather than the name of the transition.
- Group - All the elements of the state machine will be shown in the groups folder. If you have a complicated state machine it may be best to assign a unique group to the state machine before you add any elements so they are all contained in the same group. Notice transitions are categorized as events and conditional transitions are categorized with calculation events.
- Loop Back Transition - A common technique is to loop a transition back on the same state and set the transitions timer to create recurring timed events.
- Transition Timer - Transitions have a timer that can be used to create a time delay before moving to the next state. There are a few ways to utilize this feature. Setup a one-shot timer then start the timer on entering its starting state. Once the timer expires the transition will fire and move to the next state. The timer can also be started from an external event and once the timer expires the transition from state tot state will occur.
The following video example shows how to use a State Machine to monitor an engines coolant temperature and determine if it is too cold or too hot and either turn on a heater or fan cooling. There are three states the engine can exist in, nominal, under temp, and over temp. The state machine uses conditional checks to determine which state it is in.