Proto Drive / Mbed 2 deprecated Protodrive

Dependencies:   RPCInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers motor_control.h Source File

motor_control.h

00001 /***********************************************************/
00002 /*Constants                                                */
00003 /***********************************************************/
00004 
00005 #define V_MOTOR_RATED 6 //rated motor voltage [V]. this must be set to be less than the voltage of the batteries
00006 #define PERIOD_US 25 //define the PWM period in microseconds, currently set for 40kHz
00007 
00008 /***********************************************************/
00009 /*Pin setup                                                */
00010 /***********************************************************/
00011 
00012 //Enable PWM inout to motorcontroller
00013 PwmOut DUT_motor(p22);
00014 PwmOut load_motor(p21);
00015 
00016 //Motor direction control on motor controller switch 2
00017 DigitalOut DUT_direction(p5);
00018 DigitalOut load_direction(p6);
00019 
00020 /***********************************************************/
00021 /*Subroutines                                              */
00022 /***********************************************************/
00023 
00024 //initialize PWM
00025 void init_pwm () {
00026     //set the PWM channel periods
00027     DUT_motor.period_us(PERIOD_US);
00028     load_motor.period_us(PERIOD_US);
00029 
00030     //initialize duty cycle to 0 to make sure motors are off
00031     DUT_motor =0;
00032     load_motor =0;
00033 }
00034 
00035 /*
00036 Set PWM duty cycle
00037  - Motors are connected so that they will always oppose each other.
00038  - Duty cycle (a float from 0-1) determines the voltage applied to the motor terminals as a percentage of the rated voltage. eg. if duty is 0.5 and rated voltage is 6V, then terminal voltage will be 3V
00039 */
00040 void set_duty (float DUT_demand, float load_demand) {
00041 
00042 //DUT batt and cap enabled
00043 #ifdef DUT_BATT_CAP_ENABLE
00044     if (relay) {
00045         v_DUT_batt = get_voltage(v_DUT_batt_measure); //get voltage of DUT battery
00046         DUT_demand = DUT_demand*(V_MOTOR_RATED/v_DUT_batt); //scale the demand so that the voltage to the motor does not exceed its rated max
00047     } else if (relay == 0) { //if cap is currently being used
00048         v_cap = get_voltage(v_cap_measure); //get voltage of cap
00049         DUT_demand = DUT_demand*(V_MOTOR_RATED/v_cap); //scale the demand so that the voltage to the motor does not exceed its rated max
00050     } else {
00051         big_error = 1; //error flag
00052         big_error_led = 1; //turn on error LED
00053     }
00054     DUT_motor.write(DUT_demand); //write new DUT duty cycle
00055 #endif
00056 
00057 //DUT batt connected
00058 #ifdef DUT_BATT_ENABLE
00059     v_DUT_batt = get_voltage(v_DUT_batt_measure); //get voltage of DUT battery
00060     DUT_demand = DUT_demand*(V_MOTOR_RATED/v_DUT_batt); //scale the demand so that the voltage to the motor does not exceed its rated max
00061     DUT_motor.write(DUT_demand); //change DUT duty cycle based on demand
00062 #endif
00063 
00064 //Load batt connected
00065 #ifdef LOAD_BATT_ENABLE
00066     v_load_batt = get_voltage(v_load_batt_measure); //get voltage of load battery
00067     load_demand = load_demand*(V_MOTOR_RATED/v_load_batt); //scale the demand so that the voltage to the motor does not exceed its rated max
00068     load_motor.write(load_demand); //change load duty cycle based on demand
00069 #endif
00070 
00071 //External Power
00072 #ifdef DUT_EXTERNAL_POWER_ENABLE
00073     DUT_motor.write(DUT_demand); //change DUT duty cycle based on demand
00074 #endif
00075 
00076 #ifdef LOAD_EXTERNAL_POWER_ENABLE
00077     load_motor.write(load_demand); //change load duty cycle based on demand
00078 #endif
00079 
00080 //set motor directions
00081     DUT_direction.write(DUT_input_direc);
00082     load_direction.write(load_input_direc);
00083 }
00084 
00085 
00086 
00087 
00088 
00089