bara_hence
Member
Quote from another thread:
I wrote:
Peter Nachtwey wrote:
Peter showed us all some great example code how to do a basic state machine with entry, always and exit actions we even talked about timeout actions..
The code:
I wrote:
How do you do paralell tasking using State machines??
One way is to program a system of state machine and start another state machine with the other and use a interface between these state machines.. But I for one are really curios how you Peter does this??
Peter Nachtwey wrote:
Start a new thread
State Machines in SCL or ST
I don't like posting the same thing over and over and over and over again. No one will find this information if it doesn't have a title that people will search for and recognize.
I am getting grumpier and grumpier. Perhaps a fixed section of "The Best of Peter Posts" is needed.
Peter showed us all some great example code how to do a basic state machine with entry, always and exit actions we even talked about timeout actions..
The code:
Code:
CASE NewState OF
0: // STATE 0
IF OldState <> NewState
// DO OTHER ON ENTRY CODE HERE
OldState = NewState
END_IF
// DO CODE THAT GET EXECUTED EVERY SCAN HERE. POSSIBLY CHANGE NewState TO THE NEXT STATE
IF OldState <> NewState THEN
// DO CLEAN UP CODE FOR CURRENT STATE ON EXITING STATE
END_IF
1: // STATE 1
IF OldState <> NewState
// DO OTHER ON ENTRY CODE HERE
OldState = NewState
END_IF
// DO CODE THAT GET EXECUTED EVERY SCAN HERE. POSSIBLY CHANGE NewState TO THE NEXT STATE
IF OldState <> NewState THEN
// DO CLEAN UP CODE FOR CURRENT STATE ON EXITING STATE
END_IF
2: // STATE 2
IF OldState <> NewState
// DO OTHER ON ENTRY CODE HERE
OldState = NewState
END_IF
// DO CODE THAT GET EXECUTED EVERY SCAN HERE. POSSIBLY CHANGE NewState TO THE NEXT STATE
IF OldState <> NewState THEN
// DO CLEAN UP CODE FOR CURRENT STATE ON EXITING STATE
END_IF
3: // STATE 3
IF OldState <> NewState
// DO OTHER ON ENTRY CODE HERE
OldState = NewState
END_IF
// DO CODE THAT GET EXECUTED EVERY SCAN HERE. POSSIBLY CHANGE NewState TO THE NEXT STATE
IF OldState <> NewState THEN
// DO CLEAN UP CODE FOR CURRENT STATE ON EXITING STATE
END_IF
// ADD STATES AS NECESSARY
END_CASE
Quote about timeout actions:
If the state must be executed in a certain amount of time a timer should be started in the ON_ENTRY section of the code. The timer should be checked in the part of the scan that is executed every scan the state is executed. There may a potential fourth IF ON_TIMEOUT THEN section. On a timeout an error code, error message number is written to variable that is read by an HMI and displays an error message. Now when there is a hangup because some limit switch didn't get hit and error message can be displayed that tells the operator why the machine stopped.
Therefore each state could have a
ON_ENTRY
DO_EVERY_SCAN
ON_TIMEOUT
ON_EXIT