/
StepperDriver.h
173 lines (141 loc) · 4.4 KB
/
StepperDriver.h
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
#ifndef STEPPER_DRIVER_H
#define STEPPER_DRIVER_H
#include <WProgram.h>
#include "configuration.h"
#include "pins.h"
#include "PololuStepper.h"
#include "StepCommand.h"
#include "CircularBuffer.h"
#include "TimerOne.h"
#include "Assert.h"
// stepper motors
class StepperDriver {
private:
PololuStepper(X_STEP_PIN, X_DIR_PIN, X_ENABLE_PIN, INVERT_X_DIR) xAxis;
PololuStepper(Y_STEP_PIN, Y_DIR_PIN, Y_ENABLE_PIN, INVERT_Y_DIR) yAxis;
PololuStepper(Z_STEP_PIN, Z_DIR_PIN, Z_ENABLE_PIN, INVERT_Z_DIR) zAxis;
PololuStepper(E_STEP_PIN, E_DIR_PIN, E_ENABLE_PIN, INVERT_E_DIR) eAxis;
CircularBuffer<StepCommand, STEP_COMMAND_Q_SIZE>* stepCommandBuffer;
boolean skippedLast;
byte skips;
public:
StepperDriver(
CircularBuffer<StepCommand, STEP_COMMAND_Q_SIZE>* stepCommandBuffer
) {
ASSERT(stepCommandBuffer != NULL);
this->stepCommandBuffer = stepCommandBuffer;
// steppers
xAxis.init();
yAxis.init();
zAxis.init();
eAxis.init();
// endstops and pullups (copied directly from Sprinter)
#ifdef ENDSTOPPULLUPS
#if X_MIN_PIN > -1
SET_INPUT(X_MIN_PIN);
WRITE(X_MIN_PIN,HIGH);
#endif
#if X_MAX_PIN > -1
SET_INPUT(X_MAX_PIN);
WRITE(X_MAX_PIN,HIGH);
#endif
#if Y_MIN_PIN > -1
SET_INPUT(Y_MIN_PIN);
WRITE(Y_MIN_PIN,HIGH);
#endif
#if Y_MAX_PIN > -1
SET_INPUT(Y_MAX_PIN);
WRITE(Y_MAX_PIN,HIGH);
#endif
#if Z_MIN_PIN > -1
SET_INPUT(Z_MIN_PIN);
WRITE(Z_MIN_PIN,HIGH);
#endif
#if Z_MAX_PIN > -1
SET_INPUT(Z_MAX_PIN);
WRITE(Z_MAX_PIN,HIGH);
#endif
#else
#if X_MIN_PIN > -1
SET_INPUT(X_MIN_PIN);
#endif
#if X_MAX_PIN > -1
SET_INPUT(X_MAX_PIN);
#endif
#if Y_MIN_PIN > -1
SET_INPUT(Y_MIN_PIN);
#endif
#if Y_MAX_PIN > -1
SET_INPUT(Y_MAX_PIN);
#endif
#if Z_MIN_PIN > -1
SET_INPUT(Z_MIN_PIN);
#endif
#if Z_MAX_PIN > -1
SET_INPUT(Z_MAX_PIN);
#endif
#endif
}
inline void setNextStepDelay(int nextStepDelay) {
ASSERT(nextStepDelay > 0);
Timer1.setPeriod(nextStepDelay);
}
inline boolean xAtMin() {
return READ(X_MIN_PIN)^ENDSTOPS_INVERTING;
}
inline boolean yAtMin() {
return READ(Y_MIN_PIN)^ENDSTOPS_INVERTING;
}
inline boolean zAtMin() {
return READ(Z_MIN_PIN)^ENDSTOPS_INVERTING;
}
inline boolean axisAtMin(byte axis) {
switch(axis) {
case 0: return xAtMin();
case 1: return yAtMin();
case 2: return zAtMin();
default: return false;
}
}
inline void interrupt() {
StepCommand* currentCommand;
if (stepCommandBuffer->notEmpty()) {
currentCommand = stepCommandBuffer->peek();
if (currentCommand->hasNewEnableDirection()) {
xAxis.enable(!currentCommand->xEnabled());
yAxis.enable(!currentCommand->yEnabled());
zAxis.enable(!currentCommand->zEnabled());
eAxis.enable(!currentCommand->eEnabled());
}
xAxis.setDirection(currentCommand->xDir());
yAxis.setDirection(currentCommand->yDir());
zAxis.setDirection(currentCommand->zDir());
eAxis.setDirection(currentCommand->eDir());
if (currentCommand->xStep() && (!xAtMin() || xAxis.getDirection())) {
xAxis.step(true);
}
if (currentCommand->yStep() && (!yAtMin() || yAxis.getDirection())) {
yAxis.step(true);
}
if (currentCommand->zStep() && (!zAtMin() || zAxis.getDirection())) {
zAxis.step(true);
}
if (currentCommand->eStep()) {
eAxis.step(true);
}
setNextStepDelay(currentCommand->getStepDelay());
stepCommandBuffer->remove();
xAxis.step(false);
yAxis.step(false);
zAxis.step(false);
eAxis.step(false);
//skippedLast = false;
} /*else {
if (!skippedLast) {
Serial.println((unsigned int) skips++);
}
skippedLast = true;
}*/
}
};
#endif // STEPPER_DRIVER_H