brushless motor control library with l293d

Dependents:   brushless_L293D

brushlessController_L293D.cpp

Committer:
BaserK
Date:
2015-07-14
Revision:
1:f7babaac1149
Parent:
0:9df79ea8037e
Child:
2:ac825be76379

File content as of revision 1:f7babaac1149:

#include "brushlessController_L293D.h"

/* Digital Outputs */
DigitalOut en1(p18);
DigitalOut en2(p19);
DigitalOut en3(p20);
DigitalOut out1(p21);
DigitalOut out2(p22);
DigitalOut out3(p24);

/* HIGH-Z is activated with 2xL293D, now it works better */
void brushlessControl(bool dir, int delay_time, int stepNum)
{
    /* Digital outputs are initially zero */
    out1 = 0; out2 = 0; out3 = 0;
    
    /* Enable the outputs initially */
    en1 = 1; en2 = 1; en3 = 1; 
    
    int stepCount = 0; // Number of step counts
    
    /* Magical data_array to drive the brushless motor */ 
    // HIGH:1, LOW:0, HIGH-Z:2
    char data_array[]={1,2,0, 1,0,2, 2,0,1, 0,2,1, 0,1,2, 2,1,0};
    char data_arrayInv[]={0,1,2, 0,2,1, 2,0,1, 1,0,2, 1,2,0, 2,1,0};
    
    if(dir==1)      // if dir = 1, reverse the motor direction
        for(int k=0; k<18; k++)
            data_array[k] = data_arrayInv[k];      
    
    for (int i=0; i<stepNum; i++)
    {
        i%=6;  // Steps will be repeated at mod6
        out1 = data_array[3*i];    (data_array[3*i]   == 2)?(en1 = 0):(en1 = 1);   
        out2 = data_array[3*i+1];  (data_array[3*i+1] == 2)?(en2 = 0):(en2 = 1); 
        out3 = data_array[3*i+2];  (data_array[3*i+2] == 2)?(en3 = 0):(en3 = 1); 
        wait_ms(delay_time);   
        if(++stepCount == stepNum) break;    
    }   
}

void oneStep(bool dir, int delay_time, int* prevStep)
{
    /* Digital outputs are initially zero */
    out1 = 0; out2 = 0; out3 = 0;
    
    /* Enable the outputs initially */
    en1 = 1; en2 = 1; en3 = 1; 
    
      /* Magical data_array to drive the brushless motor */ 
    // HIGH:1, LOW:0, HIGH-Z:2
    char data_array[]={1,2,0, 1,0,2, 2,0,1, 0,2,1, 0,1,2, 2,1,0};
    char data_arrayInv[]={0,1,2, 0,2,1, 2,0,1, 1,0,2, 1,2,0, 2,1,0};
    
    if(dir==1)      // if dir = 1, reverse the motor direction
        for(int k=0; k<18; k++)
            data_array[k] = data_arrayInv[k];
    
    int i = *prevStep;
    i%=6;                         // Steps will be repeated at mod6
    *prevStep = *prevStep + 1;    // Increase prevStep for next step || WARNING: *prevStep++ did not work
    
    out1 = data_array[3*i];    (data_array[3*i]   == 2)?(en1 = 0):(en1 = 1);   
    out2 = data_array[3*i+1];  (data_array[3*i+1] == 2)?(en2 = 0):(en2 = 1); 
    out3 = data_array[3*i+2];  (data_array[3*i+2] == 2)?(en3 = 0):(en3 = 1);   
    wait_ms(delay_time);  
}