brushless motor control library with l293d

Dependents:   brushless_L293D

Committer:
BaserK
Date:
Thu Jul 16 15:17:01 2015 +0000
Revision:
3:d6e8fbc04a43
Parent:
2:ac825be76379
Child:
4:3f311665f813
brushless motor control library with L293D

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 3:d6e8fbc04a43 11
BaserK 3:d6e8fbc04a43 12 /* Brushless motor control function
BaserK 3:d6e8fbc04a43 13 *
BaserK 3:d6e8fbc04a43 14 * @brief: This function can control a brushless motor with L293D.
BaserK 3:d6e8fbc04a43 15 * By changing steps of the brushless motor one after another, motor
BaserK 3:d6e8fbc04a43 16 * can be controlled in either direction.
BaserK 3:d6e8fbc04a43 17 *
BaserK 3:d6e8fbc04a43 18 * @variables:
BaserK 3:d6e8fbc04a43 19 * dir: Direction of the motor
BaserK 3:d6e8fbc04a43 20 * delay_time: Time delay between steps
BaserK 3:d6e8fbc04a43 21 * stepNum: Number of steps
BaserK 3:d6e8fbc04a43 22 */
BaserK 0:9df79ea8037e 23 void brushlessControl(bool dir, int delay_time, int stepNum)
BaserK 0:9df79ea8037e 24 {
BaserK 0:9df79ea8037e 25 /* Digital outputs are initially zero */
BaserK 0:9df79ea8037e 26 out1 = 0; out2 = 0; out3 = 0;
BaserK 0:9df79ea8037e 27
BaserK 0:9df79ea8037e 28 /* Enable the outputs initially */
BaserK 0:9df79ea8037e 29 en1 = 1; en2 = 1; en3 = 1;
BaserK 0:9df79ea8037e 30
BaserK 0:9df79ea8037e 31 int stepCount = 0; // Number of step counts
BaserK 0:9df79ea8037e 32
BaserK 0:9df79ea8037e 33 /* Magical data_array to drive the brushless motor */
BaserK 0:9df79ea8037e 34 // HIGH:1, LOW:0, HIGH-Z:2
BaserK 0:9df79ea8037e 35 char data_array[]={1,2,0, 1,0,2, 2,0,1, 0,2,1, 0,1,2, 2,1,0};
BaserK 0:9df79ea8037e 36 char data_arrayInv[]={0,1,2, 0,2,1, 2,0,1, 1,0,2, 1,2,0, 2,1,0};
BaserK 0:9df79ea8037e 37
BaserK 0:9df79ea8037e 38 if(dir==1) // if dir = 1, reverse the motor direction
BaserK 0:9df79ea8037e 39 for(int k=0; k<18; k++)
BaserK 0:9df79ea8037e 40 data_array[k] = data_arrayInv[k];
BaserK 0:9df79ea8037e 41
BaserK 0:9df79ea8037e 42 for (int i=0; i<stepNum; i++)
BaserK 0:9df79ea8037e 43 {
BaserK 0:9df79ea8037e 44 i%=6; // Steps will be repeated at mod6
BaserK 0:9df79ea8037e 45 out1 = data_array[3*i]; (data_array[3*i] == 2)?(en1 = 0):(en1 = 1);
BaserK 0:9df79ea8037e 46 out2 = data_array[3*i+1]; (data_array[3*i+1] == 2)?(en2 = 0):(en2 = 1);
BaserK 0:9df79ea8037e 47 out3 = data_array[3*i+2]; (data_array[3*i+2] == 2)?(en3 = 0):(en3 = 1);
BaserK 0:9df79ea8037e 48 wait_ms(delay_time);
BaserK 0:9df79ea8037e 49 if(++stepCount == stepNum) break;
BaserK 0:9df79ea8037e 50 }
BaserK 1:f7babaac1149 51 }
BaserK 1:f7babaac1149 52
BaserK 3:d6e8fbc04a43 53 /* Brushless motor one step movement function
BaserK 3:d6e8fbc04a43 54 *
BaserK 3:d6e8fbc04a43 55 * @brief: This function can move the motor one step in either direction
BaserK 3:d6e8fbc04a43 56 * It takes the previous step as a parameter and uses it to go next.
BaserK 3:d6e8fbc04a43 57 *
BaserK 3:d6e8fbc04a43 58 * @variables:
BaserK 3:d6e8fbc04a43 59 * dir: Direction of the motor
BaserK 3:d6e8fbc04a43 60 * delay_time: Time delay between steps
BaserK 3:d6e8fbc04a43 61 * prevStep: Previous step of the motor
BaserK 3:d6e8fbc04a43 62 */
BaserK 1:f7babaac1149 63 void oneStep(bool dir, int delay_time, int* prevStep)
BaserK 1:f7babaac1149 64 {
BaserK 1:f7babaac1149 65 /* Digital outputs are initially zero */
BaserK 1:f7babaac1149 66 out1 = 0; out2 = 0; out3 = 0;
BaserK 1:f7babaac1149 67
BaserK 1:f7babaac1149 68 /* Enable the outputs initially */
BaserK 1:f7babaac1149 69 en1 = 1; en2 = 1; en3 = 1;
BaserK 1:f7babaac1149 70
BaserK 2:ac825be76379 71 /* Magical data_array to drive the brushless motor */
BaserK 1:f7babaac1149 72 // HIGH:1, LOW:0, HIGH-Z:2
BaserK 1:f7babaac1149 73 char data_array[]={1,2,0, 1,0,2, 2,0,1, 0,2,1, 0,1,2, 2,1,0};
BaserK 2:ac825be76379 74
BaserK 2:ac825be76379 75 if(dir==1)
BaserK 2:ac825be76379 76 *prevStep = *prevStep + 1; // Increase prevStep for next step || WARNING: *prevStep++ did not work
BaserK 2:ac825be76379 77 else
BaserK 2:ac825be76379 78 *prevStep = *prevStep - 1;
BaserK 2:ac825be76379 79
BaserK 1:f7babaac1149 80 int i = *prevStep;
BaserK 2:ac825be76379 81
BaserK 3:d6e8fbc04a43 82 if (i<0) // Mod operation for the negative number
BaserK 2:ac825be76379 83 {
BaserK 2:ac825be76379 84 i*=(-1); i%=6; i=6-i;
BaserK 2:ac825be76379 85 i%=6; // if i=6, it should be zero.
BaserK 2:ac825be76379 86 }
BaserK 2:ac825be76379 87 else
BaserK 2:ac825be76379 88 i%=6; // Steps will be repeated at mod6
BaserK 1:f7babaac1149 89
BaserK 1:f7babaac1149 90 out1 = data_array[3*i]; (data_array[3*i] == 2)?(en1 = 0):(en1 = 1);
BaserK 1:f7babaac1149 91 out2 = data_array[3*i+1]; (data_array[3*i+1] == 2)?(en2 = 0):(en2 = 1);
BaserK 1:f7babaac1149 92 out3 = data_array[3*i+2]; (data_array[3*i+2] == 2)?(en3 = 0):(en3 = 1);
BaserK 1:f7babaac1149 93 wait_ms(delay_time);
BaserK 1:f7babaac1149 94 }