Beruflich Dokumente
Kultur Dokumente
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
//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
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();
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;
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;
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;
}
/****************************************************************************
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
// 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);
// 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);
// 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);
// 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);
// 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);
// 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);
// 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);
/*******************************************************************************
*/
//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;
}
//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;
}