Вы находитесь на странице: 1из 5

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

Module
TimingMotor.c
Description
This is the service that controls the behavior of both the timing motor and
the vibration motor
Notes
History
When
Who
What/Why
-------------- ---------11/12/16 02:03 mwm
created it for ME218A project
****************************************************************************/
/*----------------------------- 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 "TimingMotor.h"
#include "PWM10Tiva.h"
#include "BITDEFS.H"
#include "DEFINITIONS.H"
/*----------------------------- Module Defines ----------------------------*/
#define NUM_MOTOR 1
#define MIN_MOT_POS 600
#define MAX_MOT_POS 2985
#define MOT_FREQ 25000
#define INCREMENT 60
#ifndef ALL_BITS
#define ALL_BITS (0xff<<2)
#endif
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine.They should be functions
relevant to the behavior of this state machine
*/
/*---------------------------- 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
static TimingMotorState_t CurrentState;
// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
static uint16_t PulseWidth;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitTimingMotor
Parameters
uint8_t : the priority 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
Notes
Author
Matthew Miller, 11/12/16
****************************************************************************/
bool InitTimingMotor ( uint8_t Priority )
{
ES_Event ThisEvent;
MyPriority = Priority;
// initialize PWM port
PWM_TIVA_Init(NUM_MOTOR);
// initialize period of the timing motor
PWM_TIVA_SetPeriod(MOT_FREQ, TIME_MOT_GROUP);
// put us into the Initial PseudoState
CurrentState = InitTM;
// Enable the pin for the vibration motor
// Enable Port F
HWREG(SYSCTL_RCGCGPIO) |= GPIO_PIN_5;
// Make sure the peripheral clock has been set up
while((HWREG(SYSCTL_PRGPIO) & BIT5HI) != BIT5HI)
;
// Make PF4 digital and output
HWREG(GPIO_PORTF_BASE+GPIO_O_DEN) |= (GPIO_PIN_4);
HWREG(GPIO_PORTF_BASE+GPIO_O_DIR) |= (GPIO_PIN_4);
printf("\rHardware Initialized\r\n");
// post the initial transition event
ThisEvent.EventType = ES_INIT;
if (ES_PostToService( MyPriority, ThisEvent) == true)
{
return true;
}else
{
return false;
}
}
/****************************************************************************
Function
PostTimingMotor
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
Notes
Author
Matthew Miller, 11/12/16

****************************************************************************/
bool PostTimingMotor( ES_Event ThisEvent )
{
return ES_PostToService( MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunTimingMotorSM
Parameters
ES_Event : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here
Notes
uses nested switch/case to implement the machine.
Author
Matthew Miller
11/12/16
****************************************************************************/
ES_Event RunTimingMotorSM( ES_Event ThisEvent )
{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
switch ( CurrentState )
{
case InitTM :
// If current state is initial Psedudo State
if ( ThisEvent.EventType == ES_INIT )// only respond to ES_Init
{
// Put motor into initial position
PWM_TIVA_SetPulseWidth(MAX_MOT_POS,
TIMING_CHANNEL);
// Put Vibration Motor initially off
ChangeVibrationMotor(VIB_MOT_OFF);
// Set CurrentSTate to WaitForStart
CurrentState = WaitForStart;

}
break;

case WaitForStart : // If CurrentState is WaitForStart


if( ThisEvent.EventType == WAITING) // If ThisEvent is WAITING
{
// Set the position of the motor to the max position
PWM_TIVA_SetPulseWidth(MAX_MOT_POS, TIMING_CHANNEL);

// Set the timing motor timer for one second


ES_Timer_InitTimer(TIMING_MOTOR_TIMER, ONE_SEC);

if( ThisEvent.EventType == RESET) // If ThisEvent is RESET


{
// Set PulseWidth to initial value of pulse width
PulseWidth = MAX_MOT_POS;
// Set the motor to PulseWidth
PWM_TIVA_SetPulseWidth(MAX_MOT_POS, TIMING_CHANNEL);

// Stop Current Timer


ES_Timer_StopTimer(TIMING_MOTOR_TIMER);
// Set CurrentState to Timing
CurrentState = Timing;
}
// If ThisEvent is ES_Timeout and ThisEvent Param is
TIMING_MOTOR_TIMER

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


== TIMING_MOTOR_TIMER)
{
// set variable for checking which direction to set
motor
static uint8_t reverse = true;
if(reverse == true)
{
// Set motor position to minimum
PWM_TIVA_SetPulseWidth(MIN_MOT_POS,
TIMING_CHANNEL);
// restart timer
ES_Timer_InitTimer(TIMING_MOTOR_TIMER, ONE_SEC);
// set reverse to false
reverse = false;
}else{

// Set motor position to maximum


PWM_TIVA_SetPulseWidth(MAX_MOT_POS,

TIMING_CHANNEL);

// restart timer
ES_Timer_InitTimer(TIMING_MOTOR_TIMER, ONE_SEC);
// set reverse to true
reverse = true;
}

break;
case Timing:

// If current state is

Timing

if( ThisEvent.EventType == START || (ThisEvent.EventType ==


ES_TIMEOUT && ThisEvent.EventParam == TIMING_MOTOR_TIMER ))
{
// if the motor hasn't reached the max position
if(PulseWidth > MIN_MOT_POS){
// Set pulse width to current value of PulseWidth
PWM_TIVA_SetPulseWidth(PulseWidth,
TIMING_CHANNEL);
// Start timer for one second
ES_Timer_InitTimer(TIMING_MOTOR_TIMER, ONE_SEC);
// Decrement PulseWidth by increment constant
PulseWidth -= INCREMENT;

}else{

// Set pulse width to maximum


PulseWidth = MAX_MOT_POS;
// Set pulse width of motor
PWM_TIVA_SetPulseWidth(PulseWidth,

TIMING_CHANNEL);

// Set current state to Replay


CurrentState = Replay;

break;
is Timing

}
case Replay:

// If current state

if (ThisEvent.EventType == WAITING) // If ThisEvent is WAITING


{
// Set current state to WaitForStart
CurrentState = WaitForStart;
// Post WAITING Event to this module
ES_Event PostEvent;
PostEvent.EventType = WAITING;
PostTimingMotor(PostEvent);
}

default :
;
}
return ReturnEvent;

// end switch on Current State

/****************************************************************************
Function
ChangeVibrationMotor
Parameters
integer that represents whether to turn the motor on or off
Returns
TNone
Description
Either turns the Vibration Motor ON or OFF
Notes
Author
Matthew Miller 11/18/2016
****************************************************************************/
void ChangeVibrationMotor(uint8_t change)
{
if(change == VIB_MOT_ON) // If change is vibration motor on
{
// Set the vibration pin HI
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA + ALL_BITS)) |= BIT4HI;
}
if(change == VIB_MOT_OFF) // If change is vibration motor off
{
// Set the vibration pin LO
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA + ALL_BITS)) &= BIT4LO;
}
}

Вам также может понравиться