00001 00017 #include "config.h" 00018 #include "config_motor.h" 00019 00020 #include "mc_control.h" 00021 #include "mc_drv.h" 00022 #include "mc_interface.h" 00023 00024 #include "mc_interface.h" 00025 00026 U8 duty_cycle = 0; 00027 U8 BO_BF = SPEED_LOOP; 00028 00029 /* Speed regulation variables */ 00030 S16 speed_error = 0; 00031 S16 last_speed_error = 0; 00032 S16 speed_integral = 0; 00033 S16 speed_integ = 0; 00034 S16 speed_proportional = 0; 00035 S16 speed_derivative = 0; 00036 S16 speed_der = 0; 00037 00038 /* Current regulation variables */ 00039 S16 cur_error = 0; 00040 S16 last_cur_error = 0; 00041 S16 cur_integral = 0; 00042 S16 cur_integ = 0; 00043 S16 cur_proportional = 0; 00044 S16 cur_derivative = 0; 00045 S16 cur_der = 0; 00046 00047 /* Position regulation variables */ 00048 S16 pos_error = 0; 00049 S16 last_pos_error = 0; 00050 S16 pos_integral = 0; 00051 S16 pos_integ = 0; 00052 S16 pos_proportional = 0; 00053 S16 pos_derivative = 0; 00054 S16 pos_der = 0; 00055 00056 /************************************************************************************************************/ 00057 /* Speed Regulation */ 00058 /************************************************************************************************************/ 00065 U8 mc_control_speed(U8 speed_cmd) 00066 { 00067 U8 Duty = 0; 00068 S16 increment = 0; 00069 00070 // Error calculation 00071 speed_error = (S16)speed_cmd - mci_get_measured_speed();// value -255 <=> 255 00072 00073 // proportional term calculation 00074 speed_proportional = (S16)Kp_speed * speed_error; 00075 00076 // integral term calculation 00077 speed_integral = speed_integral + speed_error; 00078 00079 if(speed_integral > (S16)255) speed_integral = 255; 00080 if(speed_integral < (S16)-255) speed_integral = -255; 00081 00082 speed_integ = (S16)Ki_speed * speed_integral; 00083 00084 // derivative term calculation 00085 /* speed_derivative = (S16)speed_error - last_speed_error; 00086 00087 if(speed_derivative > (S16)255) speed_derivative = 255; 00088 if(speed_derivative < (S16)-255) speed_derivative = -255; 00089 00090 speed_der = Kd_speed*speed_derivative; 00091 00092 last_speed_error = speed_error;*/ 00093 00094 // Duty Cycle calculation 00095 increment = speed_proportional + speed_integ; 00096 //increment += speed_der; 00097 increment = increment >> K_speed_scal; 00098 increment = increment + Klin_speed; 00099 00100 // Variable saturation 00101 if(increment >= (S16)(240)) Duty = 240; 00102 else 00103 { 00104 if(increment <= (S16)(130)) Duty = 130; 00105 else Duty = (U8)(increment); 00106 } 00107 00108 /* return the new "Duty Cycle" */ 00109 return Duty; 00110 } 00111 00112 /************************************************************************************************************/ 00113 /* Current Regulation */ 00114 /************************************************************************************************************/ 00121 U8 mc_control_current(U8 cur_cmd) 00122 { 00123 U8 Duty = 0; 00124 S32 increment = 0; 00125 00126 // Error calculation 00127 cur_error = cur_cmd - (mci_get_measured_current());// value -255 <=> 255 00128 00129 // proportional term calculation 00130 cur_proportional = Kp_cur*cur_error; 00131 00132 // integral term calculation 00133 cur_integral = cur_integral + cur_error; 00134 00135 if(cur_integral > 255) cur_integral = 255; 00136 if(cur_integral < -255) cur_integral = -255; 00137 00138 cur_integ = Ki_cur*cur_integral; 00139 00140 // derivative term calculation 00141 /*cur_derivative = cur_error - last_cur_error; 00142 00143 if(cur_derivative > 255) cur_derivative = 255; 00144 if(cur_derivative < -255) cur_derivative = -255; 00145 00146 cur_der = Kd_cur*cur_derivative; 00147 00148 last_cur_error = cur_error;*/ 00149 00150 // Duty Cycle calculation 00151 increment = cur_proportional + cur_integ; 00152 //increment += cur_der; 00153 increment = increment >> K_cur_scal; 00154 00155 // Variable saturation 00156 if(increment >= (S16)(255)) Duty = 255; 00157 else 00158 { 00159 if(increment <= (S16)(0)) Duty = 0; 00160 else Duty = (U8)(increment); 00161 } 00162 00163 // return Duty Cycle 00164 return Duty; 00165 } 00166 00167 /************************************************************************************************************/ 00168 /* Position Regulation */ 00169 /************************************************************************************************************/ 00176 U8 mc_control_position(S32 Number_of_turns) 00177 { 00178 U8 Duty = 0; 00179 S32 increment = 0; 00180 00181 // Error calculation 00182 pos_error = Number_of_turns - mc_get_Num_Turn(); 00183 00184 // proportional term calculation 00185 pos_proportional = Kp_pos*pos_error; 00186 00187 // integral term calculation 00188 pos_integral = pos_integral + pos_error; 00189 00190 if(pos_integral > 255) pos_integral = 255; 00191 if(pos_integral < -255) pos_integral = -255; 00192 00193 pos_integ = Ki_pos * pos_integral; 00194 00195 // derivative term calculation 00196 pos_derivative = pos_error - last_pos_error; 00197 00198 if(pos_derivative > 255) pos_derivative = 255; 00199 if(pos_derivative < -255) pos_derivative = -255; 00200 00201 pos_der = Kd_pos*pos_derivative; 00202 00203 last_pos_error = pos_error; 00204 00205 // Duty Cycle calculation 00206 increment = pos_proportional + pos_integ; 00207 increment += pos_der; 00208 increment = increment >> K_pos_scal; 00209 00210 // Variable saturation 00211 if(increment >= (S16)(0)) 00212 { 00213 if(increment >= (S16)(100))Duty = 100; 00214 else Duty = (U8)(increment); 00215 mci_set_motor_direction(CW); 00216 } 00217 else 00218 { 00219 if(increment <= (S16)(-100)) Duty = 100; 00220 else Duty = (U8)(-increment); 00221 mci_set_motor_direction(CCW); 00222 } 00223 // return Duty Cycle 00224 return Duty; 00225 } 00226 00227 00228 /************************************************************************************************************/ 00229 /* Selection of Regulation Loop */ 00230 /************************************************************************************************************/ 00236 void mc_regulation_loop() 00237 { 00238 // measure 00239 Set_EXT3(); 00240 00241 switch(BO_BF) 00242 { 00243 case OPEN_LOOP : duty_cycle = mci_get_motor_speed();break; 00244 case SPEED_LOOP : duty_cycle = mc_control_speed(mci_get_motor_speed());break; 00245 case CURRENT_LOOP : duty_cycle = mc_control_current(mci_get_potentiometer_value());break; 00246 case POSITION_LOOP : duty_cycle = /*mc_control_speed(*/mc_control_position(mci_get_potentiometer_value())/*)*/;break; 00247 default : break; 00248 } 00249 00250 // measure 00251 Clear_EXT3(); 00252 } 00253 00259 void mc_set_Open_Loop(){BO_BF = OPEN_LOOP;} 00260 00266 void mc_set_Speed_Loop(){BO_BF = SPEED_LOOP;} 00267 00273 void mc_set_Current_Loop(){BO_BF = CURRENT_LOOP;} 00274 00280 void mc_set_Position_Loop(){BO_BF = POSITION_LOOP;} 00281 00287 U8 mc_get_Duty_Cycle() 00288 { 00289 return duty_cycle; 00290 } 00291
1.4.7