CQ_KIT_Ver1_5

Dependencies:   mbed RateLimiter BLDCmotorDriverCQ_KIT_Ver1_5

Revision:
2:4ae769d0b112
Child:
3:4c71d3475814
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/X_NUCLEO_IHM07M1/SPN7Driver.cpp	Wed Oct 12 13:15:03 2016 +0000
@@ -0,0 +1,182 @@
+/* mbed Microcontroller Library
+* Copyright (c) 2006-2016 ARM Limited
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+/**
+  ******************************************************************************
+  * @file    SPN7Driver.cpp 
+  * @author  STMicroelectronics
+  * @brief   Implementation of SPN7Driver class
+  ******************************************************************************
+  * @copy
+  *
+  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
+  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
+  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
+  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
+  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+  *
+  * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
+  */ 
+
+#include "mbed.h"
+#include "SPN7Driver.h"
+
+// FIXME: add doxygen
+
+SPN7Driver::SPN7Driver(PinName pIN1, PinName pIN2, PinName pIN3,
+                       PinName pEN1, PinName pEN2, PinName pEN3,
+                       PinName pH1,  PinName pH2,  PinName pH3,
+                       PinName pFault) :
+                        BLDCmotorDriver(pIN1, pIN2, pIN3,
+                                        pEN1, pEN2, pEN3,
+                                        pH1,  pH2,  pH3,
+                                        pFault)
+{
+    // The BLDCmotorDriver class was implemented for half-bridge drivers
+    // so the pin names may be misleading when referring to the L6230 chip.
+    // Get a reference of each input pin and call them INx (logic input)
+    // to be consistent with the terminology used in the L6230 documentation.
+    PwmOut& IN1 = GH_A;
+    PwmOut& IN2 = GH_B;
+    PwmOut& IN3 = GH_C;
+    
+    // Set the switching period of the INx logic input pins (PWM driven)
+    IN1.period(switchingPeriod);
+    IN2.period(switchingPeriod);
+    IN3.period(switchingPeriod);
+    
+    // Set the step commutation function (triggered by the Hall sensors)
+    H1.rise(this, &SPN7Driver::commutation);
+    H2.rise(this, &SPN7Driver::commutation);
+    H3.rise(this, &SPN7Driver::commutation);
+    H1.fall(this, &SPN7Driver::commutation);
+    H2.fall(this, &SPN7Driver::commutation);
+    H3.fall(this, &SPN7Driver::commutation);    
+}
+
+// 6-step phase commutation
+//
+// Positive dutycycle:
+//      1--X--0--0--X--1
+//      X--1--1--X--0--0
+//      0--0--X--1--1--X
+//        
+// Negative dutycycle:
+//      1--1--X--0--0--X
+//      0--X--1--1--X--0
+//      X--0--0--X--1--1
+void SPN7Driver::commutation()
+{
+    // The BLDCmotorDriver class was implemented for half-bridge drivers
+    // so the pin names may be misleading when referring to the L6230 chip.
+    
+    // Get a reference of each input pin and call them INx (logic input)
+    // to be consistent with the terminology used in the L6230 documentation.
+    PwmOut& IN1 = GH_A;
+    PwmOut& IN2 = GH_B;
+    PwmOut& IN3 = GH_C;
+    // Get a reference of each enable pin and call them ENx (enable channel)
+    // to be consistent with the terminology used in the L6230 documentation.
+    DigitalOut& EN1 = GL_A;
+    DigitalOut& EN2 = GL_B;
+    DigitalOut& EN3 = GL_C;
+
+    dutyCycle = rl.out(tempDutyCycle);
+    currentSector = getSector();
+
+    if (dutyCycle > 0 ) {
+         // Positive dutycycle
+         currentSector++;
+         
+         if(currentSector > 5) currentSector = 0;
+         
+         switch(currentSector) {  
+             case 0:                   
+                 EN1 = 1; IN1 = dutyCycle;
+                 EN2 = 0; IN2 = 0;
+                 EN3 = 1; IN3 = 0;                               
+                 break;
+             case 1:            
+                 EN1 = 0; IN1= 0;
+                 EN2 = 1; IN2 = dutyCycle;
+                 EN3 = 1; IN3 = 0;                
+                 break;
+             case 2:             
+                 EN1 = 1; IN1 = 0;
+                 EN2 = 1; IN2 = dutyCycle;
+                 EN3 = 0; IN3 = 0;                
+                 break;
+            case 3:             
+                 EN1 = 1; IN1 = 0;
+                 EN2 = 0; IN2 = 0;
+                 EN3 = 1; IN3 = dutyCycle;              
+                 break;
+            case 4:              
+                 EN1 = 0; IN1 = 0;
+                 EN2 = 1; IN2 = 0;
+                 EN3 = 1; IN3 = dutyCycle;               
+                 break;
+            case 5:           
+                 EN1 = 1; IN1 = dutyCycle;
+                 EN2 = 1; IN2 = 0;
+                 EN3 = 0; IN3 = 0;
+                 break;
+        }
+    } else if (dutyCycle < 0) { 
+        // Negative dutycycle
+        currentSector--;
+        
+        if(currentSector < 0) currentSector = 5;
+        
+        switch(currentSector) {  
+           case 0:
+                EN1 = 1; IN1 = 0;
+                EN2 = 0; IN2=0;
+                EN3 = 1; IN3 = -dutyCycle;
+                break;
+            case 1:
+                EN1 = 0; IN1=0;
+                EN2 = 1; IN2 = 0;
+                EN3 = 1; IN3 = -dutyCycle;
+                break;
+           case 2:
+                EN1 = 1; IN1 = -dutyCycle;
+                EN2 = 1; IN2 = 0;
+                EN3 = 0; IN3= 0;                
+                break;
+           case 3:
+                EN1 = 1; IN1 = -dutyCycle;
+                EN2 = 0; IN2 = 0;
+                EN3 = 1; IN3 = 0;
+                break;
+           case 4:
+                EN1 = 0; IN1= 0;
+                EN2 = 1; IN2 = -dutyCycle;
+                EN3 = 1; IN3 = 0;
+                break;
+           case 5:
+                EN1 = 1; IN1 = 0;
+                EN2 = 1; IN2 = -dutyCycle;
+                EN3 = 0; IN3=0;
+                break;
+       }
+         
+    } else {
+        coast();
+    }
+    
+}