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:
6:f9aca07dbdb4
Parent:
5:6110655353ad
Child:
7:b8ef1960498e
--- a/main.cpp	Wed Apr 06 20:36:41 2016 +0000
+++ b/main.cpp	Thu Apr 07 23:27:18 2016 +0000
@@ -1,25 +1,55 @@
 #include "mbed.h"
 
-PwmOut phaseA(PA_8);    //Out1, Green
-DigitalOut phaseAEN(PC_10);
-PwmOut phaseB(PA_9);    //Out2, Blue
-DigitalOut phaseBEN(PC_11);
-PwmOut phaseC(PA_10);   //Out3, White
-DigitalOut phaseCEN(PC_12);
+#include "SixStep_Lib.h"
+#include "AnalogInBuffered.h"
+
+/* start Parameters */
+
+#define OUT1 PA_8
+#define OUT2 PA_9
+#define OUT3 PA_10
+#define OUT1EN PC_10
+#define OUT2EN PC_11
+#define OUT3EN PC_12
+
+#define HALL1 PA_15
+#define HALL2 PB_3
+#define HALL3 PB_10
+
+#define POTENTIOMETER PB_1
+#define POTENTIOMETER_BUFFER_SIZE 20
 
-AnalogIn pot(PB_1);
+#define X_NUCLEO_IHM07001_LED PB_2
+
+#define PWM_MAX 0.9f
+
+/* end Parameters */
+
+/* start globals */
+
+PwmOut phaseA(OUT1);    //Out1, Green
+DigitalOut phaseAEN(OUT1EN);
+PwmOut phaseB(OUT2);    //Out2, Blue
+DigitalOut phaseBEN(OUT2EN);
+PwmOut phaseC(OUT3);   //Out3, White
+DigitalOut phaseCEN(OUT3EN);
+
+InterruptIn hallA(HALL1);   //H1, Green
+InterruptIn hallB(HALL2);    //H2, Blue
+InterruptIn hallC(HALL3);   //H3, White
+
+AnalogInBuffered pot(POTENTIOMETER, POTENTIOMETER_BUFFER_SIZE);
 
 InterruptIn button(USER_BUTTON);
 
-DigitalOut redLed(PB_2);
+DigitalOut redLed(X_NUCLEO_IHM07001_LED);
 DigitalOut led(LED1);
-InterruptIn hallA(PA_15);   //H1, Green
-InterruptIn hallB(PB_3);    //H2, Blue
-InterruptIn hallC(PB_10);   //H3, White
 
 Ticker interrupt;
 
-float pwmMax = 0.9;
+/* end globals */
+
+float pwmMax = PWM_MAX;
 float pwmDuty;
 int stall = 0;
 void activate()
