You are on page 1of 6

Module level functions:

DuringGameNotActive
DuringFaceOff
DuringOffense
DuringDefense

Module level variables


CurrentState, MyPriority, TEAM

InitGameMasterSM
save our priority
Start the Master State machine
End of InitGameMasterSM

PostGameMasterSM
Post an event to service with saved priority number

RunGameMasterSM

set MakeTransition to false by default


set NestState to CurrentState
instead of default to normal entry to new state, we set param to TEAM and
pass down to lower level SM
assume no error, for top level sm we always need a return event set to
ES_NO_ERROR

CurrentState can be one of: GameNotActive, FaceOff, Offense, Defense


EventType can be one of: EV_WAIT4START, EV_FACEOFF, EV_REDPO, EV_BLUEPO,
EV_GAMEOVER. Except the first one, all other
events need to be responded in any of FaceOff, Offense, and Defense

If current state is GameNotActive


Execute During function for GameNotActive. ES_ENTRY & ES_EXIT are
processed here allow the lowere level state machines to re-map
or consume the event

process any events


If an event is active
If event is EV_WAIT2START
Execute action function for GameNotActive, EV_WAIT2START
Decide next state to be GameNotActive
for internal transitions, skip changing MakeTransition
mark that we are not taking a transition
if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

If event is EV_FACEOFF
Execute action function for GameNotActive, EV_FACEOFF
Decide next state to be FaceOff
for internal transitions, skip changing MakeTransition
mark that we are taking a transition
if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block
end of this block

If current state is FaceOff


Execute During function for FaceOff. ES_ENTRY & ES_EXIT are
processed here allow the lowere level state machines to re-map
or consume the event
CurrentEvent = DuringFaceOff(CurrentEvent);

process any events


If an event is active
If event is EV_GAMEOVER
Execute action function for FaceOff, EV_GAMEOVER
Decide next state to be GameNotActive
for internal transitions, skip changing MakeTransition
mark that we are taking a transition
if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

If event is EV_REDPO
Execute action function for FaceOff, EV_REDPO
if we are TEAM RED, set NextState to Offense, otherwise set
to Defense
for internal transitions, skip changing MakeTransition
mark that we are taking a transition
if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

If event is EV_BLUEPO
Execute action function for FaceOff, EV_BLUEPO
if we are TEAM BLUE, set NextState to Offense, otherwise set
to Defense
for internal transitions, skip changing MakeTransition
mark that we are taking a transition
if transitioning to a state with history change kind of entry
end of this block

If event is EV_FACEOFF
Execute action function for FaceOff: EV_FACEOFF
set NextState to FaceOff
for internal transitions, skip changing MakeTransition
mark that we are not taking a transition
if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

end of this block

If current state is Offense


Execute During function for Offense. ES_ENTRY & ES_EXIT are
processed here allow the lowere level state machines to re-map
or consume the event
process any events
If an event is active
If event is EV_GAMEOVER
Execute action function for Offense, EV_GAMEOVER
Decide NextState will be GameNotActive
for internal transitions, skip changing MakeTransition
mark that we are taking a transition
if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

If event is EV_REDPO
Execute action function for Offense, EV_REDPO
if we are TEAM RED, set NextState to Offense without
transiting state
else set NextState to Defense with transiting state

for internal transitions, skip changing MakeTransition


if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

If event is EV_BLUEPO
Execute action function for Offense, EV_BLUEPO
if we are TEAM BLUE, set NextState to Offense without
transiting state
else set NextState to Defense with transiting state

for internal transitions, skip changing MakeTransition


if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

If event is EV_FACEOFF
Execute action function for Offense: EV_FACEOFF
for internal transitions, skip changing MakeTransition
set NextState to FaceOff
mark that we are taking a transition
if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

end of this block

If current state is Defense


Execute During function for Defense. ES_ENTRY & ES_EXIT are
processed here allow the lowere level state machines to re-map
or consume the event

