Library for driving a 3-phase brushless DC motor.

Dependents:   BLDC_mainProgram STMF302R8_MotorDrive HelloWorld_IHM07M1

BLDCmotorDriver

Introduction

This library is still being developed.

This is a Wiki page about BLDCmotorDriver library developed by TVZ Mechatronics Team. The library is part of our project of creating an e-bike which is supposed to drive/run a 3-phase brushless DC motor with Hall sensors.

We are testing our library on NXP mbed LPC1768 platform and driver DRV8301 from Texas Instruments to run our testing BLDC motor. Next step of our project is creating a standalone library for DRV8301.

Hall sensors

In our BLDC motors there are three Hall sensors located 120 degrees from one another, they are identified as InterruptIn/DigitalIn on mbed and they send zeros and ones on TeraTerm with we locate in which sector

Na našim testnim BLDC motorima postoje 3 halova senzora, koji su udaljeni za 120 stupnjeva jedan od drugoga te inicijaliziranjem svakog pojedinog kao InterruptIn/DigitalIn na pinovima mbeda preko terminala na računalu (TeraTerm) dobivamo prikaz binarnih nula i jedinica koji se slažu sa trobitnim gray-evim kodom, s time da smo kombinacije 000 i 111 definirali kao fault vrijednosti. Pošto se slučajevi kada su sva tri hallova senzora upaljena ili ugašena ne mogu dogoditi tj. ne bi se smijeli dogoditi, ukoliko je došlo do takve situacije dogodio se kvar na hallovim senzorima te bi motor trebao prijeći u mod rada bez hallovih senzora.

With that we got six combinations, which we use to determine six sectors:

  • 001 - Sector 1
  • 011 - Sector 2
  • 010 - Sector 3
  • 110 - Sector 4
  • 100 - Sector 5
  • 101 - Sector 6

Nakon toga odredili smo za sve tri faze stanja mosfeta..

/media/uploads/dfraj/3-bit_gray_code_ring.jpg

Components

/media/uploads/dfraj/kotac.jpg

Revision:
3:a4b4a8e3f2a0
Parent:
1:786897114846
--- a/BLDCmotorDriver.cpp	Sun May 24 06:46:25 2015 +0000
+++ b/BLDCmotorDriver.cpp	Mon Jun 01 13:47:23 2015 +0000
@@ -1,14 +1,18 @@
 #include "BLDCmotorDriver.h"
 
 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) : 
