Difference between revisions of "FiniteStateMachine"

From Flowcode Help
Jump to navigationJump to search
Line 49: Line 49:
  
 
{{Fcfile|StateMachine3.fcfx|State Machine Example3}}
 
{{Fcfile|StateMachine3.fcfx|State Machine Example3}}
 +
 +
 +
A good technique when learning state machines is to try and break the task up into jobs and then try to implement those jobs in a orderly way.
 +
 +
 +
E.g. making a cup of tea.
 +
 +
 +
State 0 - Buy Ingredients - OK = State 1
 +
 +
State 1 - Fill Kettle With Water - OK = State 2  or  Not Enough Water = State 1
 +
 +
State 2 - Boil Kettle - OK = State 3  or Not boiled yet = State 2
 +
 +
State 3 - Place teabag in cup - OK = State 4 or No Teabags = State 0
 +
 +
State 4 - Place milk in cup - OK = State 5 or No Milk = State 0
 +
 +
State 5 - Place boiling water in cup - OK = State 6 or Not filled yet = State 5 or Not Enough Water = State 1
 +
 +
State 6 - Stir the water - OK = State 7 or Not Stirred Enough State 6
 +
 +
State 7 - Cup of tea ready
 +
 +
 +
As you can see making a cup of tea actually has a lot of stages and could be made much more complex, e.g. adding sugar, time to let the tea brew, milk before or after water....

Revision as of 16:40, 3 August 2016

Finite state machines are used all over the place in Flowcode components to keep track of things.

They are a means to allow a fixed number of deterministic states which can simplify a program.


Here is a very basic example.

We want to control a robot via a serial data connection. We control it by sending packets of 2 bytes.

The first byte is the value 'L' to signify the Left motor speed, the next byte is the motor speed 0-255.

The first byte is the value 'R' to signify the Right motor speed, the next byte is the motor speed 0-255.

The firmware for the robot could look something like this.


FC6 Icon.png State Machine Example1


we always start at state 0, and we keep receiving bytes until the characters 'L' or 'R' are received. Once we receive one of the characters we know the next byte will be the corresponding motor speed so we can jump to the right state to collect and assign the value.

This would allow some fault tolerance. e.g. if the robot received a speed byte first then it would do nothing until the 'L' or 'R' bytes are received.

You could even control the Left motor without touching the Right motor by sending the 2 bytes packet starting with 'L' over and over again.


You could then expand the state machine if you wanted to by using a 3-byte packet and including the motor direction.

The first byte is the value 'L' to signify the Left motor speed, the next byte is the motor speed 0-255, the next byte is the direction 0-1.

The first byte is the value 'R' to signify the Right motor speed, the next byte is the motor speed 0-255, the next byte is the direction 0-1.


FC6 Icon.png State Machine Example2


Of course state machine don't just apply to comms, you could do a similar thing with a switch and a LED to make a multi-mode torch.

Start State 0 - Torch Off

Press 1 the torch turns on - State 1.

Press 2 the torch automatically flashes - State 2.

Press 3 the torch turns off - State 0.


FC6 Icon.png State Machine Example3


A good technique when learning state machines is to try and break the task up into jobs and then try to implement those jobs in a orderly way.


E.g. making a cup of tea.


State 0 - Buy Ingredients - OK = State 1

State 1 - Fill Kettle With Water - OK = State 2 or Not Enough Water = State 1

State 2 - Boil Kettle - OK = State 3 or Not boiled yet = State 2

State 3 - Place teabag in cup - OK = State 4 or No Teabags = State 0

State 4 - Place milk in cup - OK = State 5 or No Milk = State 0

State 5 - Place boiling water in cup - OK = State 6 or Not filled yet = State 5 or Not Enough Water = State 1

State 6 - Stir the water - OK = State 7 or Not Stirred Enough State 6

State 7 - Cup of tea ready


As you can see making a cup of tea actually has a lot of stages and could be made much more complex, e.g. adding sugar, time to let the tea brew, milk before or after water....