@@ -29,158 +59,105 @@
 
 void Crise()    //state1, A0 B- C+
 {   
-    phaseAEN = 0;
-    phaseBEN = 1;
+//    phaseAEN = 0;
+//    phaseBEN = 1;
     
+    phaseA.write(0);
+    phaseB.write(0);
     phaseC.write(pwmDuty);
-    phaseCEN = 1;
-    redLed = ! redLed;
+//    phaseCEN = 1;
+
+    SixStep::Enable_CH1_CH2_Disable_CH3(phaseBEN, phaseCEN, phaseAEN);
 }
 
 void Bfall()    //state2, A+ B- C0
 {
-    phaseCEN = 0;
-    phaseC.write(0);
+//    phaseCEN = 0;
     phaseA.write(pwmDuty);
-    phaseAEN = 1;
-    
-    phaseBEN = 1;
+    phaseB.write(0);
+    phaseC.write(0);
+//  phaseAEN = 1;
+//    
+//    phaseBEN = 1;
+    SixStep::Enable_CH1_CH2_Disable_CH3(phaseAEN, phaseBEN, phaseCEN);
 }
 
 void Arise()    //state3, A+ B0 C-
 {
-    phaseBEN = 0;
-    phaseCEN = 1;
+//    phaseBEN = 0;
+//    phaseCEN = 1;
     
     phaseA.write(pwmDuty);
-    phaseAEN = 1;
+    phaseB.write(0);
+    phaseC.write(0);
+//    phaseAEN = 1;
+    SixStep::Enable_CH1_CH2_Disable_CH3(phaseAEN, phaseCEN, phaseBEN);
 }
 
 void Cfall()    //state4, A0 B+ C-
 {
-    phaseAEN = 0;
+//    phaseAEN = 0;
     phaseA.write(0);
     phaseB.write(pwmDuty);
-    phaseBEN = 1;
+    phaseC.write(0);
+//    phaseBEN = 1;
     
-    phaseCEN = 1;
-    redLed = ! redLed;
+//    phaseCEN = 1;
+    SixStep::Enable_CH1_CH2_Disable_CH3(phaseBEN, phaseCEN, phaseAEN);
 }
 
 void Brise()    //state5, A- B+ C0
 {
-    phaseCEN = 0;
-    phaseAEN = 1;
+//    phaseCEN = 0;
+//    phaseAEN = 1;
     
+    phaseA.write(0);
     phaseB.write(pwmDuty);
-    phaseBEN = 1;
+    phaseC.write(0);
+//    phaseBEN = 1;
+    SixStep::Enable_CH1_CH2_Disable_CH3(phaseAEN, phaseBEN, phaseCEN);
 }
 
 void Afall()    //state5, A- B0 C+
 {
-    phaseBEN = 0;
+//    phaseBEN = 0;
+    phaseA.write(0);
     phaseB.write(0);
     phaseC.write(pwmDuty);
-    phaseCEN = 1;
+//    phaseCEN = 1;
     
-    phaseAEN = 1;
+//    phaseAEN = 1;
+    SixStep::Enable_CH1_CH2_Disable_CH3(phaseAEN, phaseCEN, phaseBEN);
 }
 
-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
-    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;
-
+void Init()
+{
     phaseA.period_us(10);
     phaseB.period_us(10);
     phaseC.period_us(10);
     
     phaseA.write(0);
     phaseB.write(0);
-    phaseC.write(0);    
+    phaseC.write(0);
     
     phaseAEN = 0;
     phaseBEN = 0;
     phaseCEN = 0;
-    //begin sensored mode 
+    
+    pwmDuty = pot.read() * PWM_MAX;  
+    
+    // sensored mode
+    // attach appropriate ISRs to hall sensor interrupt pins 
     hallA.fall(&Afall);
     hallA.rise(&Arise);
     hallB.fall(&Bfall);
     hallB.rise(&Brise);
     hallC.fall(&Cfall);
-    hallC.rise(&Crise); 
-    
+    hallC.rise(&Crise);
+}
+
+void StartUp()
+{
     int h1 = hallA.read();
     int h2 = hallB.read();
     int h3 = hallC.read();
@@ -214,20 +191,102 @@
     {
         Afall();
         Crise();
-    } 
-    float ADCSum = 0;;
-    int ADCCount = 0;
+    }
+}
+
+//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
+    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();
+    
+    Init();
+    
+    StartUp();
+     
+//    float ADCSum = 0;
+//    int ADCCount = 0;
+    
     while(1) {
         led = !led;
-        ADCSum += pot.read();
-        ADCCount++;
-        if(ADCCount == 20)
-        {
-            pwmDuty = (ADCSum/20) * pwmMax;
-            ADCSum = 0;
-            ADCCount = 0;
-        }
+        //ADCSum += pot.read();
+//        ADCCount++;
+//        if(ADCCount == 20)
+//        {
+//            pwmDuty = (ADCSum/20) * PWM_MAX;
+//            ADCSum = 0;
+//            ADCCount = 0;
+//        }
 //        pwmDuty = pot.read() * 0.7;
+        pwmDuty = pot.buffered_read() * PWM_MAX;
         wait(0.05);
     }
 }