0% found this document useful (0 votes)
177 views

Actionmachine C

This document describes an ActionMachine state machine that handles game flags and starts a Location state machine. It includes: 1) Defines for the ActionMachine states and includes header files for lower level state machines. 2) A RunAction function that processes events based on the current state and can transition to a new state. 3) StartAction and QueryActionState functions to initialize the state machine and check the current state. 4) During functions for each state that define entry/exit behaviors and can process events to change states.

Uploaded by

api-272643960
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views

Actionmachine C

This document describes an ActionMachine state machine that handles game flags and starts a Location state machine. It includes: 1) Defines for the ActionMachine states and includes header files for lower level state machines. 2) A RunAction function that processes events based on the current state and can transition to a new state. 3) StartAction and QueryActionState functions to initialize the state machine and check the current state. 4) During functions for each state that define entry/exit behaviors and can process events to change states.

Uploaded by

api-272643960
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 6

/****************************************************************************

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;

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 WaitForStart :
// 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 = DuringWaitForStart(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
switch (CurrentEvent.EventType)
{
//move into location state machine
when flag dropped
case EV_FLAG_DROPPED :
NextState = Location;//Decide what the next state will be
// for internal transitions, skip changing MakeTransition
MakeTransition = true; //mark that we are taking a transition
// if transitioning to a state with history change kind of
entry
EntryEventKind.EventType = ES_ENTRY;
// optionally, consume or re-map this event for the upper
// level state machine
ReturnEvent.EventType = ES_NO_EVENT;
break;
default:break;
}
}
break;
// repeat state pattern as required for other states
case CautionFlag :
// 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 = DuringCautionFlag(CurrentEvent);
//
printf("In caution flag state in action machine\r\n");
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
switch (CurrentEvent.EventType)
{
//move into location state machine
when flag dropped
case EV_FLAG_DROPPED :
// Execute action function for state one : event one
NextState = Location;//Decide what the next state will be
// for internal transitions, skip changing MakeTransition
MakeTransition = true; //mark that we are taking a transition
// if transitioning to a state with history change kind of
entry
EntryEventKind.EventType = ES_ENTRY_HISTORY;
// optionally, consume or re-map this event for the upper
// level state machine
ReturnEvent.EventType = ES_NO_EVENT;
break;
// repeat cases as required for relevant events
}

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);

//process any events


if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
switch (CurrentEvent.EventType)
{
case EV_CAUTION_FLAG : //If event is caution flag, go into
Caution Flag state
// Execute action function for state one : event one
NextState = CautionFlag;//Decide what the next state will be
// for internal transitions, skip changing MakeTransition
MakeTransition = true; //mark that we are taking a transition
// if transitioning to a state with history change kind of
entry
EntryEventKind.EventType = ES_ENTRY;
// optionally, consume or re-map this event for the upper
// level state machine
ReturnEvent.EventType = ES_NO_EVENT;
break;
case EV_RACE_OVER_FLAG : //If event
is race over, go into Race Over state
// Execute action function for state one : event one
NextState = RaceOver;//Decide what the next state will be
// for internal transitions, skip changing MakeTransition
MakeTransition = true; //mark that we are taking a transition
// if transitioning to a state with history change kind of
entry
EntryEventKind.EventType = ES_ENTRY;
// optionally, consume or re-map this event for the upper
// level state machine
ReturnEvent.EventType = ES_NO_EVENT;
break;
// repeat cases as required for relevant events
default:break;

}
}
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);

static ES_Event DuringLocation( 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) )
{
// implement any entry actions required for this state machine
printf("STATE: Location\r\n");
state

// 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);

static ES_Event DuringCautionFlag( 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: Caution Flag\r\n");
// stop motors
StopMotors();
}
else if ( Event.EventType == ES_EXIT )
{
// do nothing
}
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);

static ES_Event DuringRaceOver( 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) )
{
// turn off LED
TurnOffLED();
printf("STATE: Race Over\r\n");
// stop motors
StopMotors();
}
else if ( Event.EventType == ES_EXIT )
{
// do nothing

}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);

You might also like