Skip to content

Commit

Permalink
Refactored Propulsion System Variables and Updated Turning Subsystem …
Browse files Browse the repository at this point in the history
…to Use PWM Instead of Static Pins

The 50% duty cycle used for the turning PWM signal is a placeholder.  This may need to change in future development.

Pin A2 = Enable Pin on the stepper motor
Pin A3 = Direction (CW/CCW) Pin on the stepper motor
PWM Module #2 (Pin P1) = signal to the stepper motor to begin turning
  • Loading branch information
ZacharyDownum committed Apr 3, 2018
1 parent b1412de commit 686f6e9
Showing 1 changed file with 70 additions and 24 deletions.
94 changes: 70 additions & 24 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.5
#define DEAD_ZONE_OFFSET 0.1

//basic initialization for all pins
void PIC_Initialization(void)
Expand Down Expand Up @@ -58,13 +58,19 @@ void IC_Module_Initialize(IC_Module* kill_switch_input, IC_Module* propulsion_th
propulsion_direction_motor_input->Update = IC3_Update;
}

void PWM_Module_Initialize(PWM_Module* propulsion_thrust_servo_output)
void PWM_Module_Initialize(PWM_Module* propulsion_thrust_servo_output, PWM_Module* duration_to_turn_propulsion_engine_output)
{
propulsion_thrust_servo_output->Initialize = PWM_OC1_Initialize;
propulsion_thrust_servo_output->GetDutyCycle = PWM_Get_OC1_DutyCycle;
propulsion_thrust_servo_output->GetFrequency = PWM_Get_OC1_Frequency;
propulsion_thrust_servo_output->UpdateDutyCycle = PWM_Update_OC1_DutyCycle;
propulsion_thrust_servo_output->UpdateFrequency = PWM_Update_OC1_Frequency;

duration_to_turn_propulsion_engine_output->Initialize = PWM_OC2_Initialize;
duration_to_turn_propulsion_engine_output->GetDutyCycle = PWM_Get_OC2_DutyCycle;
duration_to_turn_propulsion_engine_output->GetFrequency = PWM_Get_OC2_Frequency;
duration_to_turn_propulsion_engine_output->UpdateDutyCycle = PWM_Update_OC2_DutyCycle;
duration_to_turn_propulsion_engine_output->UpdateFrequency = PWM_Update_OC2_Frequency;
}

void Kill_Switch_Initialize(void)
Expand All @@ -74,6 +80,13 @@ void Kill_Switch_Initialize(void)
LATAbits.LATA1 = 0;
}

void Update_All_Inputs(IC_Module* kill_switch_input, IC_Module* propulsion_throttle_servo_input, IC_Module* propulsion_direction_motor_input)
{
kill_switch_input->Update(kill_switch_input);
propulsion_direction_motor_input->Update(propulsion_direction_motor_input);
propulsion_throttle_servo_input->Update(propulsion_throttle_servo_input);
}

int main(void)
{
SYSTEM_Initialize();
Expand All @@ -86,65 +99,98 @@ int main(void)
IC_Module_Initialize(&kill_switch_input, &propulsion_throttle_servo_input, &propulsion_direction_motor_input);

PWM_Module propulsion_throttle_servo_output;
PWM_Module_Initialize(&propulsion_throttle_servo_output);
PWM_Module turn_propulsion_engine_output;
PWM_Module_Initialize(&propulsion_throttle_servo_output, &turn_propulsion_engine_output);

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);

propulsion_throttle_servo_output.Initialize(&propulsion_throttle_servo_output);
turn_propulsion_engine_output.Initialize(&turn_propulsion_engine_output);

propulsion_throttle_servo_output.frequency = 50;
propulsion_throttle_servo_output.dutyCyclePercentage = 2;
propulsion_throttle_servo_output.UpdateFrequency(&propulsion_throttle_servo_output);
propulsion_throttle_servo_output.UpdateDutyCycle(&propulsion_throttle_servo_output);

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

