Escolar Documentos
Profissional Documentos
Cultura Documentos
Module
DriveMotorService.c
Revision
1.0.1
Description
This drives the Drive Motor for ME218B 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 "DriveMotorService.h"
#include "PWMDrive.h"
#include "InputCapture.h"
#include "DeadReckoning.h"
// H files for HWREG()
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "utils/uartstdio.h"
#include <math.h>
// H files for interupts
#include "inc/hw_timer.h"
#include "inc/hw_nvic.h"
#include "bitdefs.h"
#include "Location.h"
#define ALL_BITS (0xff<<2)
#define HI_LIMIT 100
over-driven
#define LO_LIMIT 25
#define RPM_LIMIT 70
#define SUM_ERROR_LIMIT 2000
#define RampSumError 3500
MyPriority = Priority;
// Initialize PWM for PB6 & PB7; Set wheels to have 0 Speed
InitPWMDrive();
// Initialize PWM
// Set RPMComL to 0
// Set RPMComR to 0
return true;
/****************************************************************************
Function
RunDriveMotorService
Parameters
ES_Event : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
Run function for DriveMotorService
Only needed in Test Harness
****************************************************************************/
ES_Event RunDriveMotorService( ES_Event ThisEvent )
{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
if (ThisEvent.EventType == ES_TIMEOUT && ThisEvent.EventParam
==DriveMotorUpdateTimer) // Update Timer Expires
{
// restart Update Timer
ES_Timer_InitTimer(DriveMotorUpdateTimer,UpdateTime);
}
// Keyboard debugger
if (ThisEvent.EventType == ES_NEW_KEY)
{
// Add 20 to RPMComR if L key is pressed
if (ThisEvent.EventParam == 'L')
{
int newRPMComL = RPMComL + 20;
// Reset RPMCom if greater than 50
if (newRPMComL > 50)
newRPMComL = -50;
// Set RPMComL
RPMComL = newRPMComL;
}
// Add 20 to RPMComR if R key is pressed
if (ThisEvent.EventParam == 'R')
{
int newRPMComR = RPMComR + 20;
// Reset RPMCom if greater than 50
return ReturnEvent;
}
/***************************************************************************
public functions
***************************************************************************/
// Sets New Command RPM for Left Motor
void NewRPMCommandLeft (int NewRPM)
{
// Set RPM_LIMIT
if(NewRPM > RPM_LIMIT) {
NewRPM = RPM_LIMIT;
}
// Set Negative RPM_LIMIT is NewRPM is negative
else if(NewRPM < -RPM_LIMIT) {
NewRPM = -RPM_LIMIT;
}
// Set new Command
RPMComL = NewRPM;
return;
}
// Sets New Command RPM for Right Motor
void NewRPMCommandRight (int NewRPM)
{
// Set RPM_LIMIT
if(NewRPM > RPM_LIMIT) {
NewRPM = RPM_LIMIT;
}
// Set Negative RPM_LIMIT is NewRPM is negative
else if(NewRPM < -RPM_LIMIT) {
NewRPM = -RPM_LIMIT;
}
// Set new Command
RPMComR = NewRPM;
return;
}
// Getter Function for other modules
int GetRPMComL( void ) {
return RPMComL;
}
// Getter Function for other modules
int GetRPMComR( void ) {
return RPMComR;
}
// function to stop motors
void StopMotors(void) {
// Set RPMComL & RPMComR to 0
NewRPMCommandLeft(0);
NewRPMCommandRight(0);
// Set PWM for Left & Right to 0
SetPWMDriveLeft(0);
SetPWMDriveRight(0);
int RPMErrorR;
int ReqDutyR;
float pGainL;
float pGainR;
//
//
//
//
Error
// Calculate Error
// if Command is negative, set RPMCom negative
if(RPMComL < 0) {
RPMErrorL = -RPMComL - curRPMLeft;
}
// if Command is positive, keep RPMCom positive
else {
RPMErrorL = RPMComL - curRPMLeft;
// store
// Calculate RPM
}
SumErrorL += RPMErrorL;
integrator term
// sum for
// calculate
//
}
// If RPMComL is positive
ReqDutyR = (pGainR * RPMErrorR)+(iGainR * SumErrorR);
Requested Duty, PI controller
// calculate
return;