Three phase bridge control

Dependents:   BLDC_control BLDC_RPM_meter

Files at this revision

API Documentation at this revision

Comitter:
acracan
Date:
Tue Jul 08 17:37:12 2014 +0000
Parent:
0:a129854eb4b6
Commit message:
Update.

Changed in this revision

ThreePhaseBridge.cpp Show annotated file Show diff for this revision Revisions of this file
ThreePhaseBridge.h Show annotated file Show diff for this revision Revisions of this file
diff -r a129854eb4b6 -r ba7889accd61 ThreePhaseBridge.cpp
--- a/ThreePhaseBridge.cpp	Tue Mar 11 15:42:38 2014 +0000
+++ b/ThreePhaseBridge.cpp	Tue Jul 08 17:37:12 2014 +0000
@@ -16,36 +16,53 @@
     lowSide[0] = &sw2;
     lowSide[1] = &sw4;
     lowSide[2] = &sw6;
-    switchesState = 63;
+    switchesState = activeState == ActiveLow ? 63 : 0;
     setSwitchesState(0);
-    state = 0; stateIndex = 5;
+    m_state = 0; stateIndex = 5;
+    period(10e-6);
+    pulsewidth(15e-6);
+}
+
+void ThreePhaseBridge::period(double seconds)
+{
+    pwmPeriod = seconds;
+    pwmPulseWidthOff = activeState == ActiveLow ? pwmPeriod * 1.1 : 0.0;
+    sw1.period(seconds);
+    sw3.period(seconds);
+    sw5.period(seconds);
 }
 
-void ThreePhaseBridge::setPwmPeriod_us(int32_t period_us)
+void ThreePhaseBridge::pulsewidth(double seconds)
 {
-    pwmPeriod_us = period_us;
-    sw1.period_us(period_us);
-    sw3.period_us(period_us);
-    sw5.period_us(period_us);
-}
-
-void ThreePhaseBridge::setPwmPulseWidth_us(int32_t pulseWidth_us)
-{
-    pwmPulseWidth_us = pulseWidth_us;
-    sw1.pulsewidth_us(pulseWidth_us);
-    sw3.pulsewidth_us(pulseWidth_us);
-    sw5.pulsewidth_us(pulseWidth_us);
+    if (activeState == ActiveHigh)
+        pwmPulseWidth = seconds;
+    else
+        pwmPulseWidth = pwmPeriod - seconds;
+    switch (m_state) {
+        case 1:
+        case 5:
+            sw1.pulsewidth(pwmPulseWidth);
+        break;
+        case 2:
+        case 3:
+            sw3.pulsewidth(pwmPulseWidth);
+        break;
+        case 4:
+        case 6:
+            sw5.pulsewidth(pwmPulseWidth);
+        break;
+    };        
 }
 void ThreePhaseBridge::setHighSideSwitchState(int8_t sw, ThreePhaseBridge::SwitchState state)
 {
-    state == on ? highSide[sw]->pulsewidth_us(activeState == activeLow ? pwmPeriod_us - pwmPulseWidth_us : pwmPeriod_us)
-                : highSide[sw]->pulsewidth_us(activeState == activeLow ? 2 * pwmPeriod_us : 0);
+    state == On ? highSide[sw]->pulsewidth(pwmPulseWidth)
+                : highSide[sw]->pulsewidth(pwmPulseWidthOff);
 }
 
 void ThreePhaseBridge::setLowSideSwitchState(int8_t sw, ThreePhaseBridge::SwitchState state)
 {
-    state == on ? *lowSide[sw] = (activeState == activeLow ? 0 : 1)
-                : *lowSide[sw] = (activeState == activeLow ? 1 : 0);
+    state == On ? *lowSide[sw] = (activeState == ActiveLow ? 0 : 1)
+                : *lowSide[sw] = (activeState == ActiveLow ? 1 : 0);
 }
 
 void ThreePhaseBridge::setSwitchesState(uint8_t newState)
