CQ_KIT_Ver1_5

Dependencies:   mbed RateLimiter BLDCmotorDriverCQ_KIT_Ver1_5

Revision:
11:0120619cdfb7
Parent:
8:9a6444df4256
Child:
13:038b62c7ac17
--- a/X_NUCLEO_IHM07M1/SPN7Driver.cpp	Wed Oct 19 09:30:46 2016 +0000
+++ b/X_NUCLEO_IHM07M1/SPN7Driver.cpp	Thu Oct 20 14:26:13 2016 +0000
@@ -93,11 +93,11 @@
 /**************************************************************************/
 /**
     @brief  Set duty cycle for motor control
-     * @param dc       duty cycle value
+     * @param dc       duty cycle value (>0 clockwise; <0 anti-clockwise)
 */
 /**************************************************************************/
 void SPN7Driver::setDutyCycle(float dc) {
-    if (dc >0 && dc <= 1) {
+    if (dc >= -1 && dc <= 1) {
         ticker.attach(this, &SPN7Driver::commutation, sampleTime);
         tempDutyCycle = dc;
     } else {
@@ -105,7 +105,7 @@
     }
 }
 
-// 6-step phase commutation
+// six-step phase commutation
 void SPN7Driver::commutation()
 {    
     // The BLDCmotorDriver class was implemented for half-bridge drivers
@@ -117,20 +117,17 @@
     // to be consistent with the terminology used in the L6230 documentation.
     DigitalOut* EN[3] = {&GL_A, &GL_B, &GL_C};
 
-//      1--X--0--0--X--1
-//      X--1--1--X--0--0
-//      0--0--X--1--1--X    
     st_bldc_status_t tab[6][3] = {
+                            {ST_BLDC_OFF, ST_BLDC_LOW, ST_BLDC_HIGH},
+                            {ST_BLDC_HIGH, ST_BLDC_LOW, ST_BLDC_OFF},
                             {ST_BLDC_HIGH, ST_BLDC_OFF, ST_BLDC_LOW},
                             {ST_BLDC_OFF, ST_BLDC_HIGH, ST_BLDC_LOW},
                             {ST_BLDC_LOW, ST_BLDC_HIGH, ST_BLDC_OFF},
                             {ST_BLDC_LOW, ST_BLDC_OFF, ST_BLDC_HIGH},
-                            {ST_BLDC_OFF, ST_BLDC_LOW, ST_BLDC_HIGH},
-                            {ST_BLDC_HIGH, ST_BLDC_LOW, ST_BLDC_OFF},
                             };
 
     dutyCycle = rl.out(tempDutyCycle);
-    currentSector = getSector();
+    int sector = getSector();
 
     if (dutyCycle == 0) {
         // Stop the motor
@@ -140,13 +137,15 @@
     
     // Move to next sector (i.e. commute phase)
     if (dutyCycle > 0 ) {
-         // Move forward
-         currentSector++;
-         if(currentSector > 5) currentSector = 0;
+         // Clockwise spinning
+         
+         // The rows of the phase status table are offset by one when spinning
+         // clockwise so we need to increment the current sector by two steps
+         sector = (sector + 2) % 6;
     } else {
-        // Move backward
-        currentSector--;
-        if(currentSector < 0) currentSector = 5;
+        // Anti-clockwise spinning
+        sector--;
+        if(sector < 0) sector = 5;
     }
     
     // Get the absolute value of the duty cycle for the PWM
@@ -154,7 +153,7 @@
     
     // Update the logic inputs and the enable pins
     for (int i = 0; i < 3; i++) {
-         *EN[i] = (tab[currentSector][i] == ST_BLDC_OFF) ? 0 : 1;
-         *IN[i] = (tab[currentSector][i] == ST_BLDC_HIGH) ? d : 0;
+         *EN[i] = (tab[sector][i] == ST_BLDC_OFF) ? 0 : 1;
+         *IN[i] = (tab[sector][i] == ST_BLDC_HIGH) ? d : 0;
     }    
 }