Tutorial 11: Programming with state machines

 

State machine with relating state diagram

A device / component / object / machine may include a program sequence where previous events determine the further program sequence. Such programs should be implemented as a state machine.

Program flow is controlled by a state variable, an Enumerated type, where each state is asigned a descriptive name and thus can be visualized. The function is described in a state diagram:

Here is an example of a motor starter which can start the motor in the forward or reverse direction. The state known as "Motor stopped" contains two different conditions. When one of these conditions is met, the motor is started in the forward or reverse direction.

The diagram also shows that ‘Stop’ is to be activated before the engine can be started in the opposite direction.

In a state diagram a ‘State’ is illustrated with a circle containing the name of the state.

For a given state there may be a number of associated conditions, each of which cause a change to another state when the condition is met.

A ‘Condition’ is illustrated in a state diagram with a description in text across a line with an arrow. The arrow indicates which state to change to when the condition is met.

 

On transition from one state to another, the line passes a ‘Perform’ box. The ‘Perform’ box holds a program flow to execute when changing to the new state. A ‘Perform’ box program may contain conditions that determine how the program is executed, but the program is carried out "immediately" and contains no timers or other delay functions. The arrow after the ‘Perform’ box indicates the new state value assigned to the state variable.
A condition is ‘Present’. A condition may be ‘Start button is activated’ or ‘Temperature > 23 degrees’.
However, it is not correct to write "Start button was activated" (meaning has been).
Normally, when a timer is activated, the following state includes a condition: Timer

 

Program flow implementation of a state machine corresponding to the above state diagram
The state machine is implemented as a "case" statement, which selects the program flow based on the state variable. The conditions are shown as "Condition boxes", which will perform the program in the "Perform box” when the condition is met. “Perform boxes” furthermore contain assignment of  new value for the state variable. The “case” statement must be called with an appropriate time interval.

Should you want to explore further, the type PD_17236 shows an implementation of the described motor controller.

Now let us make our own program using the state machine concept.

We want to make a setup that emulates a house temperature control system, where the set point for night time temperature is low, the set point for day time temperature is higher, and the transition from day to night and vice-versa is gradual.

Rather than spending 24 hours observing the day go by, we will implement the state machine in a timer, but the principles of the state machine will be the same.

We will update the control component "XX_master_tutorial_ component" that we created in the previous tutorial.

 

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.