/****************************************************************************
 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


 History
 When           Who     What/Why
 -------------- ---     --------
 02/23/19       bibit   converted from template to project file
 02/27/17 09:48 jec     another correction to re-assign both CurrentEvent
                        and ReturnEvent to the result of the During function
                        this eliminates the need for the prior fix and allows
                        the during function to-remap an event that will be
                        processed at a higher level.
 02/20/17 10:14 jec     correction to Run function to correctly assign
                        ReturnEvent in the situation where a lower level
                        machine consumed an event.
 02/03/16 12:38 jec     updated comments to reflect changes made in '14 & '15
                        converted unsigned char to bool where appropriate
                        spelling changes on true (was True) to match standard
                        removed local var used for debugger visibility in 'C32
                        commented out references to Start & RunLowerLevelSM so
                        that this can compile.
 02/07/13 21:00 jec     corrections to return variable (should have been
                        ReturnEvent, not CurrentEvent) and several EV_xxx
                        event names that were left over from the old version
 02/08/12 09:56 jec     revisions for the Events and Services Framework Gen2
 02/13/10 14:29 jec     revised Start and run to add new kind of entry function
                        to make implementing history entry cleaner
 02/13/10 12:29 jec     added NewEvent local variable to During function and
                        comments about using either it or Event as the return
 02/11/10 15:54 jec     more revised comments, removing last comment in during
                        function that belongs in the run function
 02/09/10 17:21 jec     updated comments about internal transitions on During funtion
 02/18/09 10:14 jec     removed redundant call to RunLowerlevelSM in EV_Entry
                        processing in During function
 02/20/07 21:37 jec     converted to use enumerated type for events & states
 02/13/05 19:38 jec     added support for self-transitions, reworked
                        to eliminate repeated transition code
 02/11/05 16:54 jec     converted to implment hierarchy explicitly
 02/25/03 10:32 jec     converted to take a passed event parameter
 02/18/99 10:19 jec     built template from MasterMachine.c
 02/14/99 10:34 jec     Began Coding
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
// Basic includes for a program using the Events and Services Framework
#include "ES_Configure.h"
#include "ES_Framework.h"

// include this module's own header file
#include "Gameplay_GameTasks_DumpRecyclingSM.h"

// include the highest level header file for this HSM
#include "GameplayHSM.h"

// include necessary connections for test harness
#include "GP_MapKeys.h"

// other project modules
#include "BeaconSense.h"
#include "MotorSM.h"
#include "ServoModule.h"
#include "TapeService.h"
#include "PWM_Module.h"
#include "Gameplay_GameTasksSM.h"
#include "GameControlService.h"
#include "CompassService.h"
#include "GameplayHSM.h"

/*----------------------------- Module Defines ----------------------------*/
// define constants for the states for this machine
// and any other local defines

#define ENTRY_STATE R_AligningWithNorth

#define ReleaseRecyclingTime       4000
#define SquaringUpTime             2000
#define ReAlignTime                2500
#define BackingUpTime              1000
#define AngleAdjustmentTime          50
#define RecyclingLineUpTime         500

/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine, things like during
   functions, entry & exit functions. They should be functions relevant to
   the behavior of this state machine
*/
static ES_Event_t DuringR_AligningWithNorth(ES_Event_t Event);
static ES_Event_t DuringR_AligningWithSouth(ES_Event_t Event);
static ES_Event_t DuringR_DrivingTowardsLandfill(ES_Event_t Event);
static ES_Event_t DuringR_BackingUp(ES_Event_t Event);
static ES_Event_t DuringR_AligningWithRecycling(ES_Event_t Event);
static ES_Event_t DuringR_DrivingTowardsRecycling(ES_Event_t Event);
static ES_Event_t DuringR_SquaringWithWallCW(ES_Event_t Event);
static ES_Event_t DuringR_SquaringWithWallCCW(ES_Event_t Event);
static ES_Event_t DuringR_BackingUp2(ES_Event_t Event);
static ES_Event_t DuringR_TurningCW90(ES_Event_t Event);
static ES_Event_t DuringR_DrivingUntilTape(ES_Event_t Event);
static ES_Event_t DuringR_TurningCCW90(ES_Event_t Event);
static ES_Event_t DuringR_DrivingUntilWall(ES_Event_t Event);
static ES_Event_t DuringReleasingRecycling(ES_Event_t Event);
static ES_Event_t DuringR_BackingUp3(ES_Event_t Event);

