Modified phase table in stepper.cpp to enable stepping a bipolar motor using a twin H-bridge driver.

Fork of stepper by Kenji Arai

Corrected single-stepping, now walking up or down just one phase table. Compile-time options for driving bipolar motor in any of single-phase, two-phase, or half-stepping. Coils remain engaged at end of specifed movement command - de-energize coils by issuing a motor.move(0) while already stopped.

Committer:
gregeric
Date:
Wed Dec 31 17:36:36 2014 +0000
Revision:
8:75fcbd49d49f
Parent:
7:9fc4b1be489c
Updates and fixes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:7b0c724fa658 1 /*
gregeric 7:9fc4b1be489c 2 * mbed library program
kenjiArai 0:7b0c724fa658 3 * Stepping Motor
kenjiArai 0:7b0c724fa658 4 *
kenjiArai 0:7b0c724fa658 5 * Copyright (c) 2014 Kenji Arai / JH1PJL
kenjiArai 0:7b0c724fa658 6 * http://www.page.sannet.ne.jp/kenjia/index.html
kenjiArai 0:7b0c724fa658 7 * http://mbed.org/users/kenjiArai/
kenjiArai 0:7b0c724fa658 8 * Created: August 20th, 2014
kenjiArai 0:7b0c724fa658 9 * Revised: August 23rd, 2014
kenjiArai 0:7b0c724fa658 10 *
kenjiArai 0:7b0c724fa658 11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
kenjiArai 0:7b0c724fa658 12 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
kenjiArai 0:7b0c724fa658 13 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kenjiArai 0:7b0c724fa658 14 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenjiArai 0:7b0c724fa658 15 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kenjiArai 0:7b0c724fa658 16 */
kenjiArai 0:7b0c724fa658 17
gregeric 8:75fcbd49d49f 18 #include "mbed.h"
gregeric 4:5b596b405573 19 #include "stepper.h"
gregeric 3:96bfb8b476f8 20
gregeric 4:5b596b405573 21 //uncomment one option
gregeric 8:75fcbd49d49f 22 //in truth, both uni- & bi- are equivalent, and just represent a different wiring of mcu GPIOs to H-Bridge
gregeric 8:75fcbd49d49f 23 //TODO: adopt one most-prevalent wiring scheme only, run-time selectable wiring & step pattern?
gregeric 8:75fcbd49d49f 24
gregeric 4:5b596b405573 25 //#define BIPOLAR_STEPPER_1PH
gregeric 4:5b596b405573 26 //#define BIPOLAR_STEPPER_2PH
gregeric 4:5b596b405573 27 #define BIPOLAR_STEPPER_12S
gregeric 7:9fc4b1be489c 28 //#define UNIPOLAR_STEPPER_1PH
gregeric 4:5b596b405573 29 //#define UNIPOLAR_STEPPER_2PH
gregeric 7:9fc4b1be489c 30 //#define UNIPOLAR_STEPPER_12S
gregeric 2:fd11d89b8ce0 31 /*
gregeric 4:5b596b405573 32 * Firing sequence for bi-polar (4 wires) stepper (2x H-Bridge driver required eg L298)
gregeric 3:96bfb8b476f8 33 */
gregeric 4:5b596b405573 34 #ifdef BIPOLAR_STEPPER_1PH
gregeric 4:5b596b405573 35 //single phase A+, B+, A-, B-
gregeric 8:75fcbd49d49f 36 //mcu wiring to H-bridge: A A' B B'
gregeric 3:96bfb8b476f8 37 const uint8_t pase_cw[4][4] = {{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}};
gregeric 4:5b596b405573 38 #endif
gregeric 4:5b596b405573 39
gregeric 4:5b596b405573 40 #ifdef BIPOLAR_STEPPER_2PH
gregeric 7:9fc4b1be489c 41 //double phase A+B+, A-B+, A-B-, A+B-
gregeric 4:5b596b405573 42 const uint8_t pase_cw[4][4] = {{1, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 1}, {1, 0, 0, 1}};
gregeric 4:5b596b405573 43 #endif
gregeric 4:5b596b405573 44
gregeric 4:5b596b405573 45 #ifdef BIPOLAR_STEPPER_12S
gregeric 4:5b596b405573 46 //half step A+B+, A+, A+B-, B-, A-B-, A-, A-B+, B+
gregeric 4:5b596b405573 47 const uint8_t pase_cw[8][4] = {{1, 0, 1, 0}, {1, 0, 0, 0}, {1, 0, 0, 1}, {0, 0, 0, 1}, {0, 1, 0, 1}, {0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}};
gregeric 4:5b596b405573 48 #endif
gregeric 4:5b596b405573 49
gregeric 3:96bfb8b476f8 50 /*
gregeric 7:9fc4b1be489c 51 * Firing sequence for uni-polar (5+ wires) stepper (4x open-collector drivers required).
gregeric 3:96bfb8b476f8 52 */
gregeric 7:9fc4b1be489c 53 #ifdef UNIPOLAR_STEPPER_1PH
gregeric 8:75fcbd49d49f 54 //mcu wiring to Driver/FETs A+ B+ A- B-
gregeric 7:9fc4b1be489c 55 const uint8_t pase_cw[4][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
gregeric 7:9fc4b1be489c 56 #endif
gregeric 7:9fc4b1be489c 57
gregeric 4:5b596b405573 58 #ifdef UNIPOLAR_STEPPER_2PH
gregeric 8:75fcbd49d49f 59 //mcu wiring to Driver/FETs A+ B+ A- B-
kenjiArai 0:7b0c724fa658 60 const uint8_t pase_cw[4][4] = {{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 1}, {1, 0, 0, 1}};
gregeric 3:96bfb8b476f8 61 #endif
gregeric 2:fd11d89b8ce0 62
gregeric 7:9fc4b1be489c 63 #ifdef UNIPOLAR_STEPPER_12S
gregeric 8:75fcbd49d49f 64 //mcu wiring to Driver/FETs A+ B+ A- B-
gregeric 7:9fc4b1be489c 65 const uint8_t pase_cw[8][4] = {{1, 1, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 1, 1}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}};
gregeric 7:9fc4b1be489c 66 #endif
gregeric 7:9fc4b1be489c 67
gregeric 8:75fcbd49d49f 68 //declare as pointer, not array, so that we can swap to different acc/deceleration arrays on the fly.
gregeric 8:75fcbd49d49f 69 extern uint8_t *pls_width;
kenjiArai 0:7b0c724fa658 70
kenjiArai 0:7b0c724fa658 71 STEPPER::STEPPER (PinName xp, PinName xn, PinName yp, PinName yn):
gregeric 7:9fc4b1be489c 72 _xp(xp), _xn(xn), _yp(yp), _yn(yn)
gregeric 7:9fc4b1be489c 73 {
gregeric 8:75fcbd49d49f 74 stop();
kenjiArai 0:7b0c724fa658 75 }
kenjiArai 0:7b0c724fa658 76
gregeric 7:9fc4b1be489c 77 void STEPPER::move (int32_t steps)
gregeric 7:9fc4b1be489c 78 {
gregeric 7:9fc4b1be489c 79 if (steps < 0) {
kenjiArai 0:7b0c724fa658 80 inf.direction = D_CCW;
kenjiArai 0:7b0c724fa658 81 steps = -steps;
gregeric 5:f2bbcd06019e 82 }
gregeric 5:f2bbcd06019e 83 // test for +ve, don't flip direction for stop command steps==0
gregeric 7:9fc4b1be489c 84 else if (steps > 0) {
gregeric 7:9fc4b1be489c 85 inf.direction = D_CW;
gregeric 5:f2bbcd06019e 86 }
kenjiArai 0:7b0c724fa658 87 inf.total_step = steps;
kenjiArai 0:7b0c724fa658 88 setup_mtr_drv_dt(&inf, &cntl);
kenjiArai 0:7b0c724fa658 89 }
kenjiArai 0:7b0c724fa658 90
gregeric 7:9fc4b1be489c 91 void STEPPER::set_max_speed (uint32_t time_base_us)
gregeric 7:9fc4b1be489c 92 {
gregeric 7:9fc4b1be489c 93 if (time_base_us < (MT_PLS_WIDTH_MIN * 1000)) {
kenjiArai 0:7b0c724fa658 94 time_base_us = (MT_PLS_WIDTH_MIN * 1000);
kenjiArai 0:7b0c724fa658 95 }
gregeric 8:75fcbd49d49f 96 _smdrv.detach();
kenjiArai 0:7b0c724fa658 97 _smdrv.attach_us(this, &STEPPER::millisec_inteval, time_base_us);
kenjiArai 0:7b0c724fa658 98 }
kenjiArai 0:7b0c724fa658 99
gregeric 7:9fc4b1be489c 100 uint8_t STEPPER::status (void)
gregeric 7:9fc4b1be489c 101 {
kenjiArai 0:7b0c724fa658 102 return cntl.state;
kenjiArai 0:7b0c724fa658 103 }
kenjiArai 0:7b0c724fa658 104
gregeric 8:75fcbd49d49f 105 void STEPPER::stop(void)
gregeric 7:9fc4b1be489c 106 {
kenjiArai 0:7b0c724fa658 107 busy_sm_drv = 0;
kenjiArai 0:7b0c724fa658 108 _xp = 0;
kenjiArai 0:7b0c724fa658 109 _xn = 0;
kenjiArai 0:7b0c724fa658 110 _yp = 0;
kenjiArai 0:7b0c724fa658 111 _yn = 0;
kenjiArai 0:7b0c724fa658 112 }
kenjiArai 0:7b0c724fa658 113
gregeric 8:75fcbd49d49f 114 void STEPPER::set4ports (void)
gregeric 7:9fc4b1be489c 115 {
gregeric 7:9fc4b1be489c 116 uint8_t i;
gregeric 8:75fcbd49d49f 117 cntl.motor_step+=inf.direction;
gregeric 8:75fcbd49d49f 118 i = (uint8_t)(cntl.motor_step % (sizeof(pase_cw)/4) );
gregeric 7:9fc4b1be489c 119 _xp = pase_cw[i][0];
gregeric 7:9fc4b1be489c 120 _xn = pase_cw[i][1];
gregeric 7:9fc4b1be489c 121 _yp = pase_cw[i][2];
gregeric 7:9fc4b1be489c 122 _yn = pase_cw[i][3];
kenjiArai 0:7b0c724fa658 123 }
kenjiArai 0:7b0c724fa658 124
gregeric 7:9fc4b1be489c 125 void STEPPER::setup_mtr_drv_dt(Motor_Inf *mi, Motor_Control *mt)
gregeric 7:9fc4b1be489c 126 {
kenjiArai 0:7b0c724fa658 127 busy_sm_drv = 1;
gregeric 7:9fc4b1be489c 128
gregeric 8:75fcbd49d49f 129 //0-steps command: if moving, bring to controlled stop & hold position
gregeric 7:9fc4b1be489c 130 if (mi->total_step == 0) {
gregeric 7:9fc4b1be489c 131 if (mt->state != M_STOP) {
kenjiArai 0:7b0c724fa658 132 mt->state = M_CHANGE;
kenjiArai 0:7b0c724fa658 133 mt->change_cnt = 5;
kenjiArai 0:7b0c724fa658 134 mt->pls_width = 0;
kenjiArai 0:7b0c724fa658 135 mt->ongoing = 0;
kenjiArai 0:7b0c724fa658 136 mt->up_cnt = 0;
kenjiArai 0:7b0c724fa658 137 mt->up_cnt_keep = 0;
kenjiArai 0:7b0c724fa658 138 mt->down_cnt = 0;
kenjiArai 0:7b0c724fa658 139 mt->continue_cnt = 0;
gregeric 8:75fcbd49d49f 140 } //else init(); // already stopped, interpret as: release HOLD currents, power down coils, motor freewheeling. init() now public, renamed stop()
kenjiArai 0:7b0c724fa658 141 busy_sm_drv = 0;
kenjiArai 0:7b0c724fa658 142 return;
kenjiArai 0:7b0c724fa658 143 }
gregeric 7:9fc4b1be489c 144
gregeric 4:5b596b405573 145 //already travelling full speed, new step in same direction
gregeric 7:9fc4b1be489c 146 if ((mt->state == M_CONTINUE) && ( mt->direction == mi->direction)) {
gregeric 7:9fc4b1be489c 147 if (mi->total_step < MT_SLOP_STEP) { //step len shorter than ramp down ledn
kenjiArai 0:7b0c724fa658 148 mt->up_cnt = 0;
kenjiArai 0:7b0c724fa658 149 mt->up_cnt_keep = 0;
kenjiArai 0:7b0c724fa658 150 mt->down_cnt = mi->total_step;
kenjiArai 0:7b0c724fa658 151 mt->continue_cnt = 0;
kenjiArai 0:7b0c724fa658 152 mt->state = M_DOWN;
kenjiArai 0:7b0c724fa658 153 mt->ongoing = 0;
kenjiArai 0:7b0c724fa658 154 } else {
kenjiArai 0:7b0c724fa658 155 mt->up_cnt = 0;
kenjiArai 0:7b0c724fa658 156 mt->up_cnt_keep = 0;
kenjiArai 0:7b0c724fa658 157 mt->down_cnt = MT_SLOP_STEP -1;
kenjiArai 0:7b0c724fa658 158 mt->continue_cnt = mi->total_step - (MT_SLOP_STEP - 1);
kenjiArai 0:7b0c724fa658 159 }
gregeric 7:9fc4b1be489c 160 } else {
gregeric 7:9fc4b1be489c 161 //already moving, reverse direction required
gregeric 7:9fc4b1be489c 162 if ((mt->state == M_CONTINUE) && ( mt->direction != mi->direction)) {
kenjiArai 0:7b0c724fa658 163 mt->state = M_CHANGE;
kenjiArai 0:7b0c724fa658 164 mt->change_cnt = 5;
gregeric 4:5b596b405573 165 } else {//motor was at rest?
gregeric 4:5b596b405573 166 //mt->motor_step = 0; // don't destroy knowledge of current phase
gregeric 7:9fc4b1be489c 167 mt->state = M_UP;
kenjiArai 0:7b0c724fa658 168 }
kenjiArai 0:7b0c724fa658 169 mt->pls_width = 0;
gregeric 7:9fc4b1be489c 170 mt->ongoing = 0;
kenjiArai 0:7b0c724fa658 171 mt->direction = mi->direction;
gregeric 7:9fc4b1be489c 172 if (mi->total_step < MT_MIN_STEP) {
gregeric 7:9fc4b1be489c 173 if (mi->total_step == MT_MIN_STEP - 1) {
kenjiArai 0:7b0c724fa658 174 mt->up_cnt = MT_SLOP_STEP;
kenjiArai 0:7b0c724fa658 175 } else {
kenjiArai 0:7b0c724fa658 176 mt->up_cnt = mi->total_step / 2;
gregeric 4:5b596b405573 177 if (mt->up_cnt==0) mt->up_cnt=1;
kenjiArai 0:7b0c724fa658 178 }
kenjiArai 0:7b0c724fa658 179 mt->up_cnt_keep = mt->up_cnt;
kenjiArai 0:7b0c724fa658 180 mt->down_cnt = mi->total_step - mt->up_cnt;
kenjiArai 0:7b0c724fa658 181 mt->continue_cnt = 0;
kenjiArai 0:7b0c724fa658 182 } else {
kenjiArai 0:7b0c724fa658 183 mt->up_cnt = MT_SLOP_STEP;
kenjiArai 0:7b0c724fa658 184 mt->up_cnt_keep = mt->up_cnt;
kenjiArai 0:7b0c724fa658 185 mt->down_cnt = MT_SLOP_STEP -1 ;
kenjiArai 0:7b0c724fa658 186 mt->continue_cnt = mi->total_step - MT_SLOP_STEP - (MT_SLOP_STEP - 1);
kenjiArai 0:7b0c724fa658 187 }
kenjiArai 0:7b0c724fa658 188 }
kenjiArai 0:7b0c724fa658 189 busy_sm_drv = 0;
kenjiArai 0:7b0c724fa658 190 }
kenjiArai 0:7b0c724fa658 191
gregeric 7:9fc4b1be489c 192 void STEPPER::millisec_inteval()
gregeric 7:9fc4b1be489c 193 {
gregeric 7:9fc4b1be489c 194 if (busy_sm_drv == 1) {
gregeric 7:9fc4b1be489c 195 return;
gregeric 7:9fc4b1be489c 196 }
gregeric 7:9fc4b1be489c 197 switch (cntl.state) {
gregeric 7:9fc4b1be489c 198 case M_STOP:
gregeric 7:9fc4b1be489c 199 /*
gregeric 8:75fcbd49d49f 200 * no move, but continue to energize coils: hold current position
gregeric 7:9fc4b1be489c 201 */
gregeric 7:9fc4b1be489c 202 break;
gregeric 7:9fc4b1be489c 203 case M_UP:
gregeric 7:9fc4b1be489c 204 if (cntl.ongoing) {
gregeric 7:9fc4b1be489c 205 if (--cntl.pls_width == 0) {
gregeric 7:9fc4b1be489c 206 if (--cntl.up_cnt == 0) {
gregeric 7:9fc4b1be489c 207 cntl.ongoing = 0;
gregeric 7:9fc4b1be489c 208 if (cntl.continue_cnt == 0) {
gregeric 7:9fc4b1be489c 209 cntl.state = M_DOWN;
gregeric 7:9fc4b1be489c 210 if (cntl.down_cnt==0) cntl.state=M_STOP;
gregeric 7:9fc4b1be489c 211 } else {
gregeric 7:9fc4b1be489c 212 cntl.state = M_CONTINUE;
gregeric 7:9fc4b1be489c 213 }
kenjiArai 0:7b0c724fa658 214 } else {
gregeric 8:75fcbd49d49f 215 set4ports();
gregeric 7:9fc4b1be489c 216 cntl.pls_width = pls_width[cntl.up_cnt_keep - cntl.up_cnt];
kenjiArai 0:7b0c724fa658 217 }
kenjiArai 0:7b0c724fa658 218 } else {
gregeric 7:9fc4b1be489c 219 break;
kenjiArai 0:7b0c724fa658 220 }
gregeric 7:9fc4b1be489c 221 } else { // 1st entry from M_STOP
gregeric 8:75fcbd49d49f 222 set4ports();
gregeric 7:9fc4b1be489c 223 cntl.pls_width = pls_width[cntl.up_cnt_keep - cntl.up_cnt];
gregeric 7:9fc4b1be489c 224 cntl.ongoing = 1;
kenjiArai 0:7b0c724fa658 225 }
gregeric 7:9fc4b1be489c 226 break;
gregeric 7:9fc4b1be489c 227 case M_CONTINUE:
gregeric 8:75fcbd49d49f 228 set4ports();
gregeric 7:9fc4b1be489c 229 if (--cntl.continue_cnt == 0) {
gregeric 7:9fc4b1be489c 230 cntl.ongoing = 0;
gregeric 7:9fc4b1be489c 231 cntl.state = M_DOWN;
kenjiArai 0:7b0c724fa658 232 }
gregeric 7:9fc4b1be489c 233 break;
gregeric 7:9fc4b1be489c 234 case M_DOWN:
gregeric 7:9fc4b1be489c 235 if (cntl.ongoing) {
gregeric 7:9fc4b1be489c 236 if (--cntl.pls_width == 0) {
gregeric 7:9fc4b1be489c 237 if (--cntl.down_cnt == 0) {
gregeric 7:9fc4b1be489c 238 cntl.ongoing = 0;
kenjiArai 0:7b0c724fa658 239 cntl.state = M_STOP;
kenjiArai 0:7b0c724fa658 240 } else {
gregeric 8:75fcbd49d49f 241 set4ports();
gregeric 7:9fc4b1be489c 242 cntl.pls_width = pls_width[cntl.down_cnt];
kenjiArai 0:7b0c724fa658 243 }
gregeric 7:9fc4b1be489c 244 } else {
gregeric 7:9fc4b1be489c 245 break;
kenjiArai 0:7b0c724fa658 246 }
gregeric 7:9fc4b1be489c 247 } else { // 1st entry from M_UP or M_CONTINUE
gregeric 8:75fcbd49d49f 248 set4ports();
gregeric 7:9fc4b1be489c 249 cntl.pls_width = pls_width[cntl.down_cnt];
gregeric 7:9fc4b1be489c 250 cntl.ongoing = 1;
kenjiArai 0:7b0c724fa658 251 }
gregeric 7:9fc4b1be489c 252 break;
gregeric 7:9fc4b1be489c 253 case M_CHANGE:
gregeric 7:9fc4b1be489c 254 if (cntl.ongoing) {
gregeric 7:9fc4b1be489c 255 if (--cntl.pls_width == 0) {
gregeric 7:9fc4b1be489c 256 if (--cntl.change_cnt == 0) {
gregeric 7:9fc4b1be489c 257 cntl.ongoing = 0;
gregeric 7:9fc4b1be489c 258 if (cntl.up_cnt == 0) {
gregeric 7:9fc4b1be489c 259 cntl.state = M_STOP;
gregeric 7:9fc4b1be489c 260 } else {
gregeric 7:9fc4b1be489c 261 cntl.state = M_UP;
gregeric 7:9fc4b1be489c 262 }
gregeric 7:9fc4b1be489c 263 } else {
gregeric 8:75fcbd49d49f 264 set4ports();
gregeric 7:9fc4b1be489c 265 cntl.pls_width = pls_width[cntl.change_cnt];
gregeric 7:9fc4b1be489c 266 }
gregeric 7:9fc4b1be489c 267 } else {
gregeric 7:9fc4b1be489c 268 break;
gregeric 7:9fc4b1be489c 269 }
gregeric 7:9fc4b1be489c 270 } else { // 1st entry
gregeric 8:75fcbd49d49f 271 set4ports();
gregeric 7:9fc4b1be489c 272 cntl.pls_width = pls_width[cntl.change_cnt];
gregeric 7:9fc4b1be489c 273 cntl.ongoing = 1;
gregeric 7:9fc4b1be489c 274 }
gregeric 7:9fc4b1be489c 275 break;
gregeric 7:9fc4b1be489c 276 default :
gregeric 7:9fc4b1be489c 277 cntl.state = M_STOP;
kenjiArai 0:7b0c724fa658 278 }
kenjiArai 0:7b0c724fa658 279 }