forked from MarginallyClever/Makelangelo-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AFMotorDrawbot.cpp
201 lines (153 loc) · 5.41 KB
/
AFMotorDrawbot.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
//------------------------------------------------------------------------------
// Adafruit Motor shield library, modified for Drawbot
// copyright Dan Royer, 2012
// this code is public domain, enjoy!
//------------------------------------------------------------------------------
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "AFMotorDrawbot.h"
//------------------------------------------------------------------------------
static uint8_t latch_state;
static AFMotorController MC;
//------------------------------------------------------------------------------
AFMotorController::AFMotorController() {}
void AFMotorController::enable() {
// setup the latch
/*
LATCH_DDR |= _BV(LATCH);
ENABLE_DDR |= _BV(ENABLE);
CLK_DDR |= _BV(CLK);
SER_DDR |= _BV(SER);
*/
pinMode(MOTORLATCH, OUTPUT);
pinMode(MOTORENABLE, OUTPUT);
pinMode(MOTORDATA, OUTPUT);
pinMode(MOTORCLK, OUTPUT);
latch_state = 0;
latch_tx(); // "reset"
//ENABLE_PORT &= ~_BV(ENABLE); // enable the chip outputs!
digitalWrite(MOTORENABLE, LOW);
}
void AFMotorController::latch_tx() {
uint8_t i;
//LATCH_PORT &= ~_BV(LATCH);
digitalWrite(MOTORLATCH, LOW);
//SER_PORT &= ~_BV(SER);
digitalWrite(MOTORDATA, LOW);
/*
for (i=0; i<8; i++) {
//CLK_PORT &= ~_BV(CLK);
digitalWrite(MOTORCLK, LOW);
if (latch_state & _BV(7-i)) {
//SER_PORT |= _BV(SER);
digitalWrite(MOTORDATA, HIGH);
} else {
//SER_PORT &= ~_BV(SER);
digitalWrite(MOTORDATA, LOW);
}
//CLK_PORT |= _BV(CLK);
digitalWrite(MOTORCLK, HIGH);
}
*/
digitalWrite(MOTORCLK, LOW); digitalWrite(MOTORDATA, HIGH * ( (latch_state & _BV(7)) >> (7) ) ); digitalWrite(MOTORCLK, HIGH);
digitalWrite(MOTORCLK, LOW); digitalWrite(MOTORDATA, HIGH * ( (latch_state & _BV(6)) >> (6) ) ); digitalWrite(MOTORCLK, HIGH);
digitalWrite(MOTORCLK, LOW); digitalWrite(MOTORDATA, HIGH * ( (latch_state & _BV(5)) >> (5) ) ); digitalWrite(MOTORCLK, HIGH);
digitalWrite(MOTORCLK, LOW); digitalWrite(MOTORDATA, HIGH * ( (latch_state & _BV(4)) >> (4) ) ); digitalWrite(MOTORCLK, HIGH);
digitalWrite(MOTORCLK, LOW); digitalWrite(MOTORDATA, HIGH * ( (latch_state & _BV(3)) >> (3) ) ); digitalWrite(MOTORCLK, HIGH);
digitalWrite(MOTORCLK, LOW); digitalWrite(MOTORDATA, HIGH * ( (latch_state & _BV(2)) >> (2) ) ); digitalWrite(MOTORCLK, HIGH);
digitalWrite(MOTORCLK, LOW); digitalWrite(MOTORDATA, HIGH * ( (latch_state & _BV(1)) >> (1) ) ); digitalWrite(MOTORCLK, HIGH);
digitalWrite(MOTORCLK, LOW); digitalWrite(MOTORDATA, HIGH * ( (latch_state & _BV(0)) >> (0) ) ); digitalWrite(MOTORCLK, HIGH);
//LATCH_PORT |= _BV(LATCH);
digitalWrite(MOTORLATCH, HIGH);
}
//------------------------------------------------------------------------------
// STEPPERS
//------------------------------------------------------------------------------
AF_Stepper::AF_Stepper(uint16_t steps, uint8_t num) {
MC.enable();
revsteps = steps;
steppernum = num;
currentstep = 0;
if (steppernum == 1) {
latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B) &
~_BV(MOTOR2_A) & ~_BV(MOTOR2_B); // all motor pins to 0
MC.latch_tx();
// enable both H bridges
pinMode(11, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(11, HIGH);
digitalWrite(3, HIGH);
a = _BV(MOTOR1_A);
b = _BV(MOTOR2_A);
c = _BV(MOTOR1_B);
d = _BV(MOTOR2_B);
} else if (steppernum == 2) {
latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B) &
~_BV(MOTOR4_A) & ~_BV(MOTOR4_B); // all motor pins to 0
MC.latch_tx();
// enable both H bridges
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
a = _BV(MOTOR3_A);
b = _BV(MOTOR4_A);
c = _BV(MOTOR3_B);
d = _BV(MOTOR4_B);
}
}
void AF_Stepper::setSpeed(uint16_t rpm) {
usperstep = 60000000 / ((uint32_t)revsteps * (uint32_t)rpm);
steppingcounter = 0;
}
void AF_Stepper::release() {
// release all
latch_state &= ~a & ~b & ~c & ~d; // all motor pins to 0
MC.latch_tx();
}
void AF_Stepper::step(uint16_t steps, uint8_t dir) {
uint32_t uspers = usperstep;
while (steps--) {
onestep(dir);
/*
delay(uspers/1000); // in ms
steppingcounter += (uspers % 1000);
if (steppingcounter >= 1000) {
delay(1);
steppingcounter -= 1000;
}
//*/
delayMicroseconds(uspers);
}
}
void AF_Stepper::onestep(uint8_t dir) {
if (dir == FORWARD) {
currentstep++;
} else {
// BACKWARDS
currentstep--;
}
currentstep += MICROSTEPS*4;
currentstep %= MICROSTEPS*4;
#ifdef MOTORDEBUG
Serial.print("current step: "); Serial.println(currentstep, DEC);
#endif
// set all of this motor's pins to 0 (don't smash other motor)
latch_state &= ~a & ~b & ~c & ~d;
// No wait! Keep some energized.
switch (currentstep/(MICROSTEPS/2)) {
case 0: latch_state |= a; break; // energize coil 1 only
case 1: latch_state |= a | b; break; // energize coil 1+2
case 2: latch_state |= b; break; // energize coil 2 only
case 3: latch_state |= b | c; break; // energize coil 2+3
case 4: latch_state |= c; break; // energize coil 3 only
case 5: latch_state |= c | d; break; // energize coil 3+4
case 6: latch_state |= d; break; // energize coil 4 only
case 7: latch_state |= d | a; break; // energize coil 1+4
}
// change the energized state now
MC.latch_tx();
}