Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

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

hierarchical state machine)


Data private to the module: CurrentState, Ball_Count, FirstShot

Functions private to the module: DuringLoadingInShooting,


DuringAligningInShooting, During ShootingInShooting

RunShootingSM (implements a state machine)


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

Data private to the function: CurrentState

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


following blocks of code:

CurrentState is LoadingInShooting
Set ReturnEvent equal to CurrentEvent equal to return of
DuringLoadingInShooting()
If CurrentEvent.EventType is not equal to ES_NO_EVENT
ThisEvent is ES_TIMEOUT
If EventParam is SHOOTING_TIMER
Set NextState to ShootingInShooting
Set MakeTransition to true
EndIf
Else if EventParam is LAUNCH_WINDUP_TIMER
Set NextState to AligningInShooting
Set FirstShot to false
Set MakeTransition to true
EndElse
EndIf
End LoadingInShooting block

CurrentState is AligningInShooting
Set ReturnEvent equal to CurrentEvent equal to return of
DuringAligningInShooting()
If CurrentEvent.EventType is not equal to ES_NO_EVENT
ThisEvent is AR1_ALIGN
Start KICKBACK_TIMER
Post DRIVE_CW to MotorService
Set MakeTransition equal to false
ThisEvent is NOT_ALIGNED
Post ES_ALIGN_QUERY to SPILeaderService
ThisEvent is ES_TIMEOUT
If EventParam is KICKBACK_TIMER
Set NextState to ShootingInShooting
Set MakeTransition to true
EndIf
End AligningInShooting block

CurrentState is ShootingInShooting
Set ReturnEvent equal to CurrentEvent equal to return of
DuringShootingInShooting()
If CurrentEvent.EventType is not equal to ES_NO_EVENT
ThisEvent is ES_TIMEOUT
If EventParam is SHOOTING_TIMER
If Ball_Count is greater than 0
Set NextState to LoadingInShooting
Set MakeTransition to ture
EndIf
Else
Set NextState to WaitingInShooting
Post STOP_SHOOTING to MasterSM
EndElse
EndIf
End ShootingInShooting block

If MakeTransition equals true


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

Return ReturnEvent
End of RunShootingSM

StartShootingSM
Takes CurrentEvent as parameter, returns nothing.

Post ES_LAUNCH_100 to SPILeaderService


Start LAUNCH_WINDUP_TIMER
Set BallCount equal to INITIAL_BALL_COUNT
Set FirstShot to true
Call RunShootingSM(CurrentEvent)
End of StartShootingSM

DuringLoadingInShooting
Takes Event as parameter, returns Event.

If Event is ES_ENTRY
Post ES_INDEX_OPEN to SPILeaderService
Endif

Else if event is ES_EXIT

Endif

Else

EndElse

Return Event

End of DuringLoadingInShooting

DuringAligningInShooting
Takes Event as parameter, returns Event.

If Event is ES_ENTRY
Post DRIVE_CW to MotorService
Post ES_ALIGN_QUERY to SPILeaderService
Endif

Else if event is ES_EXIT


Post DRIVE_STOP to MotorService
Endif

Else

EndElse

Return Event

End of DuringAligningInShooting

DuringShootingInShooting
Takes Event as parameter, returns Event.

If Event is ES_ENTRY
Post ES_INDEX_CLOSE to SPILeaderService
Ball_Count –-
Start SHOOTING_TIMER
Endif

Else if event is ES_EXIT


PStart SHOOTING_TIMER
Endif

Else
EndElse

Return Event

End of DuringShootingInShooting

You might also like