Skip to content

Commit

Permalink
Changed PWM_Module #2 and the Function of the Propulsion Engine Direc…
Browse files Browse the repository at this point in the history
…tional Stepper Motor

Changed the PWM_Module 2 to use Timer1's prescaler of 64 to allow for smaller frequency PWM signals.

Updated the main driver such that the number of pulses sent to the stepper motor has a 1:1 relationship to the number of steps taken by the motor.  The base configuration is currently a PWM of 400Hz coupled with a 30 RPM max speed on the stepper motor, with each PWM pulse moving the stepper by +/-1 count.
  • Loading branch information
ZacharyDownum committed Apr 3, 2018
1 parent 686f6e9 commit 4a98f30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
14 changes: 12 additions & 2 deletions Dependencies/PWM Generation/PWM.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,19 @@ void PWM_OC2_Initialize(PWM_Module* OC2_PWM_module)
OC2_PWM_module->UpdateFrequency(OC2_PWM_module);
OC2_PWM_module->UpdateDutyCycle(OC2_PWM_module);

//turns timer1 off to configure it
T1CON = 0b0000000000000000;
//sets a 1:64 input clock prescaler, which increments the timer every
//8th clock cycle (from Fcy by default)
T1CONbits.TCKPS = 0b10;
//sets this timer's clock source to Fcy
T1CONbits.TCS = 0b0;
//turns timer1 back on after it is configured
T1CONbits.TON = 1;

OC2CON2 = 0b00011111;

OC2CON1bits.OCTSEL = 0b111;
OC2CON1bits.OCTSEL = 0b100;

OC2CON1bits.OCM = 0b110;
}
Expand All @@ -188,7 +198,7 @@ void PWM_Update_OC2_DutyCycle(const PWM_Module* OC2_PWM_module)

void PWM_Update_OC2_Frequency(const PWM_Module* OC2_PWM_module)
{
double totalClockCycles = (double)1.0 / (OC2_PWM_module->frequency * TCY) - 1;
double totalClockCycles = (double)1.0 / (OC2_PWM_module->frequency * TCY * TIMER_PRESCALER) - 1;
OC2RS = (int)totalClockCycles;

OC2_PWM_module->UpdateDutyCycle(OC2_PWM_module);
Expand Down
32 changes: 12 additions & 20 deletions Finalized Design/Final Project/main_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

//this dead zone will be used to prevent the user from turning the propulsion motor without intentionally
//moving the left joystick to the left or right.
#define DEAD_ZONE_OFFSET 0.1
#define DEAD_ZONE_OFFSET 0.5

//basic initialization for all pins
void PIC_Initialization(void)
Expand Down Expand Up @@ -112,7 +112,7 @@ int main(void)
propulsion_throttle_servo_output.frequency = 50;
propulsion_throttle_servo_output.UpdateFrequency(&propulsion_throttle_servo_output);

turn_propulsion_engine_output.frequency = 20000;
turn_propulsion_engine_output.frequency = 400;
turn_propulsion_engine_output.UpdateFrequency(&turn_propulsion_engine_output);

while(true)
Expand Down Expand Up @@ -155,41 +155,33 @@ int main(void)
}

//represents a leftward turn of the propulsion engine
//at the moment, the frequency of the PWM signal * the number of counts per trigger = 800 (counts per rotation) * rotations per second
//this will allow for smooth movement of the stepper motor
//if PWM frequency * counts per trigger > 800 * RPS, then the user input will experience a delay in controlling the stepper motor
//if PWM frequency * counts per trigger < 800 * RPS, then the user input will cause jerking in the stepper motor response
if (propulsion_direction_motor_input.dutyCyclePercentage < MIDPOINT_INPUT_SIGNAL_DUTY_CYCLE - DEAD_ZONE_OFFSET)
{
//This is the enable bit for the stepper motor responsible for turning the propulsion engine to control direction
LATAbits.LATA2 = 1;
//this is the bit to control the direction of rotation of the stepper motor (CW or CCW)
//when the signal is high, the motor shaft rotates CCW, which makes the craft rotate to the left
LATAbits.LATA3 = 1;
LATAbits.LATA2 = 0;

turn_propulsion_engine_output.dutyCyclePercentage = 50;
turn_propulsion_engine_output.UpdateDutyCycle(&turn_propulsion_engine_output);
__delay_us(100);
turn_propulsion_engine_output.dutyCyclePercentage = 0;
turn_propulsion_engine_output.dutyCyclePercentage = 5;
turn_propulsion_engine_output.UpdateDutyCycle(&turn_propulsion_engine_output);
}
//represents a rightward turn of the propulsion engine
else if (propulsion_direction_motor_input.dutyCyclePercentage >= MIDPOINT_INPUT_SIGNAL_DUTY_CYCLE + DEAD_ZONE_OFFSET)
{
//This is the enable bit for the stepper motor responsible for turning the propulsion engine to control direction
LATAbits.LATA2 = 1;
//this is the bit to control the direction of rotation of the stepper motor (CW or CCW)
//when the signal is low, the motor shaft rotates CW, which makes the craft rotate to the right
LATAbits.LATA3 = 0;

turn_propulsion_engine_output.dutyCyclePercentage = 50;
turn_propulsion_engine_output.UpdateDutyCycle(&turn_propulsion_engine_output);
__delay_us(100);
turn_propulsion_engine_output.dutyCyclePercentage = 0;
turn_propulsion_engine_output.dutyCyclePercentage = 5;
turn_propulsion_engine_output.UpdateDutyCycle(&turn_propulsion_engine_output);
}
//the motor holds its position
else
{
LATAbits.LATA2 = 1;
LATAbits.LATA3 = 0;
turn_propulsion_engine_output.dutyCyclePercentage = 0;
LATAbits.LATA2 = 0;

turn_propulsion_engine_output.dutyCyclePercentage = 100;
turn_propulsion_engine_output.UpdateDutyCycle(&turn_propulsion_engine_output);
}
}
Expand Down

0 comments on commit 4a98f30

Please sign in to comment.