brushless motor control library with l293d

Dependents:   brushless_L293D

Committer:
BaserK
Date:
Mon Jul 13 14:12:10 2015 +0000
Revision:
0:9df79ea8037e
Child:
1:f7babaac1149
Brushless motor control library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BaserK 0:9df79ea8037e 1 #include "brushlessController_L293D.h"
BaserK 0:9df79ea8037e 2
BaserK 0:9df79ea8037e 3 /* Digital Outputs */
BaserK 0:9df79ea8037e 4 DigitalOut en1(p18);
BaserK 0:9df79ea8037e 5 DigitalOut en2(p19);
BaserK 0:9df79ea8037e 6 DigitalOut en3(p20);
BaserK 0:9df79ea8037e 7 DigitalOut out1(p21);
BaserK 0:9df79ea8037e 8 DigitalOut out2(p22);
BaserK 0:9df79ea8037e 9 DigitalOut out3(p24);
BaserK 0:9df79ea8037e 10
BaserK 0:9df79ea8037e 11 /* HIGH-Z is activated with 2xL293D, now it works better */
BaserK 0:9df79ea8037e 12 void brushlessControl(bool dir, int delay_time, int stepNum)
BaserK 0:9df79ea8037e 13 {
BaserK 0:9df79ea8037e 14 /* Digital outputs are initially zero */
BaserK 0:9df79ea8037e 15 out1 = 0; out2 = 0; out3 = 0;
BaserK 0:9df79ea8037e 16
BaserK 0:9df79ea8037e 17 /* Enable the outputs initially */
BaserK 0:9df79ea8037e 18 en1 = 1; en2 = 1; en3 = 1;
BaserK 0:9df79ea8037e 19
BaserK 0:9df79ea8037e 20 int stepCount = 0; // Number of step counts
BaserK 0:9df79ea8037e 21
BaserK 0:9df79ea8037e 22 /* Magical data_array to drive the brushless motor */
BaserK 0:9df79ea8037e 23 // HIGH:1, LOW:0, HIGH-Z:2
BaserK 0:9df79ea8037e 24 char data_array[]={1,2,0, 1,0,2, 2,0,1, 0,2,1, 0,1,2, 2,1,0};
BaserK 0:9df79ea8037e 25 char data_arrayInv[]={0,1,2, 0,2,1, 2,0,1, 1,0,2, 1,2,0, 2,1,0};
BaserK 0:9df79ea8037e 26
BaserK 0:9df79ea8037e 27 if(dir==1) // if dir = 1, reverse the motor direction
BaserK 0:9df79ea8037e 28 for(int k=0; k<18; k++)
BaserK 0:9df79ea8037e 29 data_array[k] = data_arrayInv[k];
BaserK 0:9df79ea8037e 30
BaserK 0:9df79ea8037e 31 for (int i=0; i<stepNum; i++)
BaserK 0:9df79ea8037e 32 {
BaserK 0:9df79ea8037e 33 i%=6; // Steps will be repeated at mod6
BaserK 0:9df79ea8037e 34 out1 = data_array[3*i]; (data_array[3*i] == 2)?(en1 = 0):(en1 = 1);
BaserK 0:9df79ea8037e 35 out2 = data_array[3*i+1]; (data_array[3*i+1] == 2)?(en2 = 0):(en2 = 1);
BaserK 0:9df79ea8037e 36 out3 = data_array[3*i+2]; (data_array[3*i+2] == 2)?(en3 = 0):(en3 = 1);
BaserK 0:9df79ea8037e 37 wait_ms(delay_time);
BaserK 0:9df79ea8037e 38 if(++stepCount == stepNum) break;
BaserK 0:9df79ea8037e 39 }
BaserK 0:9df79ea8037e 40 }