Sie sind auf Seite 1von 10

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

Module
TRex.c

Revision
1.0.1

Description
This is a file for implementing TRex flat state machines under the
Gen2 Events and Services Framework.

Notes

History
When Who What/Why
-------------- --- --------
01/15/12 11:12 jec revisions for Gen2 framework
11/07/11 11:26 jec made the queue static
10/30/11 17:59 jec fixed references to CurrentEvent in RunTemplateSM()
10/23/11 18:20 jec began conversion from SMTemplate.c (02/20/07 rev)
****************************************************************************/
/*----------------------------- 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 "TRex.h"
#include "PWM16Tiva.h"
// the headers to access the GPIO subsystem
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"

// the headers to access the TivaWare Library


#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"

/*----------------------------- Module Defines ----------------------------*/

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


/* prototypes for private functions for this machine.They should be functions
relevant to the behavior of this state machine
*/

void TRex_Init(void);

void TRex_Up(void);
void TRex_Down(void);
void TRex_Down_Attack(void);
void TRex_Stop(void);

/*---------------------------- Module Variables ---------------------------*/


// everybody needs a state variable, you may need others as well.
// type of state variable should match htat of enum in header file

// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
static uint8_t LastInput_LimitUp;
static uint8_t LastInput_LimitDown;
static uint8_t ResetEarly = 0;

static TRexState_t CurrentStateTRex;

#define PWM_DUTY_CYCLE 15
#define PWM_DUTY_CYCLE_ATTACK 40
#define PWM_FREQ 100

#define TREX_DOWN_TIME 1700

/*------------------------------ Module Code ------------------------------*/


/****************************************************************************
Function
InitTRex

Parameters
uint8_t : the priorty of this service

Returns
bool, false if error in initialization, true otherwise

Description
Saves away the priority, sets up the initial transition and does any
other required initialization for this state machine

****************************************************************************/
bool InitTRex(uint8_t Priority)
{
//printf("I got to InitMorseElement\r\n");
ES_Event ThisEvent;

MyPriority = Priority;

//Initialize the port lines (car PWMSs, button pins, and limit switches)
TRex_Init();

//sample the port line and use it to initialize the LastInput variables
LastInput_LimitUp = HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) & BIT6HI;
LastInput_LimitDown = HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) & BIT7HI;

// post the initial transition event


ThisEvent.EventType = ES_INIT;
CurrentStateTRex = InitializingTRex;

if (ES_PostToService(MyPriority, ThisEvent) == true)


{
return true;
}
else
{
return false;
}
}

/****************************************************************************
Function
PostTRex

Parameters
EF_Event ThisEvent , the event to post to the queue

Returns
boolean False if the Enqueue operation failed, True otherwise
Description
Posts an event to this state machine's queue

****************************************************************************/
bool PostTRex(ES_Event ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunTRex

Parameters
ES_Event : the event to process

Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise

Description
Controls movement of TRex panel

****************************************************************************/
ES_Event RunTRex(ES_Event ThisEvent)
{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
static TRexState_t NextStateTRex;
NextStateTRex = CurrentStateTRex;

switch (CurrentStateTRex)
{
case InitializingTRex:
{
if (ThisEvent.EventType == ES_INIT)
{
NextStateTRex = WaitingForPlacementTRex;
}
}
break;

case WaitingForPlacementTRex:
{
if (ThisEvent.EventType == CAR_PLACED)
{
// Drive TRex motor up
TRex_Up();

NextStateTRex = RisingTRex;
}
}
break;

case RisingTRex:
{
if ((ThisEvent.EventType == ATTACK) && (ThisEvent.EventParam == 1))
{
//start TRex timer to 1 second
ES_Timer_InitTimer(AttackTimer, TREX_DOWN_TIME);
//write motor drive down
TRex_Down();

NextStateTRex = LoweringTRex;
}
if (ThisEvent.EventType == LIMIT_UP)
{
//write motor to stop
TRex_Stop();

NextStateTRex = LimitHitEarlyTRex;
}

if (ThisEvent.EventType == EARLY_RESET)
{
TRex_Stop();
TRex_Down();
NextStateTRex = ResetFullyTRex;
ResetEarly = 1;
}

if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==


MainTimer))
{
//write motor to stop
TRex_Stop();
TRex_Down();

NextStateTRex = ResetFullyTRex;
}
}
break;

case LimitHitEarlyTRex:
{
if ((ThisEvent.EventType == ATTACK) && (ThisEvent.EventParam == 1))
{
//write motor down
TRex_Down_Attack();
//start Attack Timer
ES_Timer_InitTimer(AttackTimer, TREX_DOWN_TIME);

NextStateTRex = LoweringTRex;
}
if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
MainTimer))
{
//write motor down
TRex_Down();

NextStateTRex = ResetFullyTRex;
}