@@ -58,17 +75,17 @@
     uint8_t turnOnHigh = turnOn >> 3;
     for (uint8_t i = 0; i < 3; i++) {
         if (turnOffLow & (1 << i))
-            setLowSideSwitchState(i, off);
+            setLowSideSwitchState(i, Off);
         if (turnOffHigh & (1 << i))
-            setHighSideSwitchState(i, off);
+            setHighSideSwitchState(i, Off);
     }
     for (uint8_t i = 0; i < 3; i++) {
         if (turnOnLow & (1 << i))
         {
-            setLowSideSwitchState(i, on);
+            setLowSideSwitchState(i, On);
         }
         if (turnOnHigh & (1 << i))
-            setHighSideSwitchState(i, on);        
+            setHighSideSwitchState(i, On);        
     }
     switchesState = newState;
 }
@@ -98,7 +115,7 @@
             setSwitchesState(0);
         break;
     }
-    state = newState;
+    m_state = newState;
 }
 
 void ThreePhaseBridge::spin(ThreePhaseBridge::SpinDirection dir)
@@ -106,4 +123,17 @@
     stateIndex = (6 + stateIndex + dir) % 6;
     setState(stateOrder[stateIndex]);        
 }
+
+void ThreePhaseBridge::stop()
+{
+    setState(0);
+}
+
+void ThreePhaseBridge::overflow(void (*fptr)(void))
+{
+    if (fptr) {
+        NVIC_SetVector(TPM0_IRQn, (uint32_t)fptr);
+        NVIC_EnableIRQ(TPM0_IRQn);
+    }
+}
     
\ No newline at end of file
diff -r a129854eb4b6 -r ba7889accd61 ThreePhaseBridge.h
--- a/ThreePhaseBridge.h	Tue Mar 11 15:42:38 2014 +0000
+++ b/ThreePhaseBridge.h	Tue Jul 08 17:37:12 2014 +0000
@@ -2,37 +2,40 @@
 #define _THREEPHASEBRIDGE_H_
 
 #include "mbed.h"
-#include "FastPWM.h"
+#include "CenteredPwmOut.h"
 #include <stdint.h>
 
 class ThreePhaseBridge
 {
 public:
-    enum ActiveState {activeLow, activeHigh};
-    enum SwitchState {off, on};
+    enum ActiveState {ActiveLow, ActiveHigh};
+    enum SwitchState {Off, On};
     enum Switches {SW2 = 1, SW4 = 2, SW6 = 4, SW1 = 8, SW3 = 16, SW5 = 32};
     enum SpinDirection {CW = 1, CCW = -1};
     ThreePhaseBridge(PinName sw1PinName, PinName sw2PinName, PinName sw3PinName,
                      PinName sw4PinName, PinName sw5PinName, PinName sw6PinName,
-                     ActiveState activeState = activeLow);
-    void setPwmPeriod_us(int32_t period_us);
-    void setPwmPulseWidth_us(int32_t pulseWidth_us);
+                     ActiveState activeState = ActiveLow);
+    void period(double seconds);
+    void pulsewidth(double seconds);
+    inline int8_t state() { return m_state; };
     void setState(int8_t state);
     void spin(SpinDirection dir);
+    void stop();
     void setHighSideSwitchState(int8_t sw, SwitchState state);
     void setLowSideSwitchState(int8_t sw, SwitchState state);
+    void overflow(void (*fptr)(void));
     
 private:
     static int8_t stateOrder[];
     inline void setSwitchesState(uint8_t newState);
-    FastPWM sw1, sw3, sw5;
+    CenteredPwmOut sw1, sw3, sw5;
     DigitalOut sw2, sw4, sw6;
-    FastPWM *highSide[3];
+    CenteredPwmOut *highSide[3];
     DigitalOut *lowSide[3];
     ActiveState activeState;
-    int32_t pwmPeriod_us, pwmPulseWidth_us;
+    double pwmPeriod, pwmPulseWidth, pwmPulseWidthOff;
     uint8_t switchesState;
-    int8_t state, stateIndex;
+    int8_t m_state, stateIndex;
 };
 
 #endif /* _THREEPHASEBRIDGE_H_ */
\ No newline at end of file