Skip to content

Commit

Permalink
Fixed Errors with Stepper Motor Control
Browse files Browse the repository at this point in the history
Input Capture Module #4 is used to monitor the number of pulses sent to the stepper motor via the PWM Module #2.  This count is used to know when the user has moved the stepper too far in one direction and prevents further motion in that direction.

Fixed various issues (like changing Pin A2 unnecessarily and several incorrect if statements in the IC Module #4 Interrupt) that was causing the PIC to send inappropriate signals to the stepper motor, which caused it to malfunction.
  • Loading branch information
ZacharyDownum committed Apr 5, 2018
1 parent f5b0ca2 commit 3ba78de
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
29 changes: 26 additions & 3 deletions Dependencies/Input Capture/InputCapture.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,12 @@ void __attribute__ ((__interrupt__, auto_psv)) _IC4Interrupt(void)
{
if (LATAbits.LATA2 == 0)
{
IC4_Buffer.numberOfCounts--;
if (IC4_Buffer.numberOfCounts > -200)
{
IC4_Buffer.numberOfCounts--;
}
}
else
else if (IC4_Buffer.numberOfCounts < 200)
{
IC4_Buffer.numberOfCounts++;
}
Expand All @@ -320,7 +323,11 @@ void IC4_Initialize(Count_Monitor* IC4_Module)
{
IC4CON1 = 0x0000;

IC4_Buffer.numberOfCounts = 0;

IC4_Module->numberOfCounts = 0;
IC4_Module->allowClockwiseMotion = 1;
IC4_Module->allowCounterClockwiseMotion = 1;

TRISBbits.TRISB7 = 1;
Nop();
Expand Down Expand Up @@ -350,7 +357,23 @@ void IC4_Initialize(Count_Monitor* IC4_Module)

void IC4_Update(Count_Monitor* IC4_Module)
{

IC4_Module->numberOfCounts = IC4_Buffer.numberOfCounts;

if (IC4_Buffer.numberOfCounts <= -200)
{
IC4_Module->allowClockwiseMotion = 0;
IC4_Module->allowCounterClockwiseMotion = 1;
}
else if (IC4_Buffer.numberOfCounts >= 200)
{
IC4_Module->allowClockwiseMotion = 1;
IC4_Module->allowCounterClockwiseMotion = 0;
}
else
{
IC4_Module->allowClockwiseMotion = 1;
IC4_Module->allowCounterClockwiseMotion = 1;
}
}


Expand Down
5 changes: 5 additions & 0 deletions Dependencies/Input Capture/InputCapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ struct Count_Monitor
//stepper motor. Negative values are CW, positive values are CCW.
int numberOfCounts;

int allowClockwiseMotion;
int allowCounterClockwiseMotion;

int brakeEngaged;

void (*Initialize)(Count_Monitor*);
void (*Update)(Count_Monitor*);
};
Expand Down
21 changes: 13 additions & 8 deletions Finalized Design/Final Project/main_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void PIC_Initialization(void)
Nop();
}

void IC_Module_Initialize(IC_Module* kill_switch_input, IC_Module* propulsion_throttle_servo_input, IC_Module* propulsion_direction_motor_input)
void IC_Module_Initialize(IC_Module* kill_switch_input, IC_Module* propulsion_throttle_servo_input, IC_Module* propulsion_direction_motor_input, Count_Monitor* stepper_motor_counter_input)
{
kill_switch_input->Initialize = IC1_Initialize;
kill_switch_input->Update = IC1_Update;
Expand All @@ -56,6 +56,9 @@ void IC_Module_Initialize(IC_Module* kill_switch_input, IC_Module* propulsion_th

propulsion_direction_motor_input->Initialize = IC3_Initialize;
propulsion_direction_motor_input->Update = IC3_Update;

stepper_motor_counter_input->Initialize = IC4_Initialize;
stepper_motor_counter_input->Update = IC4_Update;
}

void PWM_Module_Initialize(PWM_Module* propulsion_thrust_servo_output, PWM_Module* duration_to_turn_propulsion_engine_output)
Expand Down Expand Up @@ -96,7 +99,8 @@ int main(void)
IC_Module kill_switch_input;
IC_Module propulsion_throttle_servo_input;
IC_Module propulsion_direction_motor_input;
IC_Module_Initialize(&kill_switch_input, &propulsion_throttle_servo_input, &propulsion_direction_motor_input);
Count_Monitor stepper_motor_counter_input;
IC_Module_Initialize(&kill_switch_input, &propulsion_throttle_servo_input, &propulsion_direction_motor_input, &stepper_motor_counter_input);

PWM_Module propulsion_throttle_servo_output;
PWM_Module turn_propulsion_engine_output;
Expand All @@ -105,6 +109,7 @@ int main(void)
kill_switch_input.Initialize(&kill_switch_input);
propulsion_throttle_servo_input.Initialize(&propulsion_throttle_servo_input);
propulsion_direction_motor_input.Initialize(&propulsion_direction_motor_input);
stepper_motor_counter_input.Initialize(&stepper_motor_counter_input);

propulsion_throttle_servo_output.Initialize(&propulsion_throttle_servo_output);
turn_propulsion_engine_output.Initialize(&turn_propulsion_engine_output);
Expand Down Expand Up @@ -159,28 +164,28 @@ int main(void)
//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)
stepper_motor_counter_input.Update(&stepper_motor_counter_input);
if (propulsion_direction_motor_input.dutyCyclePercentage < MIDPOINT_INPUT_SIGNAL_DUTY_CYCLE - DEAD_ZONE_OFFSET && stepper_motor_counter_input.allowClockwiseMotion == 1)
{
//This is the enable bit for the stepper motor responsible for turning the propulsion engine to control direction
LATAbits.LATA2 = 0;

turn_propulsion_engine_output.dutyCyclePercentage = 5;
turn_propulsion_engine_output.dutyCyclePercentage = 10;
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)

else if (propulsion_direction_motor_input.dutyCyclePercentage >= MIDPOINT_INPUT_SIGNAL_DUTY_CYCLE + DEAD_ZONE_OFFSET && stepper_motor_counter_input.allowCounterClockwiseMotion == 1)
{
//This is the enable bit for the stepper motor responsible for turning the propulsion engine to control direction
LATAbits.LATA2 = 1;

turn_propulsion_engine_output.dutyCyclePercentage = 5;
turn_propulsion_engine_output.dutyCyclePercentage = 10;
turn_propulsion_engine_output.UpdateDutyCycle(&turn_propulsion_engine_output);
}
//the motor holds its position
else
{
LATAbits.LATA2 = 0;

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

0 comments on commit 3ba78de

Please sign in to comment.