This state machine keeps track of the position of the ping pong ball in the column and posts events to other state machines when the ball has remained at the middle or top IR LEDs for longer than 3 seconds.
State Diagram
Pseudocode
Pseudo-code for the ping pong position module (a service that implements a state machine)
Data private to the module: CurrentState, LastMiddleState, LastTopState
InitPingPong
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call PingPongInitialize
Set CurrentState to Waiting4TOT
Set LastMiddleState to the current state of the middle IR LEDs
Set LastTopState to the current state of the top IR LEDs
Post the initial transition event
End of InitPingPong
PostPingPong
Posts an event to this state machine's queue (false if the enqueue operation failed, true otherwise)
Return post given event with parameters MyPriority and ThisEvent
End of PostPingPong
RunPingPong
Takes an event ThisEvent with EventType that is one of ES_TIMEOUT, SPINNER_START, SPINNER_STOP, FALLING_MIDDLE, FALLING_TOP, RISING_MIDDLE, or RISING_TOP
Returns ES_NO_EVENT if no error ES_ERROR otherwise
If ThisEvent is not ES_TIMEOUT
Reinitialize the idle timer
End
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is PPStandby
If ThisEvent is SPINNER_START
Change CurrentState to Neutral
End
End PPStandby block
CurrentState is Neutral
If ThisEvent is SPINNER_STOP
Change CurrentState to PPStandby
Else If ThisEvent is FALLING_MIDDLE
Initialize the hold timer
Play the "pop" audio clip
Change CurrentState to Middle
Else If ThisEvent is FALLING_TOP
Initialize the hold timer
Play the "pop" audio clip
Change CurrentState to Top
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop playing all audio
End
End Neutral block
CurrentState is Middle
If ThisEvent is ES_TIMEOUT from the hold timer
Post POS_MIDDLE to the Game state machine
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop playing audio
Else If ThisEvent is RISING_MIDDLE
Play the "pop" audio clip
Change CurrentState to Neutral
Else If ThisEvent is SPINNER_STOP
Change CurrentState to PPStandby
End
End Middle block
CurrentState is Top
If ThisEvent is ES_TIMEOUT from the hold timer
Post POS_TOP to the Game state machine
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop playing audio
Else If ThisEvent is RISING_TOP
Play the "pop" audio clip
Change CurrentState to Neutral
Else If ThisEvent is SPINNER_STOP
Change CurrentState to PPStandby
End
End Top block
End of RunPingPong
QueryPingPong
Return the CurrentState
End of QueryPingPong
PingPongInitialize
Set PC4 and PC5 as digital inputs
End of PingPongInitialize
GetIRMiddle
Checks the state of the middle IR, returns 1 if high, and a 0 if low
If the IR pin is high
Return 1
Else
Return 0
End
End of GetIRMiddle
GetIRTop
Checks the state of the top IR, returns 1 if high, and a 0 if low
If the IR pin is high
Return 1
Else
Return 0
End
End of GetIRTop
CheckPingPongEvents
Event checker for the IR LEDs
Get the current state of the middle IR
Get the current state of the top IR
If the the middle IR state has changed
If the middle IR is high
Post RISING_MIDDLE to PingPong
Set ReturnVal to true
Else
Post FALLING_MIDDLE to PingPong
Set ReturnVal to true
End
End
If the the top IR state has changed
If the top IR is high
Post RISING_TOP to PingPong
Set ReturnVal to true
Else
Post FALLING_TOP to PingPong
Set ReturnVal to true
End
End
Change LastMiddleState to CurrentMiddleState
Change LastTopState to CurrentTopState
Return ReturnVal
End of CheckPingPongEvents
Data private to the module: CurrentState, LastMiddleState, LastTopState
InitPingPong
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call PingPongInitialize
Set CurrentState to Waiting4TOT
Set LastMiddleState to the current state of the middle IR LEDs
Set LastTopState to the current state of the top IR LEDs
Post the initial transition event
End of InitPingPong
PostPingPong
Posts an event to this state machine's queue (false if the enqueue operation failed, true otherwise)
Return post given event with parameters MyPriority and ThisEvent
End of PostPingPong
RunPingPong
Takes an event ThisEvent with EventType that is one of ES_TIMEOUT, SPINNER_START, SPINNER_STOP, FALLING_MIDDLE, FALLING_TOP, RISING_MIDDLE, or RISING_TOP
Returns ES_NO_EVENT if no error ES_ERROR otherwise
If ThisEvent is not ES_TIMEOUT
Reinitialize the idle timer
End
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is PPStandby
If ThisEvent is SPINNER_START
Change CurrentState to Neutral
End
End PPStandby block
CurrentState is Neutral
If ThisEvent is SPINNER_STOP
Change CurrentState to PPStandby
Else If ThisEvent is FALLING_MIDDLE
Initialize the hold timer
Play the "pop" audio clip
Change CurrentState to Middle
Else If ThisEvent is FALLING_TOP
Initialize the hold timer
Play the "pop" audio clip
Change CurrentState to Top
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop playing all audio
End
End Neutral block
CurrentState is Middle
If ThisEvent is ES_TIMEOUT from the hold timer
Post POS_MIDDLE to the Game state machine
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop playing audio
Else If ThisEvent is RISING_MIDDLE
Play the "pop" audio clip
Change CurrentState to Neutral
Else If ThisEvent is SPINNER_STOP
Change CurrentState to PPStandby
End
End Middle block
CurrentState is Top
If ThisEvent is ES_TIMEOUT from the hold timer
Post POS_TOP to the Game state machine
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop playing audio
Else If ThisEvent is RISING_TOP
Play the "pop" audio clip
Change CurrentState to Neutral
Else If ThisEvent is SPINNER_STOP
Change CurrentState to PPStandby
End
End Top block
End of RunPingPong
QueryPingPong
Return the CurrentState
End of QueryPingPong
PingPongInitialize
Set PC4 and PC5 as digital inputs
End of PingPongInitialize
GetIRMiddle
Checks the state of the middle IR, returns 1 if high, and a 0 if low
If the IR pin is high
Return 1
Else
Return 0
End
End of GetIRMiddle
GetIRTop
Checks the state of the top IR, returns 1 if high, and a 0 if low
If the IR pin is high
Return 1
Else
Return 0
End
End of GetIRTop
CheckPingPongEvents
Event checker for the IR LEDs
Get the current state of the middle IR
Get the current state of the top IR
If the the middle IR state has changed
If the middle IR is high
Post RISING_MIDDLE to PingPong
Set ReturnVal to true
Else
Post FALLING_MIDDLE to PingPong
Set ReturnVal to true
End
End
If the the top IR state has changed
If the top IR is high
Post RISING_TOP to PingPong
Set ReturnVal to true
Else
Post FALLING_TOP to PingPong
Set ReturnVal to true
End
End
Change LastMiddleState to CurrentMiddleState
Change LastTopState to CurrentTopState
Return ReturnVal
End of CheckPingPongEvents
Header
ES_Configure.h
Source Code
Download
pingpong.h | |
File Size: | 0 kb |
File Type: | h |
pingpong.c | |
File Size: | 9 kb |
File Type: | c |