Actionmachine C
Actionmachine C
Module
ActionMachine.c
Revision
1.0.1
Description
handles game flags and starts the location state machine
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 "ActionMachine.h"
#include "DriveMotorService.h"
#include "Location.h"
#include "LED.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 DuringLocation( ES_Event Event);
static ES_Event DuringWaitForStart( ES_Event Event);
static ES_Event DuringCautionFlag( ES_Event Event);
static ES_Event DuringRaceOver(ES_Event Event);
/*---------------------------- Module Variables ---------------------------*/
// everybody needs a state variable, you may need others as well
static ActionState_t CurrentState;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
RunAction
Parameters
ES_Event: the event to process
Returns
ES_Event: an event to return
Description
add your description here
****************************************************************************/
ES_Event RunAction( ES_Event CurrentEvent )
{
bool MakeTransition = false;/* are we making a state transition? */
ActionState_t NextState = CurrentState;
default:break;
}
break;
case RaceOver:
// Execute During function for state one. ES_ENTRY &
ES_EXIT are
// processed here allow the lower level state machines to re-map
// or consume the event
CurrentEvent = DuringRaceOver(CurrentEvent);
//don't respond to any more events
break;
case Location :
// Execute During function for state one. ES_ENTRY & ES_EXIT are
// processed here allow the lower level state machines to re-map
// or consume the event
CurrentEvent = DuringLocation(CurrentEvent);
}
}
break;
default:
;
}
//
If we are making a state transition
if (MakeTransition == true)
//
Execute exit function for current state
CurrentEvent.EventType = ES_EXIT;
RunAction(CurrentEvent);
CurrentState = NextState; //Modify state variable
//
Execute entry function for new state
// this defaults to ES_ENTRY
RunAction(EntryEventKind);
}
return(ReturnEvent);
}
/****************************************************************************
Function
StartAction
Parameters
None
Returns
None
Description
Does any required initialization for this state machine
****************************************************************************/
void StartAction ( ES_Event 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 = WaitForStart;
}
// call the entry function (if any) for the ENTRY_STATE
printf("Entering Action Machine\r\n");
RunAction(CurrentEvent);
/****************************************************************************
Function
QueryActionState
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
****************************************************************************/
ActionState_t QueryActionState ( void )
{
return(CurrentState);
}
/***************************************************************************
private functions
***************************************************************************/
static ES_Event DuringWaitForStart( ES_Event Event)
{
ES_Event ReturnEvent = Event; // assme no re-mapping or comsumption
// process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events
if ( (Event.EventType == ES_ENTRY) ||
(Event.EventType == ES_ENTRY_HISTORY) )
{
printf("STATE: WaitForStart\r\n");
// stop motors while waiting
StopMotors();
}
else if ( Event.EventType == ES_EXIT )
{
//turn on LED
TurnOnLED();
}
else
// do the 'during' function for this state
{
// do nothing
}
// 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);
// after that start any lower level machines that run in this
StartLocationSM(Event);
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first
//RunLowerLevelSM(Event);
RunLocationSM(Event);
}
else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunLocationSM(Event);
}
// 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);
}else
// do the 'during' function for this state
{
// do nothing
}
// 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);