ares

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
benkatz
Date:
Wed May 17 14:53:22 2017 +0000
Revision:
27:501fee691e0e
Parent:
26:2b865c00d7e9
Child:
28:8c7e29f719c5
Setting PWM ARR value in config file now propagates everywhere.

Who changed what in which revision?

UserRevisionLine numberNew 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 23:2adf23ee0305 37 *dtc_u = fminf(fmaxf(((u - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX);
benkatz 23:2adf23ee0305 38 *dtc_v = fminf(fmaxf(((v - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX);
benkatz 26:2b865c00d7e9 39 *dtc_w = fminf(fmaxf(((w - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), 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 22:60276ba87ac6 61 controller->q_int = 0;
benkatz 22:60276ba87ac6 62 controller->d_int = 0;
benkatz 22:60276ba87ac6 63 }
benkatz 22:60276ba87ac6 64
benkatz 22:60276ba87ac6 65
benkatz 22:60276ba87ac6 66 void commutate(ControllerStruct *controller, GPIOStruct *gpio, float theta){
benkatz 25:f5741040c4bb 67 /// Commutation Loop ///
benkatz 26:2b865c00d7e9 68
benkatz 25:f5741040c4bb 69 controller->loop_count ++;
benkatz 25:f5741040c4bb 70 if(PHASE_ORDER){ // Check current sensor ordering
benkatz 25:f5741040c4bb 71 controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings
benkatz 22:60276ba87ac6 72 controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 73 }
benkatz 22:60276ba87ac6 74 else{
benkatz 25:f5741040c4bb 75 controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 76 controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset);
benkatz 22:60276ba87ac6 77 }
benkatz 26:2b865c00d7e9 78 controller->i_a = -controller->i_b - controller->i_c;
benkatz 22:60276ba87ac6 79
benkatz 26:2b865c00d7e9 80 float s = FastSin(theta);
benkatz 26:2b865c00d7e9 81 float c = FastCos(theta);
benkatz 26:2b865c00d7e9 82 //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 83 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 84 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 25:f5741040c4bb 85 ///Cogging compensation lookup, doesn't actually work yet///
benkatz 23:2adf23ee0305 86 //int ind = theta * (128.0f/(2.0f*PI));
benkatz 23:2adf23ee0305 87 //float cogging_current = controller->cogging[ind];
benkatz 26:2b865c00d7e9 88 float cogging_current = 0.05f*s*controller->i_q_ref;
benkatz 25:f5741040c4bb 89
benkatz 25:f5741040c4bb 90 /// PI Controller ///
benkatz 22:60276ba87ac6 91 float i_d_error = controller->i_d_ref - controller->i_d;
benkatz 23:2adf23ee0305 92 float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current;
benkatz 22:60276ba87ac6 93 float v_d_ff = 2.0f*(2*controller->i_d_ref*R_PHASE); //feed-forward voltage
benkatz 26:2b865c00d7e9 94 float v_q_ff = controller->dtheta_elec*WB*1.73205081;
benkatz 22:60276ba87ac6 95 controller->d_int += i_d_error;
benkatz 22:60276ba87ac6 96 controller->q_int += i_q_error;
benkatz 22:60276ba87ac6 97
benkatz 22:60276ba87ac6 98 //v_d_ff = 0;
benkatz 22:60276ba87ac6 99 //v_q_ff = 0;
benkatz 22:60276ba87ac6 100
benkatz 25:f5741040c4bb 101 limit_norm(&controller->d_int, &controller->q_int, V_BUS/(K_Q*KI_Q)); // Limit integrators to prevent windup
benkatz 25:f5741040c4bb 102 controller->v_d = K_SCALE*I_BW*i_d_error + K_SCALE*I_BW*controller->d_int;// + v_d_ff;
benkatz 25:f5741040c4bb 103 controller->v_q = K_SCALE*I_BW*i_q_error + K_SCALE*I_BW*controller->q_int;// + v_q_ff;
benkatz 22:60276ba87ac6 104
benkatz 22:60276ba87ac6 105 //controller->v_d = v_d_ff;
benkatz 22:60276ba87ac6 106 //controller->v_q = v_q_ff;
benkatz 22:60276ba87ac6 107
benkatz 25:f5741040c4bb 108 limit_norm(&controller->v_d, &controller->v_q, controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus
benkatz 26:2b865c00d7e9 109 //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 110
benkatz 27:501fee691e0e 111 controller->v_u = c*controller->v_d - s*controller->v_q; // Faster Inverse DQ0 transform
benkatz 26:2b865c00d7e9 112 controller->v_v = (0.86602540378f*s-.5f*c)*controller->v_d - (-0.86602540378f*c-.5f*s)*controller->v_q;
benkatz 26:2b865c00d7e9 113 controller->v_w = (-0.86602540378f*s-.5f*c)*controller->v_d - (0.86602540378f*c-.5f*s)*controller->v_q;
benkatz 22:60276ba87ac6 114 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 115
benkatz 22:60276ba87ac6 116
benkatz 25:f5741040c4bb 117 if(PHASE_ORDER){ // Check which phase order to use,
benkatz 27:501fee691e0e 118 TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-controller->dtc_u); // Write duty cycles
benkatz 27:501fee691e0e 119 TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-controller->dtc_v);
benkatz 27:501fee691e0e 120 TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 121 }
benkatz 22:60276ba87ac6 122 else{
benkatz 27:501fee691e0e 123 TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-controller->dtc_u);
benkatz 27:501fee691e0e 124 TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-controller->dtc_v);
benkatz 27:501fee691e0e 125 TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 126 }
benkatz 25:f5741040c4bb 127
benkatz 25:f5741040c4bb 128 controller->theta_elec = theta; //For some reason putting this at the front breaks thins
benkatz 22:60276ba87ac6 129
benkatz 22:60276ba87ac6 130
benkatz 26:2b865c00d7e9 131 if(controller->loop_count >400){
benkatz 22:60276ba87ac6 132 //controller->i_q_ref = -controller->i_q_ref;
benkatz 26:2b865c00d7e9 133 controller->loop_count = 0;
benkatz 22:60276ba87ac6 134
benkatz 26:2b865c00d7e9 135 //printf("%.1f %.1f %.1f %.1f %.1f\n\r", controller->i_d, controller->i_q, controller->v_d, controller->v_q, controller->dtheta_mech);
benkatz 22:60276ba87ac6 136 //printf("%f\n\r", controller->theta_elec);
benkatz 22:60276ba87ac6 137 //pc.printf("%f %f %f\n\r", controller->i_a, controller->i_b, controller->i_c);
benkatz 22:60276ba87ac6 138 //pc.printf("%f %f\n\r", controller->i_d, controller->i_q);
benkatz 22:60276ba87ac6 139 //pc.printf("%d %d\n\r", controller->adc1_raw, controller->adc2_raw);
benkatz 26:2b865c00d7e9 140 }
benkatz 22:60276ba87ac6 141 }
benkatz 26:2b865c00d7e9 142
benkatz 26:2b865c00d7e9 143
benkatz 26:2b865c00d7e9 144 void torque_control(ControllerStruct *controller){
benkatz 26:2b865c00d7e9 145 float torque_ref = -controller->kp*(controller->p_des - controller->theta_mech) + controller->t_ff;// + controller->kd*(controller->v_des - GR*controller->dtheta_mech);
benkatz 26:2b865c00d7e9 146 //float torque_ref = -.1*(controller->p_des - controller->theta_mech);
benkatz 26:2b865c00d7e9 147 controller->i_q_ref = torque_ref/KT_OUT;
benkatz 26:2b865c00d7e9 148 controller->i_d_ref = 0;
benkatz 26:2b865c00d7e9 149 }
benkatz 26:2b865c00d7e9 150
benkatz 26:2b865c00d7e9 151
benkatz 22:60276ba87ac6 152 /*
benkatz 22:60276ba87ac6 153 void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){
benkatz 22:60276ba87ac6 154
benkatz 22:60276ba87ac6 155 }
benkatz 22:60276ba87ac6 156 */