/**************************************************************************** Module HSMTemplate.c Revision 2.0.1 Description This is a template file for implementing state machines. Notes History When Who What/Why -------------- --- -------- 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 header files for this state machine as well as any machines at the next lower level in the hierarchy that are sub-machines to this machine */ #include "TopHSM.h" #include "ReloadHSM.h" #include "MotorService.h" #include "BallRequestService.h" #include "BallShootService.h" #include #include "VariableParameters.h" /*----------------------------- Module Defines ----------------------------*/ // define constants for the states for this machine // and any other local defines /*---------------------------- 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 */ /*---------------------------- Module Variables ---------------------------*/ // everybody needs a state variable, you may need others as well static ReloadState_t CurrentState; /*------------------------------ Module Code ------------------------------*/ /**************************************************************************** Function RunTemplateSM Parameters ES_Event: the event to process Returns ES_Event: an event to return Description add your description here Notes uses nested switch/case to implement the machine. Author J. Edward Carryer, 2/11/05, 10:45AM ****************************************************************************/ ES_Event RunReloadHSM( ES_Event CurrentEvent ) { ES_Event ReturnEvent = CurrentEvent; // assume we are not consuming event ES_Event PostEvent; PostEvent.EventType = ES_MOTOR; if(CurrentEvent.EventType == ES_CHANGE_STATUS) { ReturnEvent.EventType = ES_STAGE_OVER; CurrentState = FINISHEDRELOAD; } switch ( CurrentState ) { case WAITINGRELOAD: if(CurrentEvent.EventType == ES_ENTRY) { PostEvent.EventParam = 0x07; //CounterClockwise 90 degree turn PostMotorService(PostEvent); CurrentState = BACKOFFWALL; printf("Back OFF Wall\n"); } break; case BACKOFFWALL: if(CurrentEvent.EventType == ES_MOTOR_RESPONSE) { //Turn Motor 90 Degrees PostEvent.EventParam = 0x01; //CounterClockwise 90 degree turn PostMotorService(PostEvent); CurrentState = TURN90; printf("Turn 90\n"); } break; case TURN90 : if(CurrentEvent.EventType == ES_MOTOR_RESPONSE) { CurrentState = MOVE2TAPE; //Send MotorsForward : PostEvent.EventParam = 0x03; PostMotorService(PostEvent); printf("Drive To Tape\n"); } break; case MOVE2TAPE: if( (CurrentEvent.EventType == ES_CENTRAL_TAPE) && (CurrentEvent.EventParam >= blackMin) ) { CurrentState = ROTATE2TAPE; //RotateMotor2IR : Motor's stop when Other Tape Sensor Detected PostEvent.EventParam = 0x0B; //CounterClockwise PostMotorService(PostEvent); printf("Tape Found - Rotate to Loading Station\n"); } break; case ROTATE2TAPE: if((CurrentEvent.EventType == ES_LATERAL_TAPE) && (CurrentEvent.EventParam >= blackMin)) { printf("Lateral Tape Was Found"); CurrentState = MOVE2WALL; PostEvent.EventParam = 0x05; //ForwardShort PostMotorService(PostEvent); } break; case MOVE2WALL: if(CurrentEvent.EventType == ES_MOTOR_RESPONSE) { printf("Move2Wall"); CurrentState = REQUESTBALL; //Activate Ball Requesting Module PostEvent.EventType = ES_REQUEST_BALL; PostBallRequestService(PostEvent); } break; case REQUESTBALL: if(CurrentEvent.EventType == ES_FINISHED_RELOAD) { CurrentState = REVERSE; //REVERSE TO GET OFF WALL. PostEvent.EventParam = 0x07; //backward short PostMotorService(PostEvent); } break; case REVERSE: if(CurrentEvent.EventType == ES_MOTOR_RESPONSE) { CurrentState = TURN90BACK; //Move2Wall : Motor's stop when button compressed. PostEvent.EventParam = 0x02;//Rotate Clockwise PostMotorService(PostEvent); } break; case TURN90BACK: if(CurrentEvent.EventType == ES_MOTOR_RESPONSE) { CurrentState = GO2GOAL; //Move2Wall : Motor's stop when button compressed. PostEvent.EventParam = 0x08;//Forward Short PostMotorService(PostEvent); } break; case GO2GOAL: if(CurrentEvent.EventType == ES_MOTOR_RESPONSE) { CurrentState = TURN2COURSE; //Move2Wall : Motor's stop when button compressed. PostEvent.EventParam = 0x01; //Rotate CounterClockwise PostMotorService(PostEvent); } break; case TURN2COURSE: if(CurrentEvent.EventType == ES_MOTOR_RESPONSE) { CurrentState = BACK2WALL; PostEvent.EventParam = 0x05; //Rotate CounterClockwise PostMotorService(PostEvent); } break; case BACK2WALL: if(CurrentEvent.EventType == ES_MOTOR_RESPONSE) { CurrentState = FINISHEDRELOAD; PostEvent.EventType = ES_START_SHOOTER; PostBallShootService(PostEvent); //Move2Wall : Motor's stop when button compressed. } break; } return(ReturnEvent); } /**************************************************************************** Function StartTemplateSM Parameters None Returns None Description Does any required initialization for this state machine Notes Author J. Edward Carryer, 2/18/99, 10:38AM ****************************************************************************/ void StartReloadHSM ( ES_Event CurrentEvent ) { // local variable to get debugger to display the value of CurrentEvent ES_Event LocalEvent = 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 ( ES_ENTRY_HISTORY != CurrentEvent.EventType ) { CurrentState = WAITINGRELOAD; } printf("StartReload\n"); // call the entry function (if any) for the ENTRY_STATE RunReloadHSM(CurrentEvent); } /**************************************************************************** Function QueryTemplateSM Parameters None Returns TemplateState_t The current state of the Template state machine Description returns the current state of the Template state machine Notes Author J. Edward Carryer, 2/11/05, 10:38AM ****************************************************************************/ ReloadState_t QueryReloadHSM ( void ) { return(CurrentState); } /*************************************************************************** private functions ***************************************************************************/