You are on page 1of 6

Pseudo Code

Master State Machine


InitMasterSM
StartMasterSM with ES_ENTRY event
RunMasterSM
Run DuringMaster function
EV_CLEAR event
pass ES_EXIT and ES_ENTRY event into RunMasterSM
DuringMaster
ES_ENTRY
StartGamePlaySM
StartPACSM
ES_EXIT
pass EX_EXIT into RunGamePlaySM & RunPACSM
GamePlaySM
RunGamePlaySM
WaitForStart state
Run DuringWaitForStart function
GameStatusChange event && param is champaigning
Change the state to Driving
Change Color switch pressed event
Change the color of the LED
Driving state
Run DuringDrivingState function
Entering shooting area event & shooting freeze timer is already expired
Change the state to Shooting
Shooting state
Run DuringShootingState function
Succeed in shooting event
Change the state to Driving, Exit
Failed twice shooting event
Change the state to Driving
During WaitDrivingState function
ES_ENTRY event
StartDrivingSM
ES_EXIT event
Pass ES_EXIT into RunDrivingSM
During ShootingState function
ES_ENTRY event
StartShootingSM

ES_EXIT
Pass ES_EXIT to RunShootingSM
Set shooting freeze timer
PACSM
RunPACSM
WaitForSendingRequest state
Call DuringWaitForSendingRequest function
SendRequest event
Change the state to SendRequest and send request a change in polling
station to PAC with the measured frequency
SendRequest state
EOTEvent
post GetResponse event to RunGamePlaySM
Change the state to WaitForSendingRequest
request Timeout
Change the state to WaitForSendingRequest
Post event to RunGamePlaySM
DuringWaitForSendingRequest function
ES_ENTRY event
Set status timer and query for the status of the game
ES_EXIT event
null
Status timer expired
reset status timer and query for the status of the game
DuringSendRequest function
ES_ENTRY event
Set query timer & send query command & set request timer
ES_EXIT event
null
Query Timeout
Reset the query timer & send query command

GameStatusUpdate event
if game status changes (champaigning or waiting),
post GameStatusChange event to GamePlaySM

DrivingSM
RunDrivingSM
FindCurrentLocation State
Call DuringFindCurrentLocation function
Get current location event

move to GoToPollingStation state


GoToPollingStation State
Call DuringGoToPollingStaton function
Arrive location event
move to FindPollingStation state
FindPollingStation State
Call DuringFindPollingStation function
Measured field event
move to CapturePollingStation state
Search timer timeout event
move to FindCurrentLocation state
CapturePollingStation State
Call DuringCapturePollingStation function
Capture station event
move to FindCurrentLocation state
Capture timeout event
move to FindCurrentLocation state
DuringFindCurrentLocation function
ES_ENTRY
StartFindCurrentLocationSM
ES_EXIT
Pass ES_EXIT to RunFindCurrentLocationSM
If current location is within shooting area, post enter shooting area to GamePlaySM
DuringGoToPollingStaton function
ES_ENTRY
StartGoToPollingStationSM
ES_EXIT
Pass ES_EXIT to RunGoToPollingStation
DuringFindPollingStation function
ES_ENTRY
Set the motor to helix search mode
Set search timer
ES_EXIT
Stop the motor
DuringCapturePollingStation function
ES_ENTRY
Post SendRequest event to PACSM
Set capture timer
GetResponse
Check whether the capture is succeed
if succeed, check the second frequency and post SendRequest event to PACSM again,
with new parameter
Set capture timer

Post Capture Station event


if the first try doesnt succeed (wrong frequency sent or PAC is busy)
repeat the whole step again.
Will try to capture a station for maximum of three times and move on to the next if doesn't
succeed.
ES_EXIT
null
ShootingSM
RunShootingSM
DrivingToShootingPostion state
call DuringDrivingToShootingPosition function
Driving timer timeout event
Move to the shooting state
Shooting state
call DuringShooting function

DuringDrivingToShootingPosition function
ES_ENTRY
Set the rotational timer & set the PWM of the motors to rotate to the correct orientation
ES_EXIT
Stop the motor
Rotational timer expired
Set driving timer & set the PWM of the motor to move forward
DuringShooting function
ES_ENTRY
Control the servomotor to shoot one ball & set shooting timer
Shooting timer expired
Query the game status.
If ball being shot, post Succeed in shooting event to the GamePlaySM.
If unsuccessful for the first time, post Failed once shooting event to the GamePlaySM,
#ball shoot+1;
If unsuccessful twice, post Failed twice shooting event to the GamePlaySM
Failed once shooting
Control the servomotor to shoot one ball & set shooting timer
ES_EXIT
#ball shoot = 0;
FindCurrentLocationSM
RunFindCurrentLocation
AlignBeacon state
Call DuringAlignBeacon function

Aligned beacon event & If #aligned == 3


Change the state to GetLocation state
GetLocation state
Call DuringGetLocation function
Fail the get the location event
Change the state to AlignBeacon state
DuringAlignBeacon
ES_ENTRY event
Set PWM of the motor to move clockwise
Enable the interrupt
IR align event (from interrupt)
Record the IR frequency and the distance
#aligned ++, post aligned beacon event
ES_EXIT event
set #aligned = 0
DuringGetLocation function
ES_ENTRY event
Calculate and store the current location
If succeed, post get current location event with the parameter of the location value
otherwise, post fail the get the location
ES_EXIT
null
GoToPollingStation SM
RunGoToPollingStation
GetClosestAvailStation state
Call DuringGetClosestAvailStation function
GetDirection event
Change the state to DrivingToStation
DrivingToStation state
Call DuringDrivingToStation function
DuringGetClosestAvailStation function
ES_ENTRY
Get the closest available polling station position
Calculate the distance and orientation from the current location to the polling station
Post GetDirection event
ES_EXIT
null
DuringDrivingToStation function
ES_ENTRY
Set rotation timer & Set PWM to rotate the bot
Rotation timer expired
Set the forward timer & set PWM to go forward

Forward timer expired


Post arrive location event event
ES_EXIT
Stop the motor

You might also like