123487
Dependencies: mbed-dev-f303 FastPWM3
FOC/foc.cpp@59:ed4b54437e02, 2020-12-25 (annotated)
- Committer:
- shaorui
- Date:
- Fri Dec 25 07:06:28 2020 +0000
- Revision:
- 59:ed4b54437e02
- Parent:
- 55:14ac00a5f43d
l
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 | 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 | 48:74a40481740c | 36 | float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))*0.5f; |
benkatz | 47:e1196a851f76 | 37 | |
benkatz | 31:61eb6ae28215 | 38 | *dtc_u = fminf(fmaxf(((u -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 31:61eb6ae28215 | 39 | *dtc_v = fminf(fmaxf(((v -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 40 | *dtc_w = fminf(fmaxf(((w -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 41 | |
benkatz | 47:e1196a851f76 | 42 | /* |
benkatz | 47:e1196a851f76 | 43 | sinusoidal pwm |
benkatz | 47:e1196a851f76 | 44 | *dtc_u = fminf(fmaxf((u/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 45 | *dtc_v = fminf(fmaxf((v/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 46 | *dtc_w = fminf(fmaxf((w/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 47 | */ |
benkatz | 47:e1196a851f76 | 48 | |
benkatz | 22:60276ba87ac6 | 49 | |
benkatz | 22:60276ba87ac6 | 50 | } |
benkatz | 22:60276ba87ac6 | 51 | |
benkatz | 48:74a40481740c | 52 | void linearize_dtc(float *dtc){ |
benkatz | 48:74a40481740c | 53 | /// linearizes the output of the inverter, which is not linear for small duty cycles /// |
benkatz | 48:74a40481740c | 54 | float sgn = 1.0f-(2.0f*(dtc<0)); |
benkatz | 48:74a40481740c | 55 | if(abs(*dtc) >= .01f){ |
benkatz | 48:74a40481740c | 56 | *dtc = *dtc*.986f+.014f*sgn; |
benkatz | 48:74a40481740c | 57 | } |
benkatz | 48:74a40481740c | 58 | else{ |
benkatz | 48:74a40481740c | 59 | *dtc = 2.5f*(*dtc); |
benkatz | 48:74a40481740c | 60 | } |
benkatz | 48:74a40481740c | 61 | |
benkatz | 48:74a40481740c | 62 | } |
benkatz | 48:74a40481740c | 63 | |
benkatz | 48:74a40481740c | 64 | |
benkatz | 25:f5741040c4bb | 65 | void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors |
benkatz | 22:60276ba87ac6 | 66 | int adc1_offset = 0; |
benkatz | 22:60276ba87ac6 | 67 | int adc2_offset = 0; |
benkatz | 22:60276ba87ac6 | 68 | int n = 1024; |
benkatz | 25:f5741040c4bb | 69 | for (int i = 0; i<n; i++){ // Average n samples of the ADC |
benkatz | 27:501fee691e0e | 70 | TIM1->CCR3 = (PWM_ARR>>1)*(1.0f); // Write duty cycles |
benkatz | 27:501fee691e0e | 71 | TIM1->CCR2 = (PWM_ARR>>1)*(1.0f); |
benkatz | 27:501fee691e0e | 72 | TIM1->CCR1 = (PWM_ARR>>1)*(1.0f); |
benkatz | 26:2b865c00d7e9 | 73 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 74 | wait(.001); |
benkatz | 22:60276ba87ac6 | 75 | adc2_offset += ADC2->DR; |
benkatz | 22:60276ba87ac6 | 76 | adc1_offset += ADC1->DR; |
benkatz | 22:60276ba87ac6 | 77 | } |
benkatz | 22:60276ba87ac6 | 78 | *offset_1 = adc1_offset/n; |
benkatz | 22:60276ba87ac6 | 79 | *offset_2 = adc2_offset/n; |
benkatz | 22:60276ba87ac6 | 80 | } |
benkatz | 45:26801179208e | 81 | |
benkatz | 45:26801179208e | 82 | void init_controller_params(ControllerStruct *controller){ |
benkatz | 45:26801179208e | 83 | controller->ki_d = KI_D; |
benkatz | 45:26801179208e | 84 | controller->ki_q = KI_Q; |
benkatz | 45:26801179208e | 85 | controller->k_d = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 86 | controller->k_q = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 87 | controller->alpha = 1.0f - 1.0f/(1.0f - DT*I_BW*2.0f*PI); |
benkatz | 45:26801179208e | 88 | |
benkatz | 45:26801179208e | 89 | } |
benkatz | 22:60276ba87ac6 | 90 | |
benkatz | 22:60276ba87ac6 | 91 | void reset_foc(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 92 | TIM1->CCR3 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 93 | TIM1->CCR1 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 94 | TIM1->CCR2 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 95 | controller->i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 96 | controller->i_q_ref = 0; |
benkatz | 28:8c7e29f719c5 | 97 | controller->i_d = 0; |
benkatz | 28:8c7e29f719c5 | 98 | controller->i_q = 0; |
benkatz | 37:c0f352d6e8e3 | 99 | controller->i_q_filt = 0; |
benkatz | 22:60276ba87ac6 | 100 | controller->q_int = 0; |
benkatz | 22:60276ba87ac6 | 101 | controller->d_int = 0; |
benkatz | 28:8c7e29f719c5 | 102 | controller->v_q = 0; |
benkatz | 28:8c7e29f719c5 | 103 | controller->v_d = 0; |
benkatz | 47:e1196a851f76 | 104 | |
benkatz | 22:60276ba87ac6 | 105 | } |
benkatz | 45:26801179208e | 106 | |
benkatz | 48:74a40481740c | 107 | void reset_observer(ObserverStruct *observer){ |
benkatz | 48:74a40481740c | 108 | observer->temperature = 25.0f; |
benkatz | 48:74a40481740c | 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 | 22:60276ba87ac6 | 118 | |
benkatz | 48:74a40481740c | 119 | void commutate(ControllerStruct *controller, ObserverStruct *observer, GPIOStruct *gpio, float theta){ |
benkatz | 48:74a40481740c | 120 | |
benkatz | 48:74a40481740c | 121 | /// Update observer estimates /// |
benkatz | 48:74a40481740c | 122 | // Resistance observer // |
benkatz | 48:74a40481740c | 123 | // Temperature Observer // |
benkatz | 48:74a40481740c | 124 | float t_rise = (float)observer->temperature - 25.0f; |
benkatz | 49:83d83040ea51 | 125 | float q_th_in = (1.0f + .00393f*t_rise)*(controller->i_d*controller->i_d*R_PHASE*SQRT3 + controller->i_q*controller->i_q*R_PHASE*SQRT3); |
benkatz | 48:74a40481740c | 126 | float q_th_out = t_rise*R_TH; |
benkatz | 48:74a40481740c | 127 | observer->temperature += INV_M_TH*DT*(q_th_in-q_th_out); |
benkatz | 48:74a40481740c | 128 | |
benkatz | 49:83d83040ea51 | 129 | observer->resistance = (controller->v_q - SQRT3*controller->dtheta_elec*(WB))/controller->i_q; |
benkatz | 49:83d83040ea51 | 130 | //observer->resistance = controller->v_q/controller->i_q; |
benkatz | 48:74a40481740c | 131 | if(isnan(observer->resistance)){observer->resistance = R_PHASE;} |
benkatz | 48:74a40481740c | 132 | observer->temperature2 = (double)(25.0f + ((observer->resistance*6.0606f)-1.0f)*275.5f); |
benkatz | 48:74a40481740c | 133 | double e = observer->temperature - observer->temperature2; |
benkatz | 48:74a40481740c | 134 | observer->temperature -= .001*e; |
benkatz | 48:74a40481740c | 135 | //printf("%.3f\n\r", e); |
Rushu | 55:14ac00a5f43d | 136 | /* |
Rushu | 55:14ac00a5f43d | 137 | // Integrate the thermal model // |
Rushu | 55:14ac00a5f43d | 138 | delta_t = observer.temperature - T_AMBIENT; |
Rushu | 55:14ac00a5f43d | 139 | observer.qd_in = R_NOMINAL**(1.0f + .00393f*delta_t)*controller.i*controller.i; |
Rushu | 55:14ac00a5f43d | 140 | observer.qd_out = delta_t*R_TH; |
Rushu | 55:14ac00a5f43d | 141 | observer.temperature += DT*(observer.qd_in-observer.qd_out)/C_TH; |
Rushu | 55:14ac00a5f43d | 142 | |
Rushu | 55:14ac00a5f43d | 143 | // Estimate Resistance // |
Rushu | 55:14ac00a5f43d | 144 | observer.resistance = (controller.v_q - controller.omega*(L_D*controller.i_d + WB))/(controller.i_q); |
benkatz | 48:74a40481740c | 145 | |
Rushu | 55:14ac00a5f43d | 146 | // Estimate Temperature from temp-co // |
Rushu | 55:14ac00a5f43d | 147 | observer.t_measured = ((T_AMBIENT + ((observer.resistance/R_NOMINAL) - 1.0f)*254.5f)); |
Rushu | 55:14ac00a5f43d | 148 | |
Rushu | 55:14ac00a5f43d | 149 | // Update Observer with measured temperature // |
Rushu | 55:14ac00a5f43d | 150 | e = (float)observer.temperature - observer.temp_measured; |
Rushu | 55:14ac00a5f43d | 151 | //observer.temperature -= .0001f*e; |
Rushu | 55:14ac00a5f43d | 152 | // Calculate "trust" based on state // |
Rushu | 55:14ac00a5f43d | 153 | observer.trust = (1.0f - .004f*fminf(abs(controller.dtheta_elec), 250.0f)) * (.01f*(fminf(controller.current^2, 100.0f))); |
Rushu | 55:14ac00a5f43d | 154 | |
Rushu | 55:14ac00a5f43d | 155 | // Scale observer gain by "trust" // |
Rushu | 55:14ac00a5f43d | 156 | // .0001 is the default observer gain // |
Rushu | 55:14ac00a5f43d | 157 | observer.temperature -= observer.trust*.0001f*e; |
Rushu | 55:14ac00a5f43d | 158 | |
Rushu | 55:14ac00a5f43d | 159 | |
Rushu | 55:14ac00a5f43d | 160 | */ |
benkatz | 25:f5741040c4bb | 161 | /// Commutation Loop /// |
benkatz | 25:f5741040c4bb | 162 | controller->loop_count ++; |
benkatz | 25:f5741040c4bb | 163 | if(PHASE_ORDER){ // Check current sensor ordering |
benkatz | 25:f5741040c4bb | 164 | controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings |
benkatz | 22:60276ba87ac6 | 165 | controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 166 | } |
benkatz | 22:60276ba87ac6 | 167 | else{ |
benkatz | 25:f5741040c4bb | 168 | controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 169 | controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); |
benkatz | 22:60276ba87ac6 | 170 | } |
benkatz | 26:2b865c00d7e9 | 171 | controller->i_a = -controller->i_b - controller->i_c; |
benkatz | 22:60276ba87ac6 | 172 | |
benkatz | 26:2b865c00d7e9 | 173 | float s = FastSin(theta); |
benkatz | 26:2b865c00d7e9 | 174 | float c = FastCos(theta); |
benkatz | 44:8040fa2fcb0d | 175 | 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 | 176 | //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 | 177 | //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 | 178 | |
benkatz | 37:c0f352d6e8e3 | 179 | controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q; |
benkatz | 47:e1196a851f76 | 180 | controller->i_d_filt = 0.95f*controller->i_d_filt + 0.05f*controller->i_d; |
benkatz | 37:c0f352d6e8e3 | 181 | |
benkatz | 45:26801179208e | 182 | |
benkatz | 49:83d83040ea51 | 183 | // Filter the current references to the desired closed-loop bandwidth |
benkatz | 45:26801179208e | 184 | controller->i_d_ref_filt = (1.0f-controller->alpha)*controller->i_d_ref_filt + controller->alpha*controller->i_d_ref; |
benkatz | 45:26801179208e | 185 | controller->i_q_ref_filt = (1.0f-controller->alpha)*controller->i_q_ref_filt + controller->alpha*controller->i_q_ref; |
benkatz | 48:74a40481740c | 186 | |
benkatz | 48:74a40481740c | 187 | |
benkatz | 48:74a40481740c | 188 | /// Field Weakening /// |
benkatz | 48:74a40481740c | 189 | |
benkatz | 48:74a40481740c | 190 | controller->fw_int += .001f*(0.5f*OVERMODULATION*controller->v_bus - controller->v_ref); |
benkatz | 51:6cd89bd6fcaa | 191 | controller->fw_int = fmaxf(fminf(controller->fw_int, 0.0f), -I_FW_MAX); |
benkatz | 48:74a40481740c | 192 | controller->i_d_ref = controller->fw_int; |
benkatz | 48:74a40481740c | 193 | //float i_cmd_mag_sq = controller->i_d_ref*controller->i_d_ref + controller->i_q_ref*controller->i_q_ref; |
benkatz | 48:74a40481740c | 194 | limit_norm(&controller->i_d_ref, &controller->i_q_ref, I_MAX); |
benkatz | 25:f5741040c4bb | 195 | |
benkatz | 47:e1196a851f76 | 196 | |
benkatz | 47:e1196a851f76 | 197 | |
benkatz | 25:f5741040c4bb | 198 | /// PI Controller /// |
benkatz | 22:60276ba87ac6 | 199 | float i_d_error = controller->i_d_ref - controller->i_d; |
benkatz | 45:26801179208e | 200 | float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current; |
benkatz | 37:c0f352d6e8e3 | 201 | |
benkatz | 45:26801179208e | 202 | // Calculate feed-forward voltages // |
benkatz | 48:74a40481740c | 203 | float v_d_ff = SQRT3*(1.0f*controller->i_d_ref*R_PHASE - controller->dtheta_elec*L_Q*controller->i_q); //feed-forward voltages |
benkatz | 48:74a40481740c | 204 | float v_q_ff = SQRT3*(1.0f*controller->i_q_ref*R_PHASE + controller->dtheta_elec*(L_D*controller->i_d + 1.0f*WB)); |
benkatz | 37:c0f352d6e8e3 | 205 | |
benkatz | 45:26801179208e | 206 | // Integrate Error // |
benkatz | 47:e1196a851f76 | 207 | controller->d_int += controller->k_d*controller->ki_d*i_d_error; |
benkatz | 47:e1196a851f76 | 208 | controller->q_int += controller->k_q*controller->ki_q*i_q_error; |
benkatz | 47:e1196a851f76 | 209 | |
benkatz | 47:e1196a851f76 | 210 | controller->d_int = fmaxf(fminf(controller->d_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 211 | controller->q_int = fmaxf(fminf(controller->q_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 212 | |
benkatz | 47:e1196a851f76 | 213 | //limit_norm(&controller->d_int, &controller->q_int, OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 214 | controller->v_d = controller->k_d*i_d_error + controller->d_int ;//+ v_d_ff; |
benkatz | 47:e1196a851f76 | 215 | controller->v_q = controller->k_q*i_q_error + controller->q_int ;//+ v_q_ff; |
benkatz | 47:e1196a851f76 | 216 | |
benkatz | 47:e1196a851f76 | 217 | controller->v_ref = sqrt(controller->v_d*controller->v_d + controller->v_q*controller->v_q); |
benkatz | 22:60276ba87ac6 | 218 | |
benkatz | 44:8040fa2fcb0d | 219 | limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus |
benkatz | 48:74a40481740c | 220 | float dtc_d = controller->v_d/controller->v_bus; |
benkatz | 48:74a40481740c | 221 | float dtc_q = controller->v_q/controller->v_bus; |
benkatz | 48:74a40481740c | 222 | linearize_dtc(&dtc_d); |
benkatz | 48:74a40481740c | 223 | linearize_dtc(&dtc_q); |
benkatz | 48:74a40481740c | 224 | controller->v_d = dtc_d*controller->v_bus; |
benkatz | 48:74a40481740c | 225 | controller->v_q = dtc_q*controller->v_bus; |
benkatz | 47:e1196a851f76 | 226 | abc(controller->theta_elec + 0.0f*DT*controller->dtheta_elec, controller->v_d, controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages |
benkatz | 22:60276ba87ac6 | 227 | 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 | 228 | |
benkatz | 25:f5741040c4bb | 229 | if(PHASE_ORDER){ // Check which phase order to use, |
benkatz | 31:61eb6ae28215 | 230 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles |
benkatz | 31:61eb6ae28215 | 231 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 31:61eb6ae28215 | 232 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 233 | } |
benkatz | 22:60276ba87ac6 | 234 | else{ |
benkatz | 32:ccac5da77844 | 235 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); |
benkatz | 32:ccac5da77844 | 236 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 32:ccac5da77844 | 237 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 238 | } |
benkatz | 25:f5741040c4bb | 239 | |
benkatz | 48:74a40481740c | 240 | controller->theta_elec = theta; |
benkatz | 22:60276ba87ac6 | 241 | |
benkatz | 22:60276ba87ac6 | 242 | } |
benkatz | 26:2b865c00d7e9 | 243 | |
benkatz | 26:2b865c00d7e9 | 244 | |
benkatz | 26:2b865c00d7e9 | 245 | void torque_control(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 246 | 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 | 247 | //float torque_ref = -.1*(controller->p_des - controller->theta_mech); |
benkatz | 26:2b865c00d7e9 | 248 | controller->i_q_ref = torque_ref/KT_OUT; |
benkatz | 37:c0f352d6e8e3 | 249 | controller->i_d_ref = 0.0f; |
benkatz | 26:2b865c00d7e9 | 250 | } |
benkatz | 47:e1196a851f76 | 251 |