A state machine is a behavior model which can be in one of a set number of stable conditions depending on its previous condition and on the present values of its inputs.What is your definition of a state machine?
Have you created a state machine for a project. If your answer is yes, then I want to ask you why do you think you should create a state machine for the project?By that definition, every MCU program is a state machine.
There are plenty of examples. In my last project I have a set of wireless receivers that must communicate with one transmitter the transmitter being the master. Each receiver has a specific address and the communications protocol is very specific. In this case we must first have the correct address to move to the next state. In this state machine if we sit in any state for more than a set period of time we automatically move back to the starting state.There are many ways to write c code for a embedded System. I see that many people use state machines. When do you think you should implement a state machine in an embedded project. What are the benefits of implementing a state machine?
Here is a simple example. This code is used in a wireless network with one 'master' and multiple 'slaves'. Only one slave is allowed to respond to a message in order to avoid collisions and because wireless communications must deal with possible errors in messages. Each message has a header/footer, address command etc.There are many ways to write c code for a embedded System. I see that many people use state machines. When do you think you should implement a state machine in an embedded project. What are the benefits of implementing a state machine?
I offer a different perspective. I began programming before I had any formal training. I had years of experience before I broke open a book and came across the concept of a Finite State Machine. It has profoundly changed the way the way I do things. I use state machines (I think) in exactly the formal way that is taught, and what I write now is much better than what I wrote years ago; I am confronted with the stark difference every time I have to go back and look at my old work. This is mostly referring to PLC programming but also some other things.My take: the state machine is a formality that we teach to students in order to get them thinking about how you can organize a program logically. In real use, it is rarely implemented in the formal way that is taught, but the ideas are still used in a less obvious way, (my original response was already taken by Mr Chips.)
Bob
Every MCU program is a Finite State Machine.Have you created a state machine for a project. If your answer is yes, then I want to ask you why do you think you should create a state machine for the project?
I won't say that's not correct but I think that's a narrow interpretation and TS's question was meant to address the concept of a state machine for a complex application from top down. Or maybe I'm projecting; that's what it means to me when someone says "state machine."Every MCU program is a Finite State Machine.
When you write the structure:
if (...)
then ...
else ...
This is a Finite State Machine.
@strantor I am agree with you that's what i mean. If we are using state machine in our project then we should know the solid reason why we are using it.I won't say that's not correct but I think that's a narrow interpretation and TS's question was meant to address the concept of a state machine for a complex application from top down. Or maybe I'm projecting; that's what it means to me when someone says "state machine."
/*
* simple modbus master state machine
*/
int8_t controller_work(void)
{
switch (cstate) {
case CLEAR:
/*
* command specific tx buffer setup
*/
switch (modbus_command) {
case G_SET: // write code request
break;
case G_AUX: // write code request
break;
case G_ERROR: // read code request
break;
case G_MODE: // operating mode request
default:
break;
}
break;
case INIT:
break;
case SEND:
break;
case RECV:
/*
* check received response data for size and format for each command sent
*/
switch (modbus_command) {
case G_SET: // check for controller error codes
break;
case G_AUX: // check for controller error codes
break;
case G_ERROR: // check for controller error codes
break
case G_MODE: // check for current operating mode
default:
break;
default:
break;
}
return 0;
}
void main(void)
{
/* Loop forever */
while (true) { // busy work
controller_work();
}
}
You are correct. I just want TS to understand that there is nothing special about state machine when applied to embedded application.I won't say that's not correct but I think that's a narrow interpretation and TS's question was meant to address the concept of a state machine for a complex application from top down. Or maybe I'm projecting; that's what it means to me when someone says "state machine."
If startButton and doorSwitch = true:
doorSolenoid = true
playStartSound = true
fillValve = true
fillTimerRun = true
If levelSwitch or fillTimerFinished = true:
fillTimerRun = false
fillValve = false
drumMotorRun = true
drumTimerRun = true
If drumTimerFinished = true:
drumTimerRun = false
drumMotorRun = false
drainPumpRun = true
drainPumpTimerRun = true
If stopButton or drainPumpTimerFinished = true:
drainPumpRun = false
drainPumpTimerRun = false
If levelSwitch = false:
doorSolenoid = false
playStopSound = true
If state = 0: # waiting
If startButton and doorSwitch = true:
copy (10) to state
If state = 10: # play start tune
copy (5sec) to stateTimerPreset
If stateTimerDone = true:
resetStateTimer
copy (20) to state
If state = 20: # filling
copy (50sec) to stateTimerPreset
If stateTimerDone or levelSwitch = true:
resetStateTimer
copy (30) to state
If state = 30: # drum run
copy (600sec) to stateTimerPreset
If stateTimerDone = true:
resetStateTimer
copy (40) to state
If state = 40: # drain
copy (55sec) to stateTimerPreset
If stateTimerDone = true and levelSwitch = false:
resetStateTimer
copy (50) to state
If state = 50: # play stop tune
copy (5sec) to stateTimerPreset
If stateTimerDone = true:
resetStateTimer
copy (0) to state
If state >0: stateTimerRun = true; Else: false
If state >0: doorLockSolenoid = true; Else: false
If state = 10: playStartSound = true; Else: false
If state = 20: fillValve = true; Else: false
If state = 30: drumMotorRun = true; Else: false
If state = 40: drainPumpRun = true; Else: false
If state = 50: playStopSound = true; Else: false
If stopButton = true or doorSwitch = false:
if state <20:
copy (0) to state
Else:
copy (40) to state