HoneyBeeSM State Machine
HoneyBeeSM Pseudocode
HoneyBee.SM
/******************************************************************************/
HONEY BEE MODULE STATE MACHINE PSEUDO-CODE
InitializeHoneyBeeSM
Takes a priority number, returns True
Initialize servo
Initialize the MyPriority variable with the passed in parameter
Initialize HoneyTimer
Set CurrentState to be InitHoneyBee
post Event ES_INIT to InitHoneyBeeSM (this service)
End
RunHoneyBeeSM (implements the state machine for the HoneyBee Servo)
The EventType field of ThisEvent will be of one: ES_INIT, ES_HONEYWIN, ES_RESET
List of States: InitHoneyBee, HoneyWaiting, HoneyRacing, HoneyCelebrate
Returns ES_NO_EVENT
Module Variables: HoneyBeeServoCounter
Set NextState to CurrentState
Based on the state of the CurrentState variable, choose one of the following blocks of code:
CurrentState is InitHoneyBeeSM
IF ThisEvent is ES_INIT
Do initialization
Set NextState to Racing
ENDIF
CurrentState is HoneyWaiting:
IF ThisEvent.EventType is equal to ES_TIMEOUT AND ThisEvent.EventParam is equal to GoTimer
Set NextState to HoneyRacing
set the honeybee timer to 500 ticks (500ms)
start the HoneyTimerSet HoneyBee Timer
ENDIF
CurrentState is Racing
IF ThisEvent is ES_TIMEOUT
IF ThisEvent.EventParam is number for goTimer
Set NextState to Racing
Set HoneyBee Timer
Start HoneyBee Timer
ENDIF
IF ThisEvent.EventParam is number for HoneyBeeTimer
move HoneyBeeServo K steps forward (K depends on the speed of the honeyBee, counter++)
IF HoneyBeeServo reaches its maximum
Post ES_HONEYWIN to RunGameSM
ELSE
Set NextState to Racing
Set HoneyBee Timer
Start HoneyBee Timer
ENDIF
ENDIF
IF ThisEvent is ES_PLAYERWIN
call reset function
Set NextState to HoneyWaiting
ENDIF
IF ThisEvent is ES_PLAYERLOST
call reset function
Set NextState to HoneyWaiting
ENDIF
IF ThisEvent is ES_RESET
call reset function
Set NextState to HoneyWaiting
ENDIF
CurrentState is HoneyCelebrate:
IF ThisEvent.EventType is equal to ES_TIMEOUT AND ThisEvent.EventParam is equal to HoneyDanceTimer
IF DanceCount < 3
If Dance flag is 0
Move the servo forward
If Dance Flag is 1
Move the Servo backward
ENDIF
If DanceCount = 3
Run HoneyReset function
ENDIF
ENDIF
IF ThisEvent is ES_RESET
call reset function
Set NextState to HoneyWaiting
ENDIF
void HoneyBeeReset(void)
Reset HoneyBeeServo and its counter