-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrivers.cpp
More file actions
648 lines (504 loc) · 15.9 KB
/
Drivers.cpp
File metadata and controls
648 lines (504 loc) · 15.9 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
RoboCore Drivers Library
(v1.0 - 25/04/2013)
Driver classes for Arduino
(tested with Arduino 1.0.1)
Copyright 2013 RoboCore (François) ( http://www.RoboCore.net )
------------------------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------------
Supports:
- Pololu A4988 driver
NOTES:
# the Pololu A4988 driver can be
automatically disabled/put to
sleep when motor is not running
and enabled/waken up when driving
(setDriveMode() method)
*/
#include "Drivers.h"
//-------------------------------------------------------------------------------------------------
//----------------------------------- A4988 -------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// Default constructor
A4988::A4988(void){
_initialized = false;
}
// Constructor
A4988::A4988(int pinDIR, int pinSTEP){
Initialize(pinDIR, pinSTEP);
}
// Constructor
A4988::A4988(int pinDIR, int pinSTEP, int pinMS1, int pinMS2, int pinMS3){
Initialize(pinDIR, pinSTEP, pinMS1, pinMS2, pinMS3);
}
//-------------------------------------------------------------------------------------------------
// Disable the driver
void A4988::Disable(void){
//check if initialized
if(!_initialized)
return;
if(_use_ENABLE & 0x01){ //pin set
_use_ENABLE &= 0xFB; //reset bit 2
digitalWrite(_pinENABLE, HIGH);
}
}
//-------------------------------------------------------------------------------------------------
// Drive by step
// NOTE: if set to QUARTER mode (for example), will drive real 25 steps (100 quarters of step)
// NOTE: use negative steps to invert the direction
// NOTE: the method does not return until it has finished driving
void A4988::Drive(long steps){
//check if initialized
if(!_initialized)
return;
//check whether to enable or wake up
if(_use_ENABLE & 0x02)
Enable();
else if(_use_SLEEP & 0x02){
WakeUp();
delayMicroseconds(1000); //1 ms delay before returning to normal operation
}
//check number of steps
if(steps == 0)
return;
else if(steps < 0)
_dir != _dir;
//use absolute values
if(steps < 0)
steps = -1 * steps;
byte factor = getFactor();
unsigned long period = 60 * 1000000 / ((unsigned long)factor * _steps * _speed);
if(period < 2)
period = 2;
//drive
for(int i=0 ; i < steps ; i++){
digitalWrite(_pinSTEP, HIGH);
delayMicroseconds(period / 2);
digitalWrite(_pinSTEP, LOW);
delayMicroseconds(period / 2);
}
//check whether to disable or sleep
if(_use_ENABLE & 0x02)
Disable();
else if(_use_SLEEP & 0x02)
Sleep();
}
//-------------------------------------------------------------------------------------------------
// Drive by step with a given frequency [Hz]
// NOTE: if set to QUARTER mode (for example), will drive real 25 steps (100 quarters of step)
// NOTE: use negative steps to invert the direction
// NOTE: the method does not return until it has finished driving
void A4988::Drive(long steps, unsigned int frequency){
//check if initialized
if(!_initialized)
return;
//check whether to enable or wake up
if(_use_ENABLE & 0x02)
Enable();
else if(_use_SLEEP & 0x02){
WakeUp();
delayMicroseconds(1000); //1 ms delay before returning to normal operation
}
//check number of steps
if(steps == 0)
return;
else if(steps < 0)
_dir != _dir;
//use absolute values
if(steps < 0)
steps = -1 * steps;
byte factor = getFactor();
unsigned long period = 1000000 / frequency;
//drive
for(int i=0 ; i < steps ; i++){
digitalWrite(_pinSTEP, HIGH);
delayMicroseconds(period / 2);
digitalWrite(_pinSTEP, LOW);
delayMicroseconds(period / 2);
}
//check whether to disable or sleep
if(_use_ENABLE & 0x02)
Disable();
else if(_use_SLEEP & 0x02)
Sleep();
}
//-------------------------------------------------------------------------------------------------
// Drive by revolution
// NOTE: use negative revolutions to invert the direction
// NOTE: the method does not return until it has finished driving
void A4988::DriveRevolutions(int revolutions){
//check if initialized
if(!_initialized)
return;
byte factor = getFactor();
Drive(factor * _steps * revolutions);
}
//-------------------------------------------------------------------------------------------------
// Enable the driver
void A4988::Enable(void){
//check if initialized
if(!_initialized)
return;
if(_use_ENABLE & 0x01){ //pin set
_use_ENABLE |= 0x04; //set bit 2
digitalWrite(_pinENABLE, LOW);
}
}
//-------------------------------------------------------------------------------------------------
// Get the current direction
// (returns -1 if not initialized)
int A4988::getDir(void){
//check if initialized
if(!_initialized)
return -1;
return _dir;
}
//-------------------------------------------------------------------------------------------------
// Get the current direction
// (returns -1 if not initialized)
int A4988::getDriveMode(void){
//check if initialized
if(!_initialized)
return -1;
int mode = A4988_DRIVE_MODE_NONE;
if(_use_ENABLE & 0x02){
mode = A4988_DRIVE_MODE_ENABLE;
} else if(_use_SLEEP & 0x02){
mode = A4988_DRIVE_MODE_SLEEP;
}
return mode;
}
//-------------------------------------------------------------------------------------------------
// Get the factor for the mode
// (returns 0 if not initialized)
byte A4988::getFactor(void){
//check if initialized
if(!_initialized)
return 0;
byte factor = 1; // A4988_FULL_STEP
switch(_mode){
case A4988_HALF_STEP:
factor = 2;
break;
case A4988_QUARTER_STEP:
factor = 4;
break;
case A4988_EIGHTH_STEP:
factor = 8;
break;
case A4988_SIXTEENTH_STEP:
factor = 16;
break;
}
return factor;
}
//-------------------------------------------------------------------------------------------------
// Get the current mode
// (returns -1 if not initialized)
int A4988::getMode(void){
//check if initialized
if(!_initialized)
return -1;
return _mode;
}
//-------------------------------------------------------------------------------------------------
// Get the current speed
// (returns -1 if not initialized)
int A4988::getSpeed(void){
//check if initialized
if(!_initialized)
return -1;
return _speed;
}
//-------------------------------------------------------------------------------------------------
// Get the current mode
// (returns 0 if not initialized)
unsigned int A4988::getSteps(void){
//check if initialized
if(!_initialized)
return 0;
return _steps;
}
//-------------------------------------------------------------------------------------------------
// Initialize the driver
// (returns TRUE if initialized)
boolean A4988::Initialize(int pinDIR, int pinSTEP){
//check if initialized
if(_initialized)
return true;
if(pinDIR != pinSTEP){
_initialized = true; //set
_pinDIR = (byte)pinDIR;
_pinSTEP = (byte)pinSTEP;
pinMode(_pinDIR, OUTPUT);
pinMode(_pinSTEP, OUTPUT);
_dir = A4988_DIR_CW; //default
_mode = A4988_FULL_STEP; //default
_steps = A4988_DEFAULT_STEPS;
setSpeedRPM(A4988_DEFAULT_SPEED_RPM);
_mode_pins_set = false;
_reset_pin_set = false;
_use_ENABLE = 0;
_use_SLEEP = 0;
setDriveMode(A4988_DEFAULT_DRIVE_MODE);
digitalWrite(_pinDIR, _dir);
digitalWrite(_pinSTEP, LOW);
} else {
_initialized = false;
}
return _initialized;
}
//-------------------------------------------------------------------------------------------------
// Initialize the driver
// (returns TRUE if initialized)
boolean A4988::Initialize(int pinDIR, int pinSTEP, int pinMS1, int pinMS2, int pinMS3){
//check if initialized
if(_initialized)
return true;
if(pinDIR != pinSTEP){
_initialized = true; //set
_pinDIR = (byte)pinDIR;
_pinSTEP = (byte)pinSTEP;
pinMode(_pinDIR, OUTPUT);
pinMode(_pinSTEP, OUTPUT);
_dir = A4988_DIR_CW; //default
_mode = A4988_FULL_STEP; //default
_steps = A4988_DEFAULT_STEPS;
setSpeedRPM(A4988_DEFAULT_SPEED_RPM);
if(setMSpins(pinMS2, pinMS2, pinMS3))
_mode_pins_set = true;
else
_mode_pins_set = false;
_reset_pin_set = false;
_use_ENABLE = 0;
_use_SLEEP = 0;
setDriveMode(A4988_DEFAULT_DRIVE_MODE);
digitalWrite(_pinDIR, _dir);
digitalWrite(_pinSTEP, LOW);
} else {
_initialized = false;
}
return _initialized;
}
//-------------------------------------------------------------------------------------------------
// Invert the direction
void A4988::InvertDirection(void){
//check if initialized
if(!_initialized)
return;
if(_dir == A4988_DIR_CW)
_dir = A4988_DIR_CCW;
else
_dir = A4988_DIR_CW;
digitalWrite(_pinDIR, _dir);
}
//-------------------------------------------------------------------------------------------------
// Check if driver is enabled
// (returns TRUE when enabled)
// NOTE: also returns FALSE when not initialized
boolean A4988::isEnabled(void){
//check if initialized
if(!_initialized)
return false;
return (_use_ENABLE & 0x04);
}
//-------------------------------------------------------------------------------------------------
// Check if driver is sleeping
// (returns TRUE when sleeping)
// NOTE: also returns FALSE when not initialized
boolean A4988::isSleeping(void){
//check if initialized
if(!_initialized)
return false;
return (_use_SLEEP & 0x04);
}
//-------------------------------------------------------------------------------------------------
// Get if mode pins are set
boolean A4988::ModePinsSet(void){
//check if initialized
if(!_initialized)
return false;
return _mode_pins_set;
}
//-------------------------------------------------------------------------------------------------
// Send a 100 ms reset pulse
void A4988::Reset(void){
//check if initialized
if(!_initialized)
return;
if(_reset_pin_set){
digitalWrite(_pinRST, LOW);
delay(100);
digitalWrite(_pinRST, HIGH);
}
}
//-------------------------------------------------------------------------------------------------
// Set the direction
void A4988::setDir(int dir){
//check if initialized
if(!_initialized)
return;
if(dir > 0)
_dir = A4988_DIR_CCW;
else
_dir = A4988_DIR_CW;
digitalWrite(_pinDIR, _dir);
}
//-------------------------------------------------------------------------------------------------
// Set the drive mode
void A4988::setDriveMode(byte mode){
//check if initialized
if(!_initialized)
return;
if(mode == A4988_DRIVE_MODE_ENABLE){
_use_ENABLE |= 0x02; //reset bit 1
_use_SLEEP &= 0xFD; //set bit 1
} else if(mode == A4988_DRIVE_MODE_SLEEP){
_use_ENABLE &= 0xFD; //reset bit 1
_use_SLEEP |= 0x02; //set bit 1
} else { //NONE
_use_ENABLE &= 0xFD; //reset bit 1
_use_SLEEP &= 0xFD; //reset bit 1
}
}
//-------------------------------------------------------------------------------------------------
// Set the enable pin
// NOTE: enables the driver when called
void A4988::setENABLEpin(int pinENABLE){
//check if initialized
if(!_initialized)
return;
_pinENABLE = (byte)pinENABLE;
_use_ENABLE |= 0x01; //bit 0
Enable(); //default state
}
//-------------------------------------------------------------------------------------------------
// Set the mode (full, half, quarter, eighth or sixteenth)
// (returns -1 if not initialized, -2 if no pins set, 2 if set do A4988_FULL_STEP, 1 if other mode set)
// NOTE: sets to FULL if no valid mode was given
int A4988::setMode(byte mode){
//check if initialized
if(!_initialized)
return -1;
//check if pins set
if(!_mode_pins_set)
return -2;
int res;
if((mode != A4988_HALF_STEP) && (mode != A4988_QUARTER_STEP) && (mode != A4988_EIGHTH_STEP) && (mode != A4988_SIXTEENTH_STEP)){ // use default
digitalWrite(_pinsMODE[0], (A4988_FULL_STEP & 4));
digitalWrite(_pinsMODE[1], (A4988_FULL_STEP & 2));
digitalWrite(_pinsMODE[2], (A4988_FULL_STEP & 1));
_mode = A4988_FULL_STEP;
res = 2;
} else { // use mode
digitalWrite(_pinsMODE[0], (mode & 4));
digitalWrite(_pinsMODE[1], (mode & 2));
digitalWrite(_pinsMODE[2], (mode & 1));
_mode = mode;
res = 1;
}
return res;
}
//-------------------------------------------------------------------------------------------------
// Set the mode pins
// (returns -1 if not initialized, -2 if MS pin is same as DIR pin, -3 if MS pin is same as STEP pin)
int A4988::setMSpins(int pinMS1, int pinMS2, int pinMS3){
//check if initialized
if(!_initialized)
return -1;
int res = 1;
if((pinMS1 != _pinDIR) && (pinMS2 != _pinDIR) && (pinMS3 != _pinDIR)){
if((pinMS1 != _pinSTEP) && (pinMS2 != _pinSTEP) && (pinMS3 != _pinSTEP)){
//store pins
_pinsMODE[0] = pinMS1;
_pinsMODE[1] = pinMS2;
_pinsMODE[2] = pinMS3;
//configure pins
pinMode(pinMS1, OUTPUT);
pinMode(pinMS2, OUTPUT);
pinMode(pinMS3, OUTPUT);
setMode(_mode); //update
} else {
res = -3;
}
} else {
res = -2;
}
return res;
}
//-------------------------------------------------------------------------------------------------
// Set the reset pin
void A4988::setRSTpin(int pinRST){
//check if initialized
if(!_initialized)
return;
_pinRST = (byte)pinRST;
_reset_pin_set = true;
}
//-------------------------------------------------------------------------------------------------
// Set the sleep pin
// NOTE: sets the driver on normal operation when called
void A4988::setSLEEPpin(int pinSLEEP){
//check if initialized
if(!_initialized)
return;
_pinSLEEP = (byte)pinSLEEP;
_use_SLEEP |= 0x01; //bit 0
WakeUp(); //default state
}
//-------------------------------------------------------------------------------------------------
// Set the speed in [rpm]
void A4988::setSpeedRPM(int rpm){
//check if initialized
if(!_initialized)
return;
if(rpm == 0)
return;
else if(rpm < 0)
_speed = -1 * rpm;
else
_speed = rpm;
}
//-------------------------------------------------------------------------------------------------
// Set the number of steps in one revolution
// NOTE: minimum of 4 steps
void A4988::setSteps(unsigned int steps){
//check if initialized
if(!_initialized)
return;
if(steps >= 4)
_steps = steps;
}
//-------------------------------------------------------------------------------------------------
// Put the driver on sleep mode
void A4988::Sleep(void){
//check if initialized
if(!_initialized)
return;
if(_use_SLEEP & 0x01){ //pin set
_use_SLEEP |= 0x04; //set bit 2
digitalWrite(_pinSLEEP, LOW);
}
}
//-------------------------------------------------------------------------------------------------
// Wake the driver up (normal operation)
void A4988::WakeUp(void){
//check if initialized
if(!_initialized)
return;
if(_use_SLEEP & 0x01){ //pin set
_use_SLEEP &= 0xFB; //reset bit 2
digitalWrite(_pinSLEEP, HIGH);
}
}
//-------------------------------------------------------------------------------------------------