Senior design censored code to run freescale motor with X-NUCLEO-IM07M1. REFACTORED

Dependencies:   mbed

Fork of Blue_Board_Test_2 by Brad VanderWilp

Revision:
1:69c06d3676fd
Parent:
0:d445abf9a8e9
Child:
2:0fbba327c44f
--- a/main.cpp	Wed Apr 06 17:56:38 2016 +0000
+++ b/main.cpp	Wed Apr 06 19:41:25 2016 +0000
@@ -1,98 +1,132 @@
 #include "mbed.h"
 
-PwmOut phaseA(PA_8);
+PwmOut phaseA(PA_8);    //Out1, Green
 DigitalOut phaseAEN(PC_10);
-PwmOut phaseB(PA_9);
+PwmOut phaseB(PA_9);    //Out2, Blue
 DigitalOut phaseBEN(PC_11);
-PwmOut phaseC(PA_10);
+PwmOut phaseC(PA_10);   //Out3, White
 DigitalOut phaseCEN(PC_12);
 
 InterruptIn button(USER_BUTTON);
 
 DigitalOut redLed(PB_2);
 DigitalOut led(LED1);
-//actual 123?
-//InterruptIn hallA(PA_15);
-//InterruptIn hallB(PB_3);
-//InterruptIn hallC(PB_10);
-//attempt to mix and match
-InterruptIn hallA(PB_3);
-InterruptIn hallB(PB_10);
-InterruptIn hallC(PA_15);
+InterruptIn hallA(PA_15);   //H1, Green
+InterruptIn hallB(PB_3);    //H2, Blue
+InterruptIn hallC(PB_10);   //H3, White
 
+Ticker interrupt;
 
-float pwmDuty = 0.3;
+float pwmDuty = 0.5;
 int stall = 0;
 void activate()
 {
     stall = 1;
 }
 
-void Crise()    //0 or 360 degrees, B+, C-
+void Crise()    //state1, A0 B- C+
 {   
     phaseAEN = 0;
+    phaseBEN = 1;
+    
+    redLed = ! redLed;
+}
+
+void Bfall()    //state2, A+ B- C0
+{
+    phaseCEN = 0;
+    phaseC.write(0);
+    phaseA.write(pwmDuty);
+    phaseAEN = 1;
+}
+
+void Arise()    //state3, A+ B0 C-
+{
+    phaseBEN = 0;
+    phaseCEN = 1;
+}
+
+void Cfall()    //state4, A0 B+ C-
+{
+    phaseAEN = 0;
     phaseA.write(0);
     phaseB.write(pwmDuty);
     phaseBEN = 1;
-    redLed = !redLed;
+    
+    redLed = ! redLed;
 }
 
-void Afall()    //60 degrees, B+, A-
+void Brise()    //state5, A- B+ C0
 {
     phaseCEN = 0;
     phaseAEN = 1;
-    redLed = !redLed;
 }
 
-void Brise()    //120 degrees, C+, A-
+void Afall()    //state5, A- B0 C+
 {
     phaseBEN = 0;
     phaseB.write(0);
     phaseC.write(pwmDuty);
     phaseCEN = 1;
-    redLed = !redLed;
-}
-
-void Cfall()    //180 degrees, C+, B-
-{
-    phaseAEN = 0;
-    phaseBEN = 1;
-    redLed = !redLed;
 }
 
-void Arise()    //240 degrees, A+, B-
-{
-    phaseCEN = 0;
-    phaseC.write(0);
-    phaseA.write(pwmDuty);
-    phaseAEN = 1;
-    redLed = !redLed;
+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;
+    }
 }
 
-void Bfall()    //300 degrees, A+, C-
-{
-    phaseBEN = 0;
-    phaseCEN = 1;
-    redLed = !redLed;
-}
 
 int main() {     
+    //wait until button push to start
     button.rise(&activate);
     while(stall == 0) {
         led = !led;
         wait(2);
     }
-    //NOTE: falls and rises are swapped because Hall sensors use NEGATIVE LOGIC
-    hallA.fall(&Arise);
-    hallA.rise(&Afall);
-    hallB.fall(&Brise);
-    hallB.rise(&Bfall);
-    hallC.fall(&Crise);
-    hallC.rise(&Cfall);  
-    
+
+    //startup with open loop
     phaseA.period_us(10);
     phaseB.period_us(10);
     phaseC.period_us(10);
+    interrupt.attach(&intcall, .015);
+
     
     phaseA.write(0);
     phaseB.write(pwmDuty);
@@ -102,6 +136,29 @@
     phaseBEN = 1;
     phaseCEN = 1;
     
+    for(int i = 0; i < 4; i++)
+    {
+        i++;
+        led = !led;
+        wait(0.5);
+    }
+    interrupt.detach();
+    //begin sensored mode
+
+//    hallA.fall(&Arise);
+//    hallA.rise(&Afall);
+//    hallB.fall(&Brise);
+//    hallB.rise(&Bfall);
+//    hallC.fall(&Crise);
+//    hallC.rise(&Cfall);  
+
+    hallA.fall(&Afall);
+    hallA.rise(&Arise);
+    hallB.fall(&Bfall);
+    hallB.rise(&Brise);
+    hallC.fall(&Cfall);
+    hallC.rise(&Crise); 
+    
     while(1) {
         led = !led;
         wait(0.2);