brushless motor control library with l293d

Dependents:   brushless_L293D

Committer:
BaserK
Date:
Tue Jul 14 08:43:10 2015 +0000
Revision:
2:ac825be76379
Parent:
1:f7babaac1149
Child:
3:d6e8fbc04a43
oneStep function is updated (the motor could not move one step backward when it is supposed to, now it can)

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 1:f7babaac1149 40 }
BaserK 1:f7babaac1149 41
BaserK 1:f7babaac1149 42 void oneStep(bool dir, int delay_time, int* prevStep)
BaserK 1:f7babaac1149 43 {
BaserK 1:f7babaac1149 44 /* Digital outputs are initially zero */
BaserK 1:f7babaac1149 45 out1 = 0; out2 = 0; out3 = 0;
BaserK 1:f7babaac1149 46
BaserK 1:f7babaac1149 47 /* Enable the outputs initially */
BaserK 1:f7babaac1149 48 en1 = 1; en2 = 1; en3 = 1;
BaserK 1:f7babaac1149 49
BaserK 2:ac825be76379 50 /* Magical data_array to drive the brushless motor */
BaserK 1:f7babaac1149 51 // HIGH:1, LOW:0, HIGH-Z:2
BaserK 1:f7babaac1149 52 char data_array[]={1,2,0, 1,0,2, 2,0,1, 0,2,1, 0,1,2, 2,1,0};
BaserK 2:ac825be76379 53
BaserK 2:ac825be76379 54 if(dir==1)
BaserK 2:ac825be76379 55 *prevStep = *prevStep + 1; // Increase prevStep for next step || WARNING: *prevStep++ did not work
BaserK 2:ac825be76379 56 else
BaserK 2:ac825be76379 57 *prevStep = *prevStep - 1;
BaserK 2:ac825be76379 58
BaserK 1:f7babaac1149 59 int i = *prevStep;
BaserK 2:ac825be76379 60
BaserK 2:ac825be76379 61 if (i<0) // Mod operation for the negative number
BaserK 2:ac825be76379 62 {
BaserK 2:ac825be76379 63 i*=(-1); i%=6; i=6-i;
BaserK 2:ac825be76379 64 i%=6; // if i=6, it should be zero.
BaserK 2:ac825be76379 65 }
BaserK 2:ac825be76379 66 else
BaserK 2:ac825be76379 67 i%=6; // Steps will be repeated at mod6
BaserK 1:f7babaac1149 68
BaserK 1:f7babaac1149 69 out1 = data_array[3*i]; (data_array[3*i] == 2)?(en1 = 0):(en1 = 1);
BaserK 1:f7babaac1149 70 out2 = data_array[3*i+1]; (data_array[3*i+1] == 2)?(en2 = 0):(en2 = 1);
BaserK 1:f7babaac1149 71 out3 = data_array[3*i+2]; (data_array[3*i+2] == 2)?(en3 = 0):(en3 = 1);
BaserK 1:f7babaac1149 72 wait_ms(delay_time);
BaserK 1:f7babaac1149 73 }
BaserK 2:ac825be76379 74