Sie sind auf Seite 1von 8

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

Module
EventCheckers.c
Revision
1.0.1
Description
This is the sample for writing event checkers along with the event
checkers used in the basic framework test harness.
Notes
Check4Direction, Check4Start ?????
History
When
Who
What/Why
----------------------02/11/2015
13:55
Sky
Initial version
****************************************************************************/
// this will pull in the symbolic definitions for events, which we will want
// to post in response to detecting events
#include <stdint.h>
#include
#include
#include
#include
#include
#include
#include

"inc/hw_memmap.h"
"inc/hw_gpio.h"
"inc/hw_pwm.h"
"inc/hw_sysctl.h"
"inc/hw_types.h"
"bitdefs.h"
<stdio.h>

#include "ES_Configure.h"
// this will get us the structure definition for events, which we will need
// in order to post events in response to detecting events
#include "ES_Events.h"
// if you want to use distribution lists then you need those function
// definitions too.
#include "ES_PostList.h"
// This include will pull in all of the headers from the service modules
// providing the prototypes for all of the post functions
#include "ES_ServiceHeaders.h"
// this test harness for the framework references the serial routines that
// are defined in ES_Port.c
#include "ES_Port.h"
// include our own prototypes to insure consistency between header &
// actual functionsdefinition
#include "EventCheckers.h"
#include "MasterMachine.h"
#include "GameStateMachine.h"
#include "SPIStateMachine.h"
#include "RaceStateMachine.h"
#include "Scan.h"
#include "Calculation.h"
#include "InputCaptureShoot.h"
#include <math.h>
#define TOL4POS 10
#define TOL4ORI 20
#define LEFT 0

#define
#define
#define
#define

RIGHT 1
SHOOT 0
OBSTACLE 1
MASTER 2

#define ALL_BITS (0xff<<2)


