Car C
Car C
Car C
c
\\ Implements SM that controls the car motion via the key notes
******************************************************************************
****************************
InitCar
Takes a priority number, returns True
PostCar
Takes Event, returns True
RunCar
Event Types: ES_Init, CAR_PLACED, MoveForward, LimitEnd, ES_TIMEOUT, VICTORY,
WELCOME_AUDIO, LIMIT_END, LIMIT_HOME, EARLY_RESET, CORRECT_NOTE, TIMEOUT_POT,
HEADPHONES_RESET
Returns ES_NO_EVENT
Local Variables: NextState
CurrentState is WelcomeCar
If ThisEvent is WELCOME_AUDIO
Set NextState to WaitingForPlacement
EndIf
If ThisEvent is LIMIT_END
Stop car
EndIf
If ThisEvent is LIMIT_HOME
Stop car
EndIf
End WelcomeCar Block
CurrentState is WaitingForPlacement
If ThisEvent is CAR_PLACED
Set NextState to MusicSequence
PostEvent Car_Placed to all services
EndIf
If ThisEvent is EARLY_RESET
Set NextState to WelcomeCar
EndIf
If ThisEvent is LIMIT_END
Stop car
EndIf
If ThisEvent is LIMIT_HOME
Stop car
EndIf
End WaitingForPlacement Block
CurrentState is MusicSequence
If ThisEvent is CORRECT_NOTE
Start car timer
Write motor drive forward
Set NextState to MusicSequence
EndIf
If ThisEvent is LimitEnd
Write motor to stop
Write motor to reverse
Set NextState to CarResetting
EndIf
CurrentState is MovingToEnd
If ThisEvent is LimitEnd
Write motor stop
Write motor drive reverse
Set NextState to CarResetting
EndIf
If ThisEvent is HEADPHONES_RESET or EARLY_RESET
Set ResetEarly to 1
EndIf
EndMovingtoEndBlock
CurrentState is CarResetting
If ThisEvent is LimitHome
Write motor stop
Set NextState to WaitingForHeadphones
EndIf
If ThisEvent is HEADPHONES_RESET or EARLY_RESET
Set ResetEarly to 1
EndIf
EndCarResettingBlock
CurrentState is WaitingForHeadphones
If ThisEvent is HEADPHONES_RESET or ResetEarly equals 1
Set ResetEarly to 0
Set NextState to WelcomeCar
EndIf
EndWaitingForHeadphonesBlock
Set CurrentState to NextState
Return ES_No_Event
END of RunCar
Functions
CheckCarPlace
Takes no parameters, returns true if an event was posted
Local Variable: CurrentInput_CarPlace
Sample the port line and use it to initialize the CurrentInput_CarPlace variable
If CurrentInput_CarPlace not equals to LastInput_Car Place and
Current_Input_CarPlace is lo
PostEvent CAR_PLACED to this service
Set ReturnVal to true
EndIf
Set LastInput_CarPlace to CurrentInput_CarPlace
Return ReturnVal
CheckLimitEnd
Takes no parameters, returns True if an event was posted
Local Variable: CurrentInput_LimitEnd
Sample the port line and use it to initialize the CurrentInput_LimitEnd variable
If CurrentInput_LimitEnd not equals to LastInput_LimitEnd and
Current_Input_LimitEnd is hi
PostEvent LIMIT_END to this service
Set ReturnVal to true
EndIf
Set LastInput_LimitEnd to CurrentInput_LimitEnd
Return ReturnVal
CheckLimitHome
Takes no parameters, returns True if an event was posted
Local Variable: CurrentInput_LimitHome
Sample the port line and use it to initialize the CurrentInput_LimitHome variable
If CurrentInput_LimitHome not equals to LastInput_LimitHome and
Current_Input_LimitHome is hi
PostEvent LIMIT_HOME to this service
Set ReturnVal to true
EndIf
Set LastInput_LimitHome to CurrentInput_LimitHome
Return ReturnVal
Car_Init
Takes no parameters, returns nothing
Initializes the car ports.
Car_Forward
Takes no parameters, returns nothing
Local Variables: dutyCycle_PWM0, channel_PWM0 , dutyCycle_PWM1, channel_PWM1,
reqFreq, group
Car_Stop
Takes no parameters, returns nothing
Local Variables: dutyCycle_PWM0, channel_PWM0 , dutyCycle_PWM1, channel_PWM1,
reqFreq, group
Car_Reverse
Takes no parameters, returns nothing
Local Variables: dutyCycle_PWM0, channel_PWM0 , dutyCycle_PWM1, channel_PWM1,
reqFreq, group