00001 /* 00002 @file motor_controller.h 00003 */ 00004 00005 #ifndef __MOTOR_CONTROLLER_H__ 00006 #define __MOTOR_CONTROLLER_H__ 00007 00011 typedef enum mc_directions{ 00012 MC_POSITIVE = 1, 00013 MC_NEGATIVE = -1 00014 } MOTOR_DIRECTION; 00018 typedef enum mc_controls{ 00019 MC_STOP = 0, 00020 MC_START = 1 00021 } MOTOR_CONTROL; 00026 typedef enum mc_direction_multipliers { 00027 MC_NORMAL = 1, 00028 MC_BACKWARDS = -1 00029 } MC_OPERATION; 00030 00035 typedef enum mc_safety_causes{ 00036 MC_NONE = 0, 00037 MC_THERM = 1, 00038 MC_MECH = 2 00039 } MC_SAFETY_CAUSE; 00040 00046 typedef struct mc_data{ 00047 float motor_voltage_max; 00048 fixed current_max; 00049 fixed current_min; 00050 fixed target_max; 00051 fixed target_min; 00052 fixed thermal_safe; 00053 fixed thermal_limit; 00054 fixed tau_inv; 00055 fixed thermal_diff; 00056 fixed dt; 00057 unsigned int error_limit; 00058 fixed smart_error_limit; 00059 fixed smart_error_limit_inverse; 00060 fixed kp; 00061 fixed ki; 00062 fixed kd; 00063 fixed integral; 00064 fixed max_integral; 00065 int max_pwm; 00066 MC_OPERATION operation; 00067 FIXED_VOID_F get_current; 00068 FLOAT_VOID_F get_position; 00069 FLOAT_VOID_F get_velocity; 00070 } MC_DATA; 00071 00072 00073 00074 // *********************** Function Declarations ************************ 00075 // Public Functions 00076 00077 //initializes the motor controller 00078 void mc_init(float max_volts, //the maximum voltage the mc can take 00079 float max_current, //max current that can safely be put through the motor 00080 float min_current, //ditto, but in negative direction 00081 float max_target, 00082 float min_target, 00083 float thermal_current_limit, 00084 float thermal_time_constant, 00085 float kp, 00086 float ki, 00087 float kd, 00088 int max_pwm, 00089 int error_limit, 00090 float smart_error_limit, 00091 MC_OPERATION op, 00092 FIXED_VOID_F current, 00093 FLOAT_VOID_F position, 00094 FLOAT_VOID_F velocity); 00095 00096 //Control Functions 00097 void mc_pid_current(void); //PID controller for current 00098 void mc_set_target_current(float new_current);//sets the target_current of the pwm controller to new_current 00099 MC_DATA* mc_get_parameters(void); //to set/update/read parameters 00100 int mc_get_pwm(void);//returns the current pwm 00101 void mc_set_pwm(int pwm); //sets the pwm 00102 void mc_run_no_control(void); //checks current and feeds watchdog; for debugging 00103 void mc_set_error_level(unsigned long int error_level); //sets the error reporting level 00104 00105 //Safety Functions 00106 static fixed mc_get_thermal_limiter(fixed current); 00107 static fixed mc_smart_check_current(fixed current); 00108 void mc_direction_control(MOTOR_DIRECTION dir, //the direction being controlled, either positive or negative 00109 MOTOR_CONTROL command); //controls whether the motor is allowed to turn in a given direction (MC_STOP or MC_START) 00110 void mc_set_sleep(int sleep); //sets pwm to 0 if sleep 00111 void mc_set_shutdown(int shutdown); //doesn't update watchdog timer if shutdown 00112 static int mc_is_running(void); //returns true (1) if the motor controller is running, 0 otherwise 00113 void mc_set_unsafe_target_current(float new_current); 00114 float mc_get_mult(void); 00115 static void mc_turnoff(MC_SAFETY_CAUSE reason); 00116 static void mc_turnon(MC_SAFETY_CAUSE reason); 00117 00118 //Compliant control stuff 00119 void mc_compliant_control(void); 00120 void mc_set_command_current(float new_command); 00121 void mc_set_stiffness(float new_c1); 00122 void mc_set_dampness(float new_c2); 00123 float mc_get_stiffness(void); 00124 float mc_get_dampness(void); 00125 float mc_get_command_current(void); 00126 00127 //Private Functions 00128 static void mc_set_target_current_fixed(fixed new_current); 00129 void mc_update_watchdog(void);//feeds the watchdog. Call this every ms or so 00130 static void mc_check_current(fixed current); //checks the motor current; if the current is out of bounds for too long, calls mc_shutdown 00131 00132 /* HARDWARE SETUP 00133 // ******************************************************************************* 00134 // Motor Controller Setup 00135 // ******************************************************************************* 00136 //Motor control initialization 00137 //FIO0DIR |= (1<<9); // set P0.9 to be output (PWM A) 00138 //FIO0DIR |= (1<<7); // set P0.7 to be output (PWM B) 00139 FIO0DIR |= (1<<11); // set P0.11 to be output (Low side enable A) 00140 FIO1DIR |= (1<<17); // set P1.17 to be output (Low side enable B) 00141 FIO0DIR |= (1<<6); // set P0.6 to be output (Watchdog timer) 00142 //FIO0CLR = (1<<9); // turn off PWM A output 00143 //FIO0CLR = (1<<7); // turn off PWM B output 00144 FIO0SET = (1<<11); // turn on low side A enable output 00145 FIO1SET = (1<<17); // turn on low side B enable output 00146 FIO0CLR = (1<<6); // turn off watchdog timer output 00147 //Initializes the PWM registers and PWM2 and PWM6 outputs 00148 //Pin Function Select 00149 PINSEL0 &= ~(3<<14); 00150 PINSEL0 |= (2<<14); //PWM 2 on Pin P0.7 Enabled (bits 14/15) 00151 PINSEL0 &= ~(3<<18); 00152 PINSEL0 |= (2<<18); //PWM 6 on Pin P0.9 Enabled (bits 18/19) 00153 //PWM Prescale Register. Controls number of PClK cycles per Timer Counter Cycle 00154 PWMPR= 0; //Will run at maximum rate 00155 //PWM Match Registers 00156 //PWM Match Register 0. Resets Timer Counter and sets all PWM outputs to 1. 00157 PWMMR0= 600; //100 kHz PWM frequency (60000000/600 = 100,000) 00158 PWMMR1= 0; 00159 PWMMR2= 0; 00160 PWMMR3= 0; 00161 PWMMR4= 0; 00162 PWMMR5= 0; 00163 PWMMR6= 0; 00164 //PWM Match Control Register 00165 PWMMCR= (PWMMCR|0x2) & ~PWMMCR_RB; //Should set Timer Counter to reset upon reaching Match Register 0 00166 //PWM Timer Control Register. 00167 PWMTCR= (PWMTCR|0x1) // Enable Timer Counter 00168 | (PWMTCR|0x8) // Enable PWM Mode 00169 & ~PWMTCR_RB; 00170 //Must Occur after Initialization of Match Register 0. 00171 //PWM Control Register. Enables individual PWM types and outputs. 00172 PWMPCR= (PWMPCR|0x4400) & ~PWMPCR_RB; //Should mean the same thing as next 4 lines (PWM Control Register) 00173 //PWMSEL2= 0; //Enables Single Edge PWM 2 Control 00174 //PWMSEL6= 0; //Enables Single Edge PWM 6 Control 00175 //PWMENA2= 1; //PWM 2 Output Enabled 00176 //PWMENA6= 1; //PWM 6 Output Enabled 00177 //PWM Latch Enable Register. Updates Match values. 00178 //Each bit directly relates to a Match Register 00179 //PWMLER= 0; //Latch Register to zero 00180 PWMMR2= 0; // out of 600 -ve 00181 PWMMR6= 0; // out of 600 +ve 00182 // Latch enable reg automatically cleared once PWM match values are set 00183 PWMLER= (PWMLER|0x45) & ~PWMLER_RB; //Enable update of PWM match registers 0, 2 and 6. 0x45 --> bit0, bit2, bit6 00184 */ 00185 00186 #endif // MOTOR_CONTROLLER_H 00187
1.6.3