if (ThisEvent.EventType == EARLY_RESET)
{
TRex_Stop();
TRex_Down();
NextStateTRex = ResetFullyTRex;
ResetEarly = 1;
}
}
break;

case LoweringTRex:
{
if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
AttackTimer))
{
//write motor stop
TRex_Stop();

NextStateTRex = LoweringTRex;
}

if ((ThisEvent.EventType == ATTACK) && (ThisEvent.EventParam == 2))


{
//write motor down
TRex_Down_Attack();
//start AttackTimer
ES_Timer_InitTimer(AttackTimer, TREX_DOWN_TIME);

NextStateTRex = LoweringTRex;
}

if (ThisEvent.EventType == VICTORY)
{
//write motor down
TRex_Down();

NextStateTRex = ResetFullyTRex;
}

if (ThisEvent.EventType == EARLY_RESET)
{
TRex_Stop();
TRex_Down();
NextStateTRex = ResetFullyTRex;
ResetEarly = 1;
}
if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
MainTimer))
{
//write motor down
TRex_Stop();
TRex_Down();

NextStateTRex = ResetFullyTRex;
}
if (ThisEvent.EventType == LIMIT_DOWN)
{
//write motor stop
TRex_Stop();
NextStateTRex = WaitingForHeadphonesTRex;
}
}
break;

case ResetFullyTRex:
{
if (ThisEvent.EventType == LIMIT_DOWN)
{
//write motor stop
TRex_Stop();

NextStateTRex = WaitingForHeadphonesTRex;
}

if ((ThisEvent.EventType == HEADPHONES_RESET) || (ThisEvent.EventType ==


EARLY_RESET))
{
ResetEarly = 1;
NextStateTRex = ResetFullyTRex;
}
}
break;

case WaitingForHeadphonesTRex:
{
if ((ThisEvent.EventType == HEADPHONES_RESET) || (ResetEarly == 1))
{
NextStateTRex = WaitingForPlacementTRex;
ResetEarly = 0;
}
}
break;
}

CurrentStateTRex = NextStateTRex;
return ReturnEvent;
}

/***************************************************************************
private functions
***************************************************************************/

/****************************************************************************
Function
CheckLimitDown

Parameters
None

Returns
True if an event was posted

Description
Event checker for TRex hitting the limit switch at the bottom position
****************************************************************************/
bool CheckLimitDown(void)
{
bool ReturnVal = false;
uint8_t CurrentInput_LimitDown;
ES_Event ThisEvent;

// read current input of the limit switch state (HI for car reached to end, LO for
not yet reached end)
CurrentInput_LimitDown = HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) &
BIT6HI;

// checks if the state has changed from LO to HI


if ((CurrentInput_LimitDown != LastInput_LimitDown) && (CurrentInput_LimitDown))
{
ThisEvent.EventType = LIMIT_DOWN;
PostTRex(ThisEvent);

ReturnVal = true;
}

LastInput_LimitDown = CurrentInput_LimitDown;
return ReturnVal;
}

/****************************************************************************
Function
CheckLimitUp
Parameters
None

Returns
True if an event was posted

Description
Event checker for TRex hitting the limit switch at the up position

****************************************************************************/
bool CheckLimitUp(void)
{
bool ReturnVal = false;
uint8_t CurrentInput_LimitUp;
ES_Event ThisEvent;

// read current input of the limit switch state (HI for car reached home, LO for
not yet reached home)
CurrentInput_LimitUp = HWREG(GPIO_PORTD_BASE + (GPIO_O_DATA + ALL_BITS)) & BIT7HI;

// checks if the state has changed from LO to HI


if ((CurrentInput_LimitUp != LastInput_LimitUp) && (CurrentInput_LimitUp))
{
ThisEvent.EventType = LIMIT_UP;
PostTRex(ThisEvent);

ReturnVal = true;
}

LastInput_LimitUp = CurrentInput_LimitUp;
return ReturnVal;
}

