Sie sind auf Seite 1von 21

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

Module
MasterSM.c

Revision
2.0.1

Description
This implements the top level state machine for the hierarchical state machine.
Transitions between robot states based on game events and other internal events.

Notes

History
When Who What/Why
-------------- --- --------
02/20/17 14:30 jec updated to remove sample of consuming an event. We
always want to return ES_NO_EVENT at the top level
unless there is a non-recoverable error at the
framework level
02/03/16 15:27 jec updated comments to reflect small changes made in '14 & '15
converted unsigned char to bool where appropriate
spelling changes on true (was True) to match standard
removed local var used for debugger visibility in 'C32
removed Microwave specific code and replaced with generic
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
*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_ssi.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h" // Define PART_TM4C123GH6PM in project
#include "driverlib/gpio.h"
#include "termio.h"
#include "hw_nvic.h"
#include "hw_pwm.h"
#include "hw_timer.h"
#include "hw_ssi.h"

#include "MasterSM.h"
#include "MotorDrive.h"
#include "MyPWMLib.h"
#include "ShootingSM.h"
#include "IRInputCaptureModule.h"
#include "REFService.h"
#include "ReloadStateMachine.h"
#include "SquaringToWall.h"
#include "MyPWMLib.h"
#include "NewtonStep.h"
#include "LineFollowingSM.h"
#include "EnablePA25_PB23_PD7_PF0.h"
#include "NavToReloadSM.h"
#include "ReloadStateMachine.h"
#include "FaceoffNav.h"
#include "Defense.h"
#include "NavToShootReloadSM.h"
#include "MotorDrive.h"
#include "ShootingToReload.h"
#include "NavToGoalShot.h"
/*----------------------------- Module Defines ----------------------------*/
//define entry state
#define ENTRY_STATE WaitingToStart

//ports and pins


#define TEAM_BIT_PORT BIT3HI //PD1
#define TEAM_BIT_PIN BIT1HI //PD1
#define TEAM_LED_PORT BIT3HI //PD3
#define TEAM_LED_PIN BIT3HI //PD3
#define BEAM_BREAK_PORT BIT3HI //PD0
#define BEAM_BREAK_PIN BIT0HI //PD0
#define STRAT_BIT_PORT BIT1HI //PB2,3
#define STRAT_BIT_MSB_PIN BIT3HI //PB3
#define STRAT_BIT_LSB_PIN BIT2HI //PB2

//team bit values for setting team bit during waiting to start state
#define RED_TEAM 0 //16 for red possession in REFService
#define BLUE_TEAM 1 //32 for blue possession in REFService

//strat bit values


#define RELOAD_STRAT 0 //shoot from reload
#define GOAL_STRAT 1 //shoot from goal
#define STATIC_DEFENSE_STRAT (0)
#define MOVING_DEFENSE_STRAT (1)
#define OFFENSE_STRAT_MASK (0x01)
#define DEFENSE_STRAT_MASK (0x02)
//timer timings
#define FIRST_DEFENSE_TIMING 5000 //timeout before switching to defense from first
defense
#define TEAM_LED_FLASH_TIMING 500 //timeout to flash team color LED strip

/*---------------------------- Module Functions ---------------------------*/


static ES_Event_t DuringWaitingToStart(ES_Event_t Event);
static ES_Event_t DuringFaceoffNav(ES_Event_t Event);
static ES_Event_t DuringNavToReload(ES_Event_t Event);
static ES_Event_t DuringReloading(ES_Event_t Event);
static ES_Event_t DuringNavToShootReloadShot(ES_Event_t Event);
static ES_Event_t DuringNavToShootGoalShot(ES_Event_t Event);
static ES_Event_t DuringShooting(ES_Event_t Event);
static ES_Event_t DuringShootingToReload(ES_Event_t Event);
static ES_Event_t DuringDefending(ES_Event_t Event);
static ES_Event_t DuringTiebreaking(ES_Event_t Event);
static ES_Event_t DuringGameOver(ES_Event_t Event);
static void ToggleTeamLED(void);

/*---------------------------- 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
static MasterState_t CurrentState;
// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
static uint8_t CurrentTeamBit;
static uint8_t CurrentBeamBreak;
static uint8_t TeamBit;
static uint8_t StratBits;
static uint8_t NumBalls = 1;

/*------------------------------ Module Code ------------------------------*/


