auto-measurements

Dependencies:   FastPWM3 mbed-dev

Fork of Hobbyking_Cheetah_Compact by Ben Katz

Committer:
benkatz
Date:
Sun Apr 09 03:05:52 2017 +0000
Revision:
25:f5741040c4bb
Parent:
24:58c2d7571207
Child:
26:2b865c00d7e9
Set current bandwidth from serial interface

Who changed what in which revision?

UserRevisionLine numberNew 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 25:f5741040c4bb 9 /// Inverse DQ0 Transform ///
benkatz 22:60276ba87ac6 10 ///Phase current amplitude = lengh of dq vector///
benkatz 22:60276ba87ac6 11 ///i.e. iq = 1, id = 0, peak phase current of 1///
benkatz 22:60276ba87ac6 12
benkatz 22:60276ba87ac6 13 *a = d*cosf(-theta) + q*sinf(-theta);
benkatz 22:60276ba87ac6 14 *b = d*cosf((2.0f*PI/3.0f)-theta) + q*sinf((2.0f*PI/3.0f)-theta);
benkatz 22:60276ba87ac6 15 *c = d*cosf((-2.0f*PI/3.0f)-theta) + q*sinf((-2.0f*PI/3.0f)-theta);
benkatz 22:60276ba87ac6 16 }
benkatz 22:60276ba87ac6 17
benkatz 22:60276ba87ac6 18 void dq0(float theta, float a, float b, float c, float *d, float *q){
benkatz 25:f5741040c4bb 19 /// DQ0 Transform ///
benkatz 22:60276ba87ac6 20 ///Phase current amplitude = lengh of dq vector///
benkatz 22:60276ba87ac6 21 ///i.e. iq = 1, id = 0, peak phase current of 1///
benkatz 22:60276ba87ac6 22
benkatz 22:60276ba87ac6 23 *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 24 *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 25 }
benkatz 22:60276ba87ac6 26
benkatz 22:60276ba87ac6 27 void svm(float v_bus, float u, float v, float w, float *dtc_u, float *dtc_v, float *dtc_w){
benkatz 25:f5741040c4bb 28 /// Space Vector Modulation ///
benkatz 25:f5741040c4bb 29 /// u,v,w amplitude = v_bus for full modulation depth ///
benkatz 22:60276ba87ac6 30
benkatz 22:60276ba87ac6 31 float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))/2.0f;
benkatz 23:2adf23ee0305 32 *dtc_u = fminf(fmaxf(((u - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX);
benkatz 23:2adf23ee0305 33 *dtc_v = fminf(fmaxf(((v - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX);
benkatz 23:2adf23ee0305 34 *dtc_w = fminf(fmaxf(((w - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX);
benkatz 22:60276ba87ac6 35
benkatz 22:60276ba87ac6 36 }
benkatz 22:60276ba87ac6 37
benkatz 25:f5741040c4bb 38 void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors
benkatz 22:60276ba87ac6 39 int adc1_offset = 0;
benkatz 22:60276ba87ac6 40 int adc2_offset = 0;
benkatz 22:60276ba87ac6 41 int n = 1024;
benkatz 25:f5741040c4bb 42 for (int i = 0; i<n; i++){ // Average n samples of the ADC
benkatz 22:60276ba87ac6 43 ADC1->CR2 |= 0x40000000;
benkatz 22:60276ba87ac6 44 wait(.001);
benkatz 22:60276ba87ac6 45 adc2_offset += ADC2->DR;
benkatz 22:60276ba87ac6 46 adc1_offset += ADC1->DR;
benkatz 22:60276ba87ac6 47 }
benkatz 22:60276ba87ac6 48 *offset_1 = adc1_offset/n;
benkatz 22:60276ba87ac6 49 *offset_2 = adc2_offset/n;
benkatz 22:60276ba87ac6 50 }
benkatz 22:60276ba87ac6 51
benkatz 22:60276ba87ac6 52 void reset_foc(ControllerStruct *controller){
benkatz 22:60276ba87ac6 53 controller->q_int = 0;
benkatz 22:60276ba87ac6 54 controller->d_int = 0;
benkatz 22:60276ba87ac6 55 }
benkatz 22:60276ba87ac6 56
benkatz 22:60276ba87ac6 57
benkatz 22:60276ba87ac6 58 void commutate(ControllerStruct *controller, GPIOStruct *gpio, float theta){
benkatz 25:f5741040c4bb 59 /// Commutation Loop ///
benkatz 25:f5741040c4bb 60 controller->loop_count ++;
benkatz 25:f5741040c4bb 61 if(PHASE_ORDER){ // Check current sensor ordering
benkatz 25:f5741040c4bb 62 controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings
benkatz 22:60276ba87ac6 63 controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 64 }
benkatz 22:60276ba87ac6 65 else{
benkatz 25:f5741040c4bb 66 controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 67 controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset);
benkatz 22:60276ba87ac6 68 }
benkatz 25:f5741040c4bb 69 controller->i_a = -controller->i_b - controller->i_c;
benkatz 22:60276ba87ac6 70 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 71
benkatz 25:f5741040c4bb 72 ///Cogging compensation lookup, doesn't actually work yet///
benkatz 23:2adf23ee0305 73 //int ind = theta * (128.0f/(2.0f*PI));
benkatz 23:2adf23ee0305 74 //float cogging_current = controller->cogging[ind];
benkatz 23:2adf23ee0305 75 //float cogging_current = 1.0f*cos(6*theta);
benkatz 25:f5741040c4bb 76
benkatz 25:f5741040c4bb 77 /// PI Controller ///
benkatz 22:60276ba87ac6 78 float i_d_error = controller->i_d_ref - controller->i_d;
benkatz 23:2adf23ee0305 79 float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current;
benkatz 22:60276ba87ac6 80 float v_d_ff = 2.0f*(2*controller->i_d_ref*R_PHASE); //feed-forward voltage
benkatz 22:60276ba87ac6 81 float v_q_ff = 2.0f*(2*controller->i_q_ref*R_PHASE + controller->dtheta_elec*WB*0.8165f);
benkatz 22:60276ba87ac6 82 controller->d_int += i_d_error;
benkatz 22:60276ba87ac6 83 controller->q_int += i_q_error;
benkatz 22:60276ba87ac6 84
benkatz 22:60276ba87ac6 85 //v_d_ff = 0;
benkatz 22:60276ba87ac6 86 //v_q_ff = 0;
benkatz 22:60276ba87ac6 87
benkatz 25:f5741040c4bb 88 limit_norm(&controller->d_int, &controller->q_int, V_BUS/(K_Q*KI_Q)); // Limit integrators to prevent windup
benkatz 22:60276ba87ac6 89 //controller->d_int = fminf(fmaxf(controller->d_int, -D_INT_LIM), D_INT_LIM);
benkatz 22:60276ba87ac6 90 //controller->q_int = fminf(fmaxf(controller->q_int, -Q_INT_LIM), Q_INT_LIM);
benkatz 22:60276ba87ac6 91
benkatz 22:60276ba87ac6 92
benkatz 25:f5741040c4bb 93 controller->v_d = K_SCALE*I_BW*i_d_error + K_SCALE*I_BW*controller->d_int;// + v_d_ff;
benkatz 25:f5741040c4bb 94 controller->v_q = K_SCALE*I_BW*i_q_error + K_SCALE*I_BW*controller->q_int;// + v_q_ff;
benkatz 22:60276ba87ac6 95
benkatz 22:60276ba87ac6 96 //controller->v_d = v_d_ff;
benkatz 22:60276ba87ac6 97 //controller->v_q = v_q_ff;
benkatz 22:60276ba87ac6 98
benkatz 25:f5741040c4bb 99 limit_norm(&controller->v_d, &controller->v_q, controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus
benkatz 22:60276ba87ac6 100 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 101 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 102
benkatz 22:60276ba87ac6 103
benkatz 25:f5741040c4bb 104 if(PHASE_ORDER){ // Check which phase order to use,
benkatz 25:f5741040c4bb 105 TIM1->CCR3 = 0x708*(1.0f-controller->dtc_u); // Write duty cycles
benkatz 22:60276ba87ac6 106 TIM1->CCR2 = 0x708*(1.0f-controller->dtc_v);
benkatz 22:60276ba87ac6 107 TIM1->CCR1 = 0x708*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 108 }
benkatz 22:60276ba87ac6 109 else{
benkatz 22:60276ba87ac6 110 TIM1->CCR3 = 0x708*(1.0f-controller->dtc_u);
benkatz 22:60276ba87ac6 111 TIM1->CCR1 = 0x708*(1.0f-controller->dtc_v);
benkatz 22:60276ba87ac6 112 TIM1->CCR2 = 0x708*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 113 }
benkatz 25:f5741040c4bb 114
benkatz 25:f5741040c4bb 115 controller->theta_elec = theta; //For some reason putting this at the front breaks thins
benkatz 22:60276ba87ac6 116
benkatz 22:60276ba87ac6 117
benkatz 25:f5741040c4bb 118 //if(controller->loop_count >400){
benkatz 22:60276ba87ac6 119 //controller->i_q_ref = -controller->i_q_ref;
benkatz 25:f5741040c4bb 120 // controller->loop_count = 0;
benkatz 22:60276ba87ac6 121
benkatz 23:2adf23ee0305 122 //printf("%d %f\n\r", ind, cogging_current);
benkatz 22:60276ba87ac6 123 //printf("%f\n\r", controller->theta_elec);
benkatz 22:60276ba87ac6 124 //pc.printf("%f %f %f\n\r", controller->i_a, controller->i_b, controller->i_c);
benkatz 22:60276ba87ac6 125 //pc.printf("%f %f\n\r", controller->i_d, controller->i_q);
benkatz 22:60276ba87ac6 126 //pc.printf("%d %d\n\r", controller->adc1_raw, controller->adc2_raw);
benkatz 25:f5741040c4bb 127 // }
benkatz 22:60276ba87ac6 128 }
benkatz 22:60276ba87ac6 129 /*
benkatz 22:60276ba87ac6 130 void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){
benkatz 22:60276ba87ac6 131
benkatz 22:60276ba87ac6 132 }
benkatz 22:60276ba87ac6 133 */