Page 1 of 1

Flowcode and State Machine

Posted: Wed Aug 03, 2016 11:15 am
by go233
Hi,
do you have a template or a tutorial or some examples where I can study the implementation of a state machine?
Do you I need to use interrupts?
What is your best advice?
Thanks
Giovanni

Re: Flowcode and State Machine

Posted: Wed Aug 03, 2016 12:19 pm
by Benj
Hello Giovanni,

A state machine doesn't necessarily need to use interrupts. You should be able to learn about them without using interrupts.

We use state machines all over the place in Flowcode components to keep track of things.

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.

Code: Select all

State = 0
While (1)
{
  byte_received = check_for_incoming

  If (byte_received)
  {
    byte = read_incoming

    if (State == 2)
    {
      RightSpeed = byte 
      State = 0
    }
    if (State == 1)
    {
      LeftSpeed = byte 
      State = 0
    }
    if (State == 0)
    {
      if (byte == 'L')
         State = 1
      if (byte == 'R')
         State = 2
    }
  }
}
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.
StateMachine.fcfx
(10.93 KiB) Downloaded 329 times
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.
StateMachine2.fcfx
(12.12 KiB) Downloaded 325 times

Of course state machine don't just apply to comms, you could do a similar thing with a press switch and a LED to make a multi function 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.
StateMachine3.fcfx
(10.49 KiB) Downloaded 332 times
Hope this helps.

More information can be found here, https://en.wikipedia.org/wiki/Finite-state_machine

I've also created a new wiki page on the subject. http://www.matrixtsl.com/wikiv7/index.p ... ateMachine

Re: Flowcode and State Machine

Posted: Wed Aug 03, 2016 2:40 pm
by Benj
I seem to remember when learning state machines that you have to try and break the task up into jobs and then try to implement those jobs in a orderly way.

E.g. consider 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....

Glad I'm a coffee man :wink:

Re: Flowcode and State Machine

Posted: Wed Aug 03, 2016 6:02 pm
by go233
HI,
thanks!!!!
ok then...it seems that I need to sit down and go through all this materials and example!
:shock:
good luck to me!