forked from tyhenry/CheapStepper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheapStepper.cpp
312 lines (247 loc) · 6.14 KB
/
CheapStepper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* CheapStepper.cpp -
v0.2
Library for the 28BYJ-48 stepper motor, using ULN2003 driver board
https://arduino-info.wikispaces.com/SmallSteppers
Library written by Tyler Henry, 6/2016
uses 8-step sequence: A-AB-B-BC-C-CD-D-DA
motor has gear ratio of either:
64:1 (per manufacturer specs)
or
63.68395:1 measured
(see: http://forum.arduino.cc/index.php?topic=71964.15)
* 64 steps per internal motor rev
=
4096 total mini-steps / revolution
or ~4076 (4075.7728) depending on exact gear ratio
assumes 5v power source for rpm calc
*/
#include "Arduino.h"
#include "CheapStepper.h"
#if defined(ESP8266) || defined(ARDUINO_ESP8266_NODEMCU)
#define YIELD_TIME 1000 // Source https://www.sigmdel.ca/michel/program/esp8266/arduino/watchdogs_en.html#ESP8266_WDT_TIMEOUT
#else
#define YIELD_TIME (0UL - 1UL) // Large number to only call it rarely
#endif
CheapStepper::CheapStepper () : pins({8,9,10,11}) {
for (int pin=0; pin<4; pin++){
pinMode(pins[pin], OUTPUT);
}
}
CheapStepper::CheapStepper (int in1, int in2, int in3, int in4) : pins({in1,in2,in3,in4}) {
for (int pin=0; pin<4; pin++){
pinMode(pins[pin], OUTPUT);
}
}
void CheapStepper::setRpm (int rpm){
delay = calcDelay(rpm);
}
void CheapStepper::move (bool clockwise, int numSteps){
for (int n=0; n<numSteps; n++){
step(clockwise);
}
}
void CheapStepper::moveTo (bool clockwise, int toStep){
// keep to 0-(totalSteps-1) range
if (toStep >= totalSteps) toStep %= totalSteps;
else if (toStep < 0) {
toStep %= totalSteps; // returns negative if toStep not multiple of totalSteps
if (toStep < 0) toStep += totalSteps; // shift into 0-(totalSteps-1) range
}
while (stepN != toStep){
step(clockwise);
}
}
void CheapStepper::moveDegrees (bool clockwise, int deg){
int nSteps = (unsigned long) deg * totalSteps / 360;
move(clockwise, nSteps);
}
void CheapStepper::moveToDegree (bool clockwise, int deg){
// keep to 0-359 range
if (deg >= 360) deg %= 360;
else if (deg < 0) {
deg %= 360; // returns negative if deg not multiple of 360
if (deg < 0) deg += 360; // shift into 0-359 range
}
int toStep = deg * totalSteps / 360;
moveTo (clockwise, toStep);
}
// NON-BLOCKING MOVES
void CheapStepper::newMove (bool clockwise, int numSteps){
// numSteps sign ignored
// stepsLeft signed positive if clockwise, neg if ccw
if (clockwise) stepsLeft = abs(numSteps);
else stepsLeft = -1 * abs(numSteps);
lastStepTime = micros();
}
void CheapStepper::newMoveTo (bool clockwise, int toStep){
// keep toStep in 0-(totalSteps-1) range
if (toStep >= totalSteps) toStep %= totalSteps;
else if (toStep < 0) {
toStep %= totalSteps; // returns negative if toStep not multiple of totalSteps
if (toStep < 0) toStep += totalSteps; // shift into 0-(totalSteps-1) range
}
if (clockwise) stepsLeft = abs(toStep - stepN);
// clockwise: simple diff, always pos
else stepsLeft = -1*(totalSteps - abs(toStep - stepN));
// counter-clockwise: totalSteps - diff, made neg
lastStepTime = micros();
}
void CheapStepper::newMoveDegrees (bool clockwise, int deg){
int nSteps = (unsigned long) deg * totalSteps / 360;
newMove (clockwise, nSteps);
}
void CheapStepper::newMoveToDegree (bool clockwise, int deg){
// keep to 0-359 range
if (deg >= 360) deg %= 360;
else if (deg < 0) {
deg %= 360; // returns negative if deg not multiple of 360
if (deg < 0) deg += 360; // shift into 0-359 range
}
int toStep = deg * totalSteps / 360;
newMoveTo (clockwise, toStep);
}
void CheapStepper::run(){
if (micros() - lastStepTime >= delay) { // if time for step
if (stepsLeft > 0) { // clockwise
stepCW();
stepsLeft--;
} else if (stepsLeft < 0){ // counter-clockwise
stepCCW();
stepsLeft++;
}
lastStepTime = micros();
}
}
void CheapStepper::stop(){
stepsLeft = 0;
}
void CheapStepper::step(bool clockwise){
if (clockwise) seqCW();
else seqCCW();
// return control to the system between the steps so that long series of those
// doesn't trigger watch dog restart on ESP
if (millis() - lastYieldTime >= YIELD_TIME)
{
yield();
lastYieldTime = millis();
}
}
void CheapStepper::off() {
for (int p=0; p<4; p++)
digitalWrite(pins[p], 0);
}
/////////////
// PRIVATE //
/////////////
int CheapStepper::calcDelay (int rpm){
if (rpm < 6) return delay; // will overheat, no change
else if (rpm >= 24) return 600; // highest speed
unsigned long d = 60000000 / (totalSteps* (unsigned long) rpm);
// in range: 600-1465 microseconds (24-1 rpm)
return (int) d;
}
int CheapStepper::calcRpm (int _delay){
unsigned long rpm = 60000000 / (unsigned long) _delay / totalSteps;
return (int) rpm;
}
void CheapStepper::seqCW (){
seqN++;
if (seqN > 7) seqN = 0; // roll over to A seq
seq(seqN);
stepN++; // track miniSteps
if (stepN >= totalSteps){
stepN -=totalSteps; // keep stepN within 0-(totalSteps-1)
}
}
void CheapStepper::seqCCW (){
seqN--;
if (seqN < 0) seqN = 7; // roll over to DA seq
seq(seqN);
stepN--; // track miniSteps
if (stepN < 0){
stepN +=totalSteps; // keep stepN within 0-(totalSteps-1)
}
}
void CheapStepper::seq (int seqNum){
int pattern[4];
// A,B,C,D HIGH/LOW pattern to write to driver board
switch(seqNum){
case 0:
{
pattern[0] = 1;
pattern[1] = 0;
pattern[2] = 0;
pattern[3] = 0;
break;
}
case 1:
{
pattern[0] = 1;
pattern[1] = 1;
pattern[2] = 0;
pattern[3] = 0;
break;
}
case 2:
{
pattern[0] = 0;
pattern[1] = 1;
pattern[2] = 0;
pattern[3] = 0;
break;
}
case 3:
{
pattern[0] = 0;
pattern[1] = 1;
pattern[2] = 1;
pattern[3] = 0;
break;
}
case 4:
{
pattern[0] = 0;
pattern[1] = 0;
pattern[2] = 1;
pattern[3] = 0;
break;
}
case 5:
{
pattern[0] = 0;
pattern[1] = 0;
pattern[2] = 1;
pattern[3] = 1;
break;
}
case 6:
{
pattern[0] = 0;
pattern[1] = 0;
pattern[2] = 0;
pattern[3] = 1;
break;
}
case 7:
{
pattern[0] = 1;
pattern[1] = 0;
pattern[2] = 0;
pattern[3] = 1;
break;
}
default:
{
pattern[0] = 0;
pattern[1] = 0;
pattern[2] = 0;
pattern[3] = 0;
break;
}
}
// write pattern to pins
for (int p=0; p<4; p++){
digitalWrite(pins[p], pattern[p]);
}
delayMicroseconds(delay);
}