You are on page 1of 4

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

Module
TeraMotorService.c
Revision
1.0.1
Description
This is the service to control the movement of terraforming motor
Notes
History
When Who
--------------------
****************************************************************************/
/*----------------------------- 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 "ES_Port.h"
#include "ES_Events.h"
#include "ES_Timers.h"
#include "termio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h" // Define PART_TM4C123GH6PM in project
#include "driverlib/gpio.h"
#include "PWM16Tiva.h"
#include "TeraMotorService.h"
#include "Knob.h"
#include "Motor.h"
#include <math.h>
/*----------------------------- Module Defines ----------------------------*/
/* prototypes for private functions for this service.They should be functions
relevant to the behavior of this service
*/
#define ONE_SEC 976
#define TWENTY_SEC (20*ONE_SEC)
#define THIRTY_SEC (30*ONE_SEC)
#define FIFTEEN_SEC (15*ONE_SEC)
#define KNOB_THRES 0.03
/*---------------------------- Module Variables ---------------------------*/
// with the introduction of Gen2, we need a module level Priority variable
static uint8_t MyPriority;
static float LastKnobValue;
static float CurrentKnobValue;
static TeraMotorState_t CurrentState = TeraInitPState;

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


/****************************************************************************
Function
InitTeraMotor
Parameters
uint8_t : the priorty of this service
return: bool true or false
****************************************************************************/
bool InitTeraMotor ( uint8_t Priority ){
ES_Event ThisEvent;
//Initialize MyPriority with passed in parameter
MyPriority = Priority;
//init output pins for all the motors
InitMotor();
//Init Knob for event checking
InitKnob();
//get last knob state for event checking
LastKnobValue = GetKnobValue();

//Post the initial transition event to state machine


ThisEvent.EventType = ES_INIT;
if(ES_PostToService(MyPriority, ThisEvent) == true){
return true;
}else{
return false;
}
}

/****************************************************************************
Function
PostTeraMotor
Parameters
ES_Event ThisEvent ,the event to post to the queue
Returns
bool false if the Enqueue operation failed, true otherwise
Description
Posts an event to this state machine's queue
****************************************************************************/
bool PostTeraMotor(ES_Event ThisEvent ){
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
Check4Knob
Parameters
ES_Event ThisEvent, the event to post to the queue
Returns
bool false if the Enqueue operation failed, true otherwise
Description
Posts an event to this state machine's queue
****************************************************************************/
bool Check4Knob(void){
//assume no event
bool ReturnVal = false;
//get current knob state
CurrentKnobValue = GetKnobValue();
//if there is an event
if(fabs(CurrentKnobValue - LastKnobValue) > KNOB_THRES){
ES_Event ThisEvent;
ThisEvent.EventType = ES_KNOB_TURNED;
//post the event to TeraMotorService, LEDService and PopUpMotorService
ES_PostList01(ThisEvent);
//set ReturnVal to true
ReturnVal = true;
//set LastKnobState to CurrentKnobState
LastKnobValue = CurrentKnobValue;
}
//return ReturnVal
return ReturnVal;
}

/****************************************************************************
Function
RunTeraMotor
Parameters
ES_Event : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here
****************************************************************************/
ES_Event RunTeraMotor( ES_Event ThisEvent ){
ES_Event ReturnEvent;
//assume no errors
ReturnEvent.EventType = ES_NO_EVENT;
//set NextState to CurrentState
TeraMotorState_t NextState = CurrentState;

//CurrentState can be one of: TeraInitPState, TeraWaitingState, and


TeraWorkingState
switch (CurrentState) {
//if CurrentState is TeraInitPState
case TeraInitPState:
//if this event is ES_INIT
if(ThisEvent.EventType == ES_INIT){
//set NextState to TeraWaitingState
NextState = TeraWaitingState;
}
break;
//if CurrentState is TeraWaitingState
case TeraWaitingState:
//if this event is ES_LEVER_PULLED
if(ThisEvent.EventType == ES_LEVER_PULLED){
//set NextState to TeraWorkingState
NextState = TeraWorkingState;
//init TERA_TIMER to be 30s
ES_Timer_InitTimer(TERA_TIMER, THIRTY_SEC);
}
//if this event is ES_TIMEOUT from TERA_TIMER
if(ThisEvent.EventType == ES_TIMEOUT && ThisEvent.EventParam == TERA_TIMER)
{
//reset TeraMotor position
SetTeraMotor(0);
//set NextState to TeraWaitState
NextState = TeraWaitingState;
}
break;
//if CurrentState is TeraWorkingState
case TeraWorkingState:
//If this is ES_TIMEOUT from TERA_TIMER
if((ThisEvent.EventType == ES_TIMEOUT) &&(ThisEvent.EventParam ==
TERA_TIMER)){
//reset TeraMotor position
SetTeraMotor(0);
//set NextState to TeraWaitState
NextState = TeraWaitingState;
}
//if this event is ES_KNOB_TURNED
if(ThisEvent.EventType == ES_KNOB_TURNED){
//set TeraMotor position according to CurrentKnobValue
SetTeraMotor(CurrentKnobValue);
//set NextState to TeraWorkingState
NextState = TeraWorkingState;
//init TERA_TIMER to be 30s
ES_Timer_InitTimer(TERA_TIMER, THIRTY_SEC);
}
//if this event is ES_BUTTON1_PRESSED
if(ThisEvent.EventType == ES_BUTTON1_PRESSED){
//set NextState to TeraWaitingState
NextState = TeraWaitingState;
}
break;
}
//update CurrentState
CurrentState = NextState;
//return ReturnEvent
return ReturnEvent;
}

/*------------------------------- Footnotes -------------------------------*/


/*------------------------------ End of file ------------------------------*/

You might also like