Gu Jasper / Motor_200Nm_V0

Dependencies:   mbed-dev-f303 FastPWM3

Files at this revision

API Documentation at this revision

Comitter:
benkatz
Date:
Wed May 17 14:53:22 2017 +0000
Parent:
26:2b865c00d7e9
Child:
28:8c7e29f719c5
Commit message:
Setting PWM ARR value in config file now propagates everywhere.

Changed in this revision

Calibration/calibration.cpp Show annotated file Show diff for this revision Revisions of this file
Config/hw_config.h Show annotated file Show diff for this revision Revisions of this file
FOC/foc.cpp Show annotated file Show diff for this revision Revisions of this file
hw_setup.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Calibration/calibration.cpp	Mon May 01 15:22:58 2017 +0000
+++ b/Calibration/calibration.cpp	Wed May 17 14:53:22 2017 +0000
@@ -23,9 +23,9 @@
     abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                                 //inverse dq0 transform on voltages
     svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w);                            //space vector modulation
     for(int i = 0; i<20000; i++){
-        TIM1->CCR3 = 0x708*(1.0f-dtc_u);                                        // Set duty cycles
-        TIM1->CCR2 = 0x708*(1.0f-dtc_v);
-        TIM1->CCR1 = 0x708*(1.0f-dtc_w);
+        TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-dtc_u);                                        // Set duty cycles
+        TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_v);
+        TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_w);
         wait_us(100);
         }
     //ps->ZeroPosition();
@@ -45,9 +45,9 @@
         abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                             //inverse dq0 transform on voltages
         svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w);                        //space vector modulation
         wait_us(100);
-        TIM1->CCR3 = 0x708*(1.0f-dtc_u);                                        //Set duty cycles
-        TIM1->CCR2 = 0x708*(1.0f-dtc_v);
-        TIM1->CCR1 = 0x708*(1.0f-dtc_w);
+        TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-dtc_u);                                        //Set duty cycles
+        TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_v);
+        TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_w);
        ps->Sample();                                                            //sample position sensor
        theta_actual = ps->GetMechPosition();
        if(theta_ref==0){theta_start = theta_actual;}
@@ -92,14 +92,14 @@
     abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                                 // inverse dq0 transform on voltages
     svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w);                            // space vector modulation
     for(int i = 0; i<40000; i++){
-        TIM1->CCR3 = 0x708*(1.0f-dtc_u);                                        // Set duty cycles
+        TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-dtc_u);                                        // Set duty cycles
         if(PHASE_ORDER){                                   
-            TIM1->CCR2 = 0x708*(1.0f-dtc_v);
-            TIM1->CCR1 = 0x708*(1.0f-dtc_w);
+            TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_v);
+            TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_w);
             }
         else{
-            TIM1->CCR1 = 0x708*(1.0f-dtc_v);
-            TIM1->CCR2 = 0x708*(1.0f-dtc_w);
+            TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_v);
+            TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_w);
             }
         wait_us(100);
         }
@@ -115,14 +115,14 @@
         theta_ref += delta;
        abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                              // inverse dq0 transform on voltages
        svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w);                         // space vector modulation
-        TIM1->CCR3 = 0x708*(1.0f-dtc_u);
+        TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-dtc_u);
         if(PHASE_ORDER){                                                        // Check phase ordering
-            TIM1->CCR2 = 0x708*(1.0f-dtc_v);                                    // Set duty cycles
-            TIM1->CCR1 = 0x708*(1.0f-dtc_w);
+            TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_v);                                    // Set duty cycles
+            TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_w);
             }
         else{
-            TIM1->CCR1 = 0x708*(1.0f-dtc_v);
-            TIM1->CCR2 = 0x708*(1.0f-dtc_w);
+            TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_v);
+            TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_w);
             }
             wait_us(100);
             ps->Sample();
@@ -140,14 +140,14 @@
        theta_ref -= delta;
        abc(theta_ref, v_d, v_q, &v_u, &v_v, &v_w);                              // inverse dq0 transform on voltages
        svm(1.0, v_u, v_v, v_w, &dtc_u, &dtc_v, &dtc_w);                         // space vector modulation
-        TIM1->CCR3 = 0x708*(1.0f-dtc_u);
+        TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-dtc_u);
         if(PHASE_ORDER){
-            TIM1->CCR2 = 0x708*(1.0f-dtc_v);
-            TIM1->CCR1 = 0x708*(1.0f-dtc_w);
+            TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_v);
+            TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_w);
             }
         else{
-            TIM1->CCR1 = 0x708*(1.0f-dtc_v);
-            TIM1->CCR2 = 0x708*(1.0f-dtc_w);
+            TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-dtc_v);
+            TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-dtc_w);
             }
             wait_us(100);
             ps->Sample();
