You are on page 1of 3

Pseudo-code for the Starting module (a service that implements a

hierarchical state machine)


Data private to the module: CurrentState, CheckLimit

Functions private to the module: DuringAligning, DuringExiting

RunStartingSM (implements a state machine)


The EventType field of ThisEvent will be one of: AR1_ALIGN,
LIMIT_PRESS, ES_TIMEOUT

Data private to the function: MakeTransition, NextState,


CurrentState, EntryEventKind, ReturnEvent

Based on the state of the CurrentState variable choose one of the


following blocks of code:

CurrentState is Aligning
Set ReturnEvent equal to CurrentEvent equal to return of
DuringAligning()
If CurrentEvent.EventType is not equal to ES_NO_EVENT
ThisEvent is AR1_ALIGN
Start KICKBACK_TIMER
Post DRIVE_STOP to MotorService
Set MakeTransition equal to false
ThisEvent is NOT_ALIGNED
If CheckLimit is false
Post ES_ALIGN_QUERY to SPILeaderService
EndIf
Else
Post ES_LIMIT_QUERY to SPILeaderService
EndElse
ThisEvent is LIMIT_PRESS
If EventParam is LIMIT_PRESS
Set NextState equal to Exiting
Set MakeTransition to true
EndIf
Else
Post ES_LIMIT_QUERY to SPILeaderService
EndElse
ThisEvent is ES_TIMEOUT
If EventParam is KICKBACK_TIMER
Set NextState equal to Exiting
Set MakeTransition to true
EndIf
Else if EventParam is STARTING_TIMER
Set CheckLImit equal to true
EndElse
EndIf
End Aligning block

CurrentState is Exiting
Set ReturnEvent equal to CurrentEvent equal to return of
DuringExiting()
If CurrentEvent.EventType is not equal to ES_NO_EVENT
ThisEvent is ES_TIMEOUT
Set NextState equal to WaitingInStarting
Set MakeTransition equal to true
EndIf
End Exiting block

If MakeTransition equals true


Set CurrentEvent to ES_EXIT
Call RunStartingSM with CurrentEvent
Set CurrentState equal to NextState
Call RunStartingSM with EntryEvent
EndIf

Return ReturnEvent
End of RunStartingSM

StartStartingSM
Takes CurrentEvent as parameter, returns nothing.

Set CurrentState equal to Aligning


Post GAME_STARTED to SPILeaderService
Call RunStartingSM with CurrentEvent

End of StartStartingSM

DuringAligning
Takes Event as parameter, returns Event.

If Event is ES_ENTRY
Post DRIVE_RIGHT_REVERSE to MotorService
Post DRIVE_LEFT_REVERSE to MotorService
Post ES_ALIGN_QUERY to SPILeaderService
Start STARTING_TIMER
Set CheckLimit equal to false
Endif

Else if event is ES_EXIT

Endif

Else

EndElse

Return Event

End of DuringAligning

DuringExiting
Takes Event as parameter, returns Event.

If Event is ES_ENTRY
Post DRIVE_LEFT_FORWARD to MotorService
Post DRIVE_RIGHT_FORWARD to MotorService
Start STARTING_TIMER
Endif

Else if event is ES_EXIT


Post DRIVE_STOP to MotorService
Post STOP_STARTING to MasterSM
Endif

Else
Set Event equal to return of call to RunStartingSM with
parameter Event
EndElse

Return Event

End of DuringExiting

You might also like