Sie sind auf Seite 1von 5

TRex.

c
\\ Implements SM that controls the TRex via events from other SMs and the limit
switches

Module level variables: MyPriority, CurrentStateTRex, LastInput_LimitUp,


LastInput_LimitDown, ResetEarly

Module defines: PWM_DUTY_CYCLE, PWM_DUTY_CYCLE_ATTACK, PWM_FREQ, TREX_DOWN_TIME

Module Functions: TRex_Init, TRex_Up, TRex_Down, TRex_Down_Attack, TRex_Stop

******************************************************************************
*******************************
InitTRex
Takes a priority number, returns True

Sets the priority of the service


Initialize the hardware (TRex motor PWM channels, limit switches)
Sample the port line and use it to initialize the LastInput variables
Post an ES_Init event to this state machine
Set the CurrentStateTRex to InitializingTRex
Output an error if unsuccessful
END of InitTRex

PostTRex
Takes Event, returns True

Posts an event to TRex service


END of PostTRex

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

Switch based on CurrentStateTRex


CurrentState is InitializingTRex:
If ThisEvent is ES_INIT
Set NextStateTRex to WaitingforPlacementTRex
EndIf
EndBlock

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

If ThisEvent.EventType is ES_TIMEOUT and EventParam is MainTimer


Stop motor
Drive motor down
Set NextStateTRex to ResetFullyTRex
EndIf
EndBlock

CurrentState is LimitHitEarlyTRex
If ThisEvent is ATTACK and EventParam is 1
Drive motor down
Start AttackTimer
Set NextStateTRex to LoweringTRex
EndIf

If ThisEvent is ES_TIMEOUT and EventParam is MainTimer


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
EndBlock

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 ES_TIMEOUT and EventParam is MainTimer


Stop motor
Drive motor down
Set NextStateTRex to ResetFullyTRex
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

If ThisEvent is HEADPHONES_RESET or EARLY_RESET


Set ResetEarly to 1
Set NextStateTRex to ResetFullyTRex
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

Initializes the TRex hardware (motors and limit switches)

TRex_Up
Takes no parameters, returns nothing, drives TRex up
Local Variables: dutyCycle_PWM0, dutyCycle_PWM1, channel_PWM0, channel_PWM1,
reqFreq, group

Set duty cycle of the motor to drive the T-Rex up


Set frequency of the motor
TRex_Down
Takes no parameters, returns nothing, drives TRex down
Local Variables: dutyCycle_PWM0, dutyCycle_PWM1, channel_PWM0, channel_PWM1,
reqFreq, group

Set duty cycle of the motor to drive the T-Rex down


Set frequency of the motor

TRex_Down_Attack
Takes no parameters, returns nothing, drives TRex down
Local Variables: dutyCycle_PWM0, dutyCycle_PWM1, channel_PWM0, channel_PWM1,
reqFreq, group

Set duty cycle of the motor to drive the T-Rex down


Set frequency of the motor

TRex_Stop
Takes no parameters, returns nothing, stops TRex
Local Variables: dutyCycle_PWM0, dutyCycle_PWM1, channel_PWM0, channel_PWM1,
reqFreq, group

Set duty cycle of the motor to stop the T-Rex


Set frequency of the motor