You are on page 1of 6

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

Module
MasterMachine.c
Revision
1.0
Description
This is the MasterMachine for Team 9 ME218b final project.
Notes
History
When
Who
What/Why
----------------------02/08/2015 18:35 Sky
Initial version
****************************************************************************/
//#define testIR
#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
#include

"MasterMachine.h"
"GameStateMachine.h"
"SPIStateMachine.h"
"PWM4Wheel.h"
"WheelRPMControl.h"
"Motion.h"
"PWMShoot.h"
"InputCaptureShoot.h"
"RaceStateMachine.h"
"EventCheckers.h"

#ifndef ALL_BITS
#define ALL_BITS (0xff<<2)
#endif
/*********************************Module Variables******************************
*******/
static uint8_t MyPriority;
static uint8_t self;
static bool isStart = false;
/********************************Private Function Prototypes********************
*******/
static ES_Event DuringMaster(ES_Event);
/*********************************Private Functions*****************************
*******/
/*********************************************************

FUNCTION DuringMaster
ARGUMENT: ES_Event Event
RETURN: ES_Event
DESCRIPTION: deal with events, pass the events down to
the lower level state machine
*********************************************************/
static ES_Event DuringMaster(ES_Event Event)
{
volatile ES_Event LocalEvent = Event;
if ((LocalEvent.EventType == ES_ENTRY)||
(LocalEvent.EventType == ES_ENTRY_HISTORY))
{
//start lower level state machines
isStart = true;
//game indicator
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS)) |= BIT1HI;
if (LocalEvent.EventType == ES_ENTRY_HISTORY)
{
ResetShootStatus();
ResetisAligned();
}
SetSelf();
StartGameSM(LocalEvent);
}
else if (LocalEvent.EventType == ES_EXIT)
{
//give the lower level state machines a chance to clean up first
if (LocalEvent.EventParam == 0)
{
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS)) &= BIT1LO;
printf("PullLO!!!\n\r");
}
RunGameSM(LocalEvent);
isStart = false;
Stop();
}
else
{
//run lower level state machines
if (isStart)
RunGameSM(LocalEvent);
}
return LocalEvent;
}
/*********************************Public Functions******************************
*******/
/*********************************************************
FUNCTION InitMasterSM
ARGUMENT: an integer represents the priority
RETURN: ES_Event
DESCRIPTION: do initializations
*********************************************************/
bool InitMasterSM(uint8_t priority)
{
//set priority
MyPriority = priority;
//Initialize pins, PWM, Interrupt here
InitPWMDemo();

//Motor Control;
InitMotorControlPin();
InitInputCapturePeriodLeft();
InitMatchStopLeft();
InitInputCapturePeriodRight();
InitMatchStopRight();
InitLeftRPMControlInt();
InitRightRPMControlInt();
InitMotorControlPin();
//Shooting Motor & servo
InitInputCaptureShoot();
InitMatchStopShoot();
InitServoPWM();
InitShootPWM();
//Init IR
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R2;
HWREG(GPIO_PORTC_BASE+GPIO_O_DEN) |= (BIT7HI);
HWREG(GPIO_PORTC_BASE+GPIO_O_DIR) &= (BIT7LO);
//Init Switch PF2: Kart1, PF3: Kart2, PF4: Kart3
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R5;
HWREG(GPIO_PORTF_BASE+GPIO_O_DEN) |= (BIT2HI | BIT3HI | BIT4HI);
HWREG(GPIO_PORTF_BASE+GPIO_O_DIR) &= (BIT2LO | BIT3LO | BIT4LO);
//internal pull-up for switch pins
HWREG(GPIO_PORTF_BASE+GPIO_O_PUR) |= (BIT2HI | BIT3HI | BIT4HI);
//game indicator
HWREG(GPIO_PORTF_BASE+GPIO_O_DEN) |= BIT1HI;
HWREG(GPIO_PORTF_BASE+GPIO_O_DIR) |= BIT1HI;
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS)) &= BIT1LO;
Stop();
return true;
}
/*********************************************************
FUNCTION PostToMasterSM
ARGUMENT: ES_Event ThisEvent
RETURN: true if successfully post the event
DESCRIPTION: post the event to master state machine
*********************************************************/
bool PostToMasterSM(ES_Event ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/*********************************************************
FUNCTION StartMasterSM
ARGUMENT: ES_Event CurrentEvent
RETURN: ES_Event
DESCRIPTION: start master state machine
*********************************************************/
void StartMasterSM(ES_Event CurrentEvent)
{
volatile ES_Event LocalEvent;
//set LocalEvent equal to CurrentEvent
LocalEvent = CurrentEvent;
Stop();

//game indicator
printf("PullHI!!!\n\r");
//call RunMasterSM with the parameter of LocalEvent
RunMasterSM(LocalEvent);
}
/*********************************************************
FUNCTION RunMasterSM
ARGUMENT: ES_Event CurrentEvent
RETURN: ES_Event
DESCRIPTION: the run function of master state machine
*********************************************************/
ES_Event RunMasterSM(ES_Event CurrentEvent)
{
//call DuringMaster to deal with the event
CurrentEvent = DuringMaster(CurrentEvent);
//don't need to do other things in this state machine
CurrentEvent.EventType = ES_NO_EVENT;
return CurrentEvent;
}
/*********************************************************
FUNCTION QuerySelf
ARGUMENT: no argument
RETURN: an integer represents the number of self
DESCRIPTION: start master state machine
*********************************************************/
uint8_t QuerySelf(void)
{
return self;
}
/*********************************************************
FUNCTION SetSelf
ARGUMENT: no argument
RETURN: no return
DESCRIPTION: set the self number based on the self identification
switch
*********************************************************/
void SetSelf(void)
{
//check switch pin , set self Num
uint8_t CurrentSelf;
CurrentSelf = HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS)) & (BIT2HI |
BIT3HI | BIT4HI);
switch (CurrentSelf)
{
case 24 :
self = 3;
printf("3!!!!!!\n\r");
break;
case 20 : self = 2;
printf("2!!!!!!!\n\r");
break;
case 12 : self = 1;
printf("1!!!!!!\n\r");
break;
default : break;
}
}

