bldc driver firmware based on hobbyking cheetah compact

Dependencies:   BLDC_V2 mbed-dev-f303 FastPWM3

Dependents:   BLDC_V2

Revision:
47:f4ecf3e0576a
Parent:
45:aadebe074af6
Child:
48:a74e401a6d84
--- a/Calibration/calibration.cpp	Mon Jul 30 20:33:23 2018 +0000
+++ b/Calibration/calibration.cpp	Wed May 13 09:53:27 2020 +0000
@@ -9,11 +9,13 @@
 #include "motor_config.h"
 #include "current_controller_config.h"
 
+extern Serial pc;
+
 void order_phases(PositionSensor *ps, GPIOStruct *gpio, ControllerStruct *controller, PreferenceWriter *prefs){   
     
     ///Checks phase order, to ensure that positive Q current produces
     ///torque in the positive direction wrt the position sensor.
-    printf("\n\r Checking phase ordering\n\r");
+    pc.printf("\n\r Checking phase ordering\n\r");
     float theta_ref = 0;
     float theta_actual = 0;
     float v_d = .15f;                                                             //Put all volts on the D-Axis
@@ -41,8 +43,8 @@
     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
     float current = sqrt(pow(controller->i_d, 2) + pow(controller->i_q, 2));
-    printf("\n\rCurrent\n\r");
-    printf("%f    %f   %f\n\r\n\r", controller->i_d, controller->i_q, current);
+    pc.printf("\n\rCurrent\n\r");
+    pc.printf("%f    %f   %f\n\r\n\r", controller->i_d, controller->i_q, current);
     /// Rotate voltage angle
     while(theta_ref < 4*PI){                                                    //rotate for 2 electrical cycles
         abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                             //inverse dq0 transform on voltages
@@ -56,17 +58,17 @@
        if(theta_ref==0){theta_start = theta_actual;}
        if(sample_counter > 200){
            sample_counter = 0 ;
-        printf("%.4f   %.4f\n\r", theta_ref/(NPP), theta_actual);
+        pc.printf("%.4f   %.4f\n\r", theta_ref/(NPP), theta_actual);
         }
         sample_counter++;
        theta_ref += 0.001f;
         }
     float theta_end = ps->GetMechPositionFixed();
     int direction = (theta_end - theta_start)>0;
-    printf("Theta Start:   %f    Theta End:  %f\n\r", theta_start, theta_end);
-    printf("Direction:  %d\n\r", direction);
-    if(direction){printf("Phasing correct\n\r");}
-    else if(!direction){printf("Phasing incorrect.  Swapping phases V and W\n\r");}
+    pc.printf("Theta Start:   %f    Theta End:  %f\n\r", theta_start, theta_end);
+    pc.printf("Direction:  %d\n\r", direction);
+    if(direction){pc.printf("Phasing correct\n\r");}
+    else if(!direction){pc.printf("Phasing incorrect.  Swapping phases V and W\n\r");}
     PHASE_ORDER = direction;
     }
     
@@ -74,7 +76,7 @@
 void calibrate(PositionSensor *ps, GPIOStruct *gpio, ControllerStruct *controller, PreferenceWriter *prefs){
     /// Measures the electrical angle offset of the position sensor
     /// and (in the future) corrects nonlinearity due to position sensor eccentricity
-    printf("Starting calibration procedure\n\r");
+    pc.printf("Starting calibration procedure\n\r");
     
     const int n = 128*NPP;                                                      // number of positions to be sampled per mechanical rotation.  Multiple of NPP for filtering reasons (see later)
     const int n2 = 50;                                                          // increments between saved samples (for smoothing motion)
@@ -115,7 +117,7 @@
     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
     float current = sqrt(pow(controller->i_d, 2) + pow(controller->i_q, 2));
-    printf(" Current Angle : Rotor Angle : Raw Encoder \n\r\n\r");
+    pc.printf(" Current Angle : Rotor Angle : Raw Encoder \n\r\n\r");
     for(int i = 0; i<n; i++){                                                   // rotate forwards
        for(int j = 0; j<n2; j++){   
         theta_ref += delta;
@@ -137,7 +139,7 @@
        theta_actual = ps->GetMechPositionFixed();
        error_f[i] = theta_ref/NPP - theta_actual;
        raw_f[i] = ps->GetRawPosition();
-        printf("%.4f   %.4f    %d\n\r", theta_ref/(NPP), theta_actual, raw_f[i]);
+        pc.printf("%.4f   %.4f    %d\n\r", theta_ref/(NPP), theta_actual, raw_f[i]);
        //theta_ref += delta;
         }
     
@@ -162,7 +164,7 @@
        theta_actual = ps->GetMechPositionFixed();                                    // get mechanical position
        error_b[i] = theta_ref/NPP - theta_actual;
        raw_b[i] = ps->GetRawPosition();
-       printf("%.4f   %.4f    %d\n\r", theta_ref/(NPP), theta_actual, raw_b[i]);
+       pc.printf("%.4f   %.4f    %d\n\r", theta_ref/(NPP), theta_actual, raw_b[i]);
        //theta_ref -= delta;
         }    
         
@@ -208,22 +210,22 @@
         int raw_offset = (raw_f[0] + raw_b[n-1])/2;                             //Insensitive to errors in this direction, so 2 points is plenty
         
         
-        printf("\n\r Encoder non-linearity compensation table\n\r");
-        printf(" Sample Number : Lookup Index : Lookup Value : Cogging Current Lookup\n\r\n\r");
+        pc.printf("\n\r Encoder non-linearity compensation table\n\r");
+        pc.printf(" Sample Number : Lookup Index : Lookup Value : Cogging Current Lookup\n\r\n\r");
         for (int i = 0; i<n_lut; i++){                                          // build lookup table
             int ind = (raw_offset>>7) + i;
             if(ind > (n_lut-1)){ 
                 ind -= n_lut;
                 }
             lut[ind] = (int) ((error_filt[i*NPP] - mean)*(float)(ps->GetCPR())/(2.0f*PI));
-            printf("%d   %d   %d   %f\n\r", i, ind, lut[ind],  cogging_current[i]);
+            pc.printf("%d   %d   %d   %f\n\r", i, ind, lut[ind],  cogging_current[i]);
             wait(.001);
             }
             
         ps->WriteLUT(lut);                                                      // write lookup table to position sensor object
         //memcpy(controller->cogging, cogging_current, sizeof(controller->cogging));  //compensation doesn't actually work yet....
         memcpy(&ENCODER_LUT, lut, sizeof(lut));                                 // copy the lookup table to the flash array
-        printf("\n\rEncoder Electrical Offset (rad) %f\n\r",  offset);
+        pc.printf("\n\rEncoder Electrical Offset (rad) %f\n\r",  offset);
         
         if (!prefs->ready()) prefs->open();
         prefs->flush();                                                         // write offset and lookup table to flash