Motacon_20200317

Dependents:   HelloWorld_MotorKaisei

Revision:
6:959e725b6be9
Parent:
5:cf8183ae80ef
Child:
7:fb9e3b508237
--- a/BLDCmotorDriver.cpp	Fri Mar 13 01:07:30 2020 +0000
+++ b/BLDCmotorDriver.cpp	Sat Mar 14 00:56:54 2020 +0000
@@ -8,8 +8,8 @@
     sampleTime = 1e-3;
     switchingPeriod = 1.0 / 20e3;
     dutyCycle = tempDutyCycle = 0;
-    GH_A.period(switchingPeriod); // applies to all PwmOut instances
-//    rl.setLimits(0.1, -0.5, 0, sampleTime); // initial 10 second ramp
+    GL_A.period(switchingPeriod); // applies to all PwmOut instances
+    rl.setLimits(0.1, -5, 0, sampleTime); // initial 10 second ramp
     //H1.mode(PullNone);
     //H2.mode(PullNone);
     //H3.mode(PullNone);
@@ -34,12 +34,9 @@
     rl.setLimits(rampUpSlope, rampDownSlope, 0, sampleTime);
 }
 int BLDCmotorDriver::getSector(){           // hall 120°
-    
     h1 = H1.read();  
     h2 = H2.read();
     h3 = H3.read();
-    
-     
     if(h1 == 0 && h2 == 0 && h3 == 1){          
             _currentSector = 0;
              }    
@@ -58,78 +55,133 @@
     else if(h1 == 1 && h2 == 0 && h3 == 1){
             _currentSector = 5;
             }           
-     previousSector = _currentSector - 1;
-     difference = _currentSector - previousSector;
-     if (difference == 1){
-       currentSector = _currentSector;
-       Fault = 0;
-       }
-     else Fault = 1; 
-return currentSector;                
+    previousSector = _currentSector - 1;
+    difference = _currentSector - previousSector;
+    if (difference == 1){
+        currentSector = _currentSector;
+        Fault = 0;
+    }
+    else{
+        Fault = 1; 
+    }
+    return currentSector;                
 }  
 
 void BLDCmotorDriver::commutation()  {
- dutyCycle = rl.out(tempDutyCycle);
- currentSector = getSector();
+    dutyCycle = rl.out(tempDutyCycle);
+    currentSector = getSector();
     if (dutyCycle > 0) {
         currentSector++;
-        if(currentSector > 5)currentSector = 0;
-        switch(currentSector) {  
-        
-          case 0:               //001     
+        if(currentSector > 5){
+            currentSector = 0;
+        }
+        switch(currentSector) {         
+            case 0:               //001     
                 GL_C = 0;                
                 GL_B = 0;
-                GL_A = 1;
+                GL_A = dutyCycle;
                 GH_C = 0;
-                GH_B = dutyCycle;
+                GH_B = 1;
                 GH_A = 0;
                 break;
-           case 1:            
-                GL_C = 1;
+            case 1:            
+                GL_C = dutyCycle;
                 GL_B = 0;
                 GL_A = 0;
                 GH_C = 0;
-                GH_B = dutyCycle;
+                GH_B = 1;
                 GH_A = 0; 
                 break;
-           case 2:             
-                GL_C = 1;
+            case 2:             
+                GL_C = dutyCycle;
                 GL_B = 0;
                 GL_A = 0;
                 GH_C = 0;
                 GH_B = 0;
-                GH_A = dutyCycle;
+                GH_A = 1;
                 break;
             case 3:             
                 GL_C = 0;
-                GL_B = 1;
+                GL_B = dutyCycle;
                 GL_A = 0;
                 GH_C = 0;
                 GH_B = 0;
-                GH_A = dutyCycle;
+                GH_A = 1;
                 break;
             case 4:              
                 GL_C = 0;
-                GL_B = 1;
+                GL_B = dutyCycle;
                 GL_A = 0;
-                GH_C = dutyCycle;
+                GH_C = 1;
                 GH_B = 0;
                 GH_A = 0;
                 break;    
             case 5:              
                 GL_C = 0;
                 GL_B = 0;
-                GL_A = 1;
-                GH_C = dutyCycle;
+                GL_A = dutyCycle;
+                GH_C = 1;
                 GH_B = 0;
                 GH_A = 0;
                 break;
         }
-    } else if (dutyCycle < 0) { 
+    }
+    else if (dutyCycle < 0) { 
         currentSector--;
-        if(currentSector < 0)currentSector = 5;
+        if(currentSector < 0){ 
+            currentSector = 5;
+        }
         switch(currentSector) {
-          case 0:
+            case 0:  
+                GL_C = -dutyCycle;
+                GL_B = -dutyCycle;
+                GL_A = -dutyCycle;
+                GH_C = 0;
+                GH_B = 0;              
+                GH_A = 0;
+                break;
+            case 1:                 
+                GL_C = -dutyCycle;
+                GL_B = -dutyCycle;
+                GL_A = -dutyCycle;
+                GH_C = 0;
+                GH_B = 0;              
+                GH_A = 0;
+                break;
+            case 2:
+                GL_C = -dutyCycle;
+                GL_B = -dutyCycle;
+                GL_A = -dutyCycle;
+                GH_C = 0;
+                GH_B = 0;              
+                GH_A = 0;
+                break;
+            case 3:
+                GL_C = -dutyCycle;
+                GL_B = -dutyCycle;
+                GL_A = -dutyCycle;
+                GH_C = 0;
+                GH_B = 0;              
+                GH_A = 0;
+                break;
+            case 4:
+                GL_C = -dutyCycle;
+                GL_B = -dutyCycle;
+                GL_A = -dutyCycle;
+                GH_C = 0;
+                GH_B = 0;              
+                GH_A = 0;
+                break;
+            case 5:
+                GL_C = -dutyCycle;
+                GL_B = -dutyCycle;
+                GL_A = -dutyCycle;
+                GH_C = 0;
+                GH_B = 0;              
+                GH_A = 0;
+                break;
+          /*
+          case 0:  
                 GL_C = 0;
                 GL_B = 0;
                 GL_A = 1;
@@ -176,9 +228,11 @@
                 GH_C = -dutyCycle;
                 GH_B = 0;     
                 GH_A = 0;
-                break;    
-                }                
-        }else {
+                break;   
+            */ 
+        }             
+    }
+    else {
         coast();
     }
 }