STM32F302R8 with Konrad's inverter boards for senior design.

Dependencies:   mbed

Fork of Blue_Board_Ticker by Brad VanderWilp

Revision:
22:72905102b50d
Parent:
21:2bf65c29a3c6
Child:
23:cb38086e8695
--- a/main.cpp	Fri Apr 15 22:09:41 2016 +0000
+++ b/main.cpp	Fri Apr 15 22:23:37 2016 +0000
@@ -24,9 +24,12 @@
 int currentRPM;
 
 int ADCCountMax = 1000; // tune for responsiveness vs. stability, higher value -> less responsive -> more stable
+float ADCSum = 0;
+int ADCCount = 0;
+
 float pwmMax = 0.9; // tune for max pwmDuty. higher value -> more power available
 float pwmDuty;
-int stall = 0;
+int start = 0;
 int reverse = 0;
 
 int spinTickerCounter = 0;
@@ -38,6 +41,11 @@
 
 int pole_pairs = 2; // tune for proper mech rpm calculation
 
+int hallToStateNumber(int h1, int h2, int h3)
+{
+    return (h1 << 2) + (h2 << 1) + h3;   
+}
+
 void init()
 {
     phaseA.period_us(50);
@@ -56,7 +64,7 @@
     int h1 = hallA.read();
     int h2 = hallB.read();
     int h3 = hallC.read();
-    prevHallState = (h1 << 2) + (h2 << 1) + h3;   
+    prevHallState = hallToStateNumber(h1, h2, h3);
 }
 
 void rpmCalc()
@@ -139,23 +147,7 @@
 
 void SixStepNext()
 {
-    int h1 = hallA.read();
-    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
-    
+    // compute next outputs
     if (reverse == 0)
     {
         if(h1 == 0 && h2 == 1 && h3 == 1) { //state1
@@ -187,15 +179,38 @@
         } else if (h1 == 0 && h2 == 1 && h3 == 0) { //state6
             Output_ANull_BHigh_CLow();
         }
+    }   
+}
+
+void SpinTicker_callback()
+{
+    int h1 = hallA.read();
+    int h2 = hallB.read();
+    int h3 = hallC.read();
+    //check where we start
+    int currentHallState = hallToStateNumber(h1, h2, 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
+    
+    SixStepNext();
+    
     toggleRedLed();
 }
 
 void user_button_callback()
 {
-    if(stall == 0) 
+    if(start == 0) 
     {
-        stall = 1;
+        start = 1;
     } 
     else 
     {
@@ -206,33 +221,39 @@
     }
 }
 
+float buffered_ADC_read()
+{
+    if(ADCCount == ADCCountMax) {
+        ADCSum = ADCSum - (ADCSum / ADCCountMax);
+        ADCSum += pot.read();
+        return ADCSum / ADCCountMax;
+    }
+    else
+    {
+        ADCSum += pot.read();
+        ADCCount++;
+        return 0.0; 
+    }
+}
+
 int main()
 {
     //wait until button push to start
     rpmInterrupt.attach(&rpmCalc, 1);
     button.rise(&user_button_callback);
-    while(stall == 0) {
+    while(start == 0) {
         led = !led;
         wait(1);
     }
     
     init();
 
-    spinTicker.attach_us(&SixStepNext, spinTickerPeriod_us);
-    float ADCSum = 0;
-    int ADCCount = 0;
+    spinTicker.attach_us(&SpinTicker_callback, spinTickerPeriod_us);
     while(1) {
         led = !led;
-        if(ADCCount == ADCCountMax) {
-            ADCSum = ADCSum - (ADCSum / ADCCountMax);
-            ADCSum += pot.read();
-            pwmDuty = (ADCSum/ADCCountMax) * pwmMax;
-        }
-        else
-        {
-            ADCSum += pot.read();
-            ADCCount++;    
-        }
+        
+        pwmDuty = buffered_ADC_read() * pwmMax;
+        
         if(rpmPrintFlag == 1) {
             printf("%d rpm; %f duty; reverse: %d; ticksPerHall = %d\r\n", currentRPM, pwmDuty, reverse, ticksPerHall);
             rpmPrintFlag = 0;