Professional Documents
Culture Documents
c
\\ Implements SM that controls the TRex via events from other SMs and the limit
switches
******************************************************************************
*******************************
InitTRex
Takes a priority number, returns True
PostTRex
Takes Event, returns True
RunTRex
Event Types: ES_INIT, CAR_PLACED, ATTACK, LIMIT_UP, EARLY_RESET, ES_TIMEOUT, VICTORY,
LIMIT_DOWN, HEADPHONES_RESET
Returns ES_NO_EVENT
Local variables: NextStateTRex
CurrentState is WaitingforPlacementTRex:
If ThisEvent is CAR_PLACED
Drive TRex motor up
Set NextStateTRex to RisingTRex
EndIf
EndBlock
CurrentState is RisingTRex:
If ThisEvent is ATTACK and EventParam is 1
Start AttackTimer
Drive motor down
Set NextStateTRexto LoweringTRex
EndIf
If ThisEvent is LIMIT_UP
Stop motor
Set NextStateTRex to LimitHitEarlyTRex
EndIf
If ThisEvent is EARLY_RESET
Stop motor
Drive motor down
Set NextStateTRex to ResetFullyTRex
Set ResetEarly to 1
EndIf
CurrentState is LimitHitEarlyTRex
If ThisEvent is ATTACK and EventParam is 1
Drive motor down
Start AttackTimer
Set NextStateTRex to LoweringTRex
EndIf
CurrentState is LoweringTRex:
If ThisEvent is ES_TIMEOUT and EventParam is AttackTimer
Stop motor
Set NextStateTRex to LoweringTRex
EndIf
If ThisEvent is ATTACK and EventParam is 2
Drive motor down
Start AttackTimer
Set NextStateTRex to LoweringTRex
EndIf
If ThisEvent is VICTORY
Drive motor down
Set NextStateTRex to ResetFullyTRex
EndIf
If ThisEvent is EARLY_RESET
Stop motor
Drive motor down
Set NextStateTRex to ResetFullyTRex
Set ResetEarly to 1
EndIf
If ThisEvent is LIMIT_DOWN
Stop motor
Set NextStateTRex to WaitingForHeadphonesTRex
EndIf
EndBlock
CurrentState is ResetFullyTRex
If ThisEvent is LIMIT_DOWN
Stop motor
Set NextStateTRex to WaitingForHeadphonesTRex
EndIf
EndBlock
CurrentState is WaitingForHeadphonesTRex
If ThisEvent is HEADPHONES_RESET or ResetEarly is 1
Set NextStateTRex to WaitingforPlacementTRex
Set ResetEarly to 0
EndIf
EndBlock
Set CurrentStateTRex to NextStateTRex
END of RunTRex
Private Functions
CheckLimitDown
Takes no parameters, returns True if an event was posted, event checker for TRex
hitting bottom limit switch
Local variables: ReturnVal, CurrentInput_LimitDown
Read current input of the limit switch state (HI for TRex reached to limit, LO for
not yet reached to limit
If the state has changed from LO to HI
PostEvent LIMIT_DOWN to this service
Set ReturnVal to true
EndIf
Set LastInput_LimitDown to CurrentInput_LimitDown
Return ReturnVal
CheckLimitUp
Takes no parameters, returns True if an event was posted, event checker for TRex
hitting top limit switch
Local variables: ReturnVal, CurrentInput_LimitUp
Read current input of the limit switch state (HI for car reached limit, LO for not
yet reached
If the state has changed from LO to HI
PostEvent LIMIT_UP to this service
Set ReturnVal to true
EndIf
Set LastInput_LimitUp to CurrentInput_LimitUp
Return ReturnVal
TRex_Init
Takes no parameters, returns nothing, initializes hardware
TRex_Up
Takes no parameters, returns nothing, drives TRex up
Local Variables: dutyCycle_PWM0, dutyCycle_PWM1, channel_PWM0, channel_PWM1,
reqFreq, group
TRex_Down_Attack
Takes no parameters, returns nothing, drives TRex down
Local Variables: dutyCycle_PWM0, dutyCycle_PWM1, channel_PWM0, channel_PWM1,
reqFreq, group
TRex_Stop
Takes no parameters, returns nothing, stops TRex
Local Variables: dutyCycle_PWM0, dutyCycle_PWM1, channel_PWM0, channel_PWM1,
reqFreq, group