DRV8323RS Version

Dependencies:   mbed-dev-f303 FastPWM3

Revision:
47:e1196a851f76
Parent:
46:2d4b1dafcfe3
Child:
48:74a40481740c
--- a/FOC/foc.cpp	Thu Jul 12 02:50:34 2018 +0000
+++ b/FOC/foc.cpp	Wed Dec 05 04:07:46 2018 +0000
@@ -34,12 +34,22 @@
     /// u,v,w amplitude = v_bus for full modulation depth ///
     
     float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))/2.0f;
+    
     *dtc_u = fminf(fmaxf(((u -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX);
     *dtc_v = fminf(fmaxf(((v -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX);
-    *dtc_w = fminf(fmaxf(((w -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX);  
+    *dtc_w = fminf(fmaxf(((w -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); 
+    
+    /*
+    sinusoidal pwm
+    *dtc_u = fminf(fmaxf((u/v_bus + .5f), DTC_MIN), DTC_MAX);
+    *dtc_v = fminf(fmaxf((v/v_bus + .5f), DTC_MIN), DTC_MAX);
+    *dtc_w = fminf(fmaxf((w/v_bus + .5f), DTC_MIN), DTC_MAX);
+    */
+     
     
     }
 
+
 void zero_current(int *offset_1, int *offset_2){                                // Measure zero-offset of the current sensors
     int adc1_offset = 0;
     int adc2_offset = 0;
@@ -79,6 +89,7 @@
     controller->d_int = 0;
     controller->v_q = 0;
     controller->v_d = 0;
+
     }
     
 void limit_current_ref (ControllerStruct *controller){
@@ -88,11 +99,8 @@
     }
 
 
-void commutate(ControllerStruct *controller, ObserverStruct *observer, GPIOStruct *gpio, float theta){
-       /// Observer Prediction ///
-       observer->i_d_est += DT*(observer->i_d_dot);
-       observer->i_q_est += DT*(observer->i_q_dot);
-       
+void commutate(ControllerStruct *controller, GPIOStruct *gpio, float theta){
+
        /// Commutation Loop ///
        controller->loop_count ++;   
        if(PHASE_ORDER){                                                                          // Check current sensor ordering
@@ -112,21 +120,8 @@
        //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);
         
         controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q;
-        observer->i_d_m = controller->i_d;
-        observer->i_q_m = controller->i_q;
-        
-        observer->e_d = observer->i_d_m - observer->i_d_est;
-        observer->e_q = observer->i_q_m - observer->i_q_est;
-        observer->e_d_int += observer->e_d;
-        observer->e_q_int += observer->e_q;
+        controller->i_d_filt = 0.95f*controller->i_d_filt + 0.05f*controller->i_d;
         
-        observer->i_d_est +=  K_O*observer->e_d + .001f*observer->e_d_int;
-        observer->i_q_est += K_O*observer->e_q + .001f*observer->e_q_int;
-        
-        
-        //float scog12 = FastSin(12.0f*theta);
-        //float scog1 = s;
-        //float cogging_current = 0.25f*scog1 - 0.3f*scog12;
         
         // Filter the current references to the desired closed-loopbandwidth
         // Allows calculation of desired di/dt for inductance, etc
@@ -137,32 +132,42 @@
         controller->did_dt = (controller->i_d_ref_filt - controller->did_dt)/DT;
         controller->diq_dt = (controller->i_q_ref_filt - controller->diq_dt)/DT;
        
+       
+       /// Field Weakening ///
+       /*
+       controller->fw_int += .001*(0.5f*OVERMODULATION*controller->v_bus - controller->v_ref);
+       controller->fw_int = fmaxf(fminf(controller->fw_int, 0.0f), -I_MAX_FW);
+       controller->i_d_ref = controller->fw_int;
+       float i_cmd_mag_sq = controller->i_d_ref*controller->i_d_ref + controller->i_q_ref*controller->i_q_ref;
+       limit_norm(&controller->i_d_ref, &controller->i_q_ref, I_MAX);
+       
+       */
+       
        /// 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;
        
        // Calculate feed-forward voltages //
-       float v_d_ff = 2.0f*(0.0f*controller->i_d_ref*R_PHASE + 0.0f*L_D*controller->did_dt - controller->dtheta_elec*L_Q*controller->i_q);   //feed-forward voltages
-       float v_q_ff =  2.0f*(0.0f*controller->i_q_ref*R_PHASE + 0.0f*L_Q*controller->diq_dt + controller->dtheta_elec*(L_D*controller->i_d + 0.0f*WB));
+       float v_d_ff = 2.0f*(1.0f*controller->i_d_ref*R_PHASE  - controller->dtheta_elec*L_Q*controller->i_q);   //feed-forward voltages
+       float v_q_ff =  2.0f*(1.0f*controller->i_q_ref*R_PHASE +  controller->dtheta_elec*(L_D*controller->i_d + 1.0f*WB));
        
        // Integrate Error //
-       controller->d_int += i_d_error;   
-       controller->q_int += i_q_error;
-
-       limit_norm(&controller->d_int, &controller->q_int, V_BUS/(controller->k_q*controller->ki_q));        
-       controller->v_d = controller->k_d*(i_d_error + controller->ki_d*controller->d_int) + v_d_ff;  
-       controller->v_q = controller->k_q*(i_q_error + controller->ki_q*controller->q_int) + v_q_ff; 
+       controller->d_int += controller->k_d*controller->ki_d*i_d_error;   
+       controller->q_int += controller->k_q*controller->ki_q*i_q_error;
+       
+       controller->d_int = fmaxf(fminf(controller->d_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus);
+       controller->q_int = fmaxf(fminf(controller->q_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); 
+       
+       //limit_norm(&controller->d_int, &controller->q_int, OVERMODULATION*controller->v_bus);     
+       controller->v_d = controller->k_d*i_d_error + controller->d_int ;//+ v_d_ff;  
+       controller->v_q = controller->k_q*i_q_error + controller->q_int ;//+ v_q_ff; 
+       
+       controller->v_ref = sqrt(controller->v_d*controller->v_d + controller->v_q*controller->v_q);
        
        limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus);       // Normalize voltage vector to lie within curcle of radius v_bus
-       abc(controller->theta_elec + 0.5f*DT*controller->dtheta_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 + 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
        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
 
-       observer->i_d_dot = 0.5f*(controller->v_d - 2.0f*(observer->i_d_est*R_PHASE - controller->dtheta_elec*L_Q*observer->i_q_est))/L_D;   //feed-forward voltage
-       observer->i_q_dot =  0.5f*(controller->v_q - 2.0f*(observer->i_q_est*R_PHASE  + controller->dtheta_elec*(L_D*observer->i_d_est + WB)))/L_Q;
-       
-       //controller->dtc_u = 0.5f;
-       //controller->dtc_v = 0.6f;
-       //controller->dtc_w = 0.5f;
        if(PHASE_ORDER){                                                         // Check which phase order to use, 
             TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u);                        // Write duty cycles
             TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v);
@@ -176,17 +181,6 @@
 
        controller->theta_elec = theta;                                          //For some reason putting this at the front breaks thins
        
-
-       if(controller->loop_count >400){
-           //controller->i_q_ref = -controller->i_q_ref;
-          controller->loop_count  = 0;
-           
-           //printf("%.2f  %.2f  %.2f\n\r", controller->i_a, controller->i_b, controller->i_c);
-           //printf("%f\n\r", controller->dtheta_mech*GR);
-           //pc.printf("%f    %f    %f\n\r", controller->i_a, controller->i_b, controller->i_c);
-           printf("%f %f\n\r", v_q_ff, v_d_ff);
-           //pc.printf("%d    %d\n\r", controller->adc1_raw, controller->adc2_raw);
-            }
     }
     
     
@@ -196,10 +190,4 @@
     controller->i_q_ref = torque_ref/KT_OUT;    
     controller->i_d_ref = 0.0f;
     }
-
-
-/*    
-void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){
-    
-    }
-*/    
\ No newline at end of file
+ 
\ No newline at end of file