Professional Documents
Culture Documents
Playservice
Playservice
// 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 "PlayService.h"
#include "GamePlayHSM.h"
#include "RefService.h"
#include "Offense_SM.h"
#include "Defense_SM.h"
#include "MotorService.h"
#include "Reloading_SM.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_pwm.h"
#include "inc/hw_timer.h"
#include "inc/hw_nvic.h"
#define OVERTIME_FLAG 5
//#define MAKE_DEFENSE_ENTRY_STATE_ROTATING 10
Parameters
ES_Event_t: the event to process
Returns
ES_Event_t: 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_t RunPlayService(ES_Event_t CurrentEvent)
{
bool MakeTransition = false; /* are we making a state transition? */
PlayState_t NextState = CurrentState;
ES_Event_t EntryEventKind = { ES_ENTRY, 0 }; // default to normal entry to
new state
ES_Event_t ReturnEvent = CurrentEvent; // assume we are not consuming
event
switch (CurrentState)
{
case WAITING_TO_START:
{
// 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
case OFFENSE:
{
// 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
ReturnEvent = CurrentEvent = DuringOffense(CurrentEvent);
{ NextState = DEFENSE;
printf("EV early defense event received in PlayService\r\n");
// 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
case DEFENSE:
{
// 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
ReturnEvent = CurrentEvent = DuringDefense(CurrentEvent);
case GAME_OVER:
{
// 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
ReturnEvent = CurrentEvent = DuringGameOver(CurrentEvent);
case OVERTIME:
{
// 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
ReturnEvent = CurrentEvent = DuringOvertime(CurrentEvent);
if (CurrentEvent.EventType != OVERTIME)
{
NextState = (PlayState_t)CurrentEvent.EventParam;
/****************************************************************************
Function
StartPlayService
Parameters
None
Returns
None
Description
Does any required initialization for this state machine
Notes
Author
J. Edward Carryer, 2/18/99, 10:38AM
****************************************************************************/
void StartPlayService(ES_Event_t 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 = ENTRY_STATE;
}
// call the entry function (if any) for the ENTRY_STATE (NONE)
//printf("about to call runplayservice\n\r");
RunPlayService(CurrentEvent);
}
/****************************************************************************
Function
QueryPlayService
Parameters
None
Returns
PlayState_t The current state of the Play state machine
Description
returns the current state of the Play state machine
Notes
Author
J. Edward Carryer, 2/11/05, 10:38AM
****************************************************************************/
PlayState_t QueryPlayService(void)
{
return CurrentState;
}
uint8_t GetTeamColor(void)
{
return TeamColor;
}
void ResetGoalEdges(void)
{
NumGoalEdges = 0;
}
void ResetReloadEdges(void)
{
NumReloadEdges = 0;
}
/***************************************************************************
private functions
***************************************************************************/
//Reads switch for team and raises correct flag. If high then RED. If low then
BLUE.
static void ReadTeamColor(void)
{
uint8_t Color = HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) & BIT7HI;
if (Color)
{
TeamColor = RedTeam;
printf("team color is red\r\n");
//1.5ms high time
HWREG(PWM0_BASE + PWM_O_2_CMPA) = SERVO_CMP_RIGHT;
}
else
{
TeamColor = BlueTeam;
printf("team color is blue\r\n");
//2.5ms high time
HWREG(PWM0_BASE + PWM_O_2_CMPA) = SERVO_CMP_LEFT;
}
}
// after that start any lower level machines that run in this state
// 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
// repeat for any concurrently running state machines
// now do any local exit functionality (NO EXIT FUNCTIONS)
}
else
// do the 'during' function for this state
{
// run any lower level state machine
//ReturnEvent = RunLowerLevelSM(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;
}
// after that start any lower level machines that run in this state
StartReloadingSM(Event);
//checking score
ES_Event_t ScoreEvent;
ScoreEvent.EventType = EV_SCORE_UPDATE;
PostMasterSM(ScoreEvent);
// after that start any lower level machines that run in this state
StartOffenseSM(Event);
//Lower flag
HWREG(PWM0_BASE + PWM_O_2_CMPA) = SERVO_CMP_CENTER;
// after that start any lower level machines that run in this state
// 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
// repeat for any concurrently running state machines
// now do any local exit functionality (NO EXIT FUNCTIONS)
}
else
// do the 'during' function for this state
{
// run any lower level state machine
// 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;
}
void Overtime_ISR(void)
{
// start by clearing the source of the interrupt
HWREG(WTIMER1_BASE + TIMER_O_ICR) = TIMER_ICR_TATOCINT;
ES_Timer_InitTimer(GAME_TIMER,
(uint32_t)REMAINING_TIME_BEFORE_OVERTIME_RELOAD_DECISION); //32.5 seconds after
100 seconds of wide timer
void Goal_Beacon_ISR(void)
{
uint32_t ThisCapture;
// start by clearing the source of the interrupt, the input capture event
HWREG(WTIMER3_BASE + TIMER_O_ICR) = TIMER_ICR_CAECINT;
void Reload_Beacon_ISR(void)
{
uint32_t ThisCapture;
// start by clearing the source of the interrupt, the input capture event
HWREG(WTIMER3_BASE + TIMER_O_ICR) = TIMER_ICR_CBECINT;