00001
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "config.h"
00043
00044 #include "mc_interface.h"
00045 #include "mc_control.h"
00046 #include "mc_drv.h"
00047
00048 #include "adc\adc_drv.h"
00049
00050 #include "ushell_task.h"
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #ifdef MC310_MOTOR
00064 #ifdef __GNUC__
00065 U8
00066 #elif __ICCAVR__
00067 U8 __flash
00068 #endif
00069 ramp_up_time_table[] = {26,23,20,17,14,11,8,5,3,2,2};
00070 #ifdef __GNUC__
00071 U8
00072 #elif __ICCAVR__
00073 U8 __flash
00074 #endif
00075 ramp_up_duty_table[] = {122,124,126,129,131,133,135,137,140,143,145};
00076 #endif
00077
00078 #ifdef ELECTROCRAFT_MOTOR
00079 #ifdef __GNUC__
00080 U8
00081 #elif __ICCAVR__
00082 U8 __flash
00083 #endif
00084 ramp_up_time_table[] = {53,53,53,21,10,5,4,3,3,3};
00085 #ifdef __GNUC__
00086 U8
00087 #elif __ICCAVR__
00088 U8 __flash
00089 #endif
00090 ramp_up_duty_table[] = {128,129,130,132,137,139,144,151,156,159};
00091 #endif
00092
00093 #ifdef RC_MOTOR
00094
00095 #ifdef __GNUC__
00096 U8
00097 #elif __ICCAVR__
00098 U8 __flash
00099 #endif
00100 ramp_up_time_table[] = {22,20,18,16,14,12,10,8,7,6,5,4,4,4,4,4};
00101
00102 #ifdef __GNUC__
00103 U8
00104 #elif __ICCAVR__
00105 U8 __flash
00106 #endif
00107 ramp_up_duty_table[] = {128,132,136,140,144,148,152,156,160,164,166,168,170,160,160,160};
00108 #endif
00109
00110
00111
00112 U16 g_regulation_period = 0;
00113 U16 motor_speed = 0;
00114 extern volatile Bool g_tick;
00115 extern Bool overcurrent;
00116 extern U8 duty_cycle;
00117 extern U8 duty_cycle_reference;
00118
00119
00120
00121 U8 ru_step_length_cntr = 0;
00122 U8 new_position = MS_001;
00123 U8 ru_step_length = RAMP_UP_STEP_MAX;
00124 U8 ru_period_counter = 0;
00125 U8 ramp_up_index = 0;
00126 U8 motor_state = MS_STOP;
00127
00128
00130 inline void compute_new_position(void)
00131 {
00132 if (mci_get_motor_direction()==CCW)
00133 {
00134 switch(new_position)
00135 {
00136
00137 case MS_001:
00138 new_position = MS_011;
00139 break;
00140
00141 case MS_101:
00142 new_position = MS_001;
00143 break;
00144
00145 case MS_100:
00146 new_position = MS_101;
00147 break;
00148
00149 case MS_110:
00150 new_position = MS_100;
00151 break;
00152
00153 case MS_010:
00154 new_position = MS_110;
00155 break;
00156
00157 case MS_011:
00158 new_position = MS_010;
00159 break;
00160
00161 default :
00162 new_position = MS_001;
00163 break;
00164 }
00165
00166 }
00167 else
00168 {
00169 switch(new_position)
00170 {
00171
00172 case MS_001:
00173 new_position = MS_101;
00174 break;
00175
00176 case MS_101:
00177 new_position = MS_100;
00178 break;
00179
00180 case MS_100:
00181 new_position = MS_110;
00182 break;
00183
00184 case MS_110:
00185 new_position = MS_010;
00186 break;
00187
00188 case MS_010:
00189 new_position = MS_011;
00190 break;
00191
00192 case MS_011:
00193 new_position = MS_001;
00194 break;
00195
00196 default :
00197 new_position = MS_001;
00198 break;
00199 }
00200
00201 }
00202 }
00203
00204
00210 int main(void)
00211 {
00212 U8 temp;
00213
00214 mc_init();
00215
00216
00217 ushell_task_init();
00218
00219 mci_backward();
00220
00221 mci_run();
00222
00223 while(1)
00224 {
00225
00226
00227
00228 if (g_tick == TRUE)
00229 {
00230 g_tick = FALSE;
00231
00232
00233 mc_ADC_Scheduler();
00234
00235 switch(motor_state)
00236 {
00237 case (MS_ALIGN):
00238 ru_period_counter--;
00239 if (ru_period_counter==0)
00240 {
00241 Clear_PC7();
00242 motor_state=MS_RAMP_UP;
00243 }
00244 break;
00245
00246 case (MS_RAMP_UP):
00247
00248
00249
00250
00251 ru_period_counter += 1;
00252 if ( ru_period_counter >= RAMP_UP_PERIOD )
00253 {
00254 ru_period_counter = 0;
00255 ru_step_length = ramp_up_time_table[ramp_up_index];
00256 mc_duty_cycle(ramp_up_duty_table[ramp_up_index]);
00257 duty_cycle = ramp_up_duty_table[ramp_up_index];
00258 duty_cycle_reference = duty_cycle;
00259
00260 if (ramp_up_index < RAMP_UP_INDEX_MAX)
00261 {
00262 ramp_up_index += 1;
00263 }
00264 else
00265 {
00266 motor_state=MS_LAST_RAMP_UP;
00267
00268 }
00269 }
00270
00271
00272 ru_step_length_cntr += 1;
00273 if ( ru_step_length_cntr >= ru_step_length )
00274 {
00275 ru_step_length_cntr = 0;
00276 compute_new_position();
00277 mc_switch_commutation((Motor_Position)new_position);
00278
00279 }
00280 break;
00281
00282 case (MS_LAST_RAMP_UP):
00283
00284
00285 ru_step_length_cntr += 1;
00286 if ( ru_step_length_cntr >= ru_step_length )
00287 {
00288 ru_step_length_cntr = 0;
00289 compute_new_position();
00290 mc_switch_commutation((Motor_Position)new_position);
00291 start_running_phase();
00292 motor_state=MS_RUNNING;
00293 Set_PC7();
00294 }
00295 break;
00296
00297 case (MS_RUNNING):
00298 g_regulation_period += 1;
00299 if ( g_regulation_period >= 40 )
00300 {
00301 g_regulation_period = 0;
00302
00303 if (ushell_active == FALSE)
00304 {
00305
00306 temp = mc_get_potentiometer_value();
00307 if (temp > (POT_REF_MIN))
00308 {
00309 mci_set_ref_speed((U8)temp);
00310 mc_regulation_loop();
00311 mc_duty_cycle(mc_get_duty_cycle());
00312 }
00313 else
00314 {
00315 mci_set_ref_speed(POT_REF_MIN);
00316 mc_regulation_loop();
00317 mc_duty_cycle(mc_get_duty_cycle());
00318 }
00319 }
00320 else
00321 {
00322 mc_regulation_loop();
00323 mc_duty_cycle(mc_get_duty_cycle());
00324 }
00325 }
00326 break;
00327
00328 case (MS_STOP):
00329 if (ushell_active == FALSE)
00330 {
00331 temp = mc_get_potentiometer_value();
00332 if (temp > (POT_REF_MIN))
00333 {
00334 mci_run();
00335 }
00336 }
00337 break;
00338
00339 default :
00340 motor_state=MS_STOP;
00341 break;
00342 }
00343
00344
00345 ushell_task();
00346
00347 duty_cycle_filter ();
00348
00349
00350 }
00351
00352 }
00353 }