/**************************************************************************** Module TopHSMTemplate.c Revision 2.0.1 Description This is a template for the top level Hierarchical state machine Notes History When Who What/Why -------------- --- -------- 02/08/12 01:39 jec converted from MW_MasterMachine.c 02/06/12 22:02 jec converted to Gen 2 Events and Services Framework 02/13/10 11:54 jec converted During functions to return Event_t so that they match the template 02/21/07 17:04 jec converted to pass Event_t to Start...() 02/20/07 21:37 jec converted to use enumerated type for events 02/21/05 15:03 jec Began Coding ****************************************************************************/ /*----------------------------- Include Files -----------------------------*/ /* 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 */ /*----------------------------- Module Defines ----------------------------*/ /*---------------------------- Module Functions ---------------------------*/ /*---------------------------- Module Variables ---------------------------*/ // everybody needs a state variable, though if the top level state machine // is just a single state container for orthogonal regions, you could get // away without it // with the introduction of Gen2, we need a module level Priority var as well /*------------------------------ Module Code ------------------------------*/ /**************************************************************************** Function InitGamePlayHSM Parameters uint8_t : the priorty of this service Returns boolean, False if error in initialization, True otherwise Description Saves away the priority, and starts the top level state machine Notes Author J. Edward Carryer, 02/06/12, 22:06 ****************************************************************************/ boolean InitGamePlayHSM ( uint8_t Priority ) { // save our priority // Start the Master State machine } /**************************************************************************** Function PostGamePlayHSM Parameters ES_Event ThisEvent , the event to post to the queue Returns boolean False if the post operation failed, True otherwise Description Posts an event to this state machine's queue Notes Author J. Edward Carryer, 10/23/11, 19:25 ****************************************************************************/ /**************************************************************************** Function RunGamePlayHSM Parameters ES_Event: the event to process Returns ES_Event: an event to return Description the run function for the top level state machine Notes uses nested switch/case to implement the machine. Author J. Edward Carryer, 02/06/12, 22:09 ****************************************************************************/ // make recursive call warning into info { /* are we making a state transition? */ // default to normal entry to new state // Declare local variable ReturnEvent // assume we are not consuming event // assume no errors //Base response on current state { //If we are in the initial pseudo state // Execute During function for state. ES_ENTRY & ES_EXIT are // processed here //CurrentEvent = DuringInitState(CurrentEvent); //process any events //If an event is active { //if the event was an entry event //Init stuff here // Set I/O's on port P & S //Output for Red LEDs //Output for Blue LEDs //Input for Bump Sensor 1 //Input for Bump Sensor 1 //Input for Bump Sensor FR //Input for Bump Sensor FL //Input for Hopper Has Balls //Input for Hopper Is Full //Make sure all outputs are off // Set initial driving arc radius //Decide the next state will be WaitingState //mark that we are taking a transition } } //If we are in the waiting state //process any events //If an event is active { { //start event received from game monitoring { //Reset starting orientation to navigation SM //Reorient the robot so that it can scan the IR beacons behind the bot. //Post event to DrivingSM //Print an update to the screen //Make sure the back door is closed // Set next state to ScanningState //mark that we are taking a transition // consume this event } } } //If we are in the scanning state //process any events //If an event is active { { // If the event is TargetPositionReached received from navigation { //Print an to the screen //Update starting position and orientation based on scanned beacons //Start driving the a CCW loop //Print an update to the screen //Turn on the roller // Set next state to HarvestingState //mark that we are taking a transition // consume this event } //If the event was game over { //Stop the bot //Post to DrivingSM // Shut off the roller //mark that we are taking a transition //Decide what the next state will be // consume this event } } } //If we are in the harvesting state //If an event is active { { //If the event was hopperfull { //print an update to the screen //set the next state to depositing state //mark that we are taking a transition // consume this event } //if the event was time almost out { //we the hopper has balls { //print an update to the screen //Set the next state to depositing state //mark that we are taking a transition // consume this event }//otherwsie the hopper does not have balls { //print an update to the screen //Set the next state to ending game //mark that we are taking a transition // consume this event } } //If the event was time really almost out { //print an update to the screen //set the next state to ending game //mark that we are taking a transition // consume this event } //If the event was losing condition A { //print an update to the screen //set the next state to ending game //mark that we are taking a transition // consume this event } //if the event was losing condition B { //print an update to the screen //set the next state to ending game //mark that we are taking a transition // consume this event } //if the even was losing condition C { //If the condition C flag has not already been set { //print an update to the screen //Set the next state to depositing state //mark that we are taking a transition // consume this event //Set a flag so that it is known we came into this event from condition C }//otherwise the condition C flag has already been set { //print an update to the screen //set the next state to ending game //mark that we are taking a transition // consume this event } } //if the event was a 360 degree driven event { //print an update to the screen //Reduce radius by 1 foot //do not allow the radius to drop below the minimum set //Drive in to change radius //Post to DrivingSM //set the net state to harvesting state //mark that we are not taking a transition // consume this event } //Now at smaller radius, drive ccw arc { //print an update to the screen //Drive in to change radius //Post to DrivingSM //mark that we are taking a transition // consume this event } //If the event was a motor stalled event: { //If moving CW, drive CCW //print an update to the screen //reduce the driving arc radius //if the radius is less the the min //reset it to the starting value //resume driving in a CCW arc //Post event to DrivingSM }//otherwise we are driving in a CCW arc { //reduce the driving arc radius //if the radius is less the the min //reset it to the starting value //resume driving in a CW arc //Post event to DrivingSM } //Set the next state to harvesting state //mark that we are taking a transition // consume this event } //If the event was a game over event { //print an update to the screen //Stop the bot //Post to DrivingSM //Shut off the roller //mark that we are taking a transition //set the next state to game over // consume this event } } } //If we are in the depositing state //go to sub state machine //If an event is active { { //If the event is time really almost out { //print an update to the screen //set the next state to ending game //mark that we are taking a transition } //If the event is losing condition C { //print an update to the screen //set the next state to depositing state //mark that we are taking a transition // consume this event //trip the losing condition C flag } //if the event is game over event { //print an update to the screen //Stop the bot //Post to DrivingSM //shut off the roller //mark that we are taking a transition //Decide what the next state will be // consume this event } //if the event is an exit even { //if there is still time left on the clock { //print an update to the screen //if the condition C flag has been set { //print an update the screen //set the next state to endgame //mark that we are taking a transition // consume this event } //otherwise the losing conditino c flag has not been set { //if the time is really almost out { //declar variables //print an update to the screen //set the next state to ending game //set the last effort spin driection to drive at the nearest wall from the current bin //if that directino is CW { //print an update the the screen //drive CW //use the starting radius //Post to DrivingSM } //otherwise that direction is CCW { //print an update to the screen //drive CCW //use the starting radius //Post to DrivingSM } //mark that we are taking a transition // consume this event } //otherwise there is some time left { //print an update to the screen //set the next state to harvesting state //If moving CCW { // drive CW //Post event to DrivingSM } //otherwise moving CCW { //drive CW //Post event to DrivingSM } //mark that we are taking a transition // consume this event } } } } } } //if we are in the Ending game state //If an event is active { //base response on event type { //if the event is a game over event { //print an update to the screen //Stop the bot //Post to DrivingSM //turn off the roller //mark that we are taking a transition //Decide the next state will be GameOver // consume this event } } } //If we are in the game over event //print an update to the screen //make sure the roller is off //Stop the bot //Post to DrivingSM //mark that we are not taking a transition // consume this event //If an event is active { { //This is purgatory. There is no escaping GameOver. } } } // If we are making a state transition { // Execute exit function for current state //Modify state variable // Execute entry function for new state } // in the absence of an error the top level state machine should // always return ES_NO_EVENT } /**************************************************************************** Function StartGamePlayHSM Parameters ES_Event CurrentEvent Returns nothing Description Does any required initialization for this state machine Notes Author J. Edward Carryer, 02/06/12, 22:15 ****************************************************************************/ { // local variable to get debugger to display the value of CurrentEvent // always start in Expired // call the entry function (if any) for the ENTRY_STATE // there is no entry function for Expired // now we need to let the Run function init the lower level state machines // use LocalEvent to keep the compiler from complaining about unused var } /**************************************************************************** Function QueryGamePlayHSM Parameters None Returns unsigned char 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 ****************************************************************************/ { } /**************************************************************************** Function PostTargetXPosition Parameters int the coordinate to target Returns nothing Description allows outside setting of the desired coordinates Notes Author J. Edward Carryer, 2/11/05, 10:38AM ****************************************************************************/ { } /**************************************************************************** Function PostTargetYPosition Parameters int the coordinate to target Returns nothing Description allows outside setting of the desired coordinates Notes Author J. Edward Carryer, 2/11/05, 10:38AM ****************************************************************************/ { } /**************************************************************************** Function PostTargetRadius Parameters int the radius to target Returns nothing Description allows outside setting of the desired radius Notes Author J. Edward Carryer, 2/11/05, 10:38AM ****************************************************************************/ { } /*************************************************************************** private functions ***************************************************************************/ { // local variable to get debugger to display the value of CurrentEvent // process ES_ENTRY & ES_EXIT events // on exit, give the lower levels a chance to clean up first // repeat for any concurrently running state machines // now do any local exit functionality // do the 'during' function for this state // run any lower level state machine } { // local variable to get debugger to display the value of CurrentEvent // process ES_ENTRY & ES_EXIT events { { } } { // on exit, give the lower levels a chance to clean up first // repeat for any concurrently running state machines // now do any local exit functionality } // do the 'during' function for this state { // run any lower level state machine //puts("\n\r RUNNING EndGameState"); } } // void OpenDoor(void) { //Update PWM output on U2 for servo } // void CloseDoor(void) { //Update PWM output on U2 for servo } // int QueryStartingOrientation(void) { } //void TurnOnRoller(void) { //Update PWM output on U3 for roller motor } //void TurnOffRoller(void) { //Update PWM output on U3 for roller motor } //static void FindStartingPositionAndOrientation(void) { //Base response the the beacon number last sensed { //If it was bin 1 { //Display red //Set up appropriate bin scoring structure } //If it was bin 2 { //Display blue //Set up appropriate bin scoring structure } //If it was bin 3 { //Display blue //Set up appropriate bin scoring structure } //if it was bin 4 { //Display red //Set up appropriate bin scoring structure } } } //static boolean CheckHopper4Balls(void) { } /**************************************************************************** Functions QueryDesiredLeft/RightRPM Parameters void Returns integer with desiredRPM of left/right motor Description allows other state machines read desiredRPM Notes Author AAS 2/18/12 ****************************************************************************/ //int QueryTargetXPosition( void ) { } //int QueryTargetYPosition (void) { } //int QueryTargetOrientation (void) { } //int QueryTargetArcRadius(void) { } //int QueryOStart(void) { }