/****************************************************************************
Function
TRex_Init

Parameters
None

Returns
none

Description
returns the current state of the Template state machine

****************************************************************************/
void TRex_Init(void)
{
if ((HWREG(SYSCTL_PRGPIO) & BIT1HI) != BIT1HI) // if haven't enabled the port
yet
{
// Set up port B by enabling the peripheral clock
HWREG(SYSCTL_RCGCGPIO) |= BIT1HI;

// Waiting for the peripheral to be ready


while ((HWREG(SYSCTL_PRGPIO) & BIT1HI) != BIT1HI)
{
;
}
}

if ((HWREG(SYSCTL_PRGPIO) & BIT3HI) != BIT3HI) // if haven't enabled the port


yet
{
// Set up port D by enabling the peripheral clock
HWREG(SYSCTL_RCGCGPIO) |= BIT3HI;

// Waiting for the peripheral to be ready


while ((HWREG(SYSCTL_PRGPIO) & BIT3HI) != BIT3HI)
{
;
}
}

// Setting PD6 to digital input


HWREG(GPIO_PORTD_BASE + GPIO_O_DEN) |= (BIT6HI);
HWREG(GPIO_PORTD_BASE + GPIO_O_DIR) &= (BIT6LO);

// Setting PD7 to digital input


HWREG(GPIO_PORTD_BASE + GPIO_O_DEN) |= (BIT7HI);
HWREG(GPIO_PORTD_BASE + GPIO_O_DIR) &= (BIT7LO);

return;
}

/****************************************************************************
Function
TRex_Up

Parameters
None

Returns
none

Description
Drives TRex_Up

****************************************************************************/
void TRex_Up(void)
{
uint8_t dutyCycle_PWM0 = PWM_DUTY_CYCLE;
uint8_t channel_PWM0 = 2;
uint8_t dutyCycle_PWM1 = 0;
uint8_t channel_PWM1 = 3;
uint16_t reqFreq = PWM_FREQ;
uint8_t group = 0;

// set duty cycle of the motor to drive car forward


PWM_TIVA_SetDuty( dutyCycle_PWM0, channel_PWM0);
PWM_TIVA_SetDuty( dutyCycle_PWM1, channel_PWM1);

// set frequency of motor


PWM_TIVA_SetFreq(reqFreq, group);
}

/****************************************************************************
Function
TRex_Down

Parameters
None

Returns
none

Description
Drives TRex down

****************************************************************************/
void TRex_Down(void)
{
uint8_t dutyCycle_PWM1 = PWM_DUTY_CYCLE;
uint8_t channel_PWM1 = 3;
uint8_t dutyCycle_PWM0 = 0;
uint8_t channel_PWM0 = 2;
uint16_t reqFreq = PWM_FREQ;
uint8_t group = 0;

// set duty cycle of the motor to drive car reverse


PWM_TIVA_SetDuty( dutyCycle_PWM0, channel_PWM0);
PWM_TIVA_SetDuty( dutyCycle_PWM1, channel_PWM1);

//set frequency of motor


PWM_TIVA_SetFreq(reqFreq, group);
}

/****************************************************************************
Function
TRex_Down

Parameters
None

Returns
none

Description
Drives TRex down during Attack sequence

****************************************************************************/
void TRex_Down_Attack(void)
{
uint8_t dutyCycle_PWM1 = PWM_DUTY_CYCLE_ATTACK;
uint8_t channel_PWM1 = 3;
uint8_t dutyCycle_PWM0 = 0;
uint8_t channel_PWM0 = 2;
uint16_t reqFreq = PWM_FREQ;
uint8_t group = 0;

// set duty cycle of the motor to drive car reverse


PWM_TIVA_SetDuty( dutyCycle_PWM0, channel_PWM0);
PWM_TIVA_SetDuty( dutyCycle_PWM1, channel_PWM1);

//set frequency of motor


PWM_TIVA_SetFreq(reqFreq, group);
}

/****************************************************************************
Function
TRex_Stop

Parameters
None

Returns
none

Description
Stops TRex
****************************************************************************/
void TRex_Stop(void)
{
uint8_t dutyCycle_PWM1 = 0;
uint8_t channel_PWM1 = 2;
uint8_t dutyCycle_PWM0 = 0;
uint8_t channel_PWM0 = 3;
uint16_t reqFreq = 400;
uint8_t group = 0;

// set duty cycle of the motor to stop motor


PWM_TIVA_SetDuty( dutyCycle_PWM0, channel_PWM0);
PWM_TIVA_SetDuty( dutyCycle_PWM1, channel_PWM1);

//set frequency of motor


PWM_TIVA_SetFreq(reqFreq, group);
}

Das könnte Ihnen auch gefallen