/****************************************************************************
Function
InitMasterSM

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
****************************************************************************/
bool InitMasterSM(uint8_t Priority)
{
MyPriority = Priority;

ES_Event_t ThisEvent;
ThisEvent.EventType = ES_ENTRY;
// Start the Master State machine

StartMasterSM(ThisEvent);
printf("Initialized MasterSM\n\r");
return true;
}

/****************************************************************************
Function
PostMasterSM

Parameters
ES_Event_t 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
****************************************************************************/
bool PostMasterSM(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunMasterSM

Parameters
ES_Event_t: the event to process
Returns
ES_Event_t: 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
****************************************************************************/
ES_Event_t RunMasterSM(ES_Event_t CurrentEvent)
{
bool MakeTransition = false;/* are we making a state transition? */
MasterState_t NextState = CurrentState;
ES_Event_t EntryEventKind = { ES_ENTRY, 0 }; // default to normal entry to new
state
ES_Event_t ReturnEvent = { ES_NO_EVENT, 0 }; // assume no error
//printf("%u\r\n", CurrentEvent);
switch (CurrentState)
{
//state waiting to start
case WaitingToStart:
{
CurrentEvent = DuringWaitingToStart(CurrentEvent);

if (CurrentEvent.EventType != ES_NO_EVENT)
{
switch (CurrentEvent.EventType)
{
//if event is timeout from team LED flash timer
case ES_TIMEOUT:
{
if (CurrentEvent.EventParam == TEAM_LED_FLASH_TIMER)
{
//toggle the team LED
ToggleTeamLED();

//restart the flash timer


ES_Timer_InitTimer(TEAM_LED_FLASH_TIMER, TEAM_LED_FLASH_TIMING);
//printf("Toggling LED\n\r");
}
}
break;
//if event is faceoff started, transition to faceoff navigating to reload
case FACEOFF_STARTED:
{
printf("MasterSM to FaceoffNav\n\r");
NextState = FaceoffNav;
MakeTransition = true;
StratBits = ReadStratBits();
MoveReloadServo(RELOAD_SERVO_HOLD);
}
break;
//if event is team changed, update team bit
case TEAM_CHANGED:
{
CurrentTeamBit = CurrentEvent.EventParam;
}
break;
}
}
}
break;
//state navigating to reload in faceoff
case FaceoffNav:
{
CurrentEvent = DuringFaceoffNav(CurrentEvent);
if (CurrentEvent.EventType != ES_NO_EVENT)
{
switch (CurrentEvent.EventType)
{
//if event is ready for reload, transition to reloading state
case READY_FOR_RELOAD:
{
printf("MasterSM to Reloading\n\r");
NextState = Reloading;
EntryEventKind.EventParam = CurrentEvent.EventParam;
MakeTransition = true;
}
break;
//if event is defense go to defense
case DEFENSE_STARTED:
{
printf("MasterSM to Defending\n\r");
NextState = Defending;
MakeTransition = true;
}
break;
case UNEXPECTED_BUMPER:
{
printf("MasterSM to Defending\n\r");
NextState = Defending;
MakeTransition = true;
}
break;
case OFFENSE_STARTED:
{
printf("MasterSM to NavToShoot\n\r");
if (QueryOffenseStrat() == RELOAD_STRAT)
{
printf("MasterSM to NavToShootReloadShot\n\r");
NextState = NavToShootReloadShot;
}
else if (QueryOffenseStrat() == GOAL_STRAT)
{
printf("MasterSM to NavToGoalShot\n\r");
NextState = NavToShootGoalShot;
}
MakeTransition = true;
}
break;
}
}
}
break;

//state navigating to reload normally


case NavToReload:
{
CurrentEvent = DuringNavToReload(CurrentEvent);

if (CurrentEvent.EventType != ES_NO_EVENT)
{
switch (CurrentEvent.EventType)
{
//if event is ready for reload, transition to reloading state
case READY_FOR_RELOAD:
{
printf("MasterSM to Reloading\n\r");
NextState = Reloading;
EntryEventKind.EventParam = CurrentEvent.EventParam;
MakeTransition = true;
}
break;
//if event is defense, transition to defending
case DEFENSE_STARTED:
{
printf("MasterSM to Defending\n\r");
NextState = Defending;
MakeTransition = true;
}
break;
case FAILED_RELOAD_IR:
{
printf("MasterSM to Defending\n\r");
NextState = Defending;
MakeTransition = true;
}
break;
case UNEXPECTED_BUMPER:
{
printf("MasterSM to Defending\n\r");
NextState = Defending;
MakeTransition = true;
}
break;
//if event is tiebreak do nothing so we can finish!!!
case TIEBREAK_STARTED:
{
printf("MasterSM to Tiebreaking\n\r");
}
break;
//if event is game over
case GAME_OVER:
{
printf("MasterSM to Game Over\n\r");
NextState = GameOver;
MakeTransition = true;
}
break;
}
}
}
break;

//state reloading
case Reloading:
{
CurrentEvent = DuringReloading(CurrentEvent);

if (CurrentEvent.EventType != ES_NO_EVENT)
{
switch (CurrentEvent.EventType)
{
//if event is ready to navigate to shoot, transition to navigating to shoot
case READY_TO_NAV_TO_SHOOT:
{
if (QueryOffenseStrat() == RELOAD_STRAT)
{
printf("MasterSM to NavToShootReloadShot\n\r");
NextState = NavToShootReloadShot;
}
else if (QueryOffenseStrat() == GOAL_STRAT)
{
printf("MasterSM to NavToGoalShot\n\r");
NextState = NavToShootGoalShot;
}
MakeTransition = true;
}
break;
//if event is defense, transition to defending state
case DEFENSE_STARTED:
{
printf("MasterSM to Defending\n\r");
NextState = Defending;
MakeTransition = true;
}
break;

//fixed so stay if tiebreak


case TIEBREAK_STARTED:
{
printf("MasterSM to Tiebreaking\n\r");
}
break;
//if event is game over
case GAME_OVER:
{
printf("MasterSM to Game Over\n\r");
NextState = GameOver;
MakeTransition = true;
}
break;
}
}
}
break;

//state navigating to shooting position


case NavToShootReloadShot:
{
CurrentEvent = DuringNavToShootReloadShot(CurrentEvent);

if (CurrentEvent.EventType != ES_NO_EVENT)
{
switch (CurrentEvent.EventType)
{
//if event is ready to shoot, transition to shooting
case READY_TO_SHOOT:
{
printf("MasterSM to Shooting\n\r");
NextState = Shooting;
MakeTransition = true;
}
break;
//if event is defense, transition to defending state
case DEFENSE_STARTED:
{
printf("MasterSM to Defending\n\r");
NextState = Defending;
MakeTransition = true;
}
break;
//if event is tiebreak started
case TIEBREAK_STARTED:
{
printf("MasterSM to Tiebreaking\n\r");
NextState = NavToReload;
MakeTransition = true;
}
break;
//if event is game over
case GAME_OVER:
{
printf("MasterSM to Game Over\n\r");
NextState = GameOver;
MakeTransition = true;
}
break;
}
}
}
break;

case NavToShootGoalShot:
{
CurrentEvent = DuringNavToShootGoalShot(CurrentEvent);

if (CurrentEvent.EventType != ES_NO_EVENT)
{
switch (CurrentEvent.EventType)
{
//if event is ready to shoot, transition to shooting
case READY_TO_SHOOT:
{
printf("MasterSM to Shooting\n\r");
NextState = Shooting;
MakeTransition = true;
}
break;
//if event is defense, transition to defending state
case DEFENSE_STARTED:
{
printf("MasterSM to Defending while in Navigatin to Goal Shot\n\r");
SetDefenseFlag(true);
}
break;
case DONE_WITH_NAV_TO_DEFENSE:
{
printf("MasterSM to Defending after defense flag was handled\n\r");
SetDefenseFlag(false);
MakeTransition = true;
EntryEventKind.EventParam = COMING_FROM_GOAL_SHOT;
NextState = Defending;
}
break;
//if event is tiebreak started
case TIEBREAK_STARTED:
{
printf("MasterSM to Tiebreaking\n\r");
NextState = NavToReload;
MakeTransition = true;
}
break;
//if event is game over
case GAME_OVER:
{
printf("MasterSM to Game Over\n\r");
NextState = GameOver;
MakeTransition = true;
}
break;
//if event is offense, transition to navigating to reload state
case OFFENSE_STARTED:
{
printf("MasterSM to NavToReload\n\r");
NextState = NavToReload;
MakeTransition = true;
}
break;
}
}
}
break;

//state shooting
case Shooting:
{
CurrentEvent = DuringShooting(CurrentEvent);

if (CurrentEvent.EventType != ES_NO_EVENT)
{
switch (CurrentEvent.EventType)
{
//if event is out of balls, transition to defending state
case OUT_OF_BALLS:
{
//changed from shooting to reload
if (QueryOffenseStrat() == RELOAD_STRAT)
{
NextState = NavToReload;
MakeTransition = true;
printf("MasterSM to Defending\n\r");
}
}
break;
//if event is defense, transition to defending state
case DEFENSE_STARTED:
{
printf("MasterSM to Defending\n\r");
NextState = Defending;
if (QueryOffenseStrat() == GOAL_STRAT)
{
EntryEventKind.EventParam = COMING_FROM_GOAL_SHOT;
}
else if (QueryOffenseStrat() == RELOAD_STRAT)
{
EntryEventKind.EventParam = COMING_FROM_RELOAD_SHOT;
}
MakeTransition = true;
}
break;
//if event is tiebreak started
case TIEBREAK_STARTED:
{
printf("MasterSM to Tiebreaking\n\r");
NextState = NavToReload;
MakeTransition = true;
}
break;
//if event is game over
case GAME_OVER:
{
printf("MasterSM to Game Over\n\r");
NextState = GameOver;
MakeTransition = true;
}
break;
}
}
}
break;

case ShootingToReload:
{
CurrentEvent = DuringShootingToReload(CurrentEvent);

}
break;

//state defending
case Defending:
{
CurrentEvent = DuringDefending(CurrentEvent);

if (CurrentEvent.EventType != ES_NO_EVENT)
{
switch (CurrentEvent.EventType)
{
//if event is offense, transition to navigating to reload state
case OFFENSE_STARTED:
{
printf("MasterSM to NavToReload\n\r");
NextState = NavToReload;
MakeTransition = true;
}
break;
//if event is tiebreak started
case TIEBREAK_STARTED:
{
printf("MasterSM to Tiebreaking NavToReload\n\r");
NextState = NavToReload;
MakeTransition = true;
}
break;
//if event is game over
case GAME_OVER:
{
printf("MasterSM to Game Over\n\r");
NextState = GameOver;
MakeTransition = true;
}
break;
}
}
}
break;

//state game over


case GameOver:
{
CurrentEvent = DuringGameOver(CurrentEvent);

if (CurrentEvent.EventType != ES_NO_EVENT)
{
switch (CurrentEvent.EventType)
{
//if event is waiting to start, transition to waiting to start
case WAITING_TO_START:
{
printf("MasterSM to WaitingToStart\n\r");
NextState = WaitingToStart;
MakeTransition = true;
}
break;
}
}
}
break;
}

// If we are making a state transition


if (MakeTransition == true)
{
// Execute exit function for current state
CurrentEvent.EventType = ES_EXIT;
RunMasterSM(CurrentEvent);
CurrentState = NextState; //Modify state variable
// Execute entry function for new state
// this defaults to ES_ENTRY
RunMasterSM(EntryEventKind);
}
// in the absence of an error the top level state machine should
// always return ES_NO_EVENT, which we initialized at the top of func
return ReturnEvent;
}

/****************************************************************************
Function
StartMasterSM

Parameters
ES_Event_t CurrentEvent

Returns
nothing

Description
Does any required initialization for this state machine
Notes

Author
J. Edward Carryer, 02/06/12, 22:15
****************************************************************************/
void StartMasterSM(ES_Event_t CurrentEvent)
{
// if there is more than 1 state to the top level machine you will need
// to initialize the state variable
//TODO change this back to waiting to start.
CurrentState = ENTRY_STATE; //Waiting To Start
//CurrentState = FaceoffNav;
// 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

RunMasterSM(CurrentEvent);
return;
}

/***************************************************************************
private functions
***************************************************************************/
static ES_Event_t DuringWaitingToStart(ES_Event_t Event)
{
ES_Event_t 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

//start off TeamLED timer


ES_Timer_InitTimer(TEAM_LED_FLASH_TIMER, TEAM_LED_FLASH_TIMING);
StopDrive();

// after that start any lower level machines that run in this state
//StartLowerLevelSM( 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
//RunLowerLevelSM(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 = 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;
}

static ES_Event_t DuringFaceoffNav(ES_Event_t Event)


{
ES_Event_t 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))
{
//set team LED solid
HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) |= TEAM_LED_PIN;