+                    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) {
+                    H1(pH1), H2(pH2), H3(pH3), Fault(LED1){
+                     
     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.mode(PullNone);
+    //H2.mode(PullNone);
+    //H3.mode(PullNone);
     H1.rise(this, &BLDCmotorDriver::commutation);
     H2.rise(this, &BLDCmotorDriver::commutation);
     H3.rise(this, &BLDCmotorDriver::commutation);
@@ -29,59 +33,74 @@
     switchingPeriod = 1.0 / switchingFrequency;
     rl.setLimits(rampUpSlope, rampDownSlope, 0, sampleTime);
 }
-int BLDCmotorDriver::getSector() {           // hall 120°
-  
-    if(H1.read()== 1 && H2.read()== 0 && H3.read()== 0)
-            currentSector = 1; 
-    else if(H1.read()== 1 && H2.read()== 1 && H3.read()== 0)
-            currentSector = 2;         
-    else if(H1.read()== 0 && H2.read()== 1 && H3.read()== 0)
-            currentSector = 3;  
-    else if(H1.read()== 0 && H2.read()== 1 && H3.read()== 1)
-            currentSector = 4;  
-    else if(H1.read()== 0 && H2.read()== 0 && H3.read()== 1)
-            currentSector = 5;
-    else if(H1.read()== 1 && H2.read()== 0 && H3.read()== 1)
-            currentSector = 6;             
-    else {
-       currentSector = 0;
-       fault = 1; 
-    }
-   
-    return currentSector;              
-}
+int BLDCmotorDriver::getSector(){           // hall 120°
+    
+    h1 = H1.read();  
+    h2 = H2.read();
+    h3 = H3.read();
+    
+     
+    if(h1 == 0 && h2 == 0 && h3 == 1){          
+            _currentSector = 0;
+             }    
+    else if(h1 == 0 && h2 == 1 && h3 == 1){
+            _currentSector = 1;        
+            }        
+    else if(h1 == 0 && h2 == 1 && h3 == 0){  
+            _currentSector = 2;       
+            }  
+    else if(h1 == 1 && h2 == 1 && h3 == 0){
+            _currentSector = 3;
+            } 
+    else if(h1 == 1 && h2 == 0 && h3 == 0){
+            _currentSector = 4;
+            }
+    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;                
+}  
 
-void BLDCmotorDriver::commutation() {
-    getSector();
+void BLDCmotorDriver::commutation()  {
+ dutyCycle = rl.out(tempDutyCycle);
+ currentSector = getSector();
     if (dutyCycle > 0) {
-        if (currentSector > 6) currentSector = 1; // when will this condition happen?
-        // currentSector++;        
-        switch(currentSector) {           
-            case 1:               //100      
+        currentSector++;
+        if(currentSector > 5)currentSector = 0;
+        switch(currentSector) {  
+        
+          case 0:               //001     
                 GH_A = dutyCycle;
                 GL_A = 0;
                 GH_B = 0;
                 GL_B = 0;
                 GH_C = 0;
+                GL_C = 1;
+                break;
+           case 1:            
+                GH_A = 0;
+                GL_A = 0;
+                GH_B = dutyCycle;
+                GL_B = 0;
+                GH_C = 0;
                 GL_C = 1; 
                 break;
-           case 2:                //110
-                GH_A = dutyCycle;
-                GL_A = 0;
-                GH_B = 0;
-                GL_B = 1;
+           case 2:             
+                GH_A = 0;
+                GL_A = 1;
+                GH_B = dutyCycle;
+                GL_B = 0;
                 GH_C = 0;
                 GL_C = 0;
                 break;
-           case 3:                //010
-                GH_A = 0;
-                GL_A = 0;
-                GH_B = 0;
-                GL_B = 1;
-                GH_C = dutyCycle;
-                GL_C = 0; 
-                break;
-            case 4:               //011
+            case 3:             
                 GH_A = 0;
                 GL_A = 1;
                 GH_B = 0;
@@ -89,97 +108,94 @@
                 GH_C = dutyCycle;
                 GL_C = 0;
                 break;
-            case 5:               //001
-                GH_A = 0;
-                GL_A = 1;
-                GH_B = dutyCycle;
-                GL_B = 0;
-                GH_C = 0;
-                GL_C = 0;
-                break;    
-            case 6:               //101
-                GH_A = 0;
-                GL_A = 0;
-                GH_B = dutyCycle;
-                GL_B = 0;
-                GH_C = 0;
-                GL_C = 1;
-                break;
-        }
-    } else if (dutyCycle < 0) { // 
-        // if (currentSector < 1) currentSector = 6; // not necessary
-        // currentSector--;
-        switch(currentSector) {
-            case 1:               //100      
-                GH_A = dutyCycle;
-                GL_A = 0;
-                GH_B = 0;
-                GL_B = 0;
-                GH_C = 0;
-                GL_C = 1; 
-                break;
-           case 2:                //110
-                GH_A = dutyCycle;
-                GL_A = 0;
-                GH_B = 0;
-                GL_B = 1;
-                GH_C = 0;
-                GL_C = 0;
-                break;
-           case 3:                //010
+            case 4:              
                 GH_A = 0;
                 GL_A = 0;
                 GH_B = 0;
                 GL_B = 1;
                 GH_C = dutyCycle;
                 GL_C = 0; 
+                break;    
+            case 5:              
+                GH_A = dutyCycle;
+                GL_A = 0;
+                GH_B = 0;
+                GL_B = 1;
+                GH_C = 0;
+                GL_C = 0; 
                 break;
-            case 4:               //011
+        }
+    } else if (dutyCycle < 0) { 
+        currentSector--;
+        if(currentSector < 0)currentSector = 5;
+        switch(currentSector) {
+          case 0:              
+                GH_A = -dutyCycle;
+                GL_A = 0;
+                GH_B = 0;
+                GL_B = 1;
+                GH_C = 0;
+                GL_C = 0; 
+                break;
+          case 1:                 
+                GH_A = -dutyCycle;
+                GL_A = 0;
+                GH_B = 0;
+                GL_B = 0;
+                GH_C = 0;
+                GL_C = 1;
+                break;
+           case 2:            
+                GH_A = 0;
+                GL_A = 0;
+                GH_B = -dutyCycle;
+                GL_B = 0;
+                GH_C = 0;
+                GL_C = 1; 
+                break;
+           case 3:             
+                GH_A = 0;
+                GL_A = 1;
+                GH_B = -dutyCycle;
+                GL_B = 0;
+                GH_C = 0;
+                GL_C = 0;
+                break;
+            case 4:             
                 GH_A = 0;
                 GL_A = 1;
                 GH_B = 0;
                 GL_B = 0;
-                GH_C = dutyCycle;
+                GH_C = -dutyCycle;
                 GL_C = 0;
                 break;
-            case 5:               //001
-                GH_A = 0;
-                GL_A = 1;
-                GH_B = dutyCycle;
-                GL_B = 0;
-                GH_C = 0;
-                GL_C = 0;
-                break;    
-            case 6:               //101
+            case 5:              
                 GH_A = 0;
                 GL_A = 0;
-                GH_B = dutyCycle;
-                GL_B = 0;
-                GH_C = 0;
-                GL_C = 1;
-                break;
-            }                
-        } else {
-            coast();
+                GH_B = 0;
+                GL_B = 1;
+                GH_C = -dutyCycle;
+                GL_C = 0; 
+                break;    
+                }                
+        }else {
+        coast();
     }
 }
-
 void BLDCmotorDriver::setDutyCycle(float dc) {
     if (dc >= -1 && dc <= 1) {
-        ticker.attach(this, &BLDCmotorDriver::adjustDutyCycle, sampleTime);
+        ticker.attach(this, &BLDCmotorDriver::commutation, sampleTime);
         tempDutyCycle = dc;
     } else {
         coast();
     }
 }
-
-void BLDCmotorDriver::adjustDutyCycle() {
+/*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;