Modifying the HKCC for no readily apparent reason

Dependencies:   FastPWM3

Committer:
cactode
Date:
Sun Mar 28 01:10:30 2021 -0700
Revision:
59:8aa304768360
Parent:
57:57a108e15b52
Added more DSP functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 22:60276ba87ac6 1 #include "foc.h"
cactode 59:8aa304768360 2 #include "arm_math.h"
benkatz 22:60276ba87ac6 3
benkatz 22:60276ba87ac6 4 void abc( float theta, float d, float q, float *a, float *b, float *c){
benkatz 25:f5741040c4bb 5 /// Inverse DQ0 Transform ///
benkatz 22:60276ba87ac6 6 ///Phase current amplitude = lengh of dq vector///
benkatz 22:60276ba87ac6 7 ///i.e. iq = 1, id = 0, peak phase current of 1///
cactode 59:8aa304768360 8 float cf = arm_cos_f32(theta);
cactode 59:8aa304768360 9 float sf = arm_sin_f32(theta);
benkatz 37:c0f352d6e8e3 10
benkatz 37:c0f352d6e8e3 11 *a = cf*d - sf*q; // Faster Inverse DQ0 transform
benkatz 37:c0f352d6e8e3 12 *b = (0.86602540378f*sf-.5f*cf)*d - (-0.86602540378f*cf-.5f*sf)*q;
benkatz 37:c0f352d6e8e3 13 *c = (-0.86602540378f*sf-.5f*cf)*d - (0.86602540378f*cf-.5f*sf)*q;
benkatz 22:60276ba87ac6 14 }
benkatz 22:60276ba87ac6 15
benkatz 26:2b865c00d7e9 16
benkatz 22:60276ba87ac6 17 void dq0(float theta, float a, float b, float c, float *d, float *q){
benkatz 25:f5741040c4bb 18 /// DQ0 Transform ///
benkatz 22:60276ba87ac6 19 ///Phase current amplitude = lengh of dq vector///
benkatz 22:60276ba87ac6 20 ///i.e. iq = 1, id = 0, peak phase current of 1///
benkatz 22:60276ba87ac6 21
cactode 59:8aa304768360 22 float cf = arm_cos_f32(theta);
cactode 59:8aa304768360 23 float sf = arm_sin_f32(theta);
benkatz 26:2b865c00d7e9 24
benkatz 37:c0f352d6e8e3 25 *d = 0.6666667f*(cf*a + (0.86602540378f*sf-.5f*cf)*b + (-0.86602540378f*sf-.5f*cf)*c); ///Faster DQ0 Transform
benkatz 37:c0f352d6e8e3 26 *q = 0.6666667f*(-sf*a - (-0.86602540378f*cf-.5f*sf)*b - (0.86602540378f*cf-.5f*sf)*c);
benkatz 37:c0f352d6e8e3 27
benkatz 22:60276ba87ac6 28 }
cactode 59:8aa304768360 29
cactode 59:8aa304768360 30 void fast_dq0(float theta, float a, float b, float c, float *d, float *q) {
cactode 59:8aa304768360 31 float32_t sinVal;
cactode 59:8aa304768360 32 float32_t cosVal;
cactode 59:8aa304768360 33 arm_sin_cos_f32(theta, &sinVal, &cosVal);
cactode 59:8aa304768360 34 float32_t alpha;
cactode 59:8aa304768360 35 float32_t beta;
cactode 59:8aa304768360 36 arm_clarke_f32(a, b, &alpha, &beta);
cactode 59:8aa304768360 37 arm_park_f32(alpha, beta, d, q, sinVal, cosVal);
cactode 59:8aa304768360 38 }
cactode 59:8aa304768360 39
cactode 59:8aa304768360 40 void fast_abc(float theta, float d, float q, float *a, float *b, float *c) {
cactode 59:8aa304768360 41 float32_t sinVal;
cactode 59:8aa304768360 42 float32_t cosVal;
cactode 59:8aa304768360 43 arm_sin_cos_f32(theta, &sinVal, &cosVal);
cactode 59:8aa304768360 44 float32_t alpha;
cactode 59:8aa304768360 45 float32_t beta;
cactode 59:8aa304768360 46 arm_inv_park_f32(d, q, &alpha, &beta, sinVal, cosVal);
cactode 59:8aa304768360 47 arm_inv_clarke_f32(alpha, beta, a, b);
cactode 59:8aa304768360 48 *c = -*a - *b;
cactode 59:8aa304768360 49 }
benkatz 22:60276ba87ac6 50
benkatz 22:60276ba87ac6 51 void svm(float v_bus, float u, float v, float w, float *dtc_u, float *dtc_v, float *dtc_w){
benkatz 25:f5741040c4bb 52 /// Space Vector Modulation ///
cactode 59:8aa304768360 53 // u,v,w amplitude = v_bus for full modulation depth
benkatz 22:60276ba87ac6 54
benkatz 48:74a40481740c 55 float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))*0.5f;
benkatz 47:e1196a851f76 56
benkatz 31:61eb6ae28215 57 *dtc_u = fminf(fmaxf(((u -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX);
benkatz 31:61eb6ae28215 58 *dtc_v = fminf(fmaxf(((v -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX);
benkatz 47:e1196a851f76 59 *dtc_w = fminf(fmaxf(((w -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX);
benkatz 22:60276ba87ac6 60 }
benkatz 22:60276ba87ac6 61
benkatz 48:74a40481740c 62 void linearize_dtc(float *dtc){
benkatz 48:74a40481740c 63 /// linearizes the output of the inverter, which is not linear for small duty cycles ///
cactode 55:5afc2865ef7f 64 float sgn = 1.0f-(2.0f * (*dtc < 0));
benkatz 48:74a40481740c 65 if(abs(*dtc) >= .01f){
benkatz 48:74a40481740c 66 *dtc = *dtc*.986f+.014f*sgn;
benkatz 48:74a40481740c 67 }
benkatz 48:74a40481740c 68 else{
benkatz 48:74a40481740c 69 *dtc = 2.5f*(*dtc);
benkatz 48:74a40481740c 70 }
benkatz 48:74a40481740c 71
benkatz 48:74a40481740c 72 }
benkatz 48:74a40481740c 73
benkatz 48:74a40481740c 74
benkatz 25:f5741040c4bb 75 void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors
benkatz 22:60276ba87ac6 76 int adc1_offset = 0;
benkatz 22:60276ba87ac6 77 int adc2_offset = 0;
benkatz 22:60276ba87ac6 78 int n = 1024;
benkatz 25:f5741040c4bb 79 for (int i = 0; i<n; i++){ // Average n samples of the ADC
benkatz 27:501fee691e0e 80 TIM1->CCR3 = (PWM_ARR>>1)*(1.0f); // Write duty cycles
benkatz 27:501fee691e0e 81 TIM1->CCR2 = (PWM_ARR>>1)*(1.0f);
benkatz 27:501fee691e0e 82 TIM1->CCR1 = (PWM_ARR>>1)*(1.0f);
benkatz 26:2b865c00d7e9 83 ADC1->CR2 |= 0x40000000; // Begin sample and conversion
cactode 57:57a108e15b52 84 wait_us(1000);
benkatz 22:60276ba87ac6 85 adc2_offset += ADC2->DR;
benkatz 22:60276ba87ac6 86 adc1_offset += ADC1->DR;
benkatz 22:60276ba87ac6 87 }
benkatz 22:60276ba87ac6 88 *offset_1 = adc1_offset/n;
benkatz 22:60276ba87ac6 89 *offset_2 = adc2_offset/n;
benkatz 22:60276ba87ac6 90 }
benkatz 45:26801179208e 91
benkatz 45:26801179208e 92 void init_controller_params(ControllerStruct *controller){
benkatz 45:26801179208e 93 controller->ki_d = KI_D;
benkatz 45:26801179208e 94 controller->ki_q = KI_Q;
benkatz 45:26801179208e 95 controller->k_d = K_SCALE*I_BW;
benkatz 45:26801179208e 96 controller->k_q = K_SCALE*I_BW;
benkatz 45:26801179208e 97 controller->alpha = 1.0f - 1.0f/(1.0f - DT*I_BW*2.0f*PI);
benkatz 45:26801179208e 98
benkatz 45:26801179208e 99 }
benkatz 22:60276ba87ac6 100
benkatz 22:60276ba87ac6 101 void reset_foc(ControllerStruct *controller){
benkatz 28:8c7e29f719c5 102 TIM1->CCR3 = (PWM_ARR>>1)*(0.5f);
benkatz 28:8c7e29f719c5 103 TIM1->CCR1 = (PWM_ARR>>1)*(0.5f);
benkatz 28:8c7e29f719c5 104 TIM1->CCR2 = (PWM_ARR>>1)*(0.5f);
benkatz 28:8c7e29f719c5 105 controller->i_d_ref = 0;
benkatz 28:8c7e29f719c5 106 controller->i_q_ref = 0;
benkatz 28:8c7e29f719c5 107 controller->i_d = 0;
benkatz 28:8c7e29f719c5 108 controller->i_q = 0;
benkatz 37:c0f352d6e8e3 109 controller->i_q_filt = 0;
benkatz 22:60276ba87ac6 110 controller->q_int = 0;
benkatz 22:60276ba87ac6 111 controller->d_int = 0;
benkatz 28:8c7e29f719c5 112 controller->v_q = 0;
benkatz 28:8c7e29f719c5 113 controller->v_d = 0;
benkatz 47:e1196a851f76 114
benkatz 22:60276ba87ac6 115 }
benkatz 45:26801179208e 116
benkatz 48:74a40481740c 117 void reset_observer(ObserverStruct *observer){
benkatz 48:74a40481740c 118 observer->temperature = 25.0f;
benkatz 48:74a40481740c 119 observer->resistance = .1f;
benkatz 48:74a40481740c 120 }
benkatz 48:74a40481740c 121
benkatz 45:26801179208e 122 void limit_current_ref (ControllerStruct *controller){
benkatz 45:26801179208e 123 float i_q_max_limit = (0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE;
benkatz 45:26801179208e 124 float i_q_min_limit = (-0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE;
benkatz 45:26801179208e 125 controller->i_q_ref = fmaxf(fminf(i_q_max_limit, controller->i_q_ref), i_q_min_limit);
benkatz 45:26801179208e 126 }
benkatz 22:60276ba87ac6 127
benkatz 22:60276ba87ac6 128
benkatz 48:74a40481740c 129 void commutate(ControllerStruct *controller, ObserverStruct *observer, GPIOStruct *gpio, float theta){
benkatz 48:74a40481740c 130
benkatz 48:74a40481740c 131 /// Update observer estimates ///
benkatz 48:74a40481740c 132 // Resistance observer //
benkatz 48:74a40481740c 133 // Temperature Observer //
benkatz 48:74a40481740c 134 float t_rise = (float)observer->temperature - 25.0f;
benkatz 49:83d83040ea51 135 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 136 float q_th_out = t_rise*R_TH;
benkatz 48:74a40481740c 137 observer->temperature += INV_M_TH*DT*(q_th_in-q_th_out);
benkatz 48:74a40481740c 138
benkatz 49:83d83040ea51 139 observer->resistance = (controller->v_q - SQRT3*controller->dtheta_elec*(WB))/controller->i_q;
benkatz 49:83d83040ea51 140 //observer->resistance = controller->v_q/controller->i_q;
benkatz 48:74a40481740c 141 if(isnan(observer->resistance)){observer->resistance = R_PHASE;}
benkatz 48:74a40481740c 142 observer->temperature2 = (double)(25.0f + ((observer->resistance*6.0606f)-1.0f)*275.5f);
benkatz 48:74a40481740c 143 double e = observer->temperature - observer->temperature2;
benkatz 48:74a40481740c 144 observer->temperature -= .001*e;
benkatz 48:74a40481740c 145 //printf("%.3f\n\r", e);
benkatz 48:74a40481740c 146
benkatz 47:e1196a851f76 147
benkatz 25:f5741040c4bb 148 /// Commutation Loop ///
benkatz 25:f5741040c4bb 149 controller->loop_count ++;
benkatz 25:f5741040c4bb 150 if(PHASE_ORDER){ // Check current sensor ordering
benkatz 25:f5741040c4bb 151 controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings
benkatz 22:60276ba87ac6 152 controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 153 }
benkatz 22:60276ba87ac6 154 else{
benkatz 25:f5741040c4bb 155 controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 156 controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset);
benkatz 22:60276ba87ac6 157 }
benkatz 26:2b865c00d7e9 158 controller->i_a = -controller->i_b - controller->i_c;
benkatz 22:60276ba87ac6 159
cactode 59:8aa304768360 160 float c = arm_cos_f32(theta);
cactode 59:8aa304768360 161 float s = arm_sin_f32(theta);
benkatz 44:8040fa2fcb0d 162 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 163 //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 164 //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 165
benkatz 37:c0f352d6e8e3 166 controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q;
benkatz 47:e1196a851f76 167 controller->i_d_filt = 0.95f*controller->i_d_filt + 0.05f*controller->i_d;
benkatz 37:c0f352d6e8e3 168
benkatz 45:26801179208e 169
benkatz 49:83d83040ea51 170 // Filter the current references to the desired closed-loop bandwidth
benkatz 45:26801179208e 171 controller->i_d_ref_filt = (1.0f-controller->alpha)*controller->i_d_ref_filt + controller->alpha*controller->i_d_ref;
benkatz 45:26801179208e 172 controller->i_q_ref_filt = (1.0f-controller->alpha)*controller->i_q_ref_filt + controller->alpha*controller->i_q_ref;
benkatz 48:74a40481740c 173
benkatz 48:74a40481740c 174
benkatz 48:74a40481740c 175 /// Field Weakening ///
benkatz 48:74a40481740c 176
benkatz 48:74a40481740c 177 controller->fw_int += .001f*(0.5f*OVERMODULATION*controller->v_bus - controller->v_ref);
benkatz 51:6cd89bd6fcaa 178 controller->fw_int = fmaxf(fminf(controller->fw_int, 0.0f), -I_FW_MAX);
benkatz 48:74a40481740c 179 controller->i_d_ref = controller->fw_int;
benkatz 48:74a40481740c 180 //float i_cmd_mag_sq = controller->i_d_ref*controller->i_d_ref + controller->i_q_ref*controller->i_q_ref;
benkatz 48:74a40481740c 181 limit_norm(&controller->i_d_ref, &controller->i_q_ref, I_MAX);
benkatz 25:f5741040c4bb 182
benkatz 47:e1196a851f76 183
benkatz 47:e1196a851f76 184
benkatz 25:f5741040c4bb 185 /// PI Controller ///
benkatz 22:60276ba87ac6 186 float i_d_error = controller->i_d_ref - controller->i_d;
benkatz 45:26801179208e 187 float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current;
benkatz 37:c0f352d6e8e3 188
benkatz 45:26801179208e 189 // Calculate feed-forward voltages //
benkatz 48:74a40481740c 190 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 191 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 192
benkatz 45:26801179208e 193 // Integrate Error //
benkatz 47:e1196a851f76 194 controller->d_int += controller->k_d*controller->ki_d*i_d_error;
benkatz 47:e1196a851f76 195 controller->q_int += controller->k_q*controller->ki_q*i_q_error;
benkatz 47:e1196a851f76 196
benkatz 47:e1196a851f76 197 controller->d_int = fmaxf(fminf(controller->d_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus);
benkatz 47:e1196a851f76 198 controller->q_int = fmaxf(fminf(controller->q_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus);
benkatz 47:e1196a851f76 199
benkatz 47:e1196a851f76 200 //limit_norm(&controller->d_int, &controller->q_int, OVERMODULATION*controller->v_bus);
benkatz 47:e1196a851f76 201 controller->v_d = controller->k_d*i_d_error + controller->d_int ;//+ v_d_ff;
benkatz 47:e1196a851f76 202 controller->v_q = controller->k_q*i_q_error + controller->q_int ;//+ v_q_ff;
benkatz 47:e1196a851f76 203
benkatz 47:e1196a851f76 204 controller->v_ref = sqrt(controller->v_d*controller->v_d + controller->v_q*controller->v_q);
benkatz 22:60276ba87ac6 205
benkatz 44:8040fa2fcb0d 206 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 207 float dtc_d = controller->v_d/controller->v_bus;
benkatz 48:74a40481740c 208 float dtc_q = controller->v_q/controller->v_bus;
benkatz 48:74a40481740c 209 linearize_dtc(&dtc_d);
benkatz 48:74a40481740c 210 linearize_dtc(&dtc_q);
benkatz 48:74a40481740c 211 controller->v_d = dtc_d*controller->v_bus;
benkatz 48:74a40481740c 212 controller->v_q = dtc_q*controller->v_bus;
benkatz 47:e1196a851f76 213 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 214 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 215
benkatz 25:f5741040c4bb 216 if(PHASE_ORDER){ // Check which phase order to use,
benkatz 31:61eb6ae28215 217 TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles
benkatz 31:61eb6ae28215 218 TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v);
benkatz 31:61eb6ae28215 219 TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 220 }
benkatz 22:60276ba87ac6 221 else{
benkatz 32:ccac5da77844 222 TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u);
benkatz 32:ccac5da77844 223 TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v);
benkatz 32:ccac5da77844 224 TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 225 }
benkatz 25:f5741040c4bb 226
benkatz 48:74a40481740c 227 controller->theta_elec = theta;
benkatz 22:60276ba87ac6 228
benkatz 22:60276ba87ac6 229 }
benkatz 26:2b865c00d7e9 230
benkatz 26:2b865c00d7e9 231
benkatz 26:2b865c00d7e9 232 void torque_control(ControllerStruct *controller){
benkatz 28:8c7e29f719c5 233 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 234 //float torque_ref = -.1*(controller->p_des - controller->theta_mech);
benkatz 26:2b865c00d7e9 235 controller->i_q_ref = torque_ref/KT_OUT;
benkatz 37:c0f352d6e8e3 236 controller->i_d_ref = 0.0f;
benkatz 26:2b865c00d7e9 237 }
benkatz 47:e1196a851f76 238