STM32F302R8 with Konrad's inverter boards for senior design.

Dependencies:   mbed

Fork of Blue_Board_Ticker by Brad VanderWilp

Revision:
21:2bf65c29a3c6
Parent:
20:029a58eb24ab
Child:
22:72905102b50d
--- a/main.cpp	Fri Apr 15 21:54:18 2016 +0000
+++ b/main.cpp	Fri Apr 15 22:09:41 2016 +0000
@@ -34,17 +34,45 @@
 int ticksPerHall = 0;
 int prevHallState = 0;
 
+float reverseThreshold = 0.4f; // tune for minimum duty cycle to allow reverse
+
 int pole_pairs = 2; // tune for proper mech rpm calculation
 
+void init()
+{
+    phaseA.period_us(50);
+    phaseB.period_us(50);
+    phaseC.period_us(50);
+
+    phaseA.write(0);
+    phaseB.write(0);
+    phaseC.write(0);
+
+    phaseAEN = 0;
+    phaseBEN = 0;
+    phaseCEN = 0;
+    
+    // init the prevHallState
+    int h1 = hallA.read();
+    int h2 = hallB.read();
+    int h3 = hallC.read();
+    prevHallState = (h1 << 2) + (h2 << 1) + h3;   
+}
+
 void rpmCalc()
 {
     currentRPM = revCount * 60; // 60 seconds in 1 minute
     currentRPM /= pole_pairs; // account for elec vs mech rpm
+    if (reverse == 1)
+    {
+        currentRPM = -currentRPM; // indicate a reverse direction   
+    }
     revCount = 0;
     rpmPrintFlag = 1;
 }
+
 //original names: CBA CBA, new names: BAC BAC
-void Brise()    //state1, A0 B- C+
+void Output_ANull_BLow_CHigh()    //state1, A0 B- C+
 {
     phaseA.write(0);
     phaseB.write(0);
@@ -54,7 +82,7 @@
     phaseCEN = 1;
 }
 
-void Afall()    //state2, A+ B- C0
+void Output_AHigh_BLow_CNull()    //state2, A+ B- C0
 {
     phaseA.write(pwmDuty);
     phaseB.write(0);
@@ -64,7 +92,7 @@
     phaseCEN = 0;
 }
 
-void Crise()    //state3, A+ B0 C-
+void Output_AHigh_BNull_CLow()    //state3, A+ B0 C-
 {
     phaseA.write(pwmDuty);
     phaseB.write(0);
@@ -74,7 +102,7 @@
     phaseCEN = 1;
 }
 
-void Bfall()    //state4, A0 B+ C-
+void Output_ANull_BHigh_CLow()    //state4, A0 B+ C-
 {
     phaseA.write(0);
     phaseB.write(pwmDuty);
@@ -84,7 +112,7 @@
     phaseCEN = 1;
 }
 
-void Arise()    //state5, A- B+ C0
+void Output_ALow_BHigh_CNull()    //state5, A- B+ C0
 {
     phaseA.write(0);
     phaseB.write(pwmDuty);
@@ -94,7 +122,7 @@
     phaseBEN = 1;
 }
 
-void Cfall()    //state6, A- B0 C+
+void Output_ALow_BNull_CHigh()    //state6, A- B0 C+
 {
     phaseAEN = 1;
     phaseBEN = 0;
@@ -109,7 +137,7 @@
     redLed = !redLed;
 }
 
-void jumpStart()
+void SixStepNext()
 {
     int h1 = hallA.read();
     int h2 = hallB.read();
@@ -131,44 +159,47 @@
     if (reverse == 0)
     {
         if(h1 == 0 && h2 == 1 && h3 == 1) { //state1
-            Afall();
+            Output_AHigh_BLow_CNull();
         } else if(h1 == 0 && h2 == 0 && h3 == 1) { //state2
-            Crise();
+            Output_AHigh_BNull_CLow();
         } else if(h1 == 1 && h2 == 0 && h3 == 1) { //state3
-            Bfall();
+            Output_ANull_BHigh_CLow();
         } else if(h1 == 1 && h2 == 0 && h3 == 0) { //state4
-            Arise();
+            Output_ALow_BHigh_CNull();
         } else if(h1 == 1 && h2 == 1 && h3 == 0) { //state5
-            Cfall();
-        } else { //(h1 == 0 && h2 == 1 && h3 == 0)state6
-            Brise();
+            Output_ALow_BNull_CHigh();
+        } else if (h1 == 0 && h2 == 1 && h3 == 0) { //state6
+            Output_ANull_BLow_CHigh();
         }
     }
     else if (reverse == 1) // to go in reverse, shift the mappings by 180 degrees
     {
         if(h1 == 0 && h2 == 1 && h3 == 1) { //state1
-            Arise();
+            Output_ALow_BHigh_CNull();
         } else if(h1 == 0 && h2 == 0 && h3 == 1) { //state2
-            Cfall();
+            Output_ALow_BNull_CHigh();
         } else if(h1 == 1 && h2 == 0 && h3 == 1) { //state3
-            Brise();
+            Output_ANull_BLow_CHigh();
         } else if(h1 == 1 && h2 == 0 && h3 == 0) { //state4
-            Afall();
+            Output_AHigh_BLow_CNull();
         } else if(h1 == 1 && h2 == 1 && h3 == 0) { //state5
-            Crise();
-        } else { //(h1 == 0 && h2 == 1 && h3 == 0) state6
-            Bfall();
+            Output_AHigh_BNull_CLow();
+        } else if (h1 == 0 && h2 == 1 && h3 == 0) { //state6
+            Output_ANull_BHigh_CLow();
         }
     }
     toggleRedLed();
 }
 
-void activate()
+void user_button_callback()
 {
-    if(stall == 0) {
+    if(stall == 0) 
+    {
         stall = 1;
-    } else {
-        if (pwmDuty < 0.4f)
+    } 
+    else 
+    {
+        if (pwmDuty < reverseThreshold)
         {
             reverse = reverse ^ 1;
         }
@@ -179,33 +210,15 @@
 {
     //wait until button push to start
     rpmInterrupt.attach(&rpmCalc, 1);
-    button.rise(&activate);
+    button.rise(&user_button_callback);
     while(stall == 0) {
         led = !led;
         wait(1);
     }
-
-    pwmDuty = pot.read() * pwmMax;
-
-    phaseA.period_us(50);
-    phaseB.period_us(50);
-    phaseC.period_us(50);
-
-    phaseA.write(0);
-    phaseB.write(0);
-    phaseC.write(0);
+    
+    init();
 
-    phaseAEN = 0;
-    phaseBEN = 0;
-    phaseCEN = 0;
-    
-    // init the prevHallState
-    int h1 = hallA.read();
-    int h2 = hallB.read();
-    int h3 = hallC.read();
-    prevHallState = (h1 << 2) + (h2 << 1) + h3;
-
-    spinTicker.attach_us(&jumpStart, spinTickerPeriod_us);
+    spinTicker.attach_us(&SixStepNext, spinTickerPeriod_us);
     float ADCSum = 0;
     int ADCCount = 0;
     while(1) {