#define clrScrn()
#define goHome()
#define clrLine()

printf("\x1b[2J")
printf("\x1b[1,1H")
printf("\x1b[K")

#define ALL_BITS (0xff<<2)


#ifdef testIR
int main(void)
{
// Set the clock to run at 40MhZ using the PLL and 16MHz external crysta
l
SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN
| SYSCTL_XTAL_16MHZ);
TERMIO_Init();
clrScrn();
ES_Return_t ErrorType;
InitPWMDemo();
//Motor Control;
InitMotorControlPin();
InitInputCapturePeriodLeft();
InitMatchStopLeft();
InitInputCapturePeriodRight();
InitMatchStopRight();
InitLeftRPMControlInt();
InitRightRPMControlInt();
InitMotorControlPin();
//Shooting Motor & servo
InitInputCaptureShoot();
InitMatchStopShoot();
InitServoPWM();
InitShootPWM();
//Init IR
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R2;
HWREG(GPIO_PORTC_BASE+GPIO_O_DEN) |= (BIT7HI);
HWREG(GPIO_PORTC_BASE+GPIO_O_DIR) &= (BIT7LO);
static uint8_t LastState = 0;
uint8_t CurrentState = 0;
RotateLeft();
while (1)
{
CurrentState = HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) & B
IT7HI;
if (CurrentState != LastState)
{
ES_Event ThisEvent;
ThisEvent.EventType = ES_BucketAligned;
printf("BucketAligned!\n\r");
//PostToMasterSM(ThisEvent);
LastState = CurrentState;
SetTarget(60);
}
//for (long i = 0; i <= 10000000; i++);
}
return 0;

}
#endif