brushless motor control library with l293d

Dependents:   brushless_L293D

Committer:
BaserK
Date:
Thu Jul 16 20:24:16 2015 +0000
Revision:
4:3f311665f813
Parent:
3:d6e8fbc04a43
Child:
5:5df174756f84
comment update;

Who changed what in which revision?

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