/*---------------------------- Module Variables ---------------------------*/
// everybody needs a state variable, you may need others as well
static DumpRecyclingState_t CurrentState;

bool LastLookedFor = NORTH; // NORTH and SOUTH are 0 and 1, so can use bool

/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
 Function
    RunDumpRecyclingSM

 Parameters
   ES_Event_t: the event to process

 Returns
   ES_Event_t: an event to return

 Description
   run function for the DumpRecycling state machine

 Notes
   uses nested switch/case to implement the machine.

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
ES_Event_t RunDumpRecyclingSM(ES_Event_t CurrentEvent)
{
  bool                  MakeTransition = false; // making a state transition?
  DumpRecyclingState_t  NextState = CurrentState;

  // default to normal entry to new state
  ES_Event_t  EntryEventKind = { ES_ENTRY, 0 };
  ES_Event_t  ReturnEvent = CurrentEvent; // assume we don't consume event

  switch (CurrentState)
  {
    case R_AligningWithNorth:   // current state is R_AligningWithNorth
    {
      // Execute During function for AligningWithNorth. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringR_AligningWithNorth(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT) // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case BEACON_DETECTED: // if we detected the correct beacon in the
                                // rear detector
          {
            // check that it's the correct beacon (rear):
            if (CurrentEvent.EventParam == REAR)
            {
              // set LastLookedFor to NORTH
              LastLookedFor = NORTH;

              // drive towards the north landfill
              // post MOTOR_COMMAND with EventParam BACKWARDS
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = BACKWARDS;
              PostMotorSM(PostEvent);

              // we want to move to R_DrivingTowardsLandfill state
              NextState = R_DrivingTowardsLandfill;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter R_DrivingTowardsLandfill with
              // history, so will leave the default to ES_ENTRY (no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;

          case RECYCLING_CHANGE: // if the recycling center changed
          {
            // we do not want to change states

            // we are not making an internal transition (for internal
            // transitions, skip changing MakeTransition)
            //MakeTransition = true;

            // we do not want to enter the other states with history, so will
            // leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for AligningWithNorth

    case R_AligningWithSouth:   // current state is R_AligningWithSouth
    {
      // Execute During function for AligningWithSouth. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringR_AligningWithSouth(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case BEACON_DETECTED: // if we detected the correct beacon in the
                                // rear detector
          {
            // check that it's the correct beacon (rear):
            if (CurrentEvent.EventParam == REAR)
            {
              // set LastLookedFor to SOUTH
              LastLookedFor = SOUTH;

              // drive towards the north landfill
              // post MOTOR_COMMAND with EventParam BACKWARDS
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = BACKWARDS;
              PostMotorSM(PostEvent);

              // we want to move to R_DrivingTowardsLandfill state
              NextState = R_DrivingTowardsLandfill;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter R_DrivingTowardsLandfill with
              // history, so will leave the default to ES_ENTRY (no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;

          case RECYCLING_CHANGE: // if the recycling center changed
          {
            // we do not want to change states

            // we are not making an internal transition (for internal
            // transitions, skip changing MakeTransition)
            //MakeTransition = true;

            // we do not want to enter the other states with history, so will
            // leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for AligningWithSouth

    case R_DrivingTowardsLandfill:  // currently R_DrivingTowardsLandfill
    {
      // Execute During function for DrivingTowardsLandfill. ES_ENTRY &
      // ES_EXIT are processed here to allow the lower level state machines to
      // re-map or consume the event
      ReturnEvent = CurrentEvent = DuringR_DrivingTowardsLandfill(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case TAPE_DETECTED: // if we ran over the tape
          {
            // start turning clockwise
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = CW;
            PostMotorSM(PostEvent);

            // look for the latest recycling center frequency
            SetBeaconRearFreq(GetOurRecycleBeaconFreq());

            // enable interrupts for rear beacon sensor
            EnableRearBeaconInterrupts();

            // we want to move to R_AligningWithRecycling state
            NextState = R_AligningWithRecycling;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter the other states with history, so will
            // leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case REAR_LIMIT_HIT_L: // if we hit the wall in rear left corner
          {
            // back away from the wall (will be forward since we approach
            // beacons by driving backwards)
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = FORWARD;
            PostMotorSM(PostEvent);

            // start a timer for us to back away from the wall
            ES_Timer_InitTimer(GameplayTimer, BackingUpTime);

            // our next state should be R_BackingUp
            NextState = R_BackingUp;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter the other states with history, so will
            // leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case REAR_LIMIT_HIT_R: // if we hit the wall in rear right corner
          {
            // back away from the wall (will be forward since we approach
            // beacons by driving backwards)
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = FORWARD;
            PostMotorSM(PostEvent);

            // start a timer for us to back away from the wall
            ES_Timer_InitTimer(GameplayTimer, BackingUpTime);

            // our next state should be R_BackingUp
            NextState = R_BackingUp;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter the other states with history, so will
            // leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case RECYCLING_CHANGE: // if the recycling center changed
          {
            // we do not want to change states because we don't care in'
            // this state

            // we are not making an internal transition (for internal
            // transitions, skip changing MakeTransition)
            //MakeTransition = true;

            // we do not want to enter the other states with history, so will
            // leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for DrivingTowardsLandfill

    case R_BackingUp:  // currently R_BackingUp
    {
      // Execute During function for BackingUp. ES_ENTRY &
      // ES_EXIT are processed here to allow the lower level state machines to
      // re-map or consume the event
      ReturnEvent = CurrentEvent = DuringR_BackingUp(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case ES_TIMEOUT: // if our backing up timer ran out
          {
            // make sure it is the right timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // start turning clockwise
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = CW;
              PostMotorSM(PostEvent);

              // set frequency to look for based on what we last went to
              if (LastLookedFor == NORTH)
              {
                // set new BeaconRearFreq to south landfill
                SetBeaconRearFreq(SouthLandfillFreq);
                // we want to move to R_AligningWithSouth state
                NextState = R_AligningWithSouth;
              }
              else
              {
                // set new BeaconRearFreq to north landfill
                SetBeaconRearFreq(NorthLandfillFreq);
                // we want to move to R_AligningWithNorth state
                NextState = R_AligningWithNorth;
              }

              // enable interrupts for rear beacon sensor
              EnableRearBeaconInterrupts();

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter the other states with history, so will
              // leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;

          case RECYCLING_CHANGE: // if the recycling center changed
          {
            // we do not want to change states

            // we are not making an internal transition (for internal
            // transitions, skip changing MakeTransition)
            //MakeTransition = true;

            // we do not want to enter the other states with history, so will
            // leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for BackingUp

    case R_AligningWithRecycling:   // current state is AligningWithRecycling
    {
      // Execute During function for AligningWithRecycling. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringR_AligningWithRecycling(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case BEACON_DETECTED: // if we detected the correct beacon in the
                                // rear detector
          {
            // check that it's the correct beacon detector (rear):
            if (CurrentEvent.EventParam == 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
              ES_Timer_InitTimer(GameplayTimer, AngleAdjustmentTime);

              // make sure the motors are still turning clockwise
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = CW;
              PostMotorSM(PostEvent);

              // we do not want to change states

              // we are not transitioning out of this state
              //MakeTransition = true;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;

          case ES_TIMEOUT:  // if our aligned with beacon timer timed out
          {
            // check that it is the correct timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // start a timer for us to re-align with beacon after a bit
              ES_Timer_InitTimer(GameplayTimer, ReAlignTime);

              // post MOTOR_COMMAND with EventParam BACKWARDS
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = BACKWARDS;
              PostMotorSM(PostEvent);

              // we want to move to R_DrivingTowardsRecycling state
              NextState = R_DrivingTowardsRecycling;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter R_DrivingTowardsRecycling with
              // history, so will leave the default to ES_ENTRY (no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for AligningWithRecycling

    case R_DrivingTowardsRecycling:  // currently R_DrivingTowardsRecycling
    {
      // Execute During function for DrivingTowardsBeacon. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringR_DrivingTowardsRecycling(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case REAR_LIMIT_HIT_L: // if we hit the wall in rear left corner
          {
            // post MOTOR_COMMAND with EventParam LEFT_STOP_RIGHT_BACK
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = LEFT_STOP_RIGHT_BACK;
            PostMotorSM(PostEvent);

            // start a timer in case the other limit switch never hits
            ES_Timer_InitTimer(GameplayTimer, SquaringUpTime);

            // we want to move to R_SquaringWithWallCW state
            NextState = R_SquaringWithWallCW;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter R_SquaringWithWallCW with history, so
            // will leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case REAR_LIMIT_HIT_R: // if we hit the wall in rear right corner
          {
            // post MOTOR_COMMAND with EventParam RIGHT_STOP_LEFT_BACK
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = RIGHT_STOP_LEFT_BACK;
            PostMotorSM(PostEvent);

            // start a timer in case the other limit switch never hits
            ES_Timer_InitTimer(GameplayTimer, SquaringUpTime);

            // we want to move to R_SquaringWithWallCCW state
            NextState = R_SquaringWithWallCCW;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter R_SquaringWithWallCCW with history, so
            // will leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          // check if it is time for us to re-align bc we've driven a bit
          case ES_TIMEOUT:
          {
            // check it is from the correct timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // post MOTOR_COMMAND with EventParam CW
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = CW;
              PostMotorSM(PostEvent);

              // enable interrupts for rear beacon sensor
              EnableRearBeaconInterrupts();

              // we want to go back to AligningWithRecycling state
              NextState = R_AligningWithRecycling;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter AligningWithRecycling with history,
              // so will leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for DrivingTowardsRecycling

    case R_SquaringWithWallCW:      // If current state is SquaringWithWallCW
    {
      // Execute During function for SquaringWithWallCW. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringR_SquaringWithWallCW(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case REAR_LIMIT_HIT_R: // if we hit the wall in rear right corner
          {
            // post MOTOR_COMMAND with EventParam FORWARD
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = FORWARD;
            PostMotorSM(PostEvent);

            // set timer to back away from the wall
            ES_Timer_InitTimer(GameplayTimer, BackingUpTime);

            // we want to move to R_BackingUp2 state
            NextState = R_BackingUp2;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter R_BackingUp2 with
            // history, so will leave the default to ES_ENTRY (make no
            // change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case ES_TIMEOUT: // if our squaring up timer timed out
          {
            // check that it is the correct timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // post MOTOR_COMMAND with EventParam FORWARD
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = FORWARD;
              PostMotorSM(PostEvent);

              // set timer to back away from the wall
              ES_Timer_InitTimer(GameplayTimer, BackingUpTime);

              // we want to move to R_BackingUp2 state
              NextState = R_BackingUp2;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter R_BackingUp2 with
              // history, so will leave the default to ES_ENTRY (make no
              // change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for SquaringWithWallCW

    case R_SquaringWithWallCCW:    // If current state is SquaringWithWallCCW
    {
      // Execute During function for SquaringWithWallCCW. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringR_SquaringWithWallCCW(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case REAR_LIMIT_HIT_L: // if we hit the wall in rear left corner
          {
            // post MOTOR_COMMAND with EventParam FORWARD
            ES_Event_t PostEvent;
            PostEvent.EventType = MOTOR_COMMAND;
            PostEvent.EventParam = FORWARD;
            PostMotorSM(PostEvent);

            // set timer to back away from the wall
            ES_Timer_InitTimer(GameplayTimer, BackingUpTime);

            // we want to move to R_BackingUp2 state
            NextState = R_BackingUp2;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter R_BackingUp2 with
            // history, so will leave the default to ES_ENTRY (make no
            // change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case ES_TIMEOUT: // if our squaring up timer timed out
          {
            // check that it is the correct timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // post MOTOR_COMMAND with EventParam FORWARD
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = FORWARD;
              PostMotorSM(PostEvent);

              // set timer to back away from the wall
              ES_Timer_InitTimer(GameplayTimer, BackingUpTime);

              // we want to move to R_BackingUp2 state
              NextState = R_BackingUp2;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter R_BackingUp2 with
              // history, so will leave the default to ES_ENTRY (make no
              // change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for SquaringWithWallCCW

    case R_BackingUp2:  // currently R_BackingUp2
    {
      // Execute During function for BackingUp2. ES_ENTRY & ES_EXIT are
      // processed here to allow the lower level state machines to re-map or
      // consume the event
      ReturnEvent = CurrentEvent = DuringR_BackingUp2(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case ES_TIMEOUT: // if our backing up timer ran out
          {
            // make sure it is the right timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // we want to turn 90 degrees clockwise
              // (this is going to start a motor timer)
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = CW90;
              PostMotorSM(PostEvent);

              // we want to move to R_TurningCW90
              NextState = R_TurningCW90;

              // mark that we are making a transition
              MakeTransition = true;

              // we do not want to enter the other states with history, so will
              // leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for BackingUp2

    case R_TurningCW90:  // currently R_TurningCW90
    {
      // Execute During function for TurningCW90. ES_ENTRY & ES_EXIT are
      // processed here to allow the lower level state machines to re-map or
      // consume the event
      ReturnEvent = CurrentEvent = DuringR_TurningCW90(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case ES_TIMEOUT: // if the motor's 90-degree turning timer timed out
          {
            // make sure it is the right timer
            if (CurrentEvent.EventParam == MotorTimer)
            {
              // we want to drive backwards until we see the tape
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = BACKWARDS;
              PostMotorSM(PostEvent);

              // we want to move to R_DrivingUntilTape
              NextState = R_DrivingUntilTape;

              // mark that we are making a transition
              MakeTransition = true;

              // we do not want to enter the other states with history, so will
              // leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for R_TurningCW90

    case R_DrivingUntilTape:  // currently DrivingUntilTape
    {
      // Execute During function for DrivingUntilTape. ES_ENTRY & ES_EXIT are
      // processed here to allow the lower level state machines to re-map or
      // consume the event
      ReturnEvent = CurrentEvent = DuringR_DrivingUntilTape(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case TAPE_DETECTED: // if we saw the tape
          {
            // we want to start a timer to keep going forward a bit
            ES_Timer_InitTimer(GameplayTimer, RecyclingLineUpTime);

            // we do not want to change states

            // we are not making a transition
            //MakeTransition = true;

            // we do not want to enter the other states with history, so will
            // leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case ES_TIMEOUT: // we drove a little further to line up with tape
          {
            // check that it is the right timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // we want to turn counter clockwise back towards recycling
              // (this will start a motor timer)
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = CCW90;
              PostMotorSM(PostEvent);

              // we want to move to R_TurningCCW90
              NextState = R_TurningCCW90;

              // mark that we are making a transition
              MakeTransition = true;

              // we do not want to enter the other states with history, so will
              // leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for DrivingUntilTape

    case R_TurningCCW90:  // currently R_TurningCCW90
    {
      // Execute During function for TurningCCW90. ES_ENTRY & ES_EXIT are
      // processed here to allow the lower level state machines to re-map or
      // consume the event
      ReturnEvent = CurrentEvent = DuringR_TurningCCW90(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case ES_TIMEOUT: // if motor's 90-degree turning timer timed out
          {
            // make sure it is the right timer
            if (CurrentEvent.EventParam == MotorTimer)
            {
              // we want to drive backwards until we see the tape
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = BACKWARDS;
              PostMotorSM(PostEvent);

              // we want to move to R_DrivingUntilWall
              NextState = R_DrivingUntilWall;

              // mark that we are making a transition
              MakeTransition = true;

              // we do not want to enter the other states with history, so will
              // leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for R_TurningCCW90

    case R_DrivingUntilWall:  // currently R_DrivingUntilWall
    {
      // Execute During function for DrivingUntilWall. ES_ENTRY & ES_EXIT are
      // processed here to allow the lower level state machines to re-map or
      // consume the event
      ReturnEvent = CurrentEvent = DuringR_DrivingUntilWall(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case REAR_LIMIT_HIT_L: // if we hit the wall (left corner)
          {
            // set recycle servo to open position
            OpenRecycleChute();

            // set timer to allow balls to go into recycling center
            ES_Timer_InitTimer(GameplayTimer, ReleaseRecyclingTime);

            // we want to move to ReleasingRecycling state
            NextState = ReleasingRecycling;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter ReleasingRecycling with history,
            // so will leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;

          case REAR_LIMIT_HIT_R: // if we hit the wall (right corner)
          {
            // set recycle servo to open position
            OpenRecycleChute();

            // set timer to allow balls to go into recycling center
            ES_Timer_InitTimer(GameplayTimer, ReleaseRecyclingTime);

            // we want to move to ReleasingRecycling state
            NextState = ReleasingRecycling;

            // mark that we are taking a transition (for internal
            // transitions, skip changing MakeTransition)
            MakeTransition = true;

            // we do not want to enter ReleasingRecycling with history,
            // so will leave the default to ES_ENTRY (make no change)
            //EntryEventKind.EventType = ES_ENTRY_HISTORY;

            // consume this event for the upper state machines
            ReturnEvent.EventType = ES_NO_EVENT;
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for R_DrivingUntilWall

    case ReleasingRecycling:    // If current state is ReleasingRecycling
    {
      // Execute During function for ReleasingRecycling. ES_ENTRY & ES_EXIT
      // are processed here to allow the lower level state machines to re-map
      // or consume the event
      ReturnEvent = CurrentEvent = DuringReleasingRecycling(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case ES_TIMEOUT:     // if our timer for dispensing balls timed out
          {
            // check that it's the correct timer (GameplayTimer):
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // set recycle servo to closed position
              CloseRecycleChute();

              // back away from the wall (will be forward since we approach
              // beacons by driving backwards)
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = FORWARD;
              PostMotorSM(PostEvent);

              // start a backing up timer
              ES_Timer_InitTimer(GameplayTimer, BackingUpTime);

              // set next state to BackingUp3
              NextState = R_BackingUp3;

              // mark that we are taking a transition (for internal
              // transitions, skip changing MakeTransition)
              MakeTransition = true;

              // we do not want to enter the other states with history, so will
              // leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for ReleasingRecycling

    case R_BackingUp3:  // currently R_BackingUp3
    {
      // Execute During function for BackingUp3. ES_ENTRY & ES_EXIT are
      // processed here to allow the lower level state machines to re-map or
      // consume the event
      ReturnEvent = CurrentEvent = DuringR_BackingUp3(CurrentEvent);

      // process any events
      if (CurrentEvent.EventType != ES_NO_EVENT)   // if an event is active
      {
        switch (CurrentEvent.EventType)
        {
          case ES_TIMEOUT: // if our backing up timer ran out
          {
            // make sure it is the right timer
            if (CurrentEvent.EventParam == GameplayTimer)
            {
              // stop our motors
              ES_Event_t PostEvent;
              PostEvent.EventType = MOTOR_COMMAND;
              PostEvent.EventParam = STOP;
              PostMotorSM(PostEvent);

              // post RECYCLE_EMPTY to Gameplay
              PostEvent.EventType = RECYCLE_EMPTY;
              PostGameplayMasterSM(PostEvent);

              // we don't need to change states

              // we are not making a transition
              //MakeTransition = true;

              // we do not want to enter the other states with history, so will
              // leave the default to ES_ENTRY (make no change)
              //EntryEventKind.EventType = ES_ENTRY_HISTORY;

              // consume this event for the upper state machines
              ReturnEvent.EventType = ES_NO_EVENT;
            }
          }
          break;
        }
      }
    }
    break;  // end switch on CurrentState case for BackingUp3
  }

  // if we are making a state transition
  if (MakeTransition == true)
  {
    // Execute exit function for current state
    CurrentEvent.EventType = ES_EXIT;
    RunDumpRecyclingSM(CurrentEvent);

    CurrentState = NextState; // Modify state variable

    // Execute entry function for new state
    // this defaults to ES_ENTRY
    RunDumpRecyclingSM(EntryEventKind);
  }
  return ReturnEvent;
}

/****************************************************************************
 Function
     StartDumpRecyclingSM

 Parameters
     None

 Returns
     None

 Description
     Does any required initialization for this state machine

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
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
  if (CurrentEvent.EventType != ES_ENTRY_HISTORY)
  {
    CurrentState = ENTRY_STATE;
  }
  // call the entry function (if any) for the ENTRY_STATE
  RunDumpRecyclingSM(CurrentEvent);
}

/****************************************************************************
 Function
     QueryDumpRecyclingSM

 Parameters
     None

 Returns
     DumpRecyclingState_t The current state of the state machine

 Description
     returns the current state of the GameTasks state machine

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
DumpRecyclingState_t QueryDumpRecyclingSM(void)
{
  return CurrentState;
}

/***************************************************************************
 private functions
 ***************************************************************************/
/****************************************************************************
 Function
     DuringR_AligningWithNorth

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the AligningWithNorth state.  Stops the recycling
     center emitter so it doesn't mess with beacon detection.  No lower state
     machine to run.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringR_AligningWithNorth(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // make sure recycling center emitter isn't blasting while we are trying
    // to detect the beacon
    uint16_t RecycleOpenFreq = GetFreqAssignment();
    PWM_SetFrequency(RecycleOpenFreq, RecycleEmitter);
    PWM_SetDuty(0, RecycleEmitter);

    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no actions to do while exiting
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_AligningWithSouth

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the AligningWithSouth state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringR_AligningWithSouth(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into AligningWithSouth
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from AligningWithSouth
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_DrivingTowardsLandfill

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the DrivingTowardsLandfill state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringR_DrivingTowardsLandfill(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into DrivingTowardsLandfill
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from DrivingTowardsLandfill
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_BackingUp

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the BackingUp state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 3/04/2019
****************************************************************************/
static ES_Event_t DuringR_BackingUp(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into BackingUp
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from BackingUp
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_AligningWithRecycling

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the AligningWithRecycling state.  Starts/stops the
     recycling center emitter to get the center door to open.  No lower state
     machine to run.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringR_AligningWithRecycling(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // make sure recycling center emitter isn't blasting while we are trying
    // to detect the beacon
    uint16_t RecycleOpenFreq = GetFreqAssignment();
    PWM_SetFrequency(RecycleOpenFreq, RecycleEmitter);
    PWM_SetDuty(0, RecycleEmitter);

    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to

    // send frequency to get recycling center to open
    uint16_t RecycleOpenFreq = GetFreqAssignment();
    PWM_SetFrequency(RecycleOpenFreq, RecycleEmitter);
    PWM_SetDuty(50, RecycleEmitter);
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_DrivingTowardsRecycling

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the DrivingTowardsRecycling state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringR_DrivingTowardsRecycling(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into DrivingTowardsRecycling
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from DrivingTowardsRecycling
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_SquaringWithWallCW

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the SquaringWithWallCW state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringR_SquaringWithWallCW(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into SquaringWithWallCW
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from SquaringWithWallCW
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_SquaringWithWallCCW

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the SquaringWithWallCCW state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringR_SquaringWithWallCCW(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into SquaringWithWallCCW
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from SquaringWithWallCCW
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_BackingUp2

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the BackingUp2 state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 3/04/2019
****************************************************************************/
static ES_Event_t DuringR_BackingUp2(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into BackingUp2
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from BackingUp2
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_TurningCW90

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the TurningCW90 state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 3/04/2019
****************************************************************************/
static ES_Event_t DuringR_TurningCW90(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into TurningCW90
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from TurningCW90
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_DrivingUntilTape

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the DrivingUntilTape state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 3/04/2019
****************************************************************************/
static ES_Event_t DuringR_DrivingUntilTape(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into DrivingUntilTape
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from DrivingUntilTape
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_TurningCCW90

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the TurningCCW90 state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 3/04/2019
****************************************************************************/
static ES_Event_t DuringR_TurningCCW90(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into TurningCCW90
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from TurningCCW90
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_DrivingUntilWall

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the DrivingUntilWall state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 3/04/2019
****************************************************************************/
static ES_Event_t DuringR_DrivingUntilWall(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into DrivingUntilWall
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from DrivingUntilWall
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringReleasingRecycling

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the ReleasingRecycling state.  Does nothing
     because nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have
     lower state machines.

 Notes

 Author
     Bibit Bianchini, 2/23/2019
****************************************************************************/
static ES_Event_t DuringReleasingRecycling(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into ReleasingRecycling
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from ReleasingRecycling
  }

  // 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.
  return ReturnEvent;
}

/****************************************************************************
 Function
     DuringR_BackingUp3

 Parameters
     ES_Event_t Event

 Returns
     ES_Event_t

 Description
     During function for the BackingUp3 state.  Does nothing because
     nothing to do for ES_ENTRY and ES_EXIT events, and doesn't have lower
     state machines.

 Notes

 Author
     Bibit Bianchini, 3/04/2019
****************************************************************************/
static ES_Event_t DuringR_BackingUp3(ES_Event_t Event)
{
  ES_Event_t ReturnEvent = Event; // assume no re-mapping or consumption

  // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
  if ((Event.EventType == ES_ENTRY) ||
      (Event.EventType == ES_ENTRY_HISTORY))
  {
    // no tasks to do for entry into BackingUp3
    // no lower level state machine to start
  }
  else if (Event.EventType == ES_EXIT)
  {
    // no lower level state machine to pass the exit to
    // no tasks to do for exit from BackingUp3
  }

  // 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.
  return ReturnEvent;
}

/*------------------------------- Footnotes -------------------------------*/
/*------------------------------ End of file ------------------------------*/