/**************************************************************************** Module Round2DriveSM.c Revision 2.0.1 Description This is a template file for implementing state machines. Notes ****************************************************************************/ /*----------------------------- 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 "Round2DriveSM.h" #include "SimpleDriveToASM.h" #include "LauncherControl.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 */ static ES_Event DuringShooting( ES_Event Event); static ES_Event DuringRound2Driving( ES_Event Event); /*---------------------------- Module Variables ---------------------------*/ // everybody needs a state variable, you may need others as well static Round2DriveSMState_t CurrentState; /*------------------------------ Module Code ------------------------------*/ /**************************************************************************** Function RunRound2DriveSM 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 RunRound2DriveSM( ES_Event CurrentEvent ) { unsigned char MakeTransition = false;/* are we making a state transition? */ Round2DriveSMState_t NextState = CurrentState; ES_Event EntryEventKind = { ES_ENTRY, 0 };// default to normal entry to new state ES_Event ReturnEvent = CurrentEvent; // assume we are not consuming event switch ( CurrentState ) { case ST_Shooting : // If current state is state one //printf("In Shooting \n\r"); // Execute During function for state one. ES_ENTRY & ES_EXIT are // processed here allow the lowere level state machines to re-map // or consume the event CurrentEvent = DuringShooting(CurrentEvent); //process any events if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active { switch (CurrentEvent.EventType) { case ES_TIMEOUT: //If event is event one if(CurrentEvent.EventParam == SHOOTING_TIMER){ printf("Shooting Timeout \n\r"); // Execute action function for state one : event one NextState = ST_Round2Driving ;//Decide what the next state will be // for internal transitions, skip changing MakeTransition MakeTransition = true; //mark that we are taking a transition // level state machine ReturnEvent = CurrentEvent; } break; // repeat cases as required for relevant events } } break; case ST_Round2Driving : // If current state is state one //printf("In State: Round2Driving \n\r"); // Execute During function for state one. ES_ENTRY & ES_EXIT are // processed here allow the lowere level state machines to re-map // or consume the event CurrentEvent = DuringRound2Driving (CurrentEvent); //process any events if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active { // No Events Processed at this level } break; // repeat state pattern as required for other states }// end switch CurrentState // If we are making a state transition if (MakeTransition == true) { // Execute exit function for current state CurrentEvent.EventType = ES_EXIT; RunRound2DriveSM(CurrentEvent); CurrentState = NextState; //Modify state variable // Execute entry function for new state // this defaults to ES_ENTRY RunRound2DriveSM(EntryEventKind); } return(ReturnEvent); } /**************************************************************************** Function StartRound2DriveSM Parameters None Returns None Description Does any required initialization for this state machine Notes Author J. Edward Carryer, 2/18/99, 10:38AM ****************************************************************************/ void StartRound2DriveSM ( 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 = ST_Shooting; } // call the entry function (if any) for the ENTRY_STATE RunRound2DriveSM(CurrentEvent); printf("Start Round 2 Drive \n\r"); } /**************************************************************************** Function QueryRound2DriveSM 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 ****************************************************************************/ Round2DriveSMState_t QueryRound2DriveSM ( void ) { return(CurrentState); } /*************************************************************************** private functions ***************************************************************************/ static ES_Event DuringShooting( ES_Event Event) { ES_Event ReturnEvent = Event; // assmes no re-mapping or comsumption // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events if ( (Event.EventType == ES_ENTRY) || (Event.EventType == ES_ENTRY_HISTORY) ) { ES_Timer_InitTimer(SHOOTING_TIMER, SHOOTING_INTERVAL); //Launch balls!! TurnOnLauncher(); } else if ( Event.EventType == ES_EXIT ) { // Turn off the Launcher and close the gate TurnOffLauncher(); }else // do the 'during' function for this state { } // 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); } static ES_Event DuringRound2Driving( ES_Event Event) { ES_Event ReturnEvent = Event; // assmes no re-mapping or comsumption // process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events if ( (Event.EventType == ES_ENTRY) || (Event.EventType == ES_ENTRY_HISTORY) ) { StartSimpleDriveToASM(Event); printf("Start Driving \n\r"); } else if ( Event.EventType == ES_EXIT ) { // on exit, give the lower levels a chance to clean up first ReturnEvent = RunSimpleDriveToASM(Event); // repeat for any concurrently running state machines // now do any local exit functionality }else // do the 'during' function for this state { // run any lower level state machine ReturnEvent = RunSimpleDriveToASM(Event); // repeat for any concurrent lower level machines // do any activity that is repeated as long as we are in this state } // 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); }