Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

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

Module
Motor.c
Revision
1.0.1
Description
This modules contain all helper functions for servo motors (including PopUp Motors
and Teraforming Motors). Helper functions for Rocket Motor(DC Motor) are inside
the
RocketMotorService
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_DeferRecall.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 "ADMulti.h"
#include "Motor.h"
#include "Knob.h"
#include "PopUpButton.h"
#include "Lever.h"

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


// with the introduction of Gen2, we need a module level Priority variable
/*
* Channels to be initialized:
* PopUpMotor 1 : PB6/Channel 0; //The first four are servo motorss
* PopUpMotor 2: PB7/Channel 1;
* PopUpMotor 3: PB4/Channel 2;
* TeraMotor: PB5/Channel 3;
* RocketMotor: PE4/Channel 4; //DC Motor
*/
static uint8_t HowMany = 5; // we will use five motors in total, four servos and
one DC
static uint8_t PopUpMotor1 = 0; //define all the motor channels
static uint8_t PopUpMotor2 = 1; //PopUpMotors are servo motors
static uint8_t PopUpMotor3 = 2;
static uint8_t TeraMotor = 3; //TeraMotor is servo motor
static uint8_t RocketMotor = 4; //Only RocketMotor is DC motor

static uint8_t PopUpGroup1 = 0; //PopUpMotor 1 and 2


static uint8_t PopUpGroup2 = 1; //PopUpMotor 3 and TeraMotor
static uint8_t RocketGroup = 2;

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


/****************************************************************************
Function
InitMotor
Parameters: none
Returns: none
Description: this function sets up all the motor channels
****************************************************************************/
void InitMotor (void){
//Init five TIVA channels for motors
PWM_TIVA_Init(HowMany);

//Initialize duty cycles to be 0 for five motors


PWM_TIVA_SetDuty(0, TeraMotor);
PWM_TIVA_SetDuty(0, PopUpMotor1);
PWM_TIVA_SetDuty(0, PopUpMotor2);
PWM_TIVA_SetDuty(0, PopUpMotor3);
PWM_TIVA_SetDuty(0, RocketMotor);

//Initialize the period/freq of the motor


//set servo motor periods to 20 ms, which is 25000 ticks of a 0.8 uS clock
PWM_TIVA_SetPeriod(25000, PopUpGroup1);
PWM_TIVA_SetPeriod(25000, PopUpGroup2);
//set the DC Motor freq to 1000Hz
PWM_TIVA_SetFreq(1000, RocketGroup);
}

/****************************************************************************
Function
SetTeraMotor
Parameters: a float variable indicating KnobValue
Returns: none
Description: this member function set the TeraMotor according to the knob value
****************************************************************************/
void SetTeraMotor(float KnobValue){

//calculate the pulse width in terms of ticks, the clock is 0.8 uS clock
float TeraPulseWidthf = (2.4 - (2.4-0.6)*KnobValue)*1250.0;
uint16_t TeraPulseWidth = TeraPulseWidthf;
//set TeraMotor positon using PWM_TIVA_SetPulseWidth
PWM_TIVA_SetPulseWidth(TeraPulseWidth, TeraMotor);
}

/****************************************************************************
Function
SetPopUpMotorX, ResetPopUpMotorX
Parameters: None
Returns: None
Description: this member function set the PopUpMotors
****************************************************************************/
void SetPopUpMotor1(void){
//calculate proper pulse width for popup motor1, the range for pulse width is
0.6*1250~2.6*1250
float PulseWidthf = (.6+ (2.6-0.6)*0.25)*1250.0;
uint16_t PulseWidth = PulseWidthf;
//set pulse width for popup motor1 using PWM_TIVA_SetPulseWidth
PWM_TIVA_SetPulseWidth(PulseWidth, PopUpMotor1);
}

void ResetPopUpMotor1(void){
//reset popup motor1 to initial position using PWM_TIVA_SetDuty
PWM_TIVA_SetDuty(3, PopUpMotor1);
}
void SetPopUpMotor2(void){
//calculate proper pulse width for popup motor2
float PulseWidthf = 0.52*1250.0;
uint16_t PulseWidth = PulseWidthf;
//set pulse width for popup motor2 using PWM_TIVA_SetPulseWidth
PWM_TIVA_SetPulseWidth(PulseWidth, PopUpMotor2);
}

void ResetPopUpMotor2(void){
//reset popup motor2 to initial position using PWM_TIVA_SetDuty
PWM_TIVA_SetDuty(8, PopUpMotor2);

void SetPopUpMotor3(void){
//calculate proper pulse width for popup motor3
float PulseWidthf = (0.6+ (2.6-0.6)*0.05)*1250.0;
uint16_t PulseWidth = PulseWidthf;
//set pulse width for popup motor3 using PWM_TIVA_SetPulseWidth
PWM_TIVA_SetPulseWidth(PulseWidth, PopUpMotor3);
}

void ResetPopUpMotor3(void){
//reset popup motor3 to initial position using PWM_TIVA_SetDuty
PWM_TIVA_SetDuty(8, PopUpMotor3);
}
/*------------------------------- Footnotes -------------------------------*/
/*------------------------------ End of file ------------------------------*/

You might also like