auto-measurements
Dependencies: FastPWM3 mbed-dev
Fork of Hobbyking_Cheetah_Compact by
FOC/foc.cpp@24:58c2d7571207, 2017-04-07 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
benkatz | 24:58c2d7571207 | 1 | #include "user_config.h" |
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 | 23:2adf23ee0305 | 29 | *dtc_u = fminf(fmaxf(((u - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX); |
benkatz | 23:2adf23ee0305 | 30 | *dtc_v = fminf(fmaxf(((v - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX); |
benkatz | 23:2adf23ee0305 | 31 | *dtc_w = fminf(fmaxf(((w - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), 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 | 24:58c2d7571207 | 58 | if(PHASE_ORDER){ |
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 | 23:2adf23ee0305 | 71 | ///Cogging Compensation Lookup/// |
benkatz | 23:2adf23ee0305 | 72 | //int ind = theta * (128.0f/(2.0f*PI)); |
benkatz | 23:2adf23ee0305 | 73 | //float cogging_current = controller->cogging[ind]; |
benkatz | 23:2adf23ee0305 | 74 | //float cogging_current = 1.0f*cos(6*theta); |
benkatz | 22:60276ba87ac6 | 75 | ///Controller/// |
benkatz | 22:60276ba87ac6 | 76 | float i_d_error = controller->i_d_ref - controller->i_d; |
benkatz | 23:2adf23ee0305 | 77 | float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current; |
benkatz | 22:60276ba87ac6 | 78 | float v_d_ff = 2.0f*(2*controller->i_d_ref*R_PHASE); //feed-forward voltage |
benkatz | 22:60276ba87ac6 | 79 | float v_q_ff = 2.0f*(2*controller->i_q_ref*R_PHASE + controller->dtheta_elec*WB*0.8165f); |
benkatz | 22:60276ba87ac6 | 80 | controller->d_int += i_d_error; |
benkatz | 22:60276ba87ac6 | 81 | controller->q_int += i_q_error; |
benkatz | 22:60276ba87ac6 | 82 | |
benkatz | 22:60276ba87ac6 | 83 | //v_d_ff = 0; |
benkatz | 22:60276ba87ac6 | 84 | //v_q_ff = 0; |
benkatz | 22:60276ba87ac6 | 85 | |
benkatz | 22:60276ba87ac6 | 86 | limit_norm(&controller->d_int, &controller->q_int, V_BUS/(K_Q*KI_Q)); |
benkatz | 22:60276ba87ac6 | 87 | //controller->d_int = fminf(fmaxf(controller->d_int, -D_INT_LIM), D_INT_LIM); |
benkatz | 22:60276ba87ac6 | 88 | //controller->q_int = fminf(fmaxf(controller->q_int, -Q_INT_LIM), Q_INT_LIM); |
benkatz | 22:60276ba87ac6 | 89 | |
benkatz | 22:60276ba87ac6 | 90 | |
benkatz | 22:60276ba87ac6 | 91 | controller->v_d = K_D*i_d_error + K_D*KI_D*controller->d_int;// + v_d_ff; |
benkatz | 22:60276ba87ac6 | 92 | controller->v_q = K_Q*i_q_error + K_Q*KI_Q*controller->q_int;// + v_q_ff; |
benkatz | 22:60276ba87ac6 | 93 | |
benkatz | 22:60276ba87ac6 | 94 | //controller->v_d = v_d_ff; |
benkatz | 22:60276ba87ac6 | 95 | //controller->v_q = v_q_ff; |
benkatz | 22:60276ba87ac6 | 96 | |
benkatz | 22:60276ba87ac6 | 97 | limit_norm(&controller->v_d, &controller->v_q, controller->v_bus); |
benkatz | 22:60276ba87ac6 | 98 | |
benkatz | 22:60276ba87ac6 | 99 | 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 | 100 | 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 | 101 | |
benkatz | 22:60276ba87ac6 | 102 | //gpio->pwm_u->write(1.0f-controller->dtc_u); //write duty cycles |
benkatz | 22:60276ba87ac6 | 103 | //gpio->pwm_v->write(1.0f-controller->dtc_v); |
benkatz | 22:60276ba87ac6 | 104 | //gpio->pwm_w->write(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 105 | |
benkatz | 24:58c2d7571207 | 106 | if(PHASE_ORDER){ |
benkatz | 22:60276ba87ac6 | 107 | TIM1->CCR3 = 0x708*(1.0f-controller->dtc_u); |
benkatz | 22:60276ba87ac6 | 108 | TIM1->CCR2 = 0x708*(1.0f-controller->dtc_v); |
benkatz | 22:60276ba87ac6 | 109 | TIM1->CCR1 = 0x708*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 110 | } |
benkatz | 22:60276ba87ac6 | 111 | else{ |
benkatz | 22:60276ba87ac6 | 112 | TIM1->CCR3 = 0x708*(1.0f-controller->dtc_u); |
benkatz | 22:60276ba87ac6 | 113 | TIM1->CCR1 = 0x708*(1.0f-controller->dtc_v); |
benkatz | 22:60276ba87ac6 | 114 | TIM1->CCR2 = 0x708*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 115 | } |
benkatz | 22:60276ba87ac6 | 116 | //gpio->pwm_u->write(1.0f - .05f); //write duty cycles |
benkatz | 22:60276ba87ac6 | 117 | //gpio->pwm_v->write(1.0f - .05f); |
benkatz | 22:60276ba87ac6 | 118 | //gpio->pwm_w->write(1.0f - .1f); |
benkatz | 22:60276ba87ac6 | 119 | //TIM1->CCR1 = 0x708*(1.0f-controller->dtc_u); |
benkatz | 22:60276ba87ac6 | 120 | //TIM1->CCR2 = 0x708*(1.0f-controller->dtc_v); |
benkatz | 22:60276ba87ac6 | 121 | //TIM1->CCR3 = 0x708*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 122 | controller->theta_elec = theta; //For some reason putting this at the front breaks thins |
benkatz | 22:60276ba87ac6 | 123 | |
benkatz | 22:60276ba87ac6 | 124 | |
benkatz | 23:2adf23ee0305 | 125 | if(controller->loop_count >400){ |
benkatz | 22:60276ba87ac6 | 126 | //controller->i_q_ref = -controller->i_q_ref; |
benkatz | 22:60276ba87ac6 | 127 | controller->loop_count = 0; |
benkatz | 22:60276ba87ac6 | 128 | |
benkatz | 23:2adf23ee0305 | 129 | //printf("%d %f\n\r", ind, cogging_current); |
benkatz | 22:60276ba87ac6 | 130 | //printf("%f\n\r", controller->theta_elec); |
benkatz | 22:60276ba87ac6 | 131 | //pc.printf("%f %f %f\n\r", controller->i_a, controller->i_b, controller->i_c); |
benkatz | 22:60276ba87ac6 | 132 | //pc.printf("%f %f\n\r", controller->i_d, controller->i_q); |
benkatz | 22:60276ba87ac6 | 133 | //pc.printf("%d %d\n\r", controller->adc1_raw, controller->adc2_raw); |
benkatz | 22:60276ba87ac6 | 134 | } |
benkatz | 22:60276ba87ac6 | 135 | } |
benkatz | 22:60276ba87ac6 | 136 | /* |
benkatz | 22:60276ba87ac6 | 137 | void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){ |
benkatz | 22:60276ba87ac6 | 138 | |
benkatz | 22:60276ba87ac6 | 139 | } |
benkatz | 22:60276ba87ac6 | 140 | */ |