while(true)
{
kill_switch_input.Update(&kill_switch_input);
propulsion_direction_motor_input.Update(&propulsion_direction_motor_input);
Update_All_Inputs(&kill_switch_input, &propulsion_throttle_servo_input, &propulsion_direction_motor_input);

propulsion_throttle_servo_input.Update(&propulsion_throttle_servo_input);
//this is to regulate the duty cycle that is sent to the servo so that it falls within the acceptable range for
//the servo that is being used by the project.
//this duty cycle should be approximately between 5% and 15% (with 10% being directly in the center, or 90 degrees of motion in a 180 degree servo)
propulsion_throttle_servo_output.dutyCyclePercentage = (propulsion_throttle_servo_input.dutyCyclePercentage - MINIMUM_INPUT_SIGNAL_DUTY_CYCLE) / OUTPUT_DUTY_CYCLE_INCREMENT + PROPULSION_THROTTLE_SERVO_OFFSET;


//This is here to account for minor variations that put the input duty cycle above or below
//the minimum or maximum input signal duty (which could cause undefined behavior on the output signal)
if (kill_switch_input.dutyCyclePercentage < MIDPOINT_INPUT_SIGNAL_DUTY_CYCLE)
//if, for whatever reason, the duty cycle that is sent to the servo is below 0% or above 100%, this will
//regulate the duty cycle to remain within acceptable values
if (propulsion_throttle_servo_output.dutyCyclePercentage < 0)
{
LATAbits.LATA0 = 0;
LATAbits.LATA1 = 0;
propulsion_throttle_servo_output.dutyCyclePercentage = 0;
}
else if (kill_switch_input.dutyCyclePercentage >= MIDPOINT_INPUT_SIGNAL_DUTY_CYCLE)
else if (propulsion_throttle_servo_output.dutyCyclePercentage > 100)
{
LATAbits.LATA0 = 1;
LATAbits.LATA1 = 1;
propulsion_throttle_servo_output.dutyCyclePercentage = 100;
}

propulsion_throttle_servo_output.UpdateDutyCycle(&propulsion_throttle_servo_output);

propulsion_throttle_servo_output.UpdateDutyCycle(&propulsion_throttle_servo_output);


//This is here to account for minor variations that put the input duty cycle above or below
//the minimum or maximum input signal duty (which could cause undefined behavior on the output signal)
if (propulsion_throttle_servo_output.dutyCyclePercentage < 0)
//this is a binary interpretation of an input signal that could have multiple values, treating it like the switch it represents
if (kill_switch_input.dutyCyclePercentage < MIDPOINT_INPUT_SIGNAL_DUTY_CYCLE)
{
propulsion_throttle_servo_output.dutyCyclePercentage = 0;
LATAbits.LATA0 = 0;
LATAbits.LATA1 = 0;
}
else if (propulsion_throttle_servo_output.dutyCyclePercentage > 100)
else if (kill_switch_input.dutyCyclePercentage >= MIDPOINT_INPUT_SIGNAL_DUTY_CYCLE)
{
propulsion_throttle_servo_output.dutyCyclePercentage = 100;
LATAbits.LATA0 = 1;
LATAbits.LATA1 = 1;
}

if (propulsion_direction_motor_input.dutyCyclePercentage < MIDPOINT_INPUT_SIGNAL_DUTY_CYCLE - DEAD_ZONE_OFFSET)
//represents a leftward turn of the propulsion engine
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;

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.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)
{
LATAbits.LATA0 = 1;
LATAbits.LATA1 = 1;
//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.UpdateDutyCycle(&turn_propulsion_engine_output);
}
//the motor holds its position
else
{
LATAbits.LATA2 = 1;
LATAbits.LATA3 = 0;
turn_propulsion_engine_output.dutyCyclePercentage = 0;
turn_propulsion_engine_output.UpdateDutyCycle(&turn_propulsion_engine_output);
}
}

Expand Down

0 comments on commit 686f6e9

Please sign in to comment.