You are on page 1of 13

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

Module
Location.c
Revision
1.0.2
Description
State machine to determine next location
Notes
Started KF 2/25
****************************************************************************/
/*----------------------------- 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 "Location.h"
#include "TargetTracking.h"
#include "SendCommand.h"
#include "MasterMachine.h"
#include "BallShooting.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 DuringGoingToPosition1( ES_Event Event);
static ES_Event DuringGoingToPosition2( ES_Event Event);
static ES_Event DuringGoingToPosition2AfterBall( ES_Event Event);
static ES_Event DuringBallShooting( ES_Event Event);
static ES_Event DuringGoingToPosition3( ES_Event Event);
static ES_Event DuringGoingToPosition4( ES_Event Event);
static ES_Event DuringGoingToPosition5( ES_Event Event);
static ES_Event DuringGoingToPosition6( ES_Event Event);
static ES_Event DuringGoingToPosition7( ES_Event Event);
static ES_Event DuringGoingToPosition8( ES_Event Event);
/*---------------------------- Module Variables ---------------------------*/
static LocationState_t CurrentState;
static uint8_t LapNumber = 0;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
RunLocationSM
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.
****************************************************************************/
ES_Event RunLocationSM( ES_Event CurrentEvent )
{
bool MakeTransition = false;/* are we making a state transition? */
LocationState_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 GoingToPosition1 :
// Execute During function
CurrentEvent = DuringGoingToPosition1(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
//if target is reached, go to next position
if (CurrentEvent.EventType == EV_TARGET_REACHED)
{
NextState =
GoingToPosition2;//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 GoingToPosition2 :
// Execute During function
CurrentEvent = DuringGoingToPosition2(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
//if target is reached, go to next position
if (CurrentEvent.EventType == EV_TARGET_REACHED)
{
//if ball hasn't been made yet
and it's lap 1, go to shooting zone
if(GetTargetStatus() == 0)
//
IF we made a shot already
{
NextState =
GoingToPosition8;
}
//if ball has already been made,
skip the shooting zone
else
{

NextState =

GoingToPosition3;
}
skip changing MakeTransition
that we are taking a transition
with history change kind of entry

// for internal transitions,


MakeTransition = true; //mark
// if transitioning to a state
EntryEventKind.EventType =

ES_ENTRY;
this event for the upper

// optionally, consume or re-map


// level state machine
ReturnEvent.EventType =

ES_NO_EVENT;

// repeat cases as required for relevant events


}

}
break;

case GoingToPosition3 :
// Execute During function
CurrentEvent = DuringGoingToPosition3(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
//if target is reached, go to next position
if (CurrentEvent.EventType == EV_TARGET_REACHED)
{
NextState =
GoingToPosition4;//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;
// repeat cases as required for relevant events
}
}
break;
case GoingToPosition4 :
// Execute During function
CurrentEvent = DuringGoingToPosition4(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
//if target is reached, go to next position
if (CurrentEvent.EventType == EV_TARGET_REACHED)
{
// if we've already crossed the
obstacle
if(GetObstacleStatus() == 0 &&
LapNumber == 3)
{
NextState =

GoingToPosition7;
over it

}
//if obstacle is complete, skip
else
{
NextState =

GoingToPosition5;
}
skip changing MakeTransition
that we are taking a transition
with history change kind of entry

// for internal transitions,


MakeTransition = true; //mark
// if transitioning to a state
EntryEventKind.EventType =

ES_ENTRY;
this event for the upper

// optionally, consume or re-map


// level state machine
ReturnEvent.EventType =

ES_NO_EVENT;

// repeat cases as required for relevant events


}

}
break;

case GoingToPosition5 :
// Execute During function
CurrentEvent = DuringGoingToPosition5(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
//if target is reached, go to next position
if (CurrentEvent.EventType == EV_TARGET_REACHED)
{
NextState = GoingToPosition6;
// 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;
// repeat cases as required for relevant events
}
}
break;
case GoingToPosition6 :
// Execute During function
CurrentEvent = DuringGoingToPosition6(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
//if target is reached, go to next position
if (CurrentEvent.EventType == EV_TARGET_REACHED)
{
NextState = GoingToPosition1;

skip changing MakeTransition


that we are taking a transition
with history change kind of entry

// for internal transitions,


MakeTransition = true; //mark
// if transitioning to a state
EntryEventKind.EventType =

ES_ENTRY;
this event for the upper

// optionally, consume or re-map


// level state machine
ReturnEvent.EventType =

ES_NO_EVENT;

// repeat cases as required for relevant events


}

}
break;

case GoingToPosition7 :
// Execute During function
CurrentEvent = DuringGoingToPosition7(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
//if target is reached, go to next position
if (CurrentEvent.EventType == EV_TARGET_REACHED)
{
NextState = GoingToPosition1;
// 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;
// repeat cases as required for relevant events
}
}
break;
case GoingToPosition8 :
// If current state is
Position 8 (shooting zone)
// Execute During function
CurrentEvent = DuringGoingToPosition8(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
//if target is reached, go to next position
if (CurrentEvent.EventType == EV_TARGET_REACHED)
{
NextState = BallShooting;
// 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


ES_NO_EVENT;
}

// level state machine


ReturnEvent.EventType =

// repeat cases as required for relevant events

}
break;
case BallShooting :
// Execute During function
CurrentEvent = DuringBallShooting(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
//if shot is fired, go to position 2 after ball
if (CurrentEvent.EventType == EV_SHOT_FIRED)
{
NextState =
GoingToPosition2AfterBall;
// 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;
// repeat cases as required for relevant events
}
}
break;
case GoingToPosition2AfterBall :
// Execute During function
CurrentEvent = DuringGoingToPosition2AfterBall(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
//if target is reached, go to next position
if (CurrentEvent.EventType == EV_TARGET_REACHED)
{
NextState = GoingToPosition3;
// 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;
// repeat cases as required for relevant events
}
}
break;
default: break;

// repeat state pattern as required for other states


}
//
If we are making a state transition
if (MakeTransition == true)
{
//
Execute exit function for current state
CurrentEvent.EventType = ES_EXIT;
RunLocationSM(CurrentEvent);
CurrentState = NextState; //Modify state variable
//
Execute entry function for new state
// this defaults to ES_ENTRY
RunLocationSM(EntryEventKind);
}
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 StartLocationSM ( ES_Event CurrentEvent )
{
//start out in position 1
if ( ES_ENTRY_HISTORY != CurrentEvent.EventType )
{
CurrentState = GoingToPosition1;
}
// call the entry function (if any) for the ENTRY_STATE
RunLocationSM(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
****************************************************************************/
LocationState_t QueryLocationSM ( void )
{

return(CurrentState);

/***************************************************************************
private functions
***************************************************************************/
static ES_Event DuringGoingToPosition1( 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: Going to position 1 \r\n");
state

// after that start any lower level machines tht run in this
Event.EventParam = 1;
StartTargetTracking( Event );
//increment lap number
if(Event.EventType == ES_ENTRY) {
LapNumber++;
}

}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first
RunTargetTracking(Event);
}else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunTargetTracking(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 DuringGoingToPosition2( 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: Going to position 2 \r\n");
Event.EventParam = 2;
// after that start any lower level machines that run in this
state
StartTargetTracking( Event );
// repeat the StartxxxSM() functions for concurrent state
machines
// on the lower level
}
else if ( Event.EventType == ES_EXIT )
{

// on exit, give the lower levels a chance to clean up first


RunTargetTracking(Event);

}else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunTargetTracking(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 DuringGoingToPosition3( 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: Going to position 3 \r\n");
Event.EventParam = 3;
// after that start any lower level machines that run in this
state
StartTargetTracking( Event );
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first
RunTargetTracking(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 = RunTargetTracking(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 DuringGoingToPosition4( 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: Going to position 4 \r\n");
Event.EventParam = 4;
// after that start any lower level machines that run in this
state
StartTargetTracking( Event );
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first

RunTargetTracking(Event);
}else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunTargetTracking(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 DuringGoingToPosition5( 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: Going to position 5 \r\n");
Event.EventParam = 5;
// after that start any lower level machines that run in this
state
StartTargetTracking( Event );
// repeat the StartxxxSM() functions for concurrent state
machines
// on the lower level
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first
RunTargetTracking(Event);
}else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunTargetTracking(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 DuringGoingToPosition6( 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: Going to position 6 \r\n");
Event.EventParam = 6;
// after that start any lower level machines that run in this
state
StartTargetTracking( Event );
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first

RunTargetTracking(Event);

}else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunTargetTracking(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 DuringGoingToPosition7( 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: Going to position 7 \r\n");
Event.EventParam = 7;
// after that start any lower level machines that run in this
state
StartTargetTracking( Event );
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first
RunTargetTracking(Event);
}else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunTargetTracking(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 DuringGoingToPosition8( 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: Going to position 8 \r\n");
Event.EventParam = 8;
// after that start any lower level machines that run in this
state
StartTargetTracking( Event );
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first
RunTargetTracking(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 = RunTargetTracking(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 DuringBallShooting( 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: BallShooting\r\n");
// after that start any lower level machines that run in this
state
StartBallShooting( Event );
// repeat the StartxxxSM() functions for concurrent state
machines
// on the lower level
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first
RunBallShooting(Event);

}else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunBallShooting(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 DuringGoingToPosition2AfterBall( 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: Going to position 2 after ball\r\n");
Event.EventParam = 2;
// after that start any lower level machines that run in this
state
StartTargetTracking( Event );
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first
RunTargetTracking(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 = RunTargetTracking(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);
}

You might also like