//start the lower SM


StartFaceoffNav(Event);
}
else if (Event.EventType == ES_EXIT)
{
RunFaceoffNav(Event);
// now do any local exit functionality
}
else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunFaceoffNav(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;
}

static ES_Event_t DuringNavToReload(ES_Event_t Event)


{
ES_Event_t 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

// after that start any lower level machines that run in this state
StartNavToReloadSM(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
RunNavToReloadSM(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 = RunNavToReloadSM(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;
}

static ES_Event_t DuringReloading(ES_Event_t Event)


{
ES_Event_t 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

// after that start any lower level machines that run in this state
StartReloadStateMachine(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
RunReloadStateMachine(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 = RunReloadStateMachine(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;
}

static ES_Event_t DuringNavToShootReloadShot(ES_Event_t Event)


{
ES_Event_t 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

// after that start any lower level machines that run in this state
SetComingFromShooting(true);
StartNavToShootReloadSM(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
RunNavToShootReloadSM(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 = RunNavToShootReloadSM(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;
}

static ES_Event_t DuringNavToShootGoalShot(ES_Event_t Event)


{
ES_Event_t 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

// after that start any lower level machines that run in this state
// SetComingFromShooting(true);
StartNavToGoalShot(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
RunNavToGoalShot(Event);
SetDefenseFlag(false);
// 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 = RunNavToGoalShot(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;
}

static ES_Event_t DuringShooting(ES_Event_t Event)


{
ES_Event_t 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
// after that start any lower level machines that run in this state
StartShootingSM(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
RunShootingSM(Event);
// repeat for any concurrently running state machines
// now do any local exit functionality

//Reset servo positions


MoveLaunchServo(LAUNCH_SERVO_IDLE);
MoveReloadServo(RELOAD_SERVO_HOLD);
}
else
// do the 'during' function for this state
{
// run any lower level state machine
ReturnEvent = RunShootingSM(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;
}

static ES_Event_t DuringShootingToReload(ES_Event_t Event)


{
ES_Event_t 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
// after that start any lower level machines that run in this state
StartShootingToReload(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
RunShootingToReload(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 = RunShootingToReload(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;
}

static ES_Event_t DuringDefending(ES_Event_t Event)


{
ES_Event_t 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

// after that start any lower level machines that run in this state
StartDefense(Event);
SetComingFromShooting(false);
// 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
RunDefense(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 = RunDefense(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;
}

static ES_Event_t DuringTiebreaking(ES_Event_t Event)


{
ES_Event_t 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

// after that start any lower level machines that run in this state
StartNavToReloadSM(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
RunNavToReloadSM(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 = RunNavToReloadSM(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;
}

static ES_Event_t DuringGameOver(ES_Event_t Event)


{
ES_Event_t 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
StopDrive();
// 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
}
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;
}

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

//hardware init for team bit switch (PD1)


void InitTeamBit(void)
{
//initialize port D, kill a couple cycles, initialize pin PD1 as digital input
HWREG(SYSCTL_RCGCGPIO) |= TEAM_BIT_PORT;
while ((HWREG(SYSCTL_PRGPIO) & TEAM_BIT_PORT) != TEAM_BIT_PORT)
{
;
}
HWREG(GPIO_PORTD_BASE + GPIO_O_DEN) |= TEAM_BIT_PIN;
HWREG(GPIO_PORTD_BASE + GPIO_O_DIR) &= ~TEAM_BIT_PIN;
printf("Initialized team bit pin\n\r");

// Setting PD1 to pull down


HWREG(GPIO_PORTD_BASE + GPIO_O_PDR) |= TEAM_BIT_PIN;

//set team bit initially


CurrentTeamBit = !(HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) &
TEAM_BIT_PIN);
}

//event checker for team bit switch (PD1)


bool CheckTeamBit(void)
{
uint8_t NewTeamBit = !(HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) &
TEAM_BIT_PIN);

//if switch state changes, set TeamBit to CurrentTeamBit (0 for red, 1 for blue)
if (NewTeamBit != CurrentTeamBit)
{
ES_Event_t CurrentEvent;
CurrentEvent.EventType = TEAM_CHANGED;
CurrentEvent.EventParam = NewTeamBit;
PostMasterSM(CurrentEvent);
CurrentTeamBit = NewTeamBit;
return true;
}
return false;
}

//query function for team bit


uint8_t QueryTeamBit(void)
{
return CurrentTeamBit;
}

//hardware init for team color LED strip flash (PD3)


void InitTeamLEDFlash(void)
{
//initialize port D, kill a couple cycles, initialize pin PD3 as digital output
HWREG(SYSCTL_RCGCGPIO) |= TEAM_LED_PORT;
while ((HWREG(SYSCTL_PRGPIO) & TEAM_LED_PORT) != TEAM_LED_PORT)
{
;
}
HWREG(GPIO_PORTD_BASE + GPIO_O_DEN) |= TEAM_LED_PIN;
HWREG(GPIO_PORTD_BASE + GPIO_O_DIR) |= TEAM_LED_PIN;
printf("Initialized team LED pin\n\r");

//start team LED high


HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) |= TEAM_LED_PIN;
}

//toggle LED strip flash


static void ToggleTeamLED(void)
{
HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) ^= TEAM_LED_PIN;
}

//hardware init for beam break (PD0)


void InitBeamBreak(void)
{
//initialize port D, kill a couple cycles, initialize pin PD0 as digital input
HWREG(SYSCTL_RCGCGPIO) |= BEAM_BREAK_PORT;
while ((HWREG(SYSCTL_PRGPIO) & BEAM_BREAK_PORT) != BEAM_BREAK_PORT)
{
;
}
HWREG(GPIO_PORTD_BASE + GPIO_O_DEN) |= BEAM_BREAK_PIN;
HWREG(GPIO_PORTD_BASE + GPIO_O_DIR) &= ~BEAM_BREAK_PIN;

// Setting PD0 to pull up


HWREG(GPIO_PORTD_BASE + GPIO_O_PUR) |= BEAM_BREAK_PIN;

//set beam break initially


CurrentBeamBreak = HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) &
BEAM_BREAK_PIN;

printf("Initialized beam break pin\n\r");


}

//event checker for beam break


bool CheckBeamBreak(void)
{
uint8_t NewBeamBreak = HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) &
BEAM_BREAK_PIN;
//check if beam break changed and beam was broken
if ((NewBeamBreak != CurrentBeamBreak) & (!NewBeamBreak))
{
ES_Event_t BeamEvent;
BeamEvent.EventType = GOT_BALL;
PostMasterSM(BeamEvent);
CurrentBeamBreak = NewBeamBreak;
return true;
}
CurrentBeamBreak = NewBeamBreak;
return false;
}

//hardware init for strat bits (PB2,3)


void InitStratBits(void)
{
printf("Initialized strat bits\n\r");
//initialize port B, kill a couple cycles, initialize pin PB2,3 as digital input
HWREG(SYSCTL_RCGCGPIO) |= STRAT_BIT_PORT;
while ((HWREG(SYSCTL_PRGPIO) & STRAT_BIT_PORT) != STRAT_BIT_PORT)
{
;
}
HWREG(GPIO_PORTB_BASE + GPIO_O_DEN) |= (STRAT_BIT_MSB_PIN | STRAT_BIT_LSB_PIN);
HWREG(GPIO_PORTB_BASE + GPIO_O_DIR) &= ~(STRAT_BIT_MSB_PIN | STRAT_BIT_LSB_PIN);

//do initial read of strat bits (PB2,3 then right shift into least significant
positions)
StratBits = ReadStratBits();
printf( "Offense strat bit is %d\r\n.", QueryOffenseStrat());
printf( "Defense strat bit is %d\r\n.", QueryDefenseStrat());
}

uint8_t ReadStratBits(void)
{
return (HWREG(GPIO_PORTB_BASE + (GPIO_O_DATA + ALL_BITS)) & (STRAT_BIT_MSB_PIN |
STRAT_BIT_LSB_PIN)) >> 2;
}

//query function for strat bits


uint8_t QueryOffenseStrat(void)
{
return StratBits & OFFENSE_STRAT_MASK;
}

//query function for strat bits


uint8_t QueryDefenseStrat(void)
{
return (StratBits & DEFENSE_STRAT_MASK) >> 1;
}

//increment num balls


void IncrementNumBalls(void)
{
NumBalls++;
}

//decrement num balls


void DecrementNumBalls(void)
{
NumBalls--;
}

//query function for number of balls


uint8_t QueryNumBalls(void)
{
return NumBalls;
}

Das könnte Ihnen auch gefallen