Sunteți pe pagina 1din 5

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

void SetReportFlag(bool value);


void GetReportFlag(bool value);
/*---------------------------- Module Variables ---------------------------*/
static uint8_t MyPriority;
static bool ConstructionFlag = false; //game has not started
static bool ReportFlag = true; //can report frequency immediately once read
static uint8_t ReportCount = 0;
static bool GreenTeam;
static bool FirstRun = true; //to just shoot with the first 3 balls
// add a deferral queue for up to 3 pending deferrals +1 to allow for overhead
static ES_Event DeferralQueue[3+1];
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
InitLOCService
****************************************************************************/
bool InitLOCCommsService ( uint8_t Priority )
{
ES_Event ThisEvent;
// Initialize the MyPriority variable with the passed in parameter.
MyPriority = Priority;
// Initialize SPI communications

// Initialize CurrentState (DrivingState) - Max, sanity check?

//if TeamColour LED (PA3) is green (high or low pending designed circuit and t
esting)
//GreenTeam = true;
//end if
//else
//GreenTeam = false;
//end
// post the initial transition event (should this be ES_Entry as opposed to us
ual ES_INIT?)
ThisEvent.EventType = ES_INIT;
// Post Event ES_Init to StepDriveService queue
if (ES_PostToService( MyPriority, ThisEvent) == true)
{
return true;
}else
{
return false;
}
}
/****************************************************************************
Function
PostLOCCommsService
Description
Posts an event to this state machine's queue
****************************************************************************/
bool PostLOCCommsService( ES_Event ThisEvent )
{
return ES_PostToService( MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunLOCCommsService
Description
Run all command queries, is brain of the bot. Posts to all other SMs
****************************************************************************/
ES_Event RunLOCCommsService( ES_Event ThisEvent )
{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
//if ThisEvent.EventType == ES_GameStatus
//issue command 0b1100 0000 (return the status of the constructi
on cycle)
//if ConstructionFlag is false (beginning a round)
//if StatusByte3 returns WaitingtoStart (still waiting)
//post ES_GameStatus Event to LOCCommsService
//endif
//else if StatusByte3 returns ConstructionActive (GAME O
N!!!)
//set ConstructionFlag to true (we are in constr
ucting mode)
//turn light construction LED on (PA2)
//post ES_GameStatus Event to LOCCommsService (t
o find out which StagingArea is now open)
//endif
//endif
//if GreenTeam = true
//make a decision to read most significant / least signi
ficant bits and shift data accordingly
//endif
//if StatusByte1 returns CheckIn
//if FirstRun == true (we are not completely full on the
first run, but we don't want to want time reloading completely either
//Post ES_GoStagingArea event with EventParam be
ing the staging area we want to check into to DrivingSM
//StagingAreaNumber = EventParam, which should b
e the staging area number we want to check in to
//set FirstRun = false so all subsequent runs we
must make sure we are fully loaded before checking in
//endif
//else if PB1 (top IR detector) is low (no signal - we a
re full)
//Post ES_GoStagingArea event with EventParam be
ing the staging area we want to check into to DrivingSM
//StagingAreaNumber = EventParam, which should b
e the staging area number we want to check in to
//end if
//else if PB1 (top IR detector) is high (we do not have
5 balls)
// Post ES_GoDepot event to DrivingSM to reload
//endif
//endif
//else if StatusByte1 returns Shoot
//if StatusByte1 gives a GoalNumber == All Goals (last 1
8 seconds)
//if PC6 (bottom IR detector) is high (we are em
pty)
//Post ES_GoDepot event to DrivingSM to
reload
//endif
//if we are already on a StagingArea puck (how a
re we going to determine this?)
//Post ES_AtShoot event to Shooting SM
//endif
//else
//Post ES_GoStagingArea event with Event
Param of closest StagingArea (again, how exactly do we implement this?) to Drivi
ngSM
//endif
//endif
//else if StatusByte1 gives a GoalNumber == StagingAreaN
umber (i.e. we are already at the staging area corresponding to the goal)
//Post ES_AtShoot event to ShootingSM
//Post ES_GameStatus event to LOCCommsService (t
o find out if the goal has since closed)
//if PC6 (bottom IR detector) is high (we are em
pty)
//Post ES_GoDepot event to DrivingSM to
reload
//endif
//endif
//else if StatusByte1 gives a GoalNumber != StagingAreaN
umber (i.e. we need to drive to the corresponding goal)
//Post ES_GoStagingArea event to DrivingSM with
EventParam = goal number
//Set StagingAreaNumber = GoalNumber (so that th
e next time we run this comparison we don't get stuck back in driving again)
//endif
//endif
//else if ThisEvent.EventType == ES_ReportFreq (came from CheckingInSM)
//increment ReportCount (since must report twice and want to kee
p track of how many times we have reported in a given sequence)
//translate ThisEvent.EventParam into a 4-bit number correspondi
ng to staging area frequency
//if ReportFlag is true (can issue command with frequency immedi
ately) (do this via GetReportFlag function)
//issue command 0b1000 iiii, where iiii is the 4-bit num
ber corresponding to staging area frequency retrieved from EventParam
//start 200ms ReportTimer (to make sure we have the righ
t interval before making the next report)
/* side comment: Do we want to do this with one
of Ed's timers or an interrupt? In either case, we want to write in the appropri
ate either Post
function or ISR that we then set the ReportFlag
to true when the timer expires or the interrupt fires*/
//set ReportFlag to False (do this via SetReportFlag fun
ction)
//endif
//else if ReportFlag is false (do this via GetReportFlag functio
n)
//Post ES_ReportFreq event with same EventParam (the 4-b
it number we already have) to LOCCommsService
//Post ES_Query event to LOCCommsService (need to know we are re
ady before reporting another frequency)
//endif
//else if ThisEvent.EventType == ES_Query (waiting to check what the res
ponse is)
//issue command 0b0111 0000
//if ResponseReady byte is 0x00 (response not ready)
//Post ES_Query event to LOCCommsService (check again)
//endif
//else if ResponseReady byte is 0xAA (response ready)
//if ReportStatus byte says Acknowledged
//if ReportCount == 2 (have successfully checked
in)
//NextState = Driving (want to go to rel
evant shooting spot)
//ThisEvent.EventType = ES_GoShoot
//ThisEvent.EventParam = Location data f
rom ReportStatus byte corresponding to which goal is open
//Post ThisEvent to DrivingSM
//set ReportCount back to 0 (ready for n
ext time we start the checking in process)
//endif
//if ReportCount == 1 (haven't completed check i
n sequence yet)
//Post ES_Query event to CheckingInSM
//endif
//endif
//else if ReportStatus byte says Inactive
//Post ES_GameStatus event to LOCCommsService (g
ot staging area wrong / need to find which one is active)
//endif
//else if ReportStatus byte says Not Acknowledged
//set ReportCount back to 0 (read frequency wron
g, do it again)
//Post ES_Query event to CheckingInSM
//endif
//endif
//endif

return ReturnEvent;
}
/****************************************************************************
Function
SetReportFlag
Parameters
bool
Returns
void
Description
sets the Report flag to a given value (false = 200ms not yet elapsed, cannot rep
ort; true = can report immediately)
Notes
Author
Latifah Hamzah, 23 Jan 2017
****************************************************************************/
void SetReportFlag(bool value) {
ReportFlag = value;
}
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
GetReportFlag
Parameters
nothing
Returns
bool Flag
Description
Returns current state of ReportFlag
Notes
Author
Latifah Hamzah, 23 Jan 2017
****************************************************************************/
bool GetReportFlag(void) {
return ReportFlag;
}
/*------------------------------ Module Code ------------------------------*/
/***************************************************************************
*------------------------------- Footnotes -------------------------------*/
/*------------------------------ End of file ------------------------------*/

S-ar putea să vă placă și