123

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
benkatz
Date:
Fri Mar 31 18:24:46 2017 +0000
Revision:
22:60276ba87ac6
Child:
23:2adf23ee0305
Encoder autocalibration for dc offset and harmonics

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 22:60276ba87ac6 1
benkatz 22:60276ba87ac6 2 #include "foc.h"
benkatz 22:60276ba87ac6 3
benkatz 22:60276ba87ac6 4 //#include "FastMath.h"
benkatz 22:60276ba87ac6 5 //using namespace FastMath;
benkatz 22:60276ba87ac6 6
benkatz 22:60276ba87ac6 7
benkatz 22:60276ba87ac6 8 void abc( float theta, float d, float q, float *a, float *b, float *c){
benkatz 22:60276ba87ac6 9 ///Phase current amplitude = lengh of dq vector///
benkatz 22:60276ba87ac6 10 ///i.e. iq = 1, id = 0, peak phase current of 1///
benkatz 22:60276ba87ac6 11
benkatz 22:60276ba87ac6 12 *a = d*cosf(-theta) + q*sinf(-theta);
benkatz 22:60276ba87ac6 13 *b = d*cosf((2.0f*PI/3.0f)-theta) + q*sinf((2.0f*PI/3.0f)-theta);
benkatz 22:60276ba87ac6 14 *c = d*cosf((-2.0f*PI/3.0f)-theta) + q*sinf((-2.0f*PI/3.0f)-theta);
benkatz 22:60276ba87ac6 15 }
benkatz 22:60276ba87ac6 16
benkatz 22:60276ba87ac6 17 void dq0(float theta, float a, float b, float c, float *d, float *q){
benkatz 22:60276ba87ac6 18 ///Phase current amplitude = lengh of dq vector///
benkatz 22:60276ba87ac6 19 ///i.e. iq = 1, id = 0, peak phase current of 1///
benkatz 22:60276ba87ac6 20
benkatz 22:60276ba87ac6 21 *d = (2.0f/3.0f)*(a*cosf(-theta) + b*cosf((2.0f*PI/3.0f)-theta) + c*cosf((-2.0f*PI/3.0f)-theta));
benkatz 22:60276ba87ac6 22 *q = (2.0f/3.0f)*(a*sinf(-theta) + b*sinf((2.0f*PI/3.0f)-theta) + c*sinf((-2.0f*PI/3.0f)-theta));
benkatz 22:60276ba87ac6 23 }
benkatz 22:60276ba87ac6 24
benkatz 22:60276ba87ac6 25 void svm(float v_bus, float u, float v, float w, float *dtc_u, float *dtc_v, float *dtc_w){
benkatz 22:60276ba87ac6 26 ///u,v,w amplitude = v_bus for full modulation depth///
benkatz 22:60276ba87ac6 27
benkatz 22:60276ba87ac6 28 float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))/2.0f;
benkatz 22:60276ba87ac6 29 *dtc_u = fminf(fmaxf(((u - v_offset)*0.5f/v_bus + 0.5f), DTC_MIN), DTC_MAX);
benkatz 22:60276ba87ac6 30 *dtc_v = fminf(fmaxf(((v - v_offset)*0.5f/v_bus + 0.5f), DTC_MIN), DTC_MAX);
benkatz 22:60276ba87ac6 31 *dtc_w = fminf(fmaxf(((w - v_offset)*0.5f/v_bus + 0.5f), DTC_MIN), DTC_MAX);
benkatz 22:60276ba87ac6 32
benkatz 22:60276ba87ac6 33 }
benkatz 22:60276ba87ac6 34
benkatz 22:60276ba87ac6 35 void zero_current(int *offset_1, int *offset_2){
benkatz 22:60276ba87ac6 36 int adc1_offset = 0;
benkatz 22:60276ba87ac6 37 int adc2_offset = 0;
benkatz 22:60276ba87ac6 38 int n = 1024;
benkatz 22:60276ba87ac6 39 for (int i = 0; i<n; i++){
benkatz 22:60276ba87ac6 40 ADC1->CR2 |= 0x40000000;
benkatz 22:60276ba87ac6 41 wait(.001);
benkatz 22:60276ba87ac6 42 adc2_offset += ADC2->DR;
benkatz 22:60276ba87ac6 43 adc1_offset += ADC1->DR;
benkatz 22:60276ba87ac6 44 }
benkatz 22:60276ba87ac6 45 *offset_1 = adc1_offset/n;
benkatz 22:60276ba87ac6 46 *offset_2 = adc2_offset/n;
benkatz 22:60276ba87ac6 47 }
benkatz 22:60276ba87ac6 48
benkatz 22:60276ba87ac6 49 void reset_foc(ControllerStruct *controller){
benkatz 22:60276ba87ac6 50 controller->q_int = 0;
benkatz 22:60276ba87ac6 51 controller->d_int = 0;
benkatz 22:60276ba87ac6 52 }
benkatz 22:60276ba87ac6 53
benkatz 22:60276ba87ac6 54
benkatz 22:60276ba87ac6 55 void commutate(ControllerStruct *controller, GPIOStruct *gpio, float theta){
benkatz 22:60276ba87ac6 56
benkatz 22:60276ba87ac6 57 controller->loop_count ++;
benkatz 22:60276ba87ac6 58 if(gpio->phasing){
benkatz 22:60276ba87ac6 59 controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); //Calculate phase currents from ADC readings
benkatz 22:60276ba87ac6 60 controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 61 }
benkatz 22:60276ba87ac6 62 else{
benkatz 22:60276ba87ac6 63 controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); //Calculate phase currents from ADC readings
benkatz 22:60276ba87ac6 64 controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset);
benkatz 22:60276ba87ac6 65 }
benkatz 22:60276ba87ac6 66 controller->i_a = -controller->i_b - controller->i_c;
benkatz 22:60276ba87ac6 67
benkatz 22:60276ba87ac6 68
benkatz 22:60276ba87ac6 69 dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents
benkatz 22:60276ba87ac6 70
benkatz 22:60276ba87ac6 71 ///Controller///
benkatz 22:60276ba87ac6 72 float i_d_error = controller->i_d_ref - controller->i_d;
benkatz 22:60276ba87ac6 73 float i_q_error = controller->i_q_ref - controller->i_q;
benkatz 22:60276ba87ac6 74 float v_d_ff = 2.0f*(2*controller->i_d_ref*R_PHASE); //feed-forward voltage
benkatz 22:60276ba87ac6 75 float v_q_ff = 2.0f*(2*controller->i_q_ref*R_PHASE + controller->dtheta_elec*WB*0.8165f);
benkatz 22:60276ba87ac6 76 controller->d_int += i_d_error;
benkatz 22:60276ba87ac6 77 controller->q_int += i_q_error;
benkatz 22:60276ba87ac6 78
benkatz 22:60276ba87ac6 79 //v_d_ff = 0;
benkatz 22:60276ba87ac6 80 //v_q_ff = 0;
benkatz 22:60276ba87ac6 81
benkatz 22:60276ba87ac6 82 limit_norm(&controller->d_int, &controller->q_int, V_BUS/(K_Q*KI_Q));
benkatz 22:60276ba87ac6 83 //controller->d_int = fminf(fmaxf(controller->d_int, -D_INT_LIM), D_INT_LIM);
benkatz 22:60276ba87ac6 84 //controller->q_int = fminf(fmaxf(controller->q_int, -Q_INT_LIM), Q_INT_LIM);
benkatz 22:60276ba87ac6 85
benkatz 22:60276ba87ac6 86
benkatz 22:60276ba87ac6 87 controller->v_d = K_D*i_d_error + K_D*KI_D*controller->d_int;// + v_d_ff;
benkatz 22:60276ba87ac6 88 controller->v_q = K_Q*i_q_error + K_Q*KI_Q*controller->q_int;// + v_q_ff;
benkatz 22:60276ba87ac6 89
benkatz 22:60276ba87ac6 90 //controller->v_d = v_d_ff;
benkatz 22:60276ba87ac6 91 //controller->v_q = v_q_ff;
benkatz 22:60276ba87ac6 92
benkatz 22:60276ba87ac6 93 limit_norm(&controller->v_d, &controller->v_q, controller->v_bus);
benkatz 22:60276ba87ac6 94
benkatz 22:60276ba87ac6 95 abc(controller->theta_elec, controller->v_d, controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages
benkatz 22:60276ba87ac6 96 svm(controller->v_bus, controller->v_u, controller->v_v, controller->v_w, &controller->dtc_u, &controller->dtc_v, &controller->dtc_w); //space vector modulation
benkatz 22:60276ba87ac6 97
benkatz 22:60276ba87ac6 98 //gpio->pwm_u->write(1.0f-controller->dtc_u); //write duty cycles
benkatz 22:60276ba87ac6 99 //gpio->pwm_v->write(1.0f-controller->dtc_v);
benkatz 22:60276ba87ac6 100 //gpio->pwm_w->write(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 101
benkatz 22:60276ba87ac6 102 if(gpio->phasing){
benkatz 22:60276ba87ac6 103 TIM1->CCR3 = 0x708*(1.0f-controller->dtc_u);
benkatz 22:60276ba87ac6 104 TIM1->CCR2 = 0x708*(1.0f-controller->dtc_v);
benkatz 22:60276ba87ac6 105 TIM1->CCR1 = 0x708*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 106 }
benkatz 22:60276ba87ac6 107 else{
benkatz 22:60276ba87ac6 108 TIM1->CCR3 = 0x708*(1.0f-controller->dtc_u);
benkatz 22:60276ba87ac6 109 TIM1->CCR1 = 0x708*(1.0f-controller->dtc_v);
benkatz 22:60276ba87ac6 110 TIM1->CCR2 = 0x708*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 111 }
benkatz 22:60276ba87ac6 112 //gpio->pwm_u->write(1.0f - .05f); //write duty cycles
benkatz 22:60276ba87ac6 113 //gpio->pwm_v->write(1.0f - .05f);
benkatz 22:60276ba87ac6 114 //gpio->pwm_w->write(1.0f - .1f);
benkatz 22:60276ba87ac6 115 //TIM1->CCR1 = 0x708*(1.0f-controller->dtc_u);
benkatz 22:60276ba87ac6 116 //TIM1->CCR2 = 0x708*(1.0f-controller->dtc_v);
benkatz 22:60276ba87ac6 117 //TIM1->CCR3 = 0x708*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 118 controller->theta_elec = theta; //For some reason putting this at the front breaks thins
benkatz 22:60276ba87ac6 119
benkatz 22:60276ba87ac6 120
benkatz 22:60276ba87ac6 121 if(controller->loop_count >1000){
benkatz 22:60276ba87ac6 122 //controller->i_q_ref = -controller->i_q_ref;
benkatz 22:60276ba87ac6 123 controller->loop_count = 0;
benkatz 22:60276ba87ac6 124
benkatz 22:60276ba87ac6 125 //printf("%f\n\r", controller->dtheta_elec);
benkatz 22:60276ba87ac6 126 //printf("%f\n\r", controller->theta_elec);
benkatz 22:60276ba87ac6 127 //pc.printf("%f %f %f\n\r", controller->i_a, controller->i_b, controller->i_c);
benkatz 22:60276ba87ac6 128 //pc.printf("%f %f\n\r", controller->i_d, controller->i_q);
benkatz 22:60276ba87ac6 129 //pc.printf("%d %d\n\r", controller->adc1_raw, controller->adc2_raw);
benkatz 22:60276ba87ac6 130 }
benkatz 22:60276ba87ac6 131 }
benkatz 22:60276ba87ac6 132 /*
benkatz 22:60276ba87ac6 133 void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){
benkatz 22:60276ba87ac6 134
benkatz 22:60276ba87ac6 135 }
benkatz 22:60276ba87ac6 136 */