Modified Motor Driver Firmware to include Flash + Thermal

Dependencies:   FastPWM3 mbed-dev-STM-lean

Committer:
benkatz
Date:
Fri Apr 07 16:23:39 2017 +0000
Revision:
24:58c2d7571207
Parent:
23:2adf23ee0305
Child:
25:f5741040c4bb
Can flush preferences multiple times now

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 22:60276ba87ac6 1 /// Calibration procedures for determining position sensor offset,
benkatz 22:60276ba87ac6 2 /// phase ordering, and position sensor linearization
benkatz 22:60276ba87ac6 3 ///
benkatz 22:60276ba87ac6 4
benkatz 22:60276ba87ac6 5 #include "calibration.h"
benkatz 23:2adf23ee0305 6 #include "foc.h"
benkatz 23:2adf23ee0305 7 #include "PreferenceWriter.h"
benkatz 23:2adf23ee0305 8 #include "user_config.h"
benkatz 22:60276ba87ac6 9
benkatz 23:2adf23ee0305 10 void order_phases(PositionSensor *ps, GPIOStruct *gpio, ControllerStruct *controller, PreferenceWriter *prefs){
benkatz 22:60276ba87ac6 11 ///Checks phase order, to ensure that positive Q current produces
benkatz 22:60276ba87ac6 12 ///torque in the positive direction wrt the position sensor.
benkatz 22:60276ba87ac6 13 printf("\n\r Checking phase ordering\n\r");
benkatz 22:60276ba87ac6 14 float theta_ref = 0;
benkatz 22:60276ba87ac6 15 float theta_actual = 0;
benkatz 23:2adf23ee0305 16 float v_d = .2; //Put all volts on the D-Axis
benkatz 22:60276ba87ac6 17 float v_q = 0.0;
benkatz 22:60276ba87ac6 18 float v_u, v_v, v_w = 0;
benkatz 22:60276ba87ac6 19 float dtc_u, dtc_v, dtc_w = .5;
benkatz 22:60276ba87ac6 20 int sample_counter = 0;
benkatz 22:60276ba87ac6 21
benkatz 22:60276ba87ac6 22 ///Set voltage angle to zero, wait for rotor position to settle
benkatz 23:2adf23ee0305 23 abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w); //inverse dq0 transform on voltages
benkatz 23:2adf23ee0305 24 svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w); //space vector modulation
benkatz 22:60276ba87ac6 25 for(int i = 0; i<20000; i++){
benkatz 23:2adf23ee0305 26 TIM1->CCR3 = 0x708*(1.0f-dtc_u); // Set duty cycles
benkatz 22:60276ba87ac6 27 TIM1->CCR2 = 0x708*(1.0f-dtc_v);
benkatz 22:60276ba87ac6 28 TIM1->CCR1 = 0x708*(1.0f-dtc_w);
benkatz 22:60276ba87ac6 29 wait_us(100);
benkatz 22:60276ba87ac6 30 }
benkatz 22:60276ba87ac6 31 //ps->ZeroPosition();
benkatz 22:60276ba87ac6 32 ps->Sample();
benkatz 23:2adf23ee0305 33 wait_us(1000);
benkatz 23:2adf23ee0305 34 float theta_start = ps->GetMechPosition(); //get initial rotor position
benkatz 23:2adf23ee0305 35 controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); //Calculate phase currents from ADC readings
benkatz 23:2adf23ee0305 36 controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 23:2adf23ee0305 37 controller->i_a = -controller->i_b - controller->i_c;
benkatz 23:2adf23ee0305 38 dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents
benkatz 23:2adf23ee0305 39 float current = sqrt(pow(controller->i_d, 2) + pow(controller->i_q, 2));
benkatz 23:2adf23ee0305 40 printf("\n\rCurrent\n\r");
benkatz 23:2adf23ee0305 41 printf("%f %f %f\n\r\n\r", controller->i_d, controller->i_q, current);
benkatz 22:60276ba87ac6 42 /// Rotate voltage angle
benkatz 23:2adf23ee0305 43 while(theta_ref < 4*PI){ //rotate for 2 electrical cycles
benkatz 23:2adf23ee0305 44 abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w); //inverse dq0 transform on voltages
benkatz 23:2adf23ee0305 45 svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w); //space vector modulation
benkatz 22:60276ba87ac6 46 wait_us(100);
benkatz 23:2adf23ee0305 47 TIM1->CCR3 = 0x708*(1.0f-dtc_u); //Set duty cycles
benkatz 22:60276ba87ac6 48 TIM1->CCR2 = 0x708*(1.0f-dtc_v);
benkatz 22:60276ba87ac6 49 TIM1->CCR1 = 0x708*(1.0f-dtc_w);
benkatz 23:2adf23ee0305 50 ps->Sample(); //sample position sensor
benkatz 22:60276ba87ac6 51 theta_actual = ps->GetMechPosition();
benkatz 22:60276ba87ac6 52 if(sample_counter > 200){
benkatz 22:60276ba87ac6 53 sample_counter = 0 ;
benkatz 22:60276ba87ac6 54 printf("%.4f %.4f\n\r", theta_ref/(NPP), theta_actual);
benkatz 22:60276ba87ac6 55 }
benkatz 22:60276ba87ac6 56 sample_counter++;
benkatz 22:60276ba87ac6 57 theta_ref += 0.001f;
benkatz 22:60276ba87ac6 58 }
benkatz 22:60276ba87ac6 59 float theta_end = ps->GetMechPosition();
benkatz 22:60276ba87ac6 60 int direction = (theta_end - theta_start)>0;
benkatz 22:60276ba87ac6 61 printf("Theta Start: %f Theta End: %f\n\r", theta_start, theta_end);
benkatz 22:60276ba87ac6 62 printf("Direction: %d\n\r", direction);
benkatz 23:2adf23ee0305 63 if(direction){printf("Phasing correct\n\r");}
benkatz 22:60276ba87ac6 64 else if(!direction){printf("Phasing incorrect. Swapping phases V and W\n\r");}
benkatz 23:2adf23ee0305 65 PHASE_ORDER = direction;
benkatz 22:60276ba87ac6 66 }
benkatz 22:60276ba87ac6 67
benkatz 22:60276ba87ac6 68
benkatz 23:2adf23ee0305 69 void calibrate(PositionSensor *ps, GPIOStruct *gpio, ControllerStruct *controller, PreferenceWriter *prefs){
benkatz 22:60276ba87ac6 70 /// Measures the electrical angle offset of the position sensor
benkatz 22:60276ba87ac6 71 /// and (in the future) corrects nonlinearity due to position sensor eccentricity
benkatz 22:60276ba87ac6 72 printf("Starting calibration procedure\n\r");
benkatz 22:60276ba87ac6 73
benkatz 23:2adf23ee0305 74 const int n = 128*NPP; // number of positions to be sampled per mechanical rotation. Multiple of NPP for filtering reasons (see later)
benkatz 23:2adf23ee0305 75 const int n2 = 10; // increments between saved samples (for smoothing motion)
benkatz 23:2adf23ee0305 76 float delta = 2*PI*NPP/(n*n2); // change in angle between samples
benkatz 23:2adf23ee0305 77 float error_f[n] = {0}; // error vector rotating forwards
benkatz 23:2adf23ee0305 78 float error_b[n] = {0}; // error vector rotating backwards
benkatz 22:60276ba87ac6 79 int raw_f[n] = {0};
benkatz 22:60276ba87ac6 80 int raw_b[n] = {0};
benkatz 22:60276ba87ac6 81 float theta_ref = 0;
benkatz 22:60276ba87ac6 82 float theta_actual = 0;
benkatz 23:2adf23ee0305 83 float v_d = .2; // Put volts on the D-Axis
benkatz 22:60276ba87ac6 84 float v_q = 0.0;
benkatz 22:60276ba87ac6 85 float v_u, v_v, v_w = 0;
benkatz 22:60276ba87ac6 86 float dtc_u, dtc_v, dtc_w = .5;
benkatz 22:60276ba87ac6 87
benkatz 22:60276ba87ac6 88
benkatz 22:60276ba87ac6 89 ///Set voltage angle to zero, wait for rotor position to settle
benkatz 23:2adf23ee0305 90 abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w); // inverse dq0 transform on voltages
benkatz 23:2adf23ee0305 91 svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w); // space vector modulation
benkatz 22:60276ba87ac6 92 for(int i = 0; i<40000; i++){
benkatz 23:2adf23ee0305 93 TIM1->CCR3 = 0x708*(1.0f-dtc_u); // Set duty cycles
benkatz 24:58c2d7571207 94 if(PHASE_ORDER){
benkatz 22:60276ba87ac6 95 TIM1->CCR2 = 0x708*(1.0f-dtc_v);
benkatz 22:60276ba87ac6 96 TIM1->CCR1 = 0x708*(1.0f-dtc_w);
benkatz 22:60276ba87ac6 97 }
benkatz 22:60276ba87ac6 98 else{
benkatz 22:60276ba87ac6 99 TIM1->CCR1 = 0x708*(1.0f-dtc_v);
benkatz 22:60276ba87ac6 100 TIM1->CCR2 = 0x708*(1.0f-dtc_w);
benkatz 22:60276ba87ac6 101 }
benkatz 22:60276ba87ac6 102 wait_us(100);
benkatz 22:60276ba87ac6 103 }
benkatz 22:60276ba87ac6 104 ps->Sample();
benkatz 23:2adf23ee0305 105 controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); //Calculate phase currents from ADC readings
benkatz 23:2adf23ee0305 106 controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 23:2adf23ee0305 107 controller->i_a = -controller->i_b - controller->i_c;
benkatz 23:2adf23ee0305 108 dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents
benkatz 23:2adf23ee0305 109 float current = sqrt(pow(controller->i_d, 2) + pow(controller->i_q, 2));
benkatz 23:2adf23ee0305 110 for(int i = 0; i<n; i++){ // rotate forwards
benkatz 22:60276ba87ac6 111 for(int j = 0; j<n2; j++){
benkatz 22:60276ba87ac6 112 theta_ref += delta;
benkatz 23:2adf23ee0305 113 abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w); // inverse dq0 transform on voltages
benkatz 23:2adf23ee0305 114 svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w); // space vector modulation
benkatz 22:60276ba87ac6 115 TIM1->CCR3 = 0x708*(1.0f-dtc_u);
benkatz 24:58c2d7571207 116 if(PHASE_ORDER){
benkatz 22:60276ba87ac6 117 TIM1->CCR2 = 0x708*(1.0f-dtc_v);
benkatz 22:60276ba87ac6 118 TIM1->CCR1 = 0x708*(1.0f-dtc_w);
benkatz 22:60276ba87ac6 119 }
benkatz 22:60276ba87ac6 120 else{
benkatz 22:60276ba87ac6 121 TIM1->CCR1 = 0x708*(1.0f-dtc_v);
benkatz 22:60276ba87ac6 122 TIM1->CCR2 = 0x708*(1.0f-dtc_w);
benkatz 22:60276ba87ac6 123 }
benkatz 22:60276ba87ac6 124 wait_us(100);
benkatz 22:60276ba87ac6 125 ps->Sample();
benkatz 22:60276ba87ac6 126 }
benkatz 22:60276ba87ac6 127 ps->Sample();
benkatz 22:60276ba87ac6 128 theta_actual = ps->GetMechPosition();
benkatz 22:60276ba87ac6 129 error_f[i] = theta_ref/NPP - theta_actual;
benkatz 22:60276ba87ac6 130 raw_f[i] = ps->GetRawPosition();
benkatz 22:60276ba87ac6 131 printf("%.4f %.4f %d\n\r", theta_ref/(NPP), theta_actual, raw_f[i]);
benkatz 22:60276ba87ac6 132 //theta_ref += delta;
benkatz 22:60276ba87ac6 133 }
benkatz 23:2adf23ee0305 134 printf(" Current Angle : Rotor Angle : Raw Encoder \n\r\n\r");
benkatz 23:2adf23ee0305 135 for(int i = 0; i<n; i++){ // rotate backwards
benkatz 22:60276ba87ac6 136 for(int j = 0; j<n2; j++){
benkatz 22:60276ba87ac6 137 theta_ref -= delta;
benkatz 23:2adf23ee0305 138 abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w); // inverse dq0 transform on voltages
benkatz 23:2adf23ee0305 139 svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w); // space vector modulation
benkatz 22:60276ba87ac6 140 TIM1->CCR3 = 0x708*(1.0f-dtc_u);
benkatz 24:58c2d7571207 141 if(PHASE_ORDER){
benkatz 22:60276ba87ac6 142 TIM1->CCR2 = 0x708*(1.0f-dtc_v);
benkatz 22:60276ba87ac6 143 TIM1->CCR1 = 0x708*(1.0f-dtc_w);
benkatz 22:60276ba87ac6 144 }
benkatz 22:60276ba87ac6 145 else{
benkatz 22:60276ba87ac6 146 TIM1->CCR1 = 0x708*(1.0f-dtc_v);
benkatz 22:60276ba87ac6 147 TIM1->CCR2 = 0x708*(1.0f-dtc_w);
benkatz 22:60276ba87ac6 148 }
benkatz 22:60276ba87ac6 149 wait_us(100);
benkatz 22:60276ba87ac6 150 ps->Sample();
benkatz 22:60276ba87ac6 151 }
benkatz 23:2adf23ee0305 152 ps->Sample(); // sample position sensor
benkatz 23:2adf23ee0305 153 theta_actual = ps->GetMechPosition(); // get mechanical position
benkatz 22:60276ba87ac6 154 error_b[i] = theta_ref/NPP - theta_actual;
benkatz 22:60276ba87ac6 155 raw_b[i] = ps->GetRawPosition();
benkatz 22:60276ba87ac6 156 printf("%.4f %.4f %d\n\r", theta_ref/(NPP), theta_actual, raw_b[i]);
benkatz 22:60276ba87ac6 157 //theta_ref -= delta;
benkatz 22:60276ba87ac6 158 }
benkatz 22:60276ba87ac6 159
benkatz 22:60276ba87ac6 160 float offset = 0;
benkatz 22:60276ba87ac6 161 for(int i = 0; i<n; i++){
benkatz 23:2adf23ee0305 162 offset += (error_f[i] + error_b[n-1-i])/(2.0f*n); // calclate average position sensor offset
benkatz 22:60276ba87ac6 163 }
benkatz 23:2adf23ee0305 164 offset = fmod(offset*NPP, 2*PI); // convert mechanical angle to electrical angle
benkatz 23:2adf23ee0305 165
benkatz 22:60276ba87ac6 166
benkatz 23:2adf23ee0305 167 ps->SetElecOffset(offset); // Set position sensor offset
benkatz 23:2adf23ee0305 168 __float_reg[0] = offset;
benkatz 23:2adf23ee0305 169 E_OFFSET = offset;
benkatz 22:60276ba87ac6 170
benkatz 22:60276ba87ac6 171 /// Perform filtering to linearize position sensor eccentricity
benkatz 22:60276ba87ac6 172 /// FIR n-sample average, where n = number of samples in one electrical cycle
benkatz 22:60276ba87ac6 173 /// This filter has zero gain at electrical frequency and all integer multiples
benkatz 23:2adf23ee0305 174 /// So cogging should be completely filtered out.
benkatz 22:60276ba87ac6 175
benkatz 22:60276ba87ac6 176 float error[n] = {0};
benkatz 23:2adf23ee0305 177 const int window = 128;
benkatz 22:60276ba87ac6 178 float error_filt[n] = {0};
benkatz 23:2adf23ee0305 179 float cogging_current[window] = {0};
benkatz 22:60276ba87ac6 180 float mean = 0;
benkatz 23:2adf23ee0305 181 for (int i = 0; i<n; i++){ //Average the forward and back directions
benkatz 22:60276ba87ac6 182 error[i] = 0.5f*(error_f[i] + error_b[n-i-1]);
benkatz 22:60276ba87ac6 183 }
benkatz 22:60276ba87ac6 184 for (int i = 0; i<n; i++){
benkatz 22:60276ba87ac6 185 for(int j = 0; j<window; j++){
benkatz 23:2adf23ee0305 186 int ind = -window/2 + j + i; // Indexes from -window/2 to + window/2
benkatz 22:60276ba87ac6 187 if(ind<0){
benkatz 23:2adf23ee0305 188 ind += n;} // Moving average wraps around
benkatz 22:60276ba87ac6 189 else if(ind > n-1) {
benkatz 22:60276ba87ac6 190 ind -= n;}
benkatz 22:60276ba87ac6 191 error_filt[i] += error[ind]/(float)window;
benkatz 22:60276ba87ac6 192 }
benkatz 23:2adf23ee0305 193 if(i<window){
benkatz 23:2adf23ee0305 194 cogging_current[i] = current*sinf((error[i] - error_filt[i])*NPP);
benkatz 23:2adf23ee0305 195 }
benkatz 22:60276ba87ac6 196 //printf("%.4f %4f %.4f %.4f\n\r", error[i], error_filt[i], error_f[i], error_b[i]);
benkatz 22:60276ba87ac6 197 mean += error_filt[i]/n;
benkatz 22:60276ba87ac6 198 }
benkatz 23:2adf23ee0305 199 int raw_offset = (raw_f[0] + raw_b[n-1])/2; //Insensitive to errors in this direction, so 2 points is plenty
benkatz 22:60276ba87ac6 200 const int n_lut = 128;
benkatz 22:60276ba87ac6 201 int lut[n_lut];
benkatz 23:2adf23ee0305 202 printf("\n\r Encoder non-linearity compensation table\n\r");
benkatz 23:2adf23ee0305 203 printf(" Sample Number : Lookup Index : Lookup Value : Cogging Current Lookup\n\r\n\r");
benkatz 23:2adf23ee0305 204 for (int i = 0; i<n_lut; i++){ // build lookup table
benkatz 22:60276ba87ac6 205 int ind = (raw_offset>>7) + i;
benkatz 22:60276ba87ac6 206 if(ind > (n_lut-1)){
benkatz 22:60276ba87ac6 207 ind -= n_lut;
benkatz 22:60276ba87ac6 208 }
benkatz 22:60276ba87ac6 209 lut[ind] = (int) ((error_filt[i*NPP] - mean)*(float)(ps->GetCPR())/(2.0f*PI));
benkatz 23:2adf23ee0305 210 printf("%d %d %d %f\n\r", i, ind, lut[ind], cogging_current[i]);
benkatz 22:60276ba87ac6 211 }
benkatz 23:2adf23ee0305 212
benkatz 23:2adf23ee0305 213 ps->WriteLUT(lut); // write lookup table to position sensor object
benkatz 23:2adf23ee0305 214 //memcpy(controller->cogging, cogging_current, sizeof(controller->cogging)); //compensation doesn't actually work yet....
benkatz 23:2adf23ee0305 215 memcpy(&ENCODER_LUT, lut, sizeof(lut));
benkatz 23:2adf23ee0305 216 printf("\n\rEncoder Electrical Offset (rad) %f\n\r", offset);
benkatz 22:60276ba87ac6 217
benkatz 23:2adf23ee0305 218 if (!prefs->ready()) prefs->open();
benkatz 23:2adf23ee0305 219 prefs->flush();
benkatz 23:2adf23ee0305 220 //prefs->close();
benkatz 23:2adf23ee0305 221
benkatz 23:2adf23ee0305 222
benkatz 22:60276ba87ac6 223 }