/*******************************Module Constants********************************
****/
/*******************************Module Variables********************************
****/
static uint8_t self;
static bool isStart = false;
static bool DirectionRight = false;
static bool firstTime = true;
static bool isAligned;
/******************************Public Functions*********************************
****/
/********************************************************************
FUNCTION Check4Status
ARGUMENTS: no arguments
RETURN: true if status changed
DESCRIPTION: check the game status
********************************************************************/
bool Check4Status(void)
{
bool ReturnVal = false;
static uint8_t LeftLap = 10;
static uint8_t LastStatus = 100;
static uint8_t LastGameStatus = 0;
static uint8_t LastObstacleStatus = 0;
static uint8_t LastShootStatus = 0;
static bool firstStart = true;
//Query current status from SPI State Machine
GameStatusType CurrentStatus = QueryGameStatus();
//Query which Kart is self from MasterMachine
self = QuerySelf();
uint8_t tempStatus;
uint8_t CurrentGameStatus;
uint8_t CurrentObstacleStatus;
uint8_t CurrentShootStatus;
//switch self to get current game status for our Kart
switch (self)
{
case 1 : tempStatus = CurrentStatus.Kart1GameStatus;
break;
case 2 :
tempStatus = CurrentStatus.Kart2GameStatus;
break;
case 3 : tempStatus = CurrentStatus.Kart3GameStatus;
break;
default : break;
}
//get the current game status(stop, start, pause or waiting4start)

CurrentGameStatus = tempStatus & (BIT3HI | BIT4HI);


//get the current obstacle status(did the obstacle or not)
CurrentObstacleStatus = tempStatus & (BIT6HI);
//get the current shoot status(did a goal or not)
CurrentShootStatus = tempStatus & (BIT7HI);
//if game status has changed
if (CurrentGameStatus != LastGameStatus)
{
//if current game status is FlagRaised
if (CurrentGameStatus == 16)
{
printf("FlagRaised!\n\r");
//post an event ES_EXIT to master state machine
ES_Event ThisEvent;
isStart = false;
ThisEvent.EventType = ES_EXIT;
PostToMasterSM(ThisEvent);
}
//if current game status is start
else if (CurrentGameStatus == 8)
{
ES_Event ThisEvent;
//if the last game status is waiting4start or stop
if ((LastGameStatus == 0)||(LastGameStatus == 24))
{
//post an event ES_ENTRY to master state machine
printf("Start!\n\r");
//set isStart to true to indicate that the game
is now running
isStart = true;
ThisEvent.EventType = ES_ENTRY;
ThisEvent.EventParam = MASTER;
}
//if the last game status is flagraised(pause)
else if (LastGameStatus == 16)
{
//post an event ES_ENTRY_HISTORY to start Master
Machine and enter the historystate
isStart = true;
printf("ReStart!!\n\r");
ThisEvent.EventType = ES_ENTRY_HISTORY;
ThisEvent.EventParam = MASTER;
}
PostToMasterSM(ThisEvent);
}
//if current game status is stop
else if (CurrentGameStatus == 24)
{
//set isStart to false and post an event ES_EXIT to mast
er machine to stop the state machine
printf("Stop!\n\r");
firstStart = true;
isStart = false;
ES_Event ThisEvent;
ThisEvent.EventType = ES_EXIT;
PostToMasterSM(ThisEvent);
}
}

/*if (CurrentShootStatus != LastShootStatus)


{
SetShootStatus();
SetGoal();
}*/
//update the status
LastObstacleStatus = CurrentObstacleStatus;
LastShootStatus = CurrentShootStatus;
LastGameStatus = CurrentGameStatus;
return ReturnVal;
}
/********************************************************************
FUNCTION Check4Bucket
ARGUMENTS: no arguments
RETURN: true if bucket aligned
DESCRIPTION: check the IR signal
********************************************************************/
bool Check4Bucket(void)
{
bool ReturnVal = false;
static uint8_t LastState = 0;
//if the game is running, check IR status
if (isStart)
{
//if the signal has not been detected
if (!isAligned)
{
uint8_t CurrentState;
//get the current status of IR signal pin PC7
CurrentState = HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BI
TS)) & BIT7HI;
//if currentstate is different from laststate
if (CurrentState != LastState)
{
//Post an event ES_BucketAligned to Master Machi
ne
ES_Event ThisEvent;
ThisEvent.EventType = ES_BucketAligned;
PostToMasterSM(ThisEvent);
ReturnVal = true;
LastState = CurrentState;
}
}
}
return ReturnVal;
}
/********************************************************************
FUNCTION Check4Region
ARGUMENTS: no arguments
RETURN: true if region has changed
DESCRIPTION: check the current region of our kart

********************************************************************/
bool Check4Region(void)
{
bool ReturnVal = false;
static uint8_t LastRegion = 19;
//if the game is running, check the current region
if (isStart)
{
uint8_t CurrentRegion;
//Call FindRegion to calculate and find the current region
CurrentRegion = FindRegion();
//if the region of our Kart has changed
if (CurrentRegion != LastRegion)
{
printf("CurrentRegion = %d \n\r", CurrentRegion);
ES_Event ThisEvent;
//switch the CurrentRegion and post corresponding event
to Master Machine
switch (CurrentRegion)
{
case 1
:
ThisEvent.EventT
ype = ES_InRegion1;
break;
case 2

: ThisEvent.EventType =

case 3

case 4

: ThisEvent.EventType =

case 5

: ThisEvent.EventType =

case 6

: ThisEvent.EventType =

case 7

: ThisEvent.EventType =

case 8

: ThisEvent.EventType =

case 9

: ThisEvent.EventType =

case 10

: ThisEvent.EventType =

ES_InRegion2;
break;
ThisEvent.EventT

ype = ES_InRegion3;
break;
ES_InRegion4;
break;
ES_InRegion5;
break;
ES_InRegion6;
break;
ES_InRegion7;
break;
ES_InRegion8;
break;
ES_InRegion9;
break;
ES_InRegion10;
break;

case 11

ThisEvent.EventT

break;

ype = ES_InRegion11;
break;
default
}
ReturnVal = true;
LastRegion = CurrentRegion;
PostToMasterSM(ThisEvent);
}
}
return ReturnVal;
}
/********************************************************************
FUNCTION Check4Position
ARGUMENTS: no arguments
RETURN: true if our Kart arrived at the Target Position
DESCRIPTION: check if the kart has arrived at the target position
********************************************************************/
bool Check4Position(void)
{
bool ReturnVal = false;
//if the game is running, check the position of our kart
if (isStart)
{
//Query the state of Race state machine
RaceState tempState = QueryRaceState();
//if we are in the state of Race_ToShoot or Race_Region6
if ((tempState == Race_ToShoot)||(tempState == Race_Region6))
{
static Target LastTarget = {0, 0};
static Target CurrentTarget;
KartStatusType Kart;
//switch self to get the coordinates of our kart
switch (self)
{
case 1
:
Kart = QueryKart
1Status();
break;
case 2

Kart = QueryKart

case 3

Kart = QueryKart

2Status();
break;
3Status();
break;
default
:
break;
}
//query the current target position
CurrentTarget = QueryTarget();
//if the target is different from last target
if ((CurrentTarget.x != LastTarget.x) ||
(CurrentTarget.y != LastTarget.y))
{

//if our kart is close enought to the target pos


ition
if (abs(CurrentTarget.x-Kart.PositionX) <= TOL4P
OS)
{
//post an event ES_InPosition to master
machine
printf("InPosition!!!\n\r");
ES_Event ThisEvent;
ThisEvent.EventType = ES_InPosition;
PostToMasterSM(ThisEvent);
//update the target
LastTarget = CurrentTarget;
ReturnVal = true;
}
}
}
}
return ReturnVal;
}
/********************************************************************
FUNCTION Check4RPS
ARGUMENTS: no arguments
RETURN: true if RPS has reached target RPS
DESCRIPTION: check the RPS of shooting motor
********************************************************************/
bool Check4RPS(void)
{
bool ReturnVal = false;
static uint32_t LastState = 0;
uint32_t CurrentState;
//query the current RPS
CurrentState = QueryRPS();
//if RPS has not reached 64 before
if (LastState < 64)
{
//if RPS has changed
if (CurrentState != LastState)
{
//if RPS has reached or above 64
if (CurrentState >= 64)
{
//post an event ES_Shoot to Master Machi
ne
ES_Event ThisEvent;
ThisEvent.EventType = ES_Shoot;
PostToMasterSM(ThisEvent);
ReturnVal = true;
}
//update the RPS state
LastState = CurrentState;
}
}
return ReturnVal;
}

void SetisAligned(void)
{
isAligned = true;
}
void ResetisAligned(void)
{
isAligned = false;
}

Das könnte Ihnen auch gefallen