CQ Inverter Kit 2 test

Revision:
1:786897114846
Parent:
0:5602fba2a7f7
Child:
3:a4b4a8e3f2a0
--- a/BLDCmotorDriver.cpp	Fri May 22 22:43:39 2015 +0000
+++ b/BLDCmotorDriver.cpp	Sun May 24 06:46:25 2015 +0000
@@ -1,11 +1,20 @@
 #include "BLDCmotorDriver.h"
 
-BLDCmotorDriver::BLDCmotorDriver(PinName gh_a, PinName gl_a, PinName gh_b, PinName gl_b, PinName gh_c, PinName gl_c, PinName h1, PinName h2, PinName h3, PinName led1) : GH_A(gh_a), GL_A(gl_a), GH_B(gh_b), GL_B(gl_b), GH_C(gh_c), GL_C(gl_c), H1(h1), H2(h2), H3(h3), Led1(led1) {
+BLDCmotorDriver::BLDCmotorDriver(PinName pGH_A, PinName pGH_B, PinName pGH_C, PinName pGL_A, PinName pGL_B, PinName pGL_C,
+                    PinName pH1, PinName pH2, PinName pH3, PinName pfault) : 
+                    GH_A(pGH_A), GH_B(pGH_B), GH_C(pGH_C), GL_A(pGL_A), GL_B(pGL_B), GL_C(pGL_C),
+                    H1(pH1), H2(pH2), H3(pH3), fault(pfault) {
     sampleTime = 1e-3;
     switchingPeriod = 1.0 / 20e3;
     dutyCycle = tempDutyCycle = 0;
     GH_A.period(switchingPeriod); // applies to all PwmOut instances
     rl.setLimits(0.1, -0.1, 0, sampleTime); // initial 10 second ramp
+    H1.rise(this, &BLDCmotorDriver::commutation);
+    H2.rise(this, &BLDCmotorDriver::commutation);
+    H3.rise(this, &BLDCmotorDriver::commutation);
+    H1.fall(this, &BLDCmotorDriver::commutation);
+    H2.fall(this, &BLDCmotorDriver::commutation);
+    H3.fall(this, &BLDCmotorDriver::commutation);
 }
 void BLDCmotorDriver::configure(float sampleTime, float switchingFrequency, float rampUpSlope, float rampDownSlope) {
     if (sampleTime < 1e-6)
@@ -20,32 +29,34 @@
     switchingPeriod = 1.0 / switchingFrequency;
     rl.setLimits(rampUpSlope, rampDownSlope, 0, sampleTime);
 }
-int BLDCmotorDriver::HallRead(){           // hall 120°
+int BLDCmotorDriver::getSector() {           // hall 120°
   
     if(H1.read()== 1 && H2.read()== 0 && H3.read()== 0)
-            sektor = 1; 
+            currentSector = 1; 
     else if(H1.read()== 1 && H2.read()== 1 && H3.read()== 0)
-            sektor = 2;         
+            currentSector = 2;         
     else if(H1.read()== 0 && H2.read()== 1 && H3.read()== 0)
-            sektor = 3;  
+            currentSector = 3;  
     else if(H1.read()== 0 && H2.read()== 1 && H3.read()== 1)
-            sektor = 4;  
+            currentSector = 4;  
     else if(H1.read()== 0 && H2.read()== 0 && H3.read()== 1)
-            sektor = 5;
+            currentSector = 5;
     else if(H1.read()== 1 && H2.read()== 0 && H3.read()== 1)
-            sektor = 6;             
-    else 
-       Led1 = 1; 
+            currentSector = 6;             
+    else {
+       currentSector = 0;
+       fault = 1; 
+    }
    
-    return sektor;              
+    return currentSector;              
 }
-void BLDCmotorDriver::komutacijaBLDC() {
-    dutyCycle = rl.out(tempDutyCycle);
-    sektor = HallRead();
+
+void BLDCmotorDriver::commutation() {
+    getSector();
     if (dutyCycle > 0) {
-        if (sektor > 6) sektor = 1;
-        sektor++;        
-        switch(sektor) {           
+        if (currentSector > 6) currentSector = 1; // when will this condition happen?
+        // currentSector++;        
+        switch(currentSector) {           
             case 1:               //100      
                 GH_A = dutyCycle;
                 GL_A = 0;
@@ -96,9 +107,9 @@
                 break;
         }
     } else if (dutyCycle < 0) { // 
-        if (sektor < 1) sektor = 6;
-        sektor--;
-        switch(sektor) {
+        // if (currentSector < 1) currentSector = 6; // not necessary
+        // currentSector--;
+        switch(currentSector) {
             case 1:               //100      
                 GH_A = dutyCycle;
                 GL_A = 0;
@@ -147,19 +158,28 @@
                 GH_C = 0;
                 GL_C = 1;
                 break;
-                }                
-        }else {
-        coast();
+            }                
+        } else {
+            coast();
     }
 }
+
 void BLDCmotorDriver::setDutyCycle(float dc) {
     if (dc >= -1 && dc <= 1) {
-        ticker.attach(this, &BLDCmotorDriver::komutacijaBLDC, sampleTime);
+        ticker.attach(this, &BLDCmotorDriver::adjustDutyCycle, sampleTime);
         tempDutyCycle = dc;
     } else {
         coast();
     }
 }
+
+void BLDCmotorDriver::adjustDutyCycle() {
+    dutyCycle = rl.out(tempDutyCycle);
+    float diff = tempDutyCycle - dutyCycle;
+    if (diff < 0.01 || diff > -0.01)
+        ticker.detach();
+}
+
 void BLDCmotorDriver::coast() {
     GH_A = 0;
     GL_A = 0;