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

Dependencies:   mbed

Fork of Blue_Board_Test_2 by Brad VanderWilp

main.cpp

Committer:
vicyap
Date:
2016-04-07
Revision:
6:f9aca07dbdb4
Parent:
5:6110655353ad
Child:
7:b8ef1960498e

File content as of revision 6:f9aca07dbdb4:

#include "mbed.h"

#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

#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(X_NUCLEO_IHM07001_LED);
DigitalOut led(LED1);

Ticker interrupt;

/* end globals */

float pwmMax = PWM_MAX;
float pwmDuty;
int stall = 0;
void activate()
{
    stall = 1;
}

void Crise()    //state1, A0 B- C+
{   
//    phaseAEN = 0;
//    phaseBEN = 1;
    
    phaseA.write(0);
    phaseB.write(0);
    phaseC.write(pwmDuty);
//    phaseCEN = 1;

    SixStep::Enable_CH1_CH2_Disable_CH3(phaseBEN, phaseCEN, phaseAEN);
}

void Bfall()    //state2, A+ B- C0
{
//    phaseCEN = 0;
    phaseA.write(pwmDuty);
    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;
    
    phaseA.write(pwmDuty);
    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;
    phaseA.write(0);
    phaseB.write(pwmDuty);
    phaseC.write(0);
//    phaseBEN = 1;
    
//    phaseCEN = 1;
    SixStep::Enable_CH1_CH2_Disable_CH3(phaseBEN, phaseCEN, phaseAEN);
}

void Brise()    //state5, A- B+ C0
{
//    phaseCEN = 0;
//    phaseAEN = 1;
    
    phaseA.write(0);
    phaseB.write(pwmDuty);
    phaseC.write(0);
//    phaseBEN = 1;
    SixStep::Enable_CH1_CH2_Disable_CH3(phaseAEN, phaseBEN, phaseCEN);
}

void Afall()    //state5, A- B0 C+
{
//    phaseBEN = 0;
    phaseA.write(0);
    phaseB.write(0);
    phaseC.write(pwmDuty);
//    phaseCEN = 1;
    
//    phaseAEN = 1;
    SixStep::Enable_CH1_CH2_Disable_CH3(phaseAEN, phaseCEN, phaseBEN);
}

void Init()
{
    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;
    
    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);
}

void StartUp()
{
    int h1 = hallA.read();
    int h2 = hallB.read();
    int h3 = hallC.read();
    //check where we start
    if(h1 == 0 && h2 == 1 && h3 == 1)   //state1
    {
        Crise();
        Bfall();
    }
    else if(h1 == 0 && h2 == 0 && h3 == 1)  //state2
    {
        Bfall();
        Arise();
    }
    else if(h1 == 1 && h2 == 0 && h3 == 1)  //state3
    {
        Arise();
        Cfall();
    }
    else if(h1 == 1 && h2 == 0 && h3 == 0)     //state4
    {
        Cfall();
        Brise();
    }
    else if(h1 == 1 && h2 == 1 && h3 == 0)  //state5
    {
        Brise();
        Afall();
    }
    else    //state6
    {
        Afall();
        Crise();
    }
}

//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) * PWM_MAX;
//            ADCSum = 0;
//            ADCCount = 0;
//        }
//        pwmDuty = pot.read() * 0.7;
        pwmDuty = pot.buffered_read() * PWM_MAX;
        wait(0.05);
    }
}