Motor control for robots. More compact, less object-oriented revision.

Dependencies:   FastPWM3 mbed-dev-f303

Fork of Hobbyking_Cheetah_V1 by Ben Katz

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers calibration.cpp Source File

calibration.cpp

00001 /// Calibration procedures for determining position sensor offset, 
00002 /// phase ordering, and position sensor linearization
00003 /// 
00004 
00005 #include "calibration.h"
00006 #include "foc.h"
00007 #include "PreferenceWriter.h"
00008 #include "user_config.h"
00009 #include "motor_config.h"
00010 #include "current_controller_config.h"
00011 
00012 void order_phases(PositionSensor *ps, GPIOStruct *gpio, ControllerStruct *controller, PreferenceWriter *prefs){   
00013     
00014     ///Checks phase order, to ensure that positive Q current produces
00015     ///torque in the positive direction wrt the position sensor.
00016     printf("\n\r Checking phase ordering\n\r");
00017     float theta_ref = 0;
00018     float theta_actual = 0;
00019     float v_d = .15f;                                                             //Put all volts on the D-Axis
00020     float v_q = 0.0f;
00021     float v_u, v_v, v_w = 0;
00022     float dtc_u, dtc_v, dtc_w = .5f;
00023     int sample_counter = 0;
00024     
00025     ///Set voltage angle to zero, wait for rotor position to settle
00026     abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                                 //inverse dq0 transform on voltages
00027     svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w);                            //space vector modulation
00028     for(int i = 0; i<20000; i++){
00029         TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-dtc_u);                                        // Set duty cycles
00030         TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_v);
00031         TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_w);
00032         wait_us(100);
00033         }
00034     //ps->ZeroPosition();
00035     ps->Sample(DT); 
00036     wait_us(1000);
00037     //float theta_start = ps->GetMechPositionFixed();                                  //get initial rotor position
00038     float theta_start;
00039     controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset);    //Calculate phase currents from ADC readings
00040     controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
00041     controller->i_a = -controller->i_b - controller->i_c;
00042     dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q);    //dq0 transform on currents
00043     float current = sqrt(pow(controller->i_d, 2) + pow(controller->i_q, 2));
00044     printf("\n\rCurrent\n\r");
00045     printf("%f    %f   %f\n\r\n\r", controller->i_d, controller->i_q, current);
00046     /// Rotate voltage angle
00047     while(theta_ref < 4*PI){                                                    //rotate for 2 electrical cycles
00048         abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                             //inverse dq0 transform on voltages
00049         svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w);                        //space vector modulation
00050         wait_us(100);
00051         TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-dtc_u);                                        //Set duty cycles
00052         TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_v);
00053         TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_w);
00054        ps->Sample(DT);                                                            //sample position sensor
00055        theta_actual = ps->GetMechPositionFixed();
00056        if(theta_ref==0){theta_start = theta_actual;}
00057        if(sample_counter > 200){
00058            sample_counter = 0 ;
00059         printf("%.4f   %.4f\n\r", theta_ref/(NPP), theta_actual);
00060         }
00061         sample_counter++;
00062        theta_ref += 0.001f;
00063         }
00064     float theta_end = ps->GetMechPositionFixed();
00065     int direction = (theta_end - theta_start)>0;
00066     printf("Theta Start:   %f    Theta End:  %f\n\r", theta_start, theta_end);
00067     printf("Direction:  %d\n\r", direction);
00068     if(direction){printf("Phasing correct\n\r");}
00069     else if(!direction){printf("Phasing incorrect.  Swapping phases V and W\n\r");}
00070     PHASE_ORDER = direction;
00071     }
00072     
00073     
00074 void calibrate(PositionSensor *ps, GPIOStruct *gpio, ControllerStruct *controller, PreferenceWriter *prefs){
00075     /// Measures the electrical angle offset of the position sensor
00076     /// and (in the future) corrects nonlinearity due to position sensor eccentricity
00077     printf("Starting calibration procedure\n\r");
00078     
00079     const int n = 128*NPP;                                                      // number of positions to be sampled per mechanical rotation.  Multiple of NPP for filtering reasons (see later)
00080     const int n2 = 50;                                                          // increments between saved samples (for smoothing motion)
00081     float delta = 2*PI*NPP/(n*n2);                                              // change in angle between samples
00082     float error_f[n] = {0};                                                     // error vector rotating forwards
00083     float error_b[n] = {0};                                                     // error vector rotating backwards
00084     const int  n_lut = 128;
00085     int lut[n_lut]= {0};                                                        // clear any old lookup table before starting.
00086     ps->WriteLUT(lut); 
00087     int raw_f[n] = {0};
00088     int raw_b[n] = {0};
00089     float theta_ref = 0;
00090     float theta_actual = 0;
00091     float v_d = .15f;                                                             // Put volts on the D-Axis
00092     float v_q = 0.0f;
00093     float v_u, v_v, v_w = 0;
00094     float dtc_u, dtc_v, dtc_w = .5f;
00095     
00096         
00097     ///Set voltage angle to zero, wait for rotor position to settle
00098     abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                                 // inverse dq0 transform on voltages
00099     svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w);                            // space vector modulation
00100     for(int i = 0; i<40000; i++){
00101         TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-dtc_u);                                        // Set duty cycles
00102         if(PHASE_ORDER){                                   
00103             TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_v);
00104             TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_w);
00105             }
00106         else{
00107             TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_v);
00108             TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_w);
00109             }
00110         wait_us(100);
00111         }
00112     ps->Sample(DT);   
00113     controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset);    //Calculate phase currents from ADC readings
00114     controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
00115     controller->i_a = -controller->i_b - controller->i_c;
00116     dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q);    //dq0 transform on currents
00117     float current = sqrt(pow(controller->i_d, 2) + pow(controller->i_q, 2));
00118     printf(" Current Angle : Rotor Angle : Raw Encoder \n\r\n\r");
00119     for(int i = 0; i<n; i++){                                                   // rotate forwards
00120        for(int j = 0; j<n2; j++){   
00121         theta_ref += delta;
00122        abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                              // inverse dq0 transform on voltages
00123        svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w);                         // space vector modulation
00124         TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-dtc_u);
00125         if(PHASE_ORDER){                                                        // Check phase ordering
00126             TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_v);                                    // Set duty cycles
00127             TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_w);
00128             }
00129         else{
00130             TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_v);
00131             TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_w);
00132             }
00133             wait_us(100);
00134             ps->Sample(DT);
00135         }
00136        ps->Sample(DT);
00137        theta_actual = ps->GetMechPositionFixed();
00138        error_f[i] = theta_ref/NPP - theta_actual;
00139        raw_f[i] = ps->GetRawPosition();
00140         printf("%.4f   %.4f    %d\n\r", theta_ref/(NPP), theta_actual, raw_f[i]);
00141        //theta_ref += delta;
00142         }
00143     
00144     for(int i = 0; i<n; i++){                                                   // rotate backwards
00145        for(int j = 0; j<n2; j++){
00146        theta_ref -= delta;
00147        abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                              // inverse dq0 transform on voltages
00148        svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w);                         // space vector modulation
00149         TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-dtc_u);
00150         if(PHASE_ORDER){
00151             TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_v);
00152             TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_w);
00153             }
00154         else{
00155             TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_v);
00156             TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_w);
00157             }
00158             wait_us(100);
00159             ps->Sample(DT);
00160         }
00161        ps->Sample(DT);                                                            // sample position sensor
00162        theta_actual = ps->GetMechPositionFixed();                                    // get mechanical position
00163        error_b[i] = theta_ref/NPP - theta_actual;
00164        raw_b[i] = ps->GetRawPosition();
00165        printf("%.4f   %.4f    %d\n\r", theta_ref/(NPP), theta_actual, raw_b[i]);
00166        //theta_ref -= delta;
00167         }    
00168         
00169         float offset = 0;                                  
00170         for(int i = 0; i<n; i++){
00171             offset += (error_f[i] + error_b[n-1-i])/(2.0f*n);                   // calclate average position sensor offset
00172             }
00173         offset = fmod(offset*NPP, 2*PI);                                        // convert mechanical angle to electrical angle
00174         
00175             
00176         ps->SetElecOffset(offset);                                              // Set position sensor offset
00177         __float_reg[0] = offset;
00178         E_OFFSET = offset;
00179         
00180         /// Perform filtering to linearize position sensor eccentricity
00181         /// FIR n-sample average, where n = number of samples in one electrical cycle
00182         /// This filter has zero gain at electrical frequency and all integer multiples
00183         /// So cogging effects should be completely filtered out.
00184         
00185         float error[n] = {0};
00186         const int window = 128;
00187         float error_filt[n] = {0};
00188         float cogging_current[window] = {0};
00189         float mean = 0;
00190         for (int i = 0; i<n; i++){                                              //Average the forward and back directions
00191             error[i] = 0.5f*(error_f[i] + error_b[n-i-1]);
00192             }
00193         for (int i = 0; i<n; i++){
00194             for(int j = 0; j<window; j++){
00195                 int ind = -window/2 + j + i;                                    // Indexes from -window/2 to + window/2
00196                 if(ind<0){
00197                     ind += n;}                                                  // Moving average wraps around
00198                 else if(ind > n-1) {
00199                     ind -= n;}
00200                 error_filt[i] += error[ind]/(float)window;
00201                 }
00202             if(i<window){
00203                 cogging_current[i] = current*sinf((error[i] - error_filt[i])*NPP);
00204                 }
00205             //printf("%.4f   %4f    %.4f   %.4f\n\r", error[i], error_filt[i], error_f[i], error_b[i]);
00206             mean += error_filt[i]/n;
00207             }
00208         int raw_offset = (raw_f[0] + raw_b[n-1])/2;                             //Insensitive to errors in this direction, so 2 points is plenty
00209         
00210         
00211         printf("\n\r Encoder non-linearity compensation table\n\r");
00212         printf(" Sample Number : Lookup Index : Lookup Value : Cogging Current Lookup\n\r\n\r");
00213         for (int i = 0; i<n_lut; i++){                                          // build lookup table
00214             int ind = (raw_offset>>7) + i;
00215             if(ind > (n_lut-1)){ 
00216                 ind -= n_lut;
00217                 }
00218             lut[ind] = (int) ((error_filt[i*NPP] - mean)*(float)(ps->GetCPR())/(2.0f*PI));
00219             printf("%d   %d   %d   %f\n\r", i, ind, lut[ind],  cogging_current[i]);
00220             wait(.001);
00221             }
00222             
00223         ps->WriteLUT(lut);                                                      // write lookup table to position sensor object
00224         //memcpy(controller->cogging, cogging_current, sizeof(controller->cogging));  //compensation doesn't actually work yet....
00225         memcpy(&ENCODER_LUT, lut, sizeof(lut));                                 // copy the lookup table to the flash array
00226         printf("\n\rEncoder Electrical Offset (rad) %f\n\r",  offset);
00227         
00228         if (!prefs->ready()) prefs->open();
00229         prefs->flush();                                                         // write offset and lookup table to flash
00230         prefs->close();
00231 
00232 
00233     }