Vous êtes sur la page 1sur 5

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

Module
RockeMotorService.c
Revision
1.0.1
Description
This is a service that controls the movement of rocket, which records the
passage of time.
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 "RocketMotorService.h"
#include "Lever.h"
#include "Motor.h"
/*----------------------------- Module Defines ----------------------------*/
#ifndef
#define ALL_BITS (0xff<<2)
#endif
// these times assume a 1.000mS/tick timing
#define ONE_SEC 976
#define HALF_SEC (ONE_SEC*0.5)
#define TEN_SEC (10*ONE_SEC)
#define TWENTY_SEC (20*ONE_SEC)
#define THIRTY_SEC (30*ONE_SEC)
#define FIFTEEN_SEC (15*ONE_SEC)
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this service.They should be functions
relevant to the behavior of this service
*/
void InitRocketDir(void);
void SetRocketDir(uint8_t Dir);
void SetSpeed(float speed);
//void SetBackwardSpeed(void);
void StopRocketMotor(void);
/*---------------------------- Module Variables ---------------------------*/
// with the introduction of Gen2, we need a module level Priority variable
static uint8_t MyPriority;
static bool LastLeverState;
static RocketMotorState_t CurrentState = RocketInitPState;
static uint8_t RocketMotor = 4;
static uint8_t Dir;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitRocketMotor
Parameters
uint8_t : the priorty of this service
****************************************************************************/
bool InitRocketMotor ( uint8_t Priority ){
//create new event called ThisEvent
ES_Event ThisEvent;
//Initialize MyPriority with passed in parameter
MyPriority = Priority;
//initialize control pins for all the motors
InitMotor();
//Init rocket motor direction
InitRocketDir();
//Init Lever for event checking
InitLever();
//get last lever state for event checking
LastLeverState = GetLeverState();
//post the inital transition event
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true){
return true;
}else{
return false;
}
}

