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
Flowcode and State Machine
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Flowcode and State Machine
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.
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.
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.
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
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
}
}
}
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.
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.
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
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Flowcode and State Machine
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
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
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 96
- Joined: Mon Mar 02, 2015 12:32 pm
- Has thanked: 5 times
- Been thanked: 11 times
- Contact:
Re: Flowcode and State Machine
HI,
thanks!!!!
ok then...it seems that I need to sit down and go through all this materials and example!
good luck to me!
thanks!!!!
ok then...it seems that I need to sit down and go through all this materials and example!
good luck to me!