/**************************************************************************** Module Gameplay_GameTasks_DumpRecyclingSM.c Revision 2.0.1 Description This is a sub state machine (3rd level) called DumpRecycling within the GameTasks level within the Gameplay module for ME 218B project for Team 3. Implements a hierarchical state machine under the Gen2 Events and Services Framework. Notes Private Definitions ENTRY_STATE ReleaseRecyclingTime SquaringUpTime ReAlignTime BackingUpTime AngleAdjustmentTime RecyclingLineUpTime CurrentState LastLookedFor ES_Event_t RunDumpRecyclingSM(ES_Event_t CurrentEvent) ---------------------- // default to normal entry to new state // based on our current state: // if current state is R_AligningWithNorth // execute During function for R_AligningWithNorth state // process any events // based on the event type: // if we detected the correct beacon in the rear detector // check that it's the correct beacon (rear) // set LastLookedFor to NORTH // drive towards the north landfill // post MOTOR_COMMAND with EventParam BACKWARDS // we want to move to R_DrivingTowardsLandfill state // mark that we are taking a transition // we do not want to enter R_DrivingTowardsLandfill with // history, so will leave the default to ES_ENTRY (no change) // consume this event for the upper state machines // if the recycling center changed // we do not want to change states // consume this event for the upper state machines // if current state is R_AligningWithSouth // Execute During function for AligningWithSouth // process any events // based on the event type: // if we detected the correct beacon in the rear detector { // check that it's the correct beacon (rear) // set LastLookedFor to SOUTH // drive towards the south landfill // post MOTOR_COMMAND with EventParam BACKWARDS // we want to move to R_DrivingTowardsLandfill state // mark that we are taking a transition // we do not want to enter R_DrivingTowardsLandfill with // history, so will leave the default to ES_ENTRY (no change) // consume this event for the upper state machines // if the recycling center changed // we do not want to change states // consume this event for the upper state machines // if current state is R_DrivingTowardsLandfill // Execute During function for DrivingTowardsLandfill // process any events // based on the event type: // if we ran over the tape // start turning clockwise // look for the latest recycling center frequency // enable interrupts for rear beacon sensor // we want to move to R_AligningWithRecycling state // mark that we are taking a transition // consume this event for the upper state machines // if we hit the wall in rear left corner // back away from the wall (will be forward since we approach // beacons by driving backwards) // start a timer for us to back away from the wall // our next state should be R_BackingUp // mark that we are taking a transition // consume this event for the upper state machines // if we hit the wall in rear right corner // back away from the wall (will be forward since we approach // beacons by driving backwards) // start a timer for us to back away from the wall // our next state should be R_BackingUp // mark that we are taking a transition // consume this event for the upper state machines // if the recycling center changed // we do not want to change states // consume this event for the upper state machines // if current state is R_BackingUp // Execute During function for R_BackingUp // process any events // based on the event type: // if our backing up timer ran out // make sure it is the right timer // start turning clockwise // set frequency to look for based on what we last went to // if north: // set new BeaconRearFreq to south landfill // we want to move to R_AligningWithSouth state // if south: // set new BeaconRearFreq to north landfill // we want to move to R_AligningWithNorth state // enable interrupts for rear beacon sensor // mark that we are taking a transition // consume this event for the upper state machines // if the recycling center changed // we do not want to change states // consume this event for the upper state machines // if current state is R_AligningWithRecycling // Execute During function for R_AligningWithRecycling // process any events // based on the event type: // if we detected the correct beacon in the rear detector // check that it's the correct beacon detector (rear) // we want to turn a litle bit more since we are able to see // the beacon within a wide angle. so start an angle // adjustment timer // make sure the motors are still turning clockwise // we do not want to change states // consume this event for the upper state machines // if our aligned with beacon timer timed out // check that it is the correct timer // start a timer for us to re-align with beacon after a bit // post MOTOR_COMMAND with EventParam BACKWARDS // we want to move to R_DrivingTowardsRecycling state // mark that we are taking a transition // we do not want to enter R_DrivingTowardsRecycling with // history, so will leave the default to ES_ENTRY (no change) // consume this event for the upper state machines // if current state is R_DrivingTowardsRecycling // Execute During function for R_DrivingTowardsRecycling // process any events // based on the event type: // if we hit the wall in rear left corner // post MOTOR_COMMAND with EventParam LEFT_STOP_RIGHT_BACK // start a timer in case the other limit switch never hits // we want to move to R_SquaringWithWallCW state // mark that we are taking a transition // consume this event for the upper state machines // if we hit the wall in rear right corner // post MOTOR_COMMAND with EventParam RIGHT_STOP_LEFT_BACK // start a timer in case the other limit switch never hits // we want to move to R_SquaringWithWallCCW state // mark that we are taking a transition // consume this event for the upper state machines // if it is time for us to re-align bc we've driven a bit // check it is from the correct timer // post MOTOR_COMMAND with EventParam CW // enable interrupts for rear beacon sensor // we want to go back to AligningWithRecycling state // mark that we are taking a transition // consume this event for the upper state machines // if current state is R_SquaringWithWallCW // Execute During function for R_SquaringWithWallCW // process any events // based on the event type: // if we hit the wall in rear right corner // post MOTOR_COMMAND with EventParam FORWARD // set timer to back away from the wall // we want to move to R_BackingUp2 state // mark that we are taking a transition // consume this event for the upper state machines // if our squaring up timer timed out // check that it is the correct timer // post MOTOR_COMMAND with EventParam FORWARD // set timer to back away from the wall // we want to move to R_BackingUp2 state // mark that we are taking a transition // consume this event for the upper state machines // if current state is R_SquaringWithWallCCW // Execute During function for R_SquaringWithWallCCW // process any events // based on the event type: // if we hit the wall in rear left corner // post MOTOR_COMMAND with EventParam FORWARD // set timer to back away from the wall // we want to move to R_BackingUp2 state // mark that we are taking a transition // consume this event for the upper state machines // if our squaring up timer timed out // check that it is the correct timer // post MOTOR_COMMAND with EventParam FORWARD // set timer to back away from the wall // we want to move to R_BackingUp2 state // mark that we are taking a transition // we do not want to enter R_BackingUp2 with // history, so will leave the default to ES_ENTRY (make no // change) // consume this event for the upper state machines // if current state is R_BackingUp2 // Execute During function for R_BackingUp2 // process any events // based on the event type: // if our backing up timer ran out // make sure it is the right timer // we want to turn 90 degrees clockwise // (this is going to start a motor timer) // we want to move to R_TurningCW90 // mark that we are making a transition // consume this event for the upper state machines // if current state is R_TurningCW90 // Execute During function for R_TurningCW90 // process any events // based on the event type: // if the motor's 90-degree turning timer timed out // make sure it is the right timer // we want to drive backwards until we see the tape // we want to move to R_DrivingUntilTape // mark that we are making a transition // consume this event for the upper state machines // if current state is R_DrivingUntilTape // Execute During function for R_DrivingUntilTape // process any events // based on the event type: // if we saw the tape // we want to start a timer to keep going forward a bit // we do not want to change states // we are not making a transition // consume this event for the upper state machines // if we drove a little further to line up with tape // check that it is the right timer // we want to turn counter clockwise back towards recycling // (this will start a motor timer) // we want to move to R_TurningCCW90 // mark that we are making a transition // consume this event for the upper state machines // if current state is R_TurningCCW90 // Execute During function for R_TurningCCW90 // process any events // based on the event type: // if motor's 90-degree turning timer timed out // make sure it is the right timer // we want to drive backwards until we hit the wall // we want to move to R_DrivingUntilWall // mark that we are making a transition // consume this event for the upper state machines // if current state is R_DrivingUntilWall // Execute During function for R_DrivingUntilWall // process any events // based on the event type: // if we hit the wall (left corner) // set recycle servo to open position // set timer to allow balls to go into recycling center // we want to move to ReleasingRecycling state // mark that we are taking a transition // consume this event for the upper state machines // if we hit the wall (right corner) // set recycle servo to open position // set timer to allow balls to go into recycling center // we want to move to ReleasingRecycling state // mark that we are taking a transition // consume this event for the upper state machines // if current state is ReleasingRecycling // Execute During function for ReleasingRecycling // process any events // based on the event type: // if our timer for dispensing balls timed out // check that it's the correct timer (GameplayTimer) // set recycle servo to closed position // back away from the wall (will be forward since we approach // beacons by driving backwards) // start a backing up timer // set next state to BackingUp3 // mark that we are taking a transition // consume this event for the upper state machines // if current state is R_BackingUp3 // Execute During function for R_BackingUp3 // process any events // based on the event type: // if our backing up timer ran out // make sure it is the right timer // stop our motors // post RECYCLE_EMPTY to Gameplay // we don't need to change states // we are not making a transition // consume this event for the upper state machines // if we are making a state transition // Execute exit function for current state // Modify state variable // Execute entry function for new state void StartDumpRecyclingSM(ES_Event_t CurrentEvent) -------------------------- // to implement entry to a history state or directly to a substate // you can modify the initialization of the CurrentState variable // otherwise just start in the entry state every time the state machine // is started // call the entry function (if any) for the ENTRY_STATE DumpRecyclingState_t QueryDumpRecyclingSM(void) ----------------------------- // return CurrentState variable static ES_Event_t DuringR_AligningWithNorth(ES_Event_t Event) --------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // make sure recycling center emitter isn't blasting while we are trying // to detect the beacon // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from AligningWithNorth // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_AligningWithSouth(ES_Event_t Event) --------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into AligningWithSouth // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from AligningWithSouth // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_DrivingTowardsLandfill(ES_Event_t Event) ---------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into DrivingTowardsLandfill // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from DrivingTowardsLandfill // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_BackingUp(ES_Event_t Event) ----------------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into BackingUp // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from BackingUp // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_AligningWithRecycling(ES_Event_t Event) ----------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into AligningWithRecycling // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from AligningWithRecycling // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_DrivingTowardsRecycling(ES_Event_t Event) --------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into DrivingTowardsRecycling // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from DrivingTowardsRecycling // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_SquaringWithWallCW(ES_Event_t Event) -------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into SquaringWithWallCW // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from SquaringWithWallCW // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_SquaringWithWallCCW(ES_Event_t Event) ------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into SquaringWithWallCCW // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from SquaringWithWallCCW // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_BackingUp2(ES_Event_t Event) ---------------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into BackingUp2 // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from BackingUp2 // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_TurningCW90(ES_Event_t Event) --------------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into TurningCW90 // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from TurningCW90 // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_DrivingUntilTape(ES_Event_t Event) ---------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into DrivingUntilTape // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from DrivingUntilTape // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_TurningCCW90(ES_Event_t Event) -------------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into TurningCCW90 // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from TurningCCW90 // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_DrivingUntilWall(ES_Event_t Event) ---------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into DrivingUntilWall // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from DrivingUntilWall // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringReleasingRecycling(ES_Event_t Event) ---------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into ReleasingRecycling // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from ReleasingRecycling // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. static ES_Event_t DuringR_BackingUp3(ES_Event_t Event) ---------------------- // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events // no tasks to do for entry into BackingUp3 // no lower level state machine to start // process ES_EXIT event // no lower level state machine to pass the exit to // no tasks to do for exit from BackingUp3 // process other events // this state doesn't have a lower state machine to run // return either Event, if you don't want to allow the lower level machine // to remap the current event, or ReturnEvent if you do want to allow it. /***************************************************************************/