Beruflich Dokumente
Kultur Dokumente
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"
void TRex_Init(void);
void TRex_Up(void);
void TRex_Down(void);
void TRex_Down_Attack(void);
void TRex_Stop(void);
// 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;
#define PWM_DUTY_CYCLE 15
#define PWM_DUTY_CYCLE_ATTACK 40
#define PWM_FREQ 100
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;
/****************************************************************************
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;
}
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;
}
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;
}
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;
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;
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;
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;
/****************************************************************************
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;
/****************************************************************************
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;
/****************************************************************************
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;