123
Dependencies: mbed-dev-f303 FastPWM3
Diff: FOC/foc.cpp
- Revision:
- 26:2b865c00d7e9
- Parent:
- 25:f5741040c4bb
- Child:
- 27:501fee691e0e
--- a/FOC/foc.cpp Sun Apr 09 03:05:52 2017 +0000 +++ b/FOC/foc.cpp Mon May 01 15:22:58 2017 +0000 @@ -1,8 +1,8 @@ #include "user_config.h" #include "foc.h" -//#include "FastMath.h" -//using namespace FastMath; +#include "FastMath.h" +using namespace FastMath; void abc( float theta, float d, float q, float *a, float *b, float *c){ @@ -10,18 +10,22 @@ ///Phase current amplitude = lengh of dq vector/// ///i.e. iq = 1, id = 0, peak phase current of 1/// - *a = d*cosf(-theta) + q*sinf(-theta); - *b = d*cosf((2.0f*PI/3.0f)-theta) + q*sinf((2.0f*PI/3.0f)-theta); - *c = d*cosf((-2.0f*PI/3.0f)-theta) + q*sinf((-2.0f*PI/3.0f)-theta); + *a = d*cosf(theta) - q*sinf(theta); + *b = d*cosf(theta - (2.0f*PI/3.0f)) - q*sinf(theta - (2.0f*PI/3.0f)); + *c = d*cosf(theta + (2.0f*PI/3.0f)) - q*sinf(theta +(2.0f*PI/3.0f)); } + void dq0(float theta, float a, float b, float c, float *d, float *q){ /// DQ0 Transform /// ///Phase current amplitude = lengh of dq vector/// ///i.e. iq = 1, id = 0, peak phase current of 1/// - *d = (2.0f/3.0f)*(a*cosf(-theta) + b*cosf((2.0f*PI/3.0f)-theta) + c*cosf((-2.0f*PI/3.0f)-theta)); - *q = (2.0f/3.0f)*(a*sinf(-theta) + b*sinf((2.0f*PI/3.0f)-theta) + c*sinf((-2.0f*PI/3.0f)-theta)); + //float cos = cosf(theta); + //float sin = sinf(theta); + + *d = (2.0f/3.0f)*(a*cosf(theta) + b*cosf(theta - (2.0f*PI/3.0f)) + c*cosf(theta + (2.0f*PI/3.0f))); + *q = (2.0f/3.0f)*(-a*sinf(theta) - b*sinf(theta - (2.0f*PI/3.0f)) - c*sinf(theta + (2.0f*PI/3.0f))); } void svm(float v_bus, float u, float v, float w, float *dtc_u, float *dtc_v, float *dtc_w){ @@ -31,7 +35,7 @@ float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))/2.0f; *dtc_u = fminf(fmaxf(((u - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX); *dtc_v = fminf(fmaxf(((v - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX); - *dtc_w = fminf(fmaxf(((w - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX); + *dtc_w = fminf(fmaxf(((w - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX); } @@ -40,7 +44,10 @@ int adc2_offset = 0; int n = 1024; for (int i = 0; i<n; i++){ // Average n samples of the ADC - ADC1->CR2 |= 0x40000000; + TIM1->CCR3 = 0x708*(1.0f); // Write duty cycles + TIM1->CCR2 = 0x708*(1.0f); + TIM1->CCR1 = 0x708*(1.0f); + ADC1->CR2 |= 0x40000000; // Begin sample and conversion wait(.001); adc2_offset += ADC2->DR; adc1_offset += ADC1->DR; @@ -57,6 +64,7 @@ void commutate(ControllerStruct *controller, GPIOStruct *gpio, float theta){ /// Commutation Loop /// + controller->loop_count ++; if(PHASE_ORDER){ // Check current sensor ordering controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings @@ -66,19 +74,23 @@ controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); } - controller->i_a = -controller->i_b - controller->i_c; - dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents + controller->i_a = -controller->i_b - controller->i_c; + float s = FastSin(theta); + float c = FastCos(theta); + //dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents + 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); ///Fast DQ0 Transform + 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); ///Cogging compensation lookup, doesn't actually work yet/// //int ind = theta * (128.0f/(2.0f*PI)); //float cogging_current = controller->cogging[ind]; - //float cogging_current = 1.0f*cos(6*theta); + float cogging_current = 0.05f*s*controller->i_q_ref; /// PI Controller /// float i_d_error = controller->i_d_ref - controller->i_d; float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current; float v_d_ff = 2.0f*(2*controller->i_d_ref*R_PHASE); //feed-forward voltage - float v_q_ff = 2.0f*(2*controller->i_q_ref*R_PHASE + controller->dtheta_elec*WB*0.8165f); + float v_q_ff = controller->dtheta_elec*WB*1.73205081; controller->d_int += i_d_error; controller->q_int += i_q_error; @@ -86,10 +98,6 @@ //v_q_ff = 0; limit_norm(&controller->d_int, &controller->q_int, V_BUS/(K_Q*KI_Q)); // Limit integrators to prevent windup - //controller->d_int = fminf(fmaxf(controller->d_int, -D_INT_LIM), D_INT_LIM); - //controller->q_int = fminf(fmaxf(controller->q_int, -Q_INT_LIM), Q_INT_LIM); - - controller->v_d = K_SCALE*I_BW*i_d_error + K_SCALE*I_BW*controller->d_int;// + v_d_ff; controller->v_q = K_SCALE*I_BW*i_q_error + K_SCALE*I_BW*controller->q_int;// + v_q_ff; @@ -97,7 +105,11 @@ //controller->v_q = v_q_ff; limit_norm(&controller->v_d, &controller->v_q, controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus - abc(controller->theta_elec, controller->v_d, controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages + //abc(controller->theta_elec, controller->v_d, controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages + + controller->v_u = c*controller->v_d - s*controller->v_q; + controller->v_v = (0.86602540378f*s-.5f*c)*controller->v_d - (-0.86602540378f*c-.5f*s)*controller->v_q; + controller->v_w = (-0.86602540378f*s-.5f*c)*controller->v_d - (0.86602540378f*c-.5f*s)*controller->v_q; 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 @@ -115,17 +127,27 @@ controller->theta_elec = theta; //For some reason putting this at the front breaks thins - //if(controller->loop_count >400){ + if(controller->loop_count >400){ //controller->i_q_ref = -controller->i_q_ref; - // controller->loop_count = 0; + controller->loop_count = 0; - //printf("%d %f\n\r", ind, cogging_current); + //printf("%.1f %.1f %.1f %.1f %.1f\n\r", controller->i_d, controller->i_q, controller->v_d, controller->v_q, controller->dtheta_mech); //printf("%f\n\r", controller->theta_elec); //pc.printf("%f %f %f\n\r", controller->i_a, controller->i_b, controller->i_c); //pc.printf("%f %f\n\r", controller->i_d, controller->i_q); //pc.printf("%d %d\n\r", controller->adc1_raw, controller->adc2_raw); - // } + } } + + +void torque_control(ControllerStruct *controller){ + float torque_ref = -controller->kp*(controller->p_des - controller->theta_mech) + controller->t_ff;// + controller->kd*(controller->v_des - GR*controller->dtheta_mech); + //float torque_ref = -.1*(controller->p_des - controller->theta_mech); + controller->i_q_ref = torque_ref/KT_OUT; + controller->i_d_ref = 0; + } + + /* void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){