Gameplay Pseudocode
Gameplay State Machine Function: SetColor Read team color from switch Set MyHoop and which dispensers are near it based on team color Function: SetHalfCourt Read half court status from switch Set Half Court indicator variable based on switch status Function: SetCurrentDispenser Choose a new target dispenser For i=1…4 If we own CurrentDispenser + i (mod 4) Set CurrentDispenser to this dispenser Update beacon and near hoop variables Quit For loop Function: StartPlaySM If Full Court mode: Set current state to DeterminingField to determine our dispensers If Half Court mode: Set current state to ToDispenser Automatically determine our dispensers based on team color Function: RunPlaySM State DeterminingField: Run during function for DeterminingField Event: New OC Response If the response was to a query of a ball dispenser If the response was a number of balls available If the number of balls available > 0 Mark that we own the dispenser otherwise number of balls available == 0 Mark that we don't own the dispenser If we own 2 dispensers Transition to ToDispenser State otherwise we don't own enough dispensers Start querying next dispenser otherwise the query failed Retry query Event: OC counter expired if a query of a dispenser failed, retry query Event: DeterminingField state times out Start querying next dispenser without waiting for response State ToDispenser: Run during function for ToDispenser Event: Arrived at Dispenser Transition to AtDispenser state Event: ToDispenser state times out Run SetCurrentDispenser to choose a new dispenser Reenter ToDispenser state State AtDispenser Run during function for AtDispenser Event: Dispenser empty, or AtDispenser state times out Run SetCurrentDispenser to choose a new dispenser Reenter AtDispenser state Function: DuringDetermining Entry: Clear ownership of any dispensers Start querying first dispenser Exit: Choose a dispenser to travel to first Function: DuringToDispenser Entry: Start ToDispenser State Machine Exit/Else: Run ToDispenser State Machine Function DuringAtDispenser Entry: Start RequestBall State Machine If we are near our hoop Start Shoot State Machine Exit/Else Run RequestBall State Machine If we are near our hoop Run Shoot State Machine To Dispenser State Machine Function: RunToDispenserSM State Aligning: Run during function for Aligning Event: Beacon found Clear tape cache Transition to Tracking state State Tracking: Run during function for Tracking Event: Front tape sensor crossing Clear tape cache Increment count of number of tape crossings If number of tape crossings > threshold slow down aim turret forward Event: Bump sensor hit Transition to Reversing state State Reversing: Run during function for Reversing Event: State timer expired Transition to BlindTurn state State BlindTurn: Run during function for BlindTurn Event: state timer expired Transition to Orienting state State Orienting: Run during function for Orienting Event: hoop beacon found Transition to LineFinding state State LineFinding: Run during function for LineFinding Event: rear tape sensor crossing Stop driving Set flag to indicate we are at dispenser Event: state timer expired try to find dispenser again: Transition back to aligning state Function: DuringAligning Entry: Drive command -- Align to beacon of target dispenser Function DuringTracking Entry: Drive command -- Track beacon of target dispenser Function: DuringReversing Entry: Drive command -- very brief timed drive in reverse Function: DuringBlindTurn Entry: Drive command -- brief timed turn, about 90 degrees Function: DuringOrienting Entry: Drive command -- Align to beacon of nearest hoop Function: DuringLineFinding Entry: Clear back tape cache Drive command -- timed drive straight ahead to look for tape Request Ball State Machine Function: RunRequestBallSM State Querying: Run during function for Querying Event: New OC response If the response was to a dispenser query If the response was a number of balls available If the number of balls available > 0 Transition to WaitingForBall state If the number of balls available == 0 Transition to MightBeEmpty state If the response was not as expected Retry command Event: OC counter expired If the command was a dispenser query Retry command Event: state timeout Transition to WaitingForBall state anyways State WaitingForBall: Run during function for WaitingForBall Event: New OC response If the response was to a ball request Transition to querying the dispenser Event: OC counter expired If the command was a ball request Transition to querying the dispenser Event: state timeout Transition to querying the dispenser State MightBeEmpty: Run during function for MightBeEmpty Event: New OC response If the response was to a dispenser query If the response was a number of balls available If the number of balls available > 0 Transition to WaitingForBall state Event: state timeout Transition to Empty state State Empty: Run during function for MightBeEmpty Event: New OC response If the response was to a dispenser query If the response was a number of balls available If the number of balls available > 0 Transition to WaitingForBall state Function: DuringQuerying Entry: Query dispenser for balls available Function: DuringWaiting Entry: Request ball from dispenser Turn on all LEDs to indicate we are making ball request Exit: Turn off LED that is not our color Function: DuringMightBeEmpty Entry: Query dispenser for balls available Else: If Empty_Query_Interval timer has expired Query dispenser for balls available Function: DuringEmpty Entry: Query dispenser for balls available Else: If Empty_Query_Interval timer has expired Query dispenser for balls available Shoot State Machine Function: RunShootSM State Aiming: Run during function for Aiming Event: Aimed at hoop Transition to Waiting State Waiting: Run during function for Waiting Event: Ball at gate Transition to Releasing State Releasing: Run during function for Releasing Event: State timer expired Transition to Firing State Firing: Run during function for Firing Event: State timer expired Transition to Resetting State Resetting: Run during function for Resetting Event: State timer expired Transition to Waiting Function: DuringAiming Entry: Request hoop to be set to 3pt size Drive Command -- Aim at our hoop Function: DuringWaiting Entry: Reset shooter servo Function: DuringReleasing Entry: Release ball with gate servo Set shooter servo to ready position Function: DuringFiring Entry: Set shooter servo to fire position Reset gate servo Function: DuringResetting Entry: Reset shooter servo