Senior design sensored code to run freescale motor with IM07M1; Ticker algorithm control

Dependencies:   mbed

Fork of Blue_Board_Test_2 by Brad VanderWilp

Revision:
7:9d90184335aa
Parent:
6:e401c592d2c3
Child:
8:2f5dc29f9bb1
diff -r e401c592d2c3 -r 9d90184335aa main.cpp
--- a/main.cpp	Mon Apr 11 16:38:16 2016 +0000
+++ b/main.cpp	Mon Apr 11 19:53:55 2016 +0000
@@ -17,6 +17,10 @@
 InterruptIn hallB(PB_3);    //H2, Blue
 InterruptIn hallC(PB_10);   //H3, White
 
+//InterruptIn hallA(PB_3);   //New: H2; Original: H1, Green
+//InterruptIn hallB(PB_10);  //New: H3; Original: H2, Blue
+//InterruptIn hallC(PA_15);  //New: H1; Original: H3, White
+
 Ticker rpmInterrupt;
 int revCount = 0;
 int rpmPrintFlag = 0;
@@ -25,19 +29,15 @@
 float pwmMax = 0.9;
 float pwmDuty;
 int stall = 0;
+int reverse = 0;
 
 void rpmCalc()
 {
-    currentRPM = revCount * 60;
+    currentRPM = revCount * 60; //account for elec vs mech rpm
     revCount = 0;
     rpmPrintFlag = 1;
 }
 
-void activate()
-{
-    stall = 1;
-}
-
 void Crise()    //state1, A0 B- C+
 {   
     phaseAEN = 0;
@@ -45,7 +45,7 @@
     
     phaseC.write(pwmDuty);
     phaseCEN = 1;
-    redLed = ! redLed;
+//    redLed = ! redLed;
     revCount++;
 }
 
@@ -76,7 +76,7 @@
     phaseBEN = 1;
     
     phaseCEN = 1;
-    redLed = ! redLed;
+//    redLed = ! redLed;
 }
 
 void Brise()    //state5, A- B+ C0
@@ -88,7 +88,7 @@
     phaseBEN = 1;
 }
 
-void Afall()    //state5, A- B0 C+
+void Afall()    //state6, A- B0 C+
 {
     phaseBEN = 0;
     phaseB.write(0);
@@ -98,102 +98,9 @@
     phaseAEN = 1;
 }
 
-int count = 0;
-void intcall(){
-    count = (count + 1) % 6;
-    
-    if(count == 0)  //B+, C-
-    {
-        phaseAEN = 0;
-        phaseA.write(0);
-        phaseB.write(pwmDuty);
-        phaseBEN = 1;
-    }
-    else if(count == 1) //B+, A-
-    {
-        phaseCEN = 0;
-        phaseAEN = 1;
-    }
-    else if(count == 2) //C+, A-
-    {
-        phaseBEN = 0;
-        phaseB.write(0);
-        phaseC.write(pwmDuty);
-        phaseCEN = 1;
-    }
-    else if(count == 3) //C+, B-
-    {
-        phaseAEN = 0;
-        phaseBEN = 1;
-    }
-    else if(count == 4) //A+, B-
-    {
-        phaseCEN = 0;
-        phaseC.write(0);
-        phaseA.write(pwmDuty);
-        phaseAEN = 1;
-    }
-    else if(count == 5) //A+, C-
-    {
-        phaseBEN = 0;
-        phaseCEN = 1;
-    }
-}
 
-
-int main() {     
-    //wait until button push to start
-    rpmInterrupt.attach(&rpmCalc, 1);
-    button.rise(&activate);
-    while(stall == 0) {
-        led = !led;
-        wait(1);
-    }
-
-//    //startup with open loop
-//    phaseA.period_us(10);
-//    phaseB.period_us(10);
-//    phaseC.period_us(10);
-//    interrupt.attach(&intcall, .01);
-//
-//    
-//    phaseA.write(0);
-//    phaseB.write(pwmDuty);
-//    phaseC.write(0);    
-//    
-//    phaseAEN = 0;
-//    phaseBEN = 1;
-//    phaseCEN = 1;
-//    
-//    for(int i = 0; i < 4; i++)
-//    {
-//        i++;
-//        led = !led;
-//        wait(0.5);
-//    }
-//    interrupt.detach();
-
-    pwmDuty = pot.read() * pwmMax;
-
-    phaseA.period_us(10);
-    phaseB.period_us(10);
-    phaseC.period_us(10);
-    
-    phaseA.write(0);
-    phaseB.write(0);
-    phaseC.write(0);    
-    
-    phaseAEN = 0;
-    phaseBEN = 0;
-    phaseCEN = 0;
-    //begin sensored mode 
-    hallA.fall(&Afall);
-    hallA.rise(&Arise);
-    hallB.fall(&Bfall);
-    hallB.rise(&Brise);
-    hallC.fall(&Cfall);
-    hallC.rise(&Crise); 
-    
+void jumpStart()
+{
     int h1 = hallA.read();
     int h2 = hallB.read();
     int h3 = hallC.read();
@@ -228,6 +135,73 @@
         Afall();
         Crise();
     } 
+}
+
+void activate()
+{
+    if(stall == 0)
+    {
+        stall = 1;
+    }
+    else
+    {
+        jumpStart();
+//        if(reverse == 1)    //switch to forward
+//        {
+//            hallA.fall(&Afall);
+//            hallA.rise(&Arise);
+//            hallB.fall(&Bfall);
+//            hallB.rise(&Brise);
+//            hallC.fall(&Cfall);
+//            hallC.rise(&Crise);
+//            redLed = !redLed;
+//            reverse = 0;
+//        }
+//        else if(reverse == 0)    //switch to reverse
+//        {
+//            hallA.fall(&Bfall);
+//            hallA.rise(&Brise); 
+//            hallB.fall(&Cfall);
+//            hallB.rise(&Crise);
+//            hallC.fall(&Afall);
+//            hallC.rise(&Arise);
+//            redLed = !redLed;
+//            reverse = 1;
+//        }
+    }
+}
+
+int main() {     
+    //wait until button push to start
+    rpmInterrupt.attach(&rpmCalc, 1);
+    button.rise(&activate);
+    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);    
+    
+    phaseAEN = 0;
+    phaseBEN = 0;
+    phaseCEN = 0;
+    //begin sensored mode 
+    hallA.fall(&Afall);
+    hallA.rise(&Arise);
+    hallB.fall(&Bfall);
+    hallB.rise(&Brise);
+    hallC.fall(&Cfall);
+    hallC.rise(&Crise); 
+    
+    jumpStart();
     float ADCSum = 0;;
     int ADCCount = 0;
     while(1) {
@@ -242,7 +216,7 @@
         }
         if(rpmPrintFlag == 1)
         {
-            printf("%d rpm\r\n", currentRPM);
+            printf("%d rpm; %f duty\r\n", currentRPM, pwmDuty);
             rpmPrintFlag = 0;
         }
         wait(0.05);