brushless motor control library with l293d
brushlessController_L293D.cpp@6:7cab71c38d62, 2015-07-21 (annotated)
- Committer:
- BaserK
- Date:
- Tue Jul 21 07:59:50 2015 +0000
- Revision:
- 6:7cab71c38d62
- Parent:
- 5:5df174756f84
comment update
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:5df174756f84 | 5 | * |
BaserK | 5:5df174756f84 | 6 | * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr |
BaserK | 5:5df174756f84 | 7 | * |
BaserK | 5:5df174756f84 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
BaserK | 5:5df174756f84 | 9 | * of this software and associated documentation files (the "Software"), to deal |
BaserK | 5:5df174756f84 | 10 | * in the Software without restriction, including without limitation the rights |
BaserK | 5:5df174756f84 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
BaserK | 5:5df174756f84 | 12 | * copies of the Software, and to permit persons to whom the Software is |
BaserK | 5:5df174756f84 | 13 | * furnished to do so, subject to the following conditions: |
BaserK | 5:5df174756f84 | 14 | * |
BaserK | 5:5df174756f84 | 15 | * The above copyright notice and this permission notice shall be included in |
BaserK | 5:5df174756f84 | 16 | * all copies or substantial portions of the Software. |
BaserK | 5:5df174756f84 | 17 | * |
BaserK | 5:5df174756f84 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
BaserK | 5:5df174756f84 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
BaserK | 5:5df174756f84 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
BaserK | 5:5df174756f84 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
BaserK | 5:5df174756f84 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
BaserK | 5:5df174756f84 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
BaserK | 5:5df174756f84 | 24 | * THE SOFTWARE. |
BaserK | 5:5df174756f84 | 25 | * |
BaserK | 4:3f311665f813 | 26 | */ |
BaserK | 4:3f311665f813 | 27 | |
BaserK | 0:9df79ea8037e | 28 | #include "brushlessController_L293D.h" |
BaserK | 0:9df79ea8037e | 29 | |
BaserK | 0:9df79ea8037e | 30 | /* Digital Outputs */ |
BaserK | 0:9df79ea8037e | 31 | DigitalOut en1(p18); |
BaserK | 0:9df79ea8037e | 32 | DigitalOut en2(p19); |
BaserK | 0:9df79ea8037e | 33 | DigitalOut en3(p20); |
BaserK | 0:9df79ea8037e | 34 | DigitalOut out1(p21); |
BaserK | 0:9df79ea8037e | 35 | DigitalOut out2(p22); |
BaserK | 0:9df79ea8037e | 36 | DigitalOut out3(p24); |
BaserK | 0:9df79ea8037e | 37 | |
BaserK | 3:d6e8fbc04a43 | 38 | |
BaserK | 3:d6e8fbc04a43 | 39 | /* Brushless motor control function |
BaserK | 3:d6e8fbc04a43 | 40 | * |
BaserK | 3:d6e8fbc04a43 | 41 | * @brief: This function can control a brushless motor with L293D. |
BaserK | 3:d6e8fbc04a43 | 42 | * By changing steps of the brushless motor one after another, motor |
BaserK | 3:d6e8fbc04a43 | 43 | * can be controlled in either direction. |
BaserK | 3:d6e8fbc04a43 | 44 | * |
BaserK | 3:d6e8fbc04a43 | 45 | * @variables: |
BaserK | 3:d6e8fbc04a43 | 46 | * dir: Direction of the motor |
BaserK | 3:d6e8fbc04a43 | 47 | * delay_time: Time delay between steps |
BaserK | 3:d6e8fbc04a43 | 48 | * stepNum: Number of steps |
BaserK | 3:d6e8fbc04a43 | 49 | */ |
BaserK | 0:9df79ea8037e | 50 | void brushlessControl(bool dir, int delay_time, int stepNum) |
BaserK | 0:9df79ea8037e | 51 | { |
BaserK | 0:9df79ea8037e | 52 | /* Digital outputs are initially zero */ |
BaserK | 0:9df79ea8037e | 53 | out1 = 0; out2 = 0; out3 = 0; |
BaserK | 0:9df79ea8037e | 54 | |
BaserK | 0:9df79ea8037e | 55 | /* Enable the outputs initially */ |
BaserK | 0:9df79ea8037e | 56 | en1 = 1; en2 = 1; en3 = 1; |
BaserK | 0:9df79ea8037e | 57 | |
BaserK | 0:9df79ea8037e | 58 | int stepCount = 0; // Number of step counts |
BaserK | 0:9df79ea8037e | 59 | |
BaserK | 0:9df79ea8037e | 60 | /* Magical data_array to drive the brushless motor */ |
BaserK | 0:9df79ea8037e | 61 | // HIGH:1, LOW:0, HIGH-Z:2 |
BaserK | 0:9df79ea8037e | 62 | char data_array[]={1,2,0, 1,0,2, 2,0,1, 0,2,1, 0,1,2, 2,1,0}; |
BaserK | 0:9df79ea8037e | 63 | char data_arrayInv[]={0,1,2, 0,2,1, 2,0,1, 1,0,2, 1,2,0, 2,1,0}; |
BaserK | 0:9df79ea8037e | 64 | |
BaserK | 0:9df79ea8037e | 65 | if(dir==1) // if dir = 1, reverse the motor direction |
BaserK | 0:9df79ea8037e | 66 | for(int k=0; k<18; k++) |
BaserK | 0:9df79ea8037e | 67 | data_array[k] = data_arrayInv[k]; |
BaserK | 0:9df79ea8037e | 68 | |
BaserK | 0:9df79ea8037e | 69 | for (int i=0; i<stepNum; i++) |
BaserK | 0:9df79ea8037e | 70 | { |
BaserK | 0:9df79ea8037e | 71 | i%=6; // Steps will be repeated at mod6 |
BaserK | 0:9df79ea8037e | 72 | out1 = data_array[3*i]; (data_array[3*i] == 2)?(en1 = 0):(en1 = 1); |
BaserK | 0:9df79ea8037e | 73 | out2 = data_array[3*i+1]; (data_array[3*i+1] == 2)?(en2 = 0):(en2 = 1); |
BaserK | 0:9df79ea8037e | 74 | out3 = data_array[3*i+2]; (data_array[3*i+2] == 2)?(en3 = 0):(en3 = 1); |
BaserK | 0:9df79ea8037e | 75 | wait_ms(delay_time); |
BaserK | 0:9df79ea8037e | 76 | if(++stepCount == stepNum) break; |
BaserK | 0:9df79ea8037e | 77 | } |
BaserK | 1:f7babaac1149 | 78 | } |
BaserK | 1:f7babaac1149 | 79 | |
BaserK | 3:d6e8fbc04a43 | 80 | /* Brushless motor one step movement function |
BaserK | 3:d6e8fbc04a43 | 81 | * |
BaserK | 3:d6e8fbc04a43 | 82 | * @brief: This function can move the motor one step in either direction |
BaserK | 3:d6e8fbc04a43 | 83 | * It takes the previous step as a parameter and uses it to go next. |
BaserK | 3:d6e8fbc04a43 | 84 | * |
BaserK | 3:d6e8fbc04a43 | 85 | * @variables: |
BaserK | 3:d6e8fbc04a43 | 86 | * dir: Direction of the motor |
BaserK | 3:d6e8fbc04a43 | 87 | * delay_time: Time delay between steps |
BaserK | 3:d6e8fbc04a43 | 88 | * prevStep: Previous step of the motor |
BaserK | 3:d6e8fbc04a43 | 89 | */ |
BaserK | 1:f7babaac1149 | 90 | void oneStep(bool dir, int delay_time, int* prevStep) |
BaserK | 1:f7babaac1149 | 91 | { |
BaserK | 1:f7babaac1149 | 92 | /* Digital outputs are initially zero */ |
BaserK | 1:f7babaac1149 | 93 | out1 = 0; out2 = 0; out3 = 0; |
BaserK | 1:f7babaac1149 | 94 | |
BaserK | 1:f7babaac1149 | 95 | /* Enable the outputs initially */ |
BaserK | 1:f7babaac1149 | 96 | en1 = 1; en2 = 1; en3 = 1; |
BaserK | 1:f7babaac1149 | 97 | |
BaserK | 2:ac825be76379 | 98 | /* Magical data_array to drive the brushless motor */ |
BaserK | 1:f7babaac1149 | 99 | // HIGH:1, LOW:0, HIGH-Z:2 |
BaserK | 1:f7babaac1149 | 100 | char data_array[]={1,2,0, 1,0,2, 2,0,1, 0,2,1, 0,1,2, 2,1,0}; |
BaserK | 2:ac825be76379 | 101 | |
BaserK | 2:ac825be76379 | 102 | if(dir==1) |
BaserK | 2:ac825be76379 | 103 | *prevStep = *prevStep + 1; // Increase prevStep for next step || WARNING: *prevStep++ did not work |
BaserK | 2:ac825be76379 | 104 | else |
BaserK | 2:ac825be76379 | 105 | *prevStep = *prevStep - 1; |
BaserK | 2:ac825be76379 | 106 | |
BaserK | 1:f7babaac1149 | 107 | int i = *prevStep; |
BaserK | 2:ac825be76379 | 108 | |
BaserK | 3:d6e8fbc04a43 | 109 | if (i<0) // Mod operation for the negative number |
BaserK | 2:ac825be76379 | 110 | { |
BaserK | 2:ac825be76379 | 111 | i*=(-1); i%=6; i=6-i; |
BaserK | 2:ac825be76379 | 112 | i%=6; // if i=6, it should be zero. |
BaserK | 2:ac825be76379 | 113 | } |
BaserK | 2:ac825be76379 | 114 | else |
BaserK | 2:ac825be76379 | 115 | i%=6; // Steps will be repeated at mod6 |
BaserK | 1:f7babaac1149 | 116 | |
BaserK | 1:f7babaac1149 | 117 | out1 = data_array[3*i]; (data_array[3*i] == 2)?(en1 = 0):(en1 = 1); |
BaserK | 1:f7babaac1149 | 118 | out2 = data_array[3*i+1]; (data_array[3*i+1] == 2)?(en2 = 0):(en2 = 1); |
BaserK | 1:f7babaac1149 | 119 | out3 = data_array[3*i+2]; (data_array[3*i+2] == 2)?(en3 = 0):(en3 = 1); |
BaserK | 1:f7babaac1149 | 120 | wait_ms(delay_time); |
BaserK | 1:f7babaac1149 | 121 | } |