STM32F302R8 with Konrad's inverter boards for senior design.

Dependencies:   mbed

Fork of Blue_Board_Ticker by Brad VanderWilp

Revision:
19:085eb0579185
Parent:
18:d7033a38f20b
Child:
20:029a58eb24ab
--- a/main.cpp	Wed Apr 13 19:27:01 2016 +0000
+++ b/main.cpp	Fri Apr 15 21:35:50 2016 +0000
@@ -28,9 +28,16 @@
 int stall = 0;
 int reverse = 0;
 
+int spinTickerCounter = 0;
+int ticksPerHall = 0;
+int prevHallState = 0;
+
+int pole_pairs = 2;
+
 void rpmCalc()
 {
-    currentRPM = revCount * 60; //account for elec vs mech rpm
+    currentRPM = revCount * 60; // 60 seconds in 1 minute
+    currentRPM /= pole_pairs; // account for elec vs mech rpm
     revCount = 0;
     rpmPrintFlag = 1;
 }
@@ -106,6 +113,18 @@
     int h2 = hallB.read();
     int h3 = hallC.read();
     //check where we start
+    int currentHallState = (h1 << 2) + (h2 << 1) + h3;
+    if (currentHallState != prevHallState)
+    {
+        ticksPerHall = spinTickerCounter;
+        spinTickerCounter = 0; // reset the number of ticks per hall sensor change
+        if (prevHallState == 1) // arbitrarily choose state 1
+        {
+            revCount += 1;
+        }
+    }
+    prevHallState = currentHallState;
+    spinTickerCounter += 1; // count the number of times spinTicker runs per hall sensor
     
     if (reverse == 0)
     {
@@ -126,27 +145,20 @@
     else if (reverse == 1) // to go in reverse, shift the mappings by 180 degrees
     {
         if(h1 == 0 && h2 == 1 && h3 == 1) { //state1
-//            Cfall();
             Arise();
         } else if(h1 == 0 && h2 == 0 && h3 == 1) { //state2
-//            Brise();
             Cfall();
         } else if(h1 == 1 && h2 == 0 && h3 == 1) { //state3
-//            Afall();
             Brise();
         } else if(h1 == 1 && h2 == 0 && h3 == 0) { //state4
-//            Crise();
             Afall();
         } else if(h1 == 1 && h2 == 1 && h3 == 0) { //state5
-//            Bfall();
             Crise();
         } else { //(h1 == 0 && h2 == 1 && h3 == 0) state6
-//            Arise();
             Bfall();
         }
     }
     toggleRedLed();
-    revCount++;
 }
 
 void activate()
@@ -184,6 +196,12 @@
     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, 50);
     float ADCSum = 0;
@@ -198,7 +216,7 @@
             ADCCount = 0;
         }
         if(rpmPrintFlag == 1) {
-            printf("%d rpm; %f duty; reverse: %d\r\n", currentRPM, pwmDuty, reverse);
+            printf("%d rpm; %f duty; reverse: %d; ticksPerHall = %d\r\n", currentRPM, pwmDuty, reverse, ticksPerHall);
             rpmPrintFlag = 0;
         }
         wait(0.05);