Modified Motor Driver Firmware to include Flash + Thermal
Dependencies: FastPWM3 mbed-dev-STM-lean
FOC/foc.cpp@78:4f481ff040ee, 21 months ago (annotated)
- Committer:
- adimmit
- Date:
- Fri Nov 11 16:58:43 2022 +0000
- Revision:
- 78:4f481ff040ee
- Parent:
- 70:2ea6c555a2cb
changes w/ filter @ high speed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benkatz | 37:c0f352d6e8e3 | 1 | |
benkatz | 22:60276ba87ac6 | 2 | #include "foc.h" |
benkatz | 26:2b865c00d7e9 | 3 | using namespace FastMath; |
benkatz | 22:60276ba87ac6 | 4 | |
benkatz | 22:60276ba87ac6 | 5 | |
benkatz | 22:60276ba87ac6 | 6 | void abc( float theta, float d, float q, float *a, float *b, float *c){ |
benkatz | 25:f5741040c4bb | 7 | /// Inverse DQ0 Transform /// |
benkatz | 22:60276ba87ac6 | 8 | ///Phase current amplitude = lengh of dq vector/// |
benkatz | 22:60276ba87ac6 | 9 | ///i.e. iq = 1, id = 0, peak phase current of 1/// |
benkatz | 37:c0f352d6e8e3 | 10 | float cf = FastCos(theta); |
benkatz | 37:c0f352d6e8e3 | 11 | float sf = FastSin(theta); |
benkatz | 37:c0f352d6e8e3 | 12 | |
benkatz | 37:c0f352d6e8e3 | 13 | *a = cf*d - sf*q; // Faster Inverse DQ0 transform |
benkatz | 37:c0f352d6e8e3 | 14 | *b = (0.86602540378f*sf-.5f*cf)*d - (-0.86602540378f*cf-.5f*sf)*q; |
benkatz | 37:c0f352d6e8e3 | 15 | *c = (-0.86602540378f*sf-.5f*cf)*d - (0.86602540378f*cf-.5f*sf)*q; |
benkatz | 22:60276ba87ac6 | 16 | } |
benkatz | 22:60276ba87ac6 | 17 | |
benkatz | 26:2b865c00d7e9 | 18 | |
benkatz | 22:60276ba87ac6 | 19 | void dq0(float theta, float a, float b, float c, float *d, float *q){ |
benkatz | 25:f5741040c4bb | 20 | /// DQ0 Transform /// |
benkatz | 22:60276ba87ac6 | 21 | ///Phase current amplitude = lengh of dq vector/// |
benkatz | 22:60276ba87ac6 | 22 | ///i.e. iq = 1, id = 0, peak phase current of 1/// |
benkatz | 22:60276ba87ac6 | 23 | |
benkatz | 37:c0f352d6e8e3 | 24 | float cf = FastCos(theta); |
benkatz | 37:c0f352d6e8e3 | 25 | float sf = FastSin(theta); |
benkatz | 26:2b865c00d7e9 | 26 | |
benkatz | 37:c0f352d6e8e3 | 27 | *d = 0.6666667f*(cf*a + (0.86602540378f*sf-.5f*cf)*b + (-0.86602540378f*sf-.5f*cf)*c); ///Faster DQ0 Transform |
benkatz | 37:c0f352d6e8e3 | 28 | *q = 0.6666667f*(-sf*a - (-0.86602540378f*cf-.5f*sf)*b - (0.86602540378f*cf-.5f*sf)*c); |
benkatz | 37:c0f352d6e8e3 | 29 | |
benkatz | 22:60276ba87ac6 | 30 | } |
benkatz | 22:60276ba87ac6 | 31 | |
benkatz | 55:c4c9fec8539c | 32 | void svm(float v_bus, float u, float v, float w, int i_sector, 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 | 48:74a40481740c | 36 | float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))*0.5f; |
benkatz | 47:e1196a851f76 | 37 | |
benkatz | 55:c4c9fec8539c | 38 | // Dead-time compensation |
benkatz | 55:c4c9fec8539c | 39 | float u_comp = DTC_COMP*(-(i_sector==4) + (i_sector==3)); |
benkatz | 55:c4c9fec8539c | 40 | float v_comp = DTC_COMP*(-(i_sector==2) + (i_sector==5)); |
benkatz | 55:c4c9fec8539c | 41 | float w_comp = DTC_COMP*((i_sector==6) - (i_sector==1)); |
benkatz | 55:c4c9fec8539c | 42 | |
benkatz | 55:c4c9fec8539c | 43 | |
benkatz | 55:c4c9fec8539c | 44 | *dtc_u = fminf(fmaxf((.5f*(u -v_offset)/(v_bus*(DTC_MAX-DTC_MIN)) + (DTC_MAX+DTC_MIN)*.5f + u_comp), DTC_MIN), DTC_MAX); |
benkatz | 55:c4c9fec8539c | 45 | *dtc_v = fminf(fmaxf((.5f*(v -v_offset)/(v_bus*(DTC_MAX-DTC_MIN)) + (DTC_MAX+DTC_MIN)*.5f + v_comp), DTC_MIN), DTC_MAX); |
benkatz | 55:c4c9fec8539c | 46 | *dtc_w = fminf(fmaxf((.5f*(w -v_offset)/(v_bus*(DTC_MAX-DTC_MIN)) + (DTC_MAX+DTC_MIN)*.5f + w_comp), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 47 | |
benkatz | 47:e1196a851f76 | 48 | /* |
benkatz | 47:e1196a851f76 | 49 | sinusoidal pwm |
benkatz | 47:e1196a851f76 | 50 | *dtc_u = fminf(fmaxf((u/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 51 | *dtc_v = fminf(fmaxf((v/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 52 | *dtc_w = fminf(fmaxf((w/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 53 | */ |
benkatz | 47:e1196a851f76 | 54 | |
benkatz | 22:60276ba87ac6 | 55 | |
benkatz | 22:60276ba87ac6 | 56 | } |
benkatz | 22:60276ba87ac6 | 57 | |
benkatz | 25:f5741040c4bb | 58 | void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors |
benkatz | 22:60276ba87ac6 | 59 | int adc1_offset = 0; |
benkatz | 22:60276ba87ac6 | 60 | int adc2_offset = 0; |
benkatz | 22:60276ba87ac6 | 61 | int n = 1024; |
benkatz | 25:f5741040c4bb | 62 | for (int i = 0; i<n; i++){ // Average n samples of the ADC |
benkatz | 27:501fee691e0e | 63 | TIM1->CCR3 = (PWM_ARR>>1)*(1.0f); // Write duty cycles |
benkatz | 27:501fee691e0e | 64 | TIM1->CCR2 = (PWM_ARR>>1)*(1.0f); |
benkatz | 27:501fee691e0e | 65 | TIM1->CCR1 = (PWM_ARR>>1)*(1.0f); |
benkatz | 26:2b865c00d7e9 | 66 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 67 | wait(.001); |
benkatz | 22:60276ba87ac6 | 68 | adc2_offset += ADC2->DR; |
benkatz | 22:60276ba87ac6 | 69 | adc1_offset += ADC1->DR; |
benkatz | 22:60276ba87ac6 | 70 | } |
benkatz | 22:60276ba87ac6 | 71 | *offset_1 = adc1_offset/n; |
benkatz | 22:60276ba87ac6 | 72 | *offset_2 = adc2_offset/n; |
benkatz | 22:60276ba87ac6 | 73 | } |
benkatz | 45:26801179208e | 74 | |
benkatz | 45:26801179208e | 75 | void init_controller_params(ControllerStruct *controller){ |
benkatz | 45:26801179208e | 76 | controller->ki_d = KI_D; |
benkatz | 45:26801179208e | 77 | controller->ki_q = KI_Q; |
benkatz | 45:26801179208e | 78 | controller->k_d = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 79 | controller->k_q = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 80 | controller->alpha = 1.0f - 1.0f/(1.0f - DT*I_BW*2.0f*PI); |
benkatz | 55:c4c9fec8539c | 81 | for(int i = 0; i<128; i++) |
benkatz | 55:c4c9fec8539c | 82 | { |
benkatz | 55:c4c9fec8539c | 83 | controller->inverter_tab[i] = 1.0f + 1.2f*exp(-0.0078125f*i/.032f); |
benkatz | 55:c4c9fec8539c | 84 | } |
benkatz | 45:26801179208e | 85 | } |
benkatz | 22:60276ba87ac6 | 86 | |
benkatz | 22:60276ba87ac6 | 87 | void reset_foc(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 88 | TIM1->CCR3 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 89 | TIM1->CCR1 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 90 | TIM1->CCR2 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 91 | controller->i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 92 | controller->i_q_ref = 0; |
benkatz | 28:8c7e29f719c5 | 93 | controller->i_d = 0; |
benkatz | 28:8c7e29f719c5 | 94 | controller->i_q = 0; |
benkatz | 37:c0f352d6e8e3 | 95 | controller->i_q_filt = 0; |
benkatz | 22:60276ba87ac6 | 96 | controller->q_int = 0; |
benkatz | 22:60276ba87ac6 | 97 | controller->d_int = 0; |
benkatz | 28:8c7e29f719c5 | 98 | controller->v_q = 0; |
benkatz | 28:8c7e29f719c5 | 99 | controller->v_d = 0; |
benkatz | 55:c4c9fec8539c | 100 | controller->otw_flag = 0; |
elijahsj | 69:ba1c89835a54 | 101 | controller->velocity_sum = 0; |
benkatz | 47:e1196a851f76 | 102 | |
benkatz | 22:60276ba87ac6 | 103 | } |
benkatz | 45:26801179208e | 104 | |
benkatz | 48:74a40481740c | 105 | void reset_observer(ObserverStruct *observer){ |
benkatz | 55:c4c9fec8539c | 106 | |
benkatz | 48:74a40481740c | 107 | observer->temperature = 25.0f; |
benkatz | 55:c4c9fec8539c | 108 | observer->temp_measured = 25.0f; |
benkatz | 55:c4c9fec8539c | 109 | //observer->resistance = .1f; |
benkatz | 48:74a40481740c | 110 | } |
benkatz | 48:74a40481740c | 111 | |
benkatz | 45:26801179208e | 112 | void limit_current_ref (ControllerStruct *controller){ |
benkatz | 45:26801179208e | 113 | float i_q_max_limit = (0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; |
benkatz | 45:26801179208e | 114 | float i_q_min_limit = (-0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; |
benkatz | 45:26801179208e | 115 | controller->i_q_ref = fmaxf(fminf(i_q_max_limit, controller->i_q_ref), i_q_min_limit); |
benkatz | 45:26801179208e | 116 | } |
benkatz | 22:60276ba87ac6 | 117 | |
benkatz | 55:c4c9fec8539c | 118 | void update_observer(ControllerStruct *controller, ObserverStruct *observer) |
benkatz | 55:c4c9fec8539c | 119 | { |
benkatz | 55:c4c9fec8539c | 120 | /// Update observer estimates /// |
benkatz | 55:c4c9fec8539c | 121 | // Resistance observer // |
benkatz | 55:c4c9fec8539c | 122 | // Temperature Observer // |
benkatz | 55:c4c9fec8539c | 123 | observer->delta_t = (float)observer->temperature - T_AMBIENT; |
benkatz | 55:c4c9fec8539c | 124 | float i_sq = controller->i_d*controller->i_d + controller->i_q*controller->i_q; |
benkatz | 55:c4c9fec8539c | 125 | observer->q_in = (R_NOMINAL*1.5f)*(1.0f + .00393f*observer->delta_t)*i_sq; |
benkatz | 55:c4c9fec8539c | 126 | observer->q_out = observer->delta_t*R_TH; |
benkatz | 55:c4c9fec8539c | 127 | observer->temperature += (INV_M_TH*DT)*(observer->q_in-observer->q_out); |
benkatz | 55:c4c9fec8539c | 128 | |
benkatz | 55:c4c9fec8539c | 129 | //float r_d = (controller->v_d*(DTC_MAX-DTC_MIN) + SQRT3*controller->dtheta_elec*(L_Q*controller->i_q))/(controller->i_d*SQRT3); |
benkatz | 55:c4c9fec8539c | 130 | float r_q = (controller->v_q*(DTC_MAX-DTC_MIN) - SQRT3*controller->dtheta_elec*(L_D*controller->i_d + WB))/(controller->i_q*SQRT3); |
benkatz | 55:c4c9fec8539c | 131 | observer->resistance = r_q;//(r_d*controller->i_d + r_q*controller->i_q)/(controller->i_d + controller->i_q); // voltages more accurate at higher duty cycles |
benkatz | 55:c4c9fec8539c | 132 | |
benkatz | 55:c4c9fec8539c | 133 | //observer->resistance = controller->v_q/controller->i_q; |
benkatz | 55:c4c9fec8539c | 134 | if(isnan(observer->resistance) || isinf(observer->resistance)){observer->resistance = R_NOMINAL;} |
benkatz | 55:c4c9fec8539c | 135 | float t_raw = ((T_AMBIENT + ((observer->resistance/R_NOMINAL) - 1.0f)*254.5f)); |
benkatz | 55:c4c9fec8539c | 136 | if(t_raw > 200.0f){t_raw = 200.0f;} |
benkatz | 55:c4c9fec8539c | 137 | else if(t_raw < 0.0f){t_raw = 0.0f;} |
benkatz | 55:c4c9fec8539c | 138 | observer->temp_measured = .999f*observer->temp_measured + .001f*t_raw; |
benkatz | 55:c4c9fec8539c | 139 | float e = (float)observer->temperature - observer->temp_measured; |
benkatz | 55:c4c9fec8539c | 140 | observer->trust = (1.0f - .004f*fminf(abs(controller->dtheta_elec), 250.0f)) * (.01f*(fminf(i_sq, 100.0f))); |
benkatz | 55:c4c9fec8539c | 141 | observer->temperature -= observer->trust*.0001f*e; |
benkatz | 55:c4c9fec8539c | 142 | //printf("%.3f\n\r", e); |
benkatz | 55:c4c9fec8539c | 143 | |
benkatz | 55:c4c9fec8539c | 144 | if(observer->temperature > TEMP_MAX){controller->otw_flag = 1;} |
benkatz | 55:c4c9fec8539c | 145 | else{controller->otw_flag = 0;} |
benkatz | 55:c4c9fec8539c | 146 | } |
benkatz | 22:60276ba87ac6 | 147 | |
benkatz | 55:c4c9fec8539c | 148 | float linearize_dtc(ControllerStruct *controller, float dtc) |
benkatz | 55:c4c9fec8539c | 149 | { |
benkatz | 55:c4c9fec8539c | 150 | float duty = fmaxf(fminf(abs(dtc), .999f), 0.0f);; |
benkatz | 55:c4c9fec8539c | 151 | int index = (int) (duty*127.0f); |
benkatz | 55:c4c9fec8539c | 152 | float val1 = controller->inverter_tab[index]; |
benkatz | 55:c4c9fec8539c | 153 | float val2 = controller->inverter_tab[index+1]; |
benkatz | 55:c4c9fec8539c | 154 | return val1 + (val2 - val1)*(duty*128.0f - (float)index); |
benkatz | 55:c4c9fec8539c | 155 | } |
benkatz | 47:e1196a851f76 | 156 | |
benkatz | 55:c4c9fec8539c | 157 | void field_weaken(ControllerStruct *controller) |
benkatz | 55:c4c9fec8539c | 158 | { |
benkatz | 55:c4c9fec8539c | 159 | /// Field Weakening /// |
benkatz | 55:c4c9fec8539c | 160 | |
benkatz | 55:c4c9fec8539c | 161 | controller->fw_int += .001f*(0.5f*OVERMODULATION*controller->v_bus - controller->v_ref); |
benkatz | 55:c4c9fec8539c | 162 | controller->fw_int = fmaxf(fminf(controller->fw_int, 0.0f), -I_FW_MAX); |
benkatz | 55:c4c9fec8539c | 163 | controller->i_d_ref = controller->fw_int; |
benkatz | 55:c4c9fec8539c | 164 | float q_max = sqrt(controller->i_max*controller->i_max - controller->i_d_ref*controller->i_d_ref); |
benkatz | 55:c4c9fec8539c | 165 | controller->i_q_ref = fmaxf(fminf(controller->i_q_ref, q_max), -q_max); |
benkatz | 55:c4c9fec8539c | 166 | //float i_cmd_mag_sq = controller->i_d_ref*controller->i_d_ref + controller->i_q_ref*controller->i_q_ref; |
benkatz | 55:c4c9fec8539c | 167 | |
benkatz | 55:c4c9fec8539c | 168 | } |
benkatz | 55:c4c9fec8539c | 169 | void commutate(ControllerStruct *controller, ObserverStruct *observer, GPIOStruct *gpio, float theta) |
benkatz | 55:c4c9fec8539c | 170 | { |
benkatz | 25:f5741040c4bb | 171 | /// Commutation Loop /// |
benkatz | 25:f5741040c4bb | 172 | controller->loop_count ++; |
benkatz | 25:f5741040c4bb | 173 | if(PHASE_ORDER){ // Check current sensor ordering |
benkatz | 25:f5741040c4bb | 174 | controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings |
benkatz | 22:60276ba87ac6 | 175 | controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 176 | } |
benkatz | 22:60276ba87ac6 | 177 | else{ |
benkatz | 25:f5741040c4bb | 178 | controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 179 | controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); |
benkatz | 22:60276ba87ac6 | 180 | } |
benkatz | 26:2b865c00d7e9 | 181 | controller->i_a = -controller->i_b - controller->i_c; |
benkatz | 55:c4c9fec8539c | 182 | if((abs(controller->i_b) > 41.0f)|(abs(controller->i_c) > 41.0f)|(abs(controller->i_a) > 41.0f)){controller->oc_flag = 1;} |
benkatz | 22:60276ba87ac6 | 183 | |
benkatz | 26:2b865c00d7e9 | 184 | float s = FastSin(theta); |
benkatz | 26:2b865c00d7e9 | 185 | float c = FastCos(theta); |
benkatz | 44:8040fa2fcb0d | 186 | dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents |
benkatz | 44:8040fa2fcb0d | 187 | //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 | 44:8040fa2fcb0d | 188 | //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 | 37:c0f352d6e8e3 | 189 | |
benkatz | 37:c0f352d6e8e3 | 190 | controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q; |
benkatz | 47:e1196a851f76 | 191 | controller->i_d_filt = 0.95f*controller->i_d_filt + 0.05f*controller->i_d; |
benkatz | 37:c0f352d6e8e3 | 192 | |
benkatz | 45:26801179208e | 193 | |
benkatz | 49:83d83040ea51 | 194 | // Filter the current references to the desired closed-loop bandwidth |
benkatz | 55:c4c9fec8539c | 195 | //controller->i_d_ref_filt = (1.0f-controller->alpha)*controller->i_d_ref_filt + controller->alpha*controller->i_d_ref; |
benkatz | 55:c4c9fec8539c | 196 | //controller->i_q_ref_filt = (1.0f-controller->alpha)*controller->i_q_ref_filt + controller->alpha*controller->i_q_ref; |
benkatz | 55:c4c9fec8539c | 197 | |
benkatz | 55:c4c9fec8539c | 198 | controller->i_max = I_MAX*(!controller->otw_flag) + I_MAX_CONT*controller->otw_flag; |
benkatz | 55:c4c9fec8539c | 199 | |
benkatz | 55:c4c9fec8539c | 200 | // Temperature Controller // |
benkatz | 55:c4c9fec8539c | 201 | /* |
benkatz | 55:c4c9fec8539c | 202 | if(observer->temperature > TEMP_MAX) |
benkatz | 55:c4c9fec8539c | 203 | { |
benkatz | 55:c4c9fec8539c | 204 | float qdot_des = 1.0f*(TEMP_MAX - observer->temperature); |
benkatz | 55:c4c9fec8539c | 205 | float i_limit = sqrt((qdot_des + observer->q_out)/(R_NOMINAL*1.5f)); |
benkatz | 55:c4c9fec8539c | 206 | controller->i_max = fmaxf(fminf(i_limit, I_MAX), I_MAX_CONT); |
benkatz | 55:c4c9fec8539c | 207 | } |
benkatz | 55:c4c9fec8539c | 208 | else{controller->i_max = I_MAX;} |
benkatz | 55:c4c9fec8539c | 209 | */ |
benkatz | 55:c4c9fec8539c | 210 | |
benkatz | 55:c4c9fec8539c | 211 | limit_norm(&controller->i_d_ref, &controller->i_q_ref, controller->i_max); |
benkatz | 48:74a40481740c | 212 | |
benkatz | 25:f5741040c4bb | 213 | /// PI Controller /// |
benkatz | 22:60276ba87ac6 | 214 | float i_d_error = controller->i_d_ref - controller->i_d; |
benkatz | 45:26801179208e | 215 | float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current; |
benkatz | 37:c0f352d6e8e3 | 216 | |
benkatz | 45:26801179208e | 217 | // Calculate feed-forward voltages // |
benkatz | 55:c4c9fec8539c | 218 | float v_d_ff = SQRT3*(0.0f*controller->i_d_ref*R_PHASE - controller->dtheta_elec*L_Q*controller->i_q); //feed-forward voltages |
benkatz | 55:c4c9fec8539c | 219 | float v_q_ff = SQRT3*(0.0f*controller->i_q_ref*R_PHASE + controller->dtheta_elec*(L_D*controller->i_d + 0.0f*WB)); |
benkatz | 37:c0f352d6e8e3 | 220 | |
benkatz | 45:26801179208e | 221 | // Integrate Error // |
benkatz | 47:e1196a851f76 | 222 | controller->d_int += controller->k_d*controller->ki_d*i_d_error; |
benkatz | 47:e1196a851f76 | 223 | controller->q_int += controller->k_q*controller->ki_q*i_q_error; |
benkatz | 47:e1196a851f76 | 224 | |
benkatz | 47:e1196a851f76 | 225 | controller->d_int = fmaxf(fminf(controller->d_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 226 | controller->q_int = fmaxf(fminf(controller->q_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 227 | |
benkatz | 47:e1196a851f76 | 228 | //limit_norm(&controller->d_int, &controller->q_int, OVERMODULATION*controller->v_bus); |
benkatz | 55:c4c9fec8539c | 229 | controller->v_d = controller->k_d*i_d_error + controller->d_int;// + v_d_ff; |
benkatz | 55:c4c9fec8539c | 230 | controller->v_q = controller->k_q*i_q_error + controller->q_int;// + v_q_ff; |
benkatz | 55:c4c9fec8539c | 231 | //controller->v_q = 0.0f; |
benkatz | 55:c4c9fec8539c | 232 | //controller->v_d = 1.0f*controller->v_bus; |
benkatz | 47:e1196a851f76 | 233 | controller->v_ref = sqrt(controller->v_d*controller->v_d + controller->v_q*controller->v_q); |
benkatz | 22:60276ba87ac6 | 234 | |
benkatz | 44:8040fa2fcb0d | 235 | limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus |
benkatz | 55:c4c9fec8539c | 236 | float dtc = controller->v_ref/controller->v_bus; |
benkatz | 55:c4c9fec8539c | 237 | float scale = linearize_dtc(controller, dtc); |
benkatz | 55:c4c9fec8539c | 238 | //controller->v_d = scale*controller->v_d; |
benkatz | 55:c4c9fec8539c | 239 | //controller->v_q = scale*controller->v_q; |
benkatz | 53:e85efce8c1eb | 240 | //float dtc_q = controller->v_q/controller->v_bus; |
benkatz | 55:c4c9fec8539c | 241 | |
benkatz | 53:e85efce8c1eb | 242 | //linearize_dtc(&dtc_q); |
benkatz | 53:e85efce8c1eb | 243 | //controller->v_d = dtc_d*controller->v_bus; |
benkatz | 53:e85efce8c1eb | 244 | //controller->v_q = dtc_q*controller->v_bus; |
benkatz | 55:c4c9fec8539c | 245 | abc(controller->theta_elec + 0.0f*DT*controller->dtheta_elec, scale*controller->v_d, scale*controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages |
benkatz | 55:c4c9fec8539c | 246 | controller->current_sector = ((controller->i_a>0)<<2)|((controller->i_b>0)<<1)|(controller->i_c>0); |
saloutos | 58:32e8927fe39f | 247 | svm(controller->v_bus, controller->v_u, controller->v_v, controller->v_w, controller->current_sector, &controller->dtc_u, &controller->dtc_v, &controller->dtc_w); //space vector modulation |
benkatz | 22:60276ba87ac6 | 248 | |
benkatz | 25:f5741040c4bb | 249 | if(PHASE_ORDER){ // Check which phase order to use, |
benkatz | 31:61eb6ae28215 | 250 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles |
benkatz | 31:61eb6ae28215 | 251 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 31:61eb6ae28215 | 252 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 253 | } |
benkatz | 22:60276ba87ac6 | 254 | else{ |
benkatz | 32:ccac5da77844 | 255 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); |
benkatz | 32:ccac5da77844 | 256 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 32:ccac5da77844 | 257 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 258 | } |
benkatz | 25:f5741040c4bb | 259 | |
benkatz | 48:74a40481740c | 260 | controller->theta_elec = theta; |
benkatz | 22:60276ba87ac6 | 261 | |
benkatz | 22:60276ba87ac6 | 262 | } |
benkatz | 26:2b865c00d7e9 | 263 | |
benkatz | 26:2b865c00d7e9 | 264 | |
benkatz | 26:2b865c00d7e9 | 265 | void torque_control(ControllerStruct *controller){ |
elijahsj | 70:2ea6c555a2cb | 266 | //SPEED CONTROLLER FOR DYNO |
elijahsj | 70:2ea6c555a2cb | 267 | //float windup = 50.0; |
elijahsj | 70:2ea6c555a2cb | 268 | //controller->velocity_sum += controller->kd/100*(controller->v_des - controller->dtheta_mech); |
elijahsj | 70:2ea6c555a2cb | 269 | //controller->velocity_sum = fmaxf(fminf(controller->velocity_sum, windup), -windup); |
elijahsj | 70:2ea6c555a2cb | 270 | //float torque_ref = controller->kp*(controller->v_des - controller->dtheta_mech) + controller->velocity_sum + controller->t_ff; |
elijahsj | 70:2ea6c555a2cb | 271 | |
elijahsj | 70:2ea6c555a2cb | 272 | 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 | 273 | //float torque_ref = -.1*(controller->p_des - controller->theta_mech); |
benkatz | 26:2b865c00d7e9 | 274 | controller->i_q_ref = torque_ref/KT_OUT; |
benkatz | 37:c0f352d6e8e3 | 275 | controller->i_d_ref = 0.0f; |
benkatz | 26:2b865c00d7e9 | 276 | } |
benkatz | 47:e1196a851f76 | 277 |