brushless motor control library with l293d

Dependents:   brushless_L293D

Committer:
BaserK
Date:
Tue Jul 14 07:35:41 2015 +0000
Revision:
1:f7babaac1149
Parent:
0:9df79ea8037e
Child:
2:ac825be76379
oneStep function is added:; - One step movement in either direction; - It takes the previous state and uses it for the next state; - It is required for gimbalController

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 1:f7babaac1149 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 1:f7babaac1149 53 char data_arrayInv[]={0,1,2, 0,2,1, 2,0,1, 1,0,2, 1,2,0, 2,1,0};
BaserK 1:f7babaac1149 54
BaserK 1:f7babaac1149 55 if(dir==1) // if dir = 1, reverse the motor direction
BaserK 1:f7babaac1149 56 for(int k=0; k<18; k++)
BaserK 1:f7babaac1149 57 data_array[k] = data_arrayInv[k];
BaserK 1:f7babaac1149 58
BaserK 1:f7babaac1149 59 int i = *prevStep;
BaserK 1:f7babaac1149 60 i%=6; // Steps will be repeated at mod6
BaserK 1:f7babaac1149 61 *prevStep = *prevStep + 1; // Increase prevStep for next step || WARNING: *prevStep++ did not work
BaserK 1:f7babaac1149 62
BaserK 1:f7babaac1149 63 out1 = data_array[3*i]; (data_array[3*i] == 2)?(en1 = 0):(en1 = 1);
BaserK 1:f7babaac1149 64 out2 = data_array[3*i+1]; (data_array[3*i+1] == 2)?(en2 = 0):(en2 = 1);
BaserK 1:f7babaac1149 65 out3 = data_array[3*i+2]; (data_array[3*i+2] == 2)?(en3 = 0):(en3 = 1);
BaserK 1:f7babaac1149 66 wait_ms(delay_time);
BaserK 1:f7babaac1149 67 }