Alvee Ahmed
/
Hobbyking_Cheetah_Compact_DRV8323_14bit
customizing code for Aliexpress 14 bit version with MA702
FOC/foc.cpp@35:69b24894c11d, 2017-11-18 (annotated)
- Committer:
- benkatz
- Date:
- Sat Nov 18 18:41:42 2017 +0000
- Revision:
- 35:69b24894c11d
- Parent:
- 34:51647c6c500d
- Child:
- 37:c0f352d6e8e3
minor updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benkatz | 24:58c2d7571207 | 1 | #include "user_config.h" |
benkatz | 27:501fee691e0e | 2 | #include "hw_config.h" |
benkatz | 22:60276ba87ac6 | 3 | #include "foc.h" |
benkatz | 22:60276ba87ac6 | 4 | |
benkatz | 26:2b865c00d7e9 | 5 | #include "FastMath.h" |
benkatz | 26:2b865c00d7e9 | 6 | using namespace FastMath; |
benkatz | 22:60276ba87ac6 | 7 | |
benkatz | 22:60276ba87ac6 | 8 | |
benkatz | 22:60276ba87ac6 | 9 | void abc( float theta, float d, float q, float *a, float *b, float *c){ |
benkatz | 25:f5741040c4bb | 10 | /// Inverse DQ0 Transform /// |
benkatz | 22:60276ba87ac6 | 11 | ///Phase current amplitude = lengh of dq vector/// |
benkatz | 22:60276ba87ac6 | 12 | ///i.e. iq = 1, id = 0, peak phase current of 1/// |
benkatz | 22:60276ba87ac6 | 13 | |
benkatz | 26:2b865c00d7e9 | 14 | *a = d*cosf(theta) - q*sinf(theta); |
benkatz | 26:2b865c00d7e9 | 15 | *b = d*cosf(theta - (2.0f*PI/3.0f)) - q*sinf(theta - (2.0f*PI/3.0f)); |
benkatz | 26:2b865c00d7e9 | 16 | *c = d*cosf(theta + (2.0f*PI/3.0f)) - q*sinf(theta +(2.0f*PI/3.0f)); |
benkatz | 22:60276ba87ac6 | 17 | } |
benkatz | 22:60276ba87ac6 | 18 | |
benkatz | 26:2b865c00d7e9 | 19 | |
benkatz | 22:60276ba87ac6 | 20 | void dq0(float theta, float a, float b, float c, float *d, float *q){ |
benkatz | 25:f5741040c4bb | 21 | /// DQ0 Transform /// |
benkatz | 22:60276ba87ac6 | 22 | ///Phase current amplitude = lengh of dq vector/// |
benkatz | 22:60276ba87ac6 | 23 | ///i.e. iq = 1, id = 0, peak phase current of 1/// |
benkatz | 22:60276ba87ac6 | 24 | |
benkatz | 26:2b865c00d7e9 | 25 | //float cos = cosf(theta); |
benkatz | 26:2b865c00d7e9 | 26 | //float sin = sinf(theta); |
benkatz | 26:2b865c00d7e9 | 27 | |
benkatz | 26:2b865c00d7e9 | 28 | *d = (2.0f/3.0f)*(a*cosf(theta) + b*cosf(theta - (2.0f*PI/3.0f)) + c*cosf(theta + (2.0f*PI/3.0f))); |
benkatz | 26:2b865c00d7e9 | 29 | *q = (2.0f/3.0f)*(-a*sinf(theta) - b*sinf(theta - (2.0f*PI/3.0f)) - c*sinf(theta + (2.0f*PI/3.0f))); |
benkatz | 22:60276ba87ac6 | 30 | } |
benkatz | 22:60276ba87ac6 | 31 | |
benkatz | 22:60276ba87ac6 | 32 | void svm(float v_bus, float u, float v, float w, float *dtc_u, float *dtc_v, float *dtc_w){ |
benkatz | 25:f5741040c4bb | 33 | /// Space Vector Modulation /// |
benkatz | 25:f5741040c4bb | 34 | /// u,v,w amplitude = v_bus for full modulation depth /// |
benkatz | 22:60276ba87ac6 | 35 | |
benkatz | 22:60276ba87ac6 | 36 | float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))/2.0f; |
benkatz | 31:61eb6ae28215 | 37 | *dtc_u = fminf(fmaxf(((u -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 31:61eb6ae28215 | 38 | *dtc_v = fminf(fmaxf(((v -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 31:61eb6ae28215 | 39 | *dtc_w = fminf(fmaxf(((w -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 22:60276ba87ac6 | 40 | |
benkatz | 22:60276ba87ac6 | 41 | } |
benkatz | 22:60276ba87ac6 | 42 | |
benkatz | 25:f5741040c4bb | 43 | void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors |
benkatz | 22:60276ba87ac6 | 44 | int adc1_offset = 0; |
benkatz | 22:60276ba87ac6 | 45 | int adc2_offset = 0; |
benkatz | 22:60276ba87ac6 | 46 | int n = 1024; |
benkatz | 25:f5741040c4bb | 47 | for (int i = 0; i<n; i++){ // Average n samples of the ADC |
benkatz | 27:501fee691e0e | 48 | TIM1->CCR3 = (PWM_ARR>>1)*(1.0f); // Write duty cycles |
benkatz | 27:501fee691e0e | 49 | TIM1->CCR2 = (PWM_ARR>>1)*(1.0f); |
benkatz | 27:501fee691e0e | 50 | TIM1->CCR1 = (PWM_ARR>>1)*(1.0f); |
benkatz | 26:2b865c00d7e9 | 51 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 52 | wait(.001); |
benkatz | 22:60276ba87ac6 | 53 | adc2_offset += ADC2->DR; |
benkatz | 22:60276ba87ac6 | 54 | adc1_offset += ADC1->DR; |
benkatz | 22:60276ba87ac6 | 55 | } |
benkatz | 22:60276ba87ac6 | 56 | *offset_1 = adc1_offset/n; |
benkatz | 22:60276ba87ac6 | 57 | *offset_2 = adc2_offset/n; |
benkatz | 22:60276ba87ac6 | 58 | } |
benkatz | 22:60276ba87ac6 | 59 | |
benkatz | 22:60276ba87ac6 | 60 | void reset_foc(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 61 | TIM1->CCR3 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 62 | TIM1->CCR1 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 63 | TIM1->CCR2 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 64 | controller->i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 65 | controller->i_q_ref = 0; |
benkatz | 28:8c7e29f719c5 | 66 | controller->i_d = 0; |
benkatz | 28:8c7e29f719c5 | 67 | controller->i_q = 0; |
benkatz | 22:60276ba87ac6 | 68 | controller->q_int = 0; |
benkatz | 22:60276ba87ac6 | 69 | controller->d_int = 0; |
benkatz | 28:8c7e29f719c5 | 70 | controller->v_q = 0; |
benkatz | 28:8c7e29f719c5 | 71 | controller->v_d = 0; |
benkatz | 22:60276ba87ac6 | 72 | } |
benkatz | 22:60276ba87ac6 | 73 | |
benkatz | 22:60276ba87ac6 | 74 | |
benkatz | 22:60276ba87ac6 | 75 | void commutate(ControllerStruct *controller, GPIOStruct *gpio, float theta){ |
benkatz | 25:f5741040c4bb | 76 | /// Commutation Loop /// |
benkatz | 26:2b865c00d7e9 | 77 | |
benkatz | 25:f5741040c4bb | 78 | controller->loop_count ++; |
benkatz | 25:f5741040c4bb | 79 | if(PHASE_ORDER){ // Check current sensor ordering |
benkatz | 25:f5741040c4bb | 80 | controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings |
benkatz | 22:60276ba87ac6 | 81 | controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 82 | } |
benkatz | 22:60276ba87ac6 | 83 | else{ |
benkatz | 25:f5741040c4bb | 84 | controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 85 | controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); |
benkatz | 22:60276ba87ac6 | 86 | } |
benkatz | 26:2b865c00d7e9 | 87 | controller->i_a = -controller->i_b - controller->i_c; |
benkatz | 22:60276ba87ac6 | 88 | |
benkatz | 26:2b865c00d7e9 | 89 | float s = FastSin(theta); |
benkatz | 26:2b865c00d7e9 | 90 | float c = FastCos(theta); |
benkatz | 26:2b865c00d7e9 | 91 | //dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents |
benkatz | 27:501fee691e0e | 92 | controller->i_d = 0.6666667f*(c*controller->i_a + (0.86602540378f*s-.5f*c)*controller->i_b + (-0.86602540378f*s-.5f*c)*controller->i_c); ///Faster DQ0 Transform |
benkatz | 26:2b865c00d7e9 | 93 | controller->i_q = 0.6666667f*(-s*controller->i_a - (-0.86602540378f*c-.5f*s)*controller->i_b - (0.86602540378f*c-.5f*s)*controller->i_c); |
benkatz | 34:51647c6c500d | 94 | controller->i_q_filt = .95f*controller->i_q_filt + .05f*controller->i_q; |
benkatz | 34:51647c6c500d | 95 | float s_cog = sinf(12.0f*theta); |
benkatz | 34:51647c6c500d | 96 | float cogging_current =-0.33f*s_cog + .25f*s; |
benkatz | 25:f5741040c4bb | 97 | |
benkatz | 25:f5741040c4bb | 98 | /// PI Controller /// |
benkatz | 22:60276ba87ac6 | 99 | float i_d_error = controller->i_d_ref - controller->i_d; |
benkatz | 35:69b24894c11d | 100 | float i_q_error = controller->i_q_ref - controller->i_q + cogging_current; |
benkatz | 22:60276ba87ac6 | 101 | float v_d_ff = 2.0f*(2*controller->i_d_ref*R_PHASE); //feed-forward voltage |
benkatz | 28:8c7e29f719c5 | 102 | float v_q_ff = controller->dtheta_elec*WB*1.73205081f; |
benkatz | 22:60276ba87ac6 | 103 | controller->d_int += i_d_error; |
benkatz | 22:60276ba87ac6 | 104 | controller->q_int += i_q_error; |
benkatz | 22:60276ba87ac6 | 105 | |
benkatz | 22:60276ba87ac6 | 106 | //v_d_ff = 0; |
benkatz | 22:60276ba87ac6 | 107 | //v_q_ff = 0; |
benkatz | 22:60276ba87ac6 | 108 | |
benkatz | 31:61eb6ae28215 | 109 | limit_norm(&controller->d_int, &controller->q_int, V_BUS/(K_SCALE*I_BW*KI_Q)); // Limit integrators to prevent windup |
benkatz | 31:61eb6ae28215 | 110 | controller->v_d = K_SCALE*I_BW*i_d_error + K_SCALE*I_BW*KI_D*controller->d_int;// + v_d_ff; |
benkatz | 31:61eb6ae28215 | 111 | controller->v_q = K_SCALE*I_BW*i_q_error + K_SCALE*I_BW*KI_Q*controller->q_int;// + v_q_ff; |
benkatz | 22:60276ba87ac6 | 112 | |
benkatz | 34:51647c6c500d | 113 | //controller->v_q = 4.0f; |
benkatz | 28:8c7e29f719c5 | 114 | //controller->v_d = 0.0f; |
benkatz | 28:8c7e29f719c5 | 115 | |
benkatz | 22:60276ba87ac6 | 116 | //controller->v_d = v_d_ff; |
benkatz | 22:60276ba87ac6 | 117 | //controller->v_q = v_q_ff; |
benkatz | 22:60276ba87ac6 | 118 | |
benkatz | 29:0dbc822dd29a | 119 | limit_norm(&controller->v_d, &controller->v_q, 1.2f*controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus |
benkatz | 26:2b865c00d7e9 | 120 | //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 | 26:2b865c00d7e9 | 121 | |
benkatz | 27:501fee691e0e | 122 | controller->v_u = c*controller->v_d - s*controller->v_q; // Faster Inverse DQ0 transform |
benkatz | 26:2b865c00d7e9 | 123 | controller->v_v = (0.86602540378f*s-.5f*c)*controller->v_d - (-0.86602540378f*c-.5f*s)*controller->v_q; |
benkatz | 26:2b865c00d7e9 | 124 | controller->v_w = (-0.86602540378f*s-.5f*c)*controller->v_d - (0.86602540378f*c-.5f*s)*controller->v_q; |
benkatz | 22:60276ba87ac6 | 125 | 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 | 126 | |
benkatz | 22:60276ba87ac6 | 127 | |
benkatz | 25:f5741040c4bb | 128 | if(PHASE_ORDER){ // Check which phase order to use, |
benkatz | 31:61eb6ae28215 | 129 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles |
benkatz | 31:61eb6ae28215 | 130 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 31:61eb6ae28215 | 131 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 132 | } |
benkatz | 22:60276ba87ac6 | 133 | else{ |
benkatz | 32:ccac5da77844 | 134 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); |
benkatz | 32:ccac5da77844 | 135 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 32:ccac5da77844 | 136 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 137 | } |
benkatz | 25:f5741040c4bb | 138 | |
benkatz | 25:f5741040c4bb | 139 | controller->theta_elec = theta; //For some reason putting this at the front breaks thins |
benkatz | 22:60276ba87ac6 | 140 | |
benkatz | 22:60276ba87ac6 | 141 | |
benkatz | 31:61eb6ae28215 | 142 | if(controller->loop_count >400){ |
benkatz | 22:60276ba87ac6 | 143 | //controller->i_q_ref = -controller->i_q_ref; |
benkatz | 26:2b865c00d7e9 | 144 | controller->loop_count = 0; |
benkatz | 22:60276ba87ac6 | 145 | |
benkatz | 28:8c7e29f719c5 | 146 | //printf("%.2f %.2f %.2f\n\r", controller->i_a, controller->i_b, controller->i_c); |
benkatz | 31:61eb6ae28215 | 147 | //printf("%f\n\r", controller->dtheta_mech*GR); |
benkatz | 22:60276ba87ac6 | 148 | //pc.printf("%f %f %f\n\r", controller->i_a, controller->i_b, controller->i_c); |
benkatz | 22:60276ba87ac6 | 149 | //pc.printf("%f %f\n\r", controller->i_d, controller->i_q); |
benkatz | 22:60276ba87ac6 | 150 | //pc.printf("%d %d\n\r", controller->adc1_raw, controller->adc2_raw); |
benkatz | 26:2b865c00d7e9 | 151 | } |
benkatz | 22:60276ba87ac6 | 152 | } |
benkatz | 26:2b865c00d7e9 | 153 | |
benkatz | 26:2b865c00d7e9 | 154 | |
benkatz | 26:2b865c00d7e9 | 155 | void torque_control(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 156 | float torque_ref = controller->kp*(controller->p_des - controller->theta_mech) + controller->t_ff + controller->kd*(controller->v_des - controller->dtheta_mech); |
benkatz | 26:2b865c00d7e9 | 157 | //float torque_ref = -.1*(controller->p_des - controller->theta_mech); |
benkatz | 26:2b865c00d7e9 | 158 | controller->i_q_ref = torque_ref/KT_OUT; |
benkatz | 26:2b865c00d7e9 | 159 | controller->i_d_ref = 0; |
benkatz | 26:2b865c00d7e9 | 160 | } |
benkatz | 26:2b865c00d7e9 | 161 | |
benkatz | 26:2b865c00d7e9 | 162 | |
benkatz | 22:60276ba87ac6 | 163 | /* |
benkatz | 22:60276ba87ac6 | 164 | void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){ |
benkatz | 22:60276ba87ac6 | 165 | |
benkatz | 22:60276ba87ac6 | 166 | } |
benkatz | 22:60276ba87ac6 | 167 | */ |