Você está na página 1de 6

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

Module
ShootStateMachine.c
Revision
1.0
Description
This is the ShootStateMachine for Team 9 ME218b final project. It is a substate machine of GameStateMachine.
Notes
History
When
Who
What/Why
----------------------02/11/2015 09:41
Sky
Added servo control
02/09/2015
14:00 Sky
Initial version
****************************************************************************/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include
#include
#include
#include
#include
#include
#include

"inc/hw_memmap.h"
"inc/hw_types.h"
"inc/hw_gpio.h"
"inc/hw_sysctl.h"
"driverlib/sysctl.h"
"driverlib/pin_map.h" // Define PART_TM4C123GH6PM in project
"driverlib/gpio.h"

#include
#include
#include
#include
#include
#include
#include
#include
#include

"MasterMachine.h"
"GameStateMachine.h"
"ShootStateMachine.h"
"Motion.h"
"Scan.h"
"Servo.h"
"InputCaptureShoot.h"
"PWMShoot.h"
"EventCheckers.h"

#define
#define
#define
#define
#define

SPEEDLV1
SPEEDLV2
SPEEDLV3
SHOOT 0
OBSTACLE

0
1
2
1

/**********************************Module Variables*****************************
*/
static ShootState CurrentState, HistoryState;
/**************************Private Function Prototypes**************************
*/
static ES_Event DuringToShoot(ES_Event);
static ES_Event DuringRegion10(ES_Event);
/*******************************Public Functions********************************
*/

/**************************************************
FUNCTION RunShootSM
ARGUMENT: ES_Event CurrentEvent
RETURN: ES_Event
DESCRIPTION: Run function for Shoot state machine
**************************************************/
ES_Event RunShootSM(ES_Event CurrentEvent)
{
ShootState NextState;
ES_Event LocalEvent;
bool MakeTransition;
ES_Event ReturnEvent;
MakeTransition = false;
NextState = CurrentState;
ReturnEvent = CurrentEvent;
//switch currentstate
switch (CurrentState)
{
case Shoot_ToShoot

{
//pass down

the event to any lower level state machine


LocalEvent = DuringToShoot(CurrentEvent);
if (ES_NO_EVENT != LocalEvent.EventType) // if there is still an active event
{
switch (LocalEvent.EventType)
{
case ES_TIMEOUT

if (LocalEvent.EventParam == Timer_RotateRightShoot)
{
Stop();
ResetisAligned();
//set MakeTransition to true
MakeTransition = true;
//set NextState to Shoot_Region10
NextState = Shoot_Region10;
ReturnEvent.EventType = ES_NO_EVENT;
}
break;
}

default
: break;
}
}
break;
}
case Shoot_Region10

{
//pass down

the event to any lower level state machine


LocalEvent = DuringRegion10(CurrentEvent);
if (ES_NO_EVENT != LocalEvent.EventType) // if there is still an active event
{
switch (LocalEvent.EventType)
{
case ES_BucketAligned

//stop rotation
//printf("Aligned!\n\r");
SetisAligned();
Stop();
ReturnEvent.EventType = ES_NO_EVENT;
SetTargetRPMShoot(68);
//ES_Event ThisEvent;
//ThisEvent.EventType = ES_Shoot;
//PostToMasterSM(ThisEvent);
break;
}
case ES_Shoot
//control the servo to shoot
printf("Shoot!\n\r");
SetServo(1);
ES_Timer_InitTimer(Timer_ShootDelay, 1000);
ReturnEvent.EventType = ES_NO_EVENT;

break;
}
case ES_TIMEOUT

if (LocalEvent.EventParam == Timer_ShootDelay)
{
ReturnEvent.EventType = ES_NO_EVENT;
ES_Event ThisEvent;
ThisEvent.EventType = ES_ToRace;
ThisEvent.EventParam = SHOOT;
PostToMasterSM(ThisEvent);
}
break;
}
default
: break;
}
}
break;
}
}
//if we are making transition
if (MakeTransition)
{
//execute exit function for current state
LocalEvent.EventType = ES_EXIT;
RunShootSM(LocalEvent);
//execute entry function for the next state
CurrentState = NextState;
LocalEvent.EventType = ES_ENTRY;
RunShootSM(LocalEvent);
}
return ReturnEvent;
}
/**************************************************
FUNCTION StartShootSM
ARGUMENT: ES_Event CurrentEvent
RETURN: no return
DESCRIPTION: start shoot state machine
**************************************************/

void StartShootSM(ES_Event CurrentEvent)


{
ES_Event LocalEvent;
LocalEvent = CurrentEvent;
if (LocalEvent.EventType == ES_ENTRY)
{
printf("StartShootSM!\n\r");
CurrentState = Shoot_ToShoot;
HistoryState = CurrentState;
RunShootSM(LocalEvent);
}
else if (LocalEvent.EventType == ES_ENTRY_HISTORY)
{
CurrentState = HistoryState;
RunShootSM(LocalEvent);
}
}
/***************************************Private Functions***********************
*************/
/**************************************************
FUNCTION DuringToShoot
ARGUMENT: ES_Event CurrentEvent
RETURN: ES_Event
DESCRIPTION: during function for Shoot_ToShoot state
**************************************************/
static ES_Event DuringToShoot(ES_Event CurrentEvent)
{
ES_Event LocalEvent;
LocalEvent = CurrentEvent;
if ((LocalEvent.EventType == ES_ENTRY)||
(LocalEvent.EventType == ES_ENTRY_HISTORY))
{
//stop and rotate right
Stop();
RotateRight();
ES_Timer_InitTimer(Timer_RotateRightShoot, 1000);
}
else if (LocalEvent.EventType == ES_EXIT)
{
HistoryState = CurrentState;
}
else
{
//nothing to do here
}
return LocalEvent;
}
/**************************************************
FUNCTION DuringRegion10
ARGUMENT: ES_Event CurrentEvent
RETURN: ES_Event
DESCRIPTION: during function for Shoot_Region10 state
**************************************************/
static ES_Event DuringRegion10(ES_Event CurrentEvent)
{
ES_Event LocalEvent;

LocalEvent = CurrentEvent;
if ((LocalEvent.EventType == ES_ENTRY)||
(LocalEvent.EventType == ES_ENTRY_HISTORY))
{
printf("RotateLeft!\n\r");
//start rotate to align with the bucket
RotateLeft();
//start shooting motor
}
else if (LocalEvent.EventType == ES_EXIT)
{
HistoryState = CurrentState;
//stop shooting motor
SetTargetRPMShoot(0);
//set servo to middle position
SetServo(0);
}
else
{
//nothing to do here
}
return LocalEvent;
}

Você também pode gostar