--- a/Config/hw_config.h	Mon May 01 15:22:58 2017 +0000
+++ b/Config/hw_config.h	Wed May 17 14:53:22 2017 +0000
@@ -8,6 +8,7 @@
 #define I_SCALE 0.02014160156f  // Amps per A/D Count
 #define DTC_MAX 0.95f          // Max phase duty cycle
 #define DTC_MIN 0.05f          // Min phase duty cycle
+#define PWM_ARR 0x8CA           /// timer autoreload value
 
 
 #endif
--- a/FOC/foc.cpp	Mon May 01 15:22:58 2017 +0000
+++ b/FOC/foc.cpp	Wed May 17 14:53:22 2017 +0000
@@ -1,4 +1,5 @@
 #include "user_config.h"
+#include "hw_config.h"
 #include "foc.h"
 
 #include "FastMath.h"
@@ -44,9 +45,9 @@
     int adc2_offset = 0;
     int n = 1024;
     for (int i = 0; i<n; i++){                                                  // Average n samples of the ADC
-        TIM1->CCR3 = 0x708*(1.0f);                                               // Write duty cycles
-        TIM1->CCR2 = 0x708*(1.0f);
-        TIM1->CCR1 = 0x708*(1.0f);
+        TIM1->CCR3 = (PWM_ARR>>1)*(1.0f);                                               // Write duty cycles
+        TIM1->CCR2 = (PWM_ARR>>1)*(1.0f);
+        TIM1->CCR1 = (PWM_ARR>>1)*(1.0f);
         ADC1->CR2  |= 0x40000000;                                               // Begin sample and conversion
         wait(.001);
         adc2_offset += ADC2->DR;
@@ -79,7 +80,7 @@
        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_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
        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));
@@ -107,21 +108,21 @@
        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
     
-        controller->v_u = c*controller->v_d - s*controller->v_q;
+        controller->v_u = c*controller->v_d - s*controller->v_q;                // Faster Inverse DQ0 transform
         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
 
        
        if(PHASE_ORDER){                                                         // Check which phase order to use, 
-            TIM1->CCR3 = 0x708*(1.0f-controller->dtc_u);                        // Write duty cycles
-            TIM1->CCR2 = 0x708*(1.0f-controller->dtc_v);
-            TIM1->CCR1 = 0x708*(1.0f-controller->dtc_w);
+            TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-controller->dtc_u);                        // Write duty cycles
+            TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-controller->dtc_v);
+            TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-controller->dtc_w);
         }
         else{
-            TIM1->CCR3 = 0x708*(1.0f-controller->dtc_u);
-            TIM1->CCR1 = 0x708*(1.0f-controller->dtc_v);
-            TIM1->CCR2 = 0x708*(1.0f-controller->dtc_w);
+            TIM1->CCR3 = (PWM_ARR>>1)*(1.0f-controller->dtc_u);
+            TIM1->CCR1 = (PWM_ARR>>1)*(1.0f-controller->dtc_v);
+            TIM1->CCR2 = (PWM_ARR>>1)*(1.0f-controller->dtc_w);
         }
 
        controller->theta_elec = theta;                                          //For some reason putting this at the front breaks thins
--- a/hw_setup.cpp	Mon May 01 15:22:58 2017 +0000
+++ b/hw_setup.cpp	Wed May 17 14:53:22 2017 +0000
@@ -34,7 +34,7 @@
 
     TIM1->PSC = 0x0;                                            // no prescaler, timer counts up in sync with the peripheral clock
     //TIM1->ARR = 0x1194; // 20 khz
-    TIM1->ARR = 0x8CA;                                          // set auto reload, 40 khz
+    TIM1->ARR = PWM_ARR;                                          // set auto reload, 40 khz
     TIM1->CCER |= ~(TIM_CCER_CC1NP);                            // Interupt when low side is on.
     TIM1->CR1 |= TIM_CR1_CEN;                                   // enable TIM1
     
--- a/main.cpp	Mon May 01 15:22:58 2017 +0000
+++ b/main.cpp	Wed May 17 14:53:22 2017 +0000
@@ -249,8 +249,8 @@
                 //TIM1->CCR2 = 0x708*(1.0f);     
                 
                 //controller.i_q_ref = controller.t_ff/KT_OUT;   
-                //torque_control(&controller);      
-                controller.i_q_ref = 1; 
+                torque_control(&controller);      
+                //controller.i_q_ref = 1; 
                 commutate(&controller, &gpio, controller.theta_elec);           // Run current loop
                 spi.Sample();                                                   // Sample position sensor
                 toggle.write(0);