process any events


If an event is active
If event is EV_GAMEOVE
Execute action function for Defense, EV_GAMEOVER
Decide next state to be GameNotActive
for internal transitions, skip changing MakeTransition
mark that we are taking a transition
if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block
If event is EV_REDPO
Execute action function for Defense, EV_REDPO
if we are TEAM RED, set NextState to Offense with transiting
state
else set NextState to Defense without transiting state

for internal transitions, skip changing MakeTransition


if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

If event is EV_BLUEPO
Execute action function for Defense, EV_BLUEPO
if we are TEAM BLUE, set NextState to Offense with transiting
state
else set NextState to Defense without transiting state

for internal transitions, skip changing MakeTransition


mark that we are taking a transition
if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

If event is EV_FACEOFF
Execute action function for Defense, EV_FACEOFF
set NextState to FaceOff
for internal transitions, skip changing MakeTransition
mark that we are taking a transition
if transitioning to a state with history change kind of entry
we will decide if we need a history state or not
end of this block

If event is ES_TIMEOUT
set NextState to FaceOff
mark that we are making a transition
end of this block

end of this block

If we are making a state transition


Execute exit function for current stat
Modify state variable
Execute entry function for new state
this defaults to ES_ENTRY
end if

in the absence of an error the top level state machine should


always return ES_NO_EVENT, which we initialized at the top of func
End of RunGameMasterSM

void StartGameMasterSM ( ES_Event_t CurrentEvent )


init CurrentState to GameNotActive
now we need to let the Run function init the lower level state machines
use LocalEvent to keep the compiler from complaining about unused var
End of StartGameMasterSM
/***************************************************************************
private functions
***************************************************************************/

static ES_Event_t DuringGameNotActive( ES_Event_t Event)


{
ES_Event_t ReturnEvent = Event; // assme no re-mapping or comsumption

process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events


if ES_ENTRY
implement any entry actions required for this state machine
Init and turn off LEDs
Init DCMotors
Init Servo motors

after that start any lower level machines that run in this state
else if ES_EXIT
on exit, give the lower levels a chance to clean up first
repeat for any concurrently running state machines
now do any local exit functionality
turn on game indicator
grab team color info from GetTeamColor()
Light up corresponding LED (red or blue)
else
do the 'during' function for this state
run any lower level state machine

return either Event, if you don't want to allow the lower level machine
to remap the current event, or ReturnEvent if you do want to allow it.
End of DuringGameNotActive

DuringFaceOff
assme no re-mapping or comsumption

process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events


if ES_ENTRY
implement any entry actions required for this state machine
after that start any lower level machines that run in this state
else if ES_EXIT
on exit, give the lower levels a chance to clean up first
repeat for any concurrently running state machines
now do any local exit functionality
else
do the 'during' function for this state
run any lower level state machine

return either Event, if you don't want to allow the lower level machine
to remap the current event, or ReturnEvent if you do want to allow it.
End of DuringFaceOff

DuringOffense
assme no re-mapping or comsumption

process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events


if ES_ENTRY
implement any entry actions required for this state machine
after that start any lower level machines that run in this state
else if ES_EXIT
on exit, give the lower levels a chance to clean up first
repeat for any concurrently running state machines
now do any local exit functionality
else
do the 'during' function for this state
run any lower level state machine

return either Event, if you don't want to allow the lower level machine
to remap the current event, or ReturnEvent if you do want to allow it.
End of DuringOffense

DuringDefense
assme no re-mapping or comsumption

process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events


if ES_ENTRY
implement any entry actions required for this state machine
after that start any lower level machines that run in this state
else if ES_EXIT
on exit, give the lower levels a chance to clean up first
repeat for any concurrently running state machines
now do any local exit functionality
else
do the 'during' function for this state
run any lower level state machine

return either Event, if you don't want to allow the lower level machine
to remap the current event, or ReturnEvent if you do want to allow it.
End of DuringDefense

You might also like