/****************************************************************************
Function
PostRocketMotor
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 PostRocketMotor( ES_Event ThisEvent ){
//return an ES_PostToService Event to with the priority and event
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
Check4Lever
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 Check4Lever(void){
//assume no event
bool ReturnVal = false;
//get current lever state
bool CurrentLeverState = GetLeverState();
//check if there is an event
//if there is a new event
if(CurrentLeverState != LastLeverState){
ES_Event ThisEvent;
ThisEvent.EventType = ES_LEVER_PULLED;
//post the event to RocketMotorService, LEDService and TeraMotorService
ES_PostList00(ThisEvent);
//set ReturnVal to true
ReturnVal = true;
}
//update last lever state with current lever state
LastLeverState = CurrentLeverState;
//return ReturnVal
return ReturnVal;
}

/****************************************************************************
Function
RunRocketMotor
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 RunRocketMotor( ES_Event ThisEvent ){
//create ReturnEvent
ES_Event ReturnEvent;
//assume no errors
ReturnEvent.EventType = ES_NO_EVENT;
//set NextState of rocket motor to CurrentState
RocketMotorState_t NextState = CurrentState;
//CurrentState of rocket motor can be one of: RocketInitPState,
RocektForwardState1, RocketForwardState2,
//RocketBackState1, RocketBackState2 and RocketWaitingState
switch (CurrentState) {
//if CurrentState is RocketInitPState
case RocketInitPState:
//if ThisEvent is ES_Init
if (ThisEvent.EventType == ES_INIT) {
//set NextState to RocketForwardState1
NextState = RocketForwardState1;
//init ROCKET_TIMER to be 30s
ES_Timer_InitTimer(ROCKET_TIMER, THIRTY_SEC);
}
break;
//if CurrentState is RocketForwardState1
case RocketForwardState1:
//if this a ROCKET_TIMER timeout event, which means the user never
interacts with the Lever,
if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
ROCKET_TIMER)){
//we stay in this state waiting for new event
NextState = RocketForwardState1;
//restart ROCKET_TIMER
ES_Timer_InitTimer(ROCKET_TIMER, THIRTY_SEC);
}
//if this event is ES_LEVER_PULLED
if(ThisEvent.EventType == ES_LEVER_PULLED){
//set the rocket motor direction
Dir = 0;
SetRocketDir(Dir);
//set rocket motor speed to be high to tick it on
SetSpeed(0.55);
//init ROCKETPULSE_TIMER to be 0.5s
ES_Timer_InitTimer(ROCKETPULSE_TIMER, HALF_SEC);
//set NextState to RocketForwardState2
NextState = RocketForwardState2;
}
break;
//if CurrentState is RocketForwardState2
case RocketForwardState2:
//if this is a ROCKETPULSE_TIMER timeout event
if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
ROCKETPULSE_TIMER)){
//set rocket motor speed to be low to move forward for the rest of
distance
SetSpeed(0.4);
//set NextState to RocketBackState1
NextState = RocketBackState1;
//init ROCKETMOVE_TIMER to be 20s
ES_Timer_InitTimer(ROCKETMOVE_TIMER, TWENTY_SEC);
}
break;
//if CurrentState is RocketBackState1
case RocketBackState1:
//if this is a ROCKETMOVE_TIMER timeout event
if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
ROCKETMOVE_TIMER)){
//stop rocket motor
StopRocketMotor();
//set rocket motor direction to the opposite
Dir = 1;
SetRocketDir(Dir);
//set rocket motor speed to be high to tick it on
SetSpeed(0.55);
//init ROCKETPULSE_TIMER to be 0.5s
ES_Timer_InitTimer(ROCKETPULSE_TIMER, HALF_SEC);
//set NextState to be RocketBackState2
NextState = RocketBackState2;
}
break;
//if CurrentState is RocketBackState2
case RocketBackState2:
//if this is a ROCKETPULSE_TIMER timeout event
if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
ROCKETPULSE_TIMER)){
//set rocket motor speed low to finish the rest of distance
SetSpeed(0.41);
//set NextState to RocketWaitingState
NextState = RocketWaitingState;
//init ROCKETMOVE_TIMER to be 20s
ES_Timer_InitTimer(ROCKETMOVE_TIMER, TWENTY_SEC);
}
break;
//if CurrentState is RocketWaitingState
case RocketWaitingState:
//if this is a ROCKETMOVE_TIMER timeout event
if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
ROCKETMOVE_TIMER)){
//stop rocket motor
StopRocketMotor();
//set NextState to RocketForwardState1
NextState = RocketForwardState1;
}
}
//update CurrentState
CurrentState = NextState;
//return ReturnVal
return ReturnEvent;
}

/***************************************************************************
Private Funcitons
****************************************************************************/
//Init hardware for RocketMotor direction
void InitRocketDir(void){
//set up port A by enabling the peripheral clock, Lever Input is PA3
HWREG(SYSCTL_RCGCGPIO) |= BIT0HI;
//wait for the peripheral to be ready
while ((HWREG(SYSCTL_PRGPIO) & BIT0HI) != BIT0HI);
//set the direction of PA2 to be digital Output
HWREG(GPIO_PORTA_BASE+GPIO_O_DEN) |= BIT2HI;
HWREG(GPIO_PORTA_BASE+GPIO_O_DIR) |= BIT2HI;
}

//Set Rocket Motor Direction, takes a uint8_t variable as parameter, returns


nothing
void SetRocketDir(uint8_t Dir){
//if Dir is 1
if(Dir){
//set PA2 pin HI
HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA + ALL_BITS)) |= BIT2HI;
}else{//else
//set PA2 pin LO
HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA + ALL_BITS)) &= BIT2LO;
}
}

//Set rocket motor speed, takes a float number as input, returns nothing
void SetSpeed(float speed){
//convert the float number of speed into pulse width in terms of clock ticks (1
ms is 1250 ticks)
float PulseWidthf = speed*1250;
uint16_t PulseWidth = PulseWidthf;
//set rocket motor speed using PWM_TIVA_SetPulseWidth function
PWM_TIVA_SetPulseWidth(PulseWidth, RocketMotor);
}

//stop rocket motor, takes nothing and returns nothing


void StopRocketMotor(void){
//use PWN_TIVE_SetDuty to set speed to 0
PWM_TIVA_SetDuty(0, RocketMotor);
}
/*------------------------------- Footnotes -------------------------------*/
/*------------------------------ End of file ------------------------------*/

Vous aimerez peut-être aussi