|
@@ -3,6 +3,7 @@
|
3
|
3
|
|
4
|
4
|
#include "config.h"
|
5
|
5
|
#include "config_pins.h"
|
|
6
|
+#include "lcd.h"
|
6
|
7
|
#include "steppers.h"
|
7
|
8
|
|
8
|
9
|
static AccelStepper stepper_x(AccelStepper::DRIVER, X_STEP_PIN, X_DIR_PIN, 0, 0, false);
|
|
@@ -29,8 +30,27 @@ enum stepper_states {
|
29
|
30
|
};
|
30
|
31
|
|
31
|
32
|
static stepper_states state = step_disabled;
|
|
33
|
+static unsigned long steppers_home_move_start_time = 0;
|
32
|
34
|
|
33
|
35
|
void steppers_init(void) {
|
|
36
|
+ pinMode(X_ENDSTOP_PIN, INPUT);
|
|
37
|
+ pinMode(Y_ENDSTOP_PIN, INPUT);
|
|
38
|
+ pinMode(Z_ENDSTOP_PIN, INPUT);
|
|
39
|
+ pinMode(E_ENDSTOP_PIN, INPUT);
|
|
40
|
+
|
|
41
|
+ pinMode(X_ENABLE_PIN, OUTPUT);
|
|
42
|
+ pinMode(X_STEP_PIN, OUTPUT);
|
|
43
|
+ pinMode(X_DIR_PIN, OUTPUT);
|
|
44
|
+ pinMode(Y_ENABLE_PIN, OUTPUT);
|
|
45
|
+ pinMode(Y_STEP_PIN, OUTPUT);
|
|
46
|
+ pinMode(Y_DIR_PIN, OUTPUT);
|
|
47
|
+ pinMode(Z_ENABLE_PIN, OUTPUT);
|
|
48
|
+ pinMode(Z_STEP_PIN, OUTPUT);
|
|
49
|
+ pinMode(Z_DIR_PIN, OUTPUT);
|
|
50
|
+ pinMode(E0_ENABLE_PIN, OUTPUT);
|
|
51
|
+ pinMode(E0_STEP_PIN, OUTPUT);
|
|
52
|
+ pinMode(E0_DIR_PIN, OUTPUT);
|
|
53
|
+
|
34
|
54
|
stepper_x.setEnablePin(X_ENABLE_PIN);
|
35
|
55
|
stepper_x.setMaxSpeed(XY_MAX_SPEED * XY_STEPS_PER_MM);
|
36
|
56
|
stepper_x.setAcceleration(XY_MAX_ACCEL * XY_STEPS_PER_MM);
|
|
@@ -48,27 +68,177 @@ void steppers_init(void) {
|
48
|
68
|
stepper_e.setAcceleration(E_MAX_ACCEL * E_STEPS_PER_MM);
|
49
|
69
|
}
|
50
|
70
|
|
|
71
|
+static void steppers_initiate_home(int axis, int phase) {
|
|
72
|
+ steppers_home_move_start_time = millis();
|
|
73
|
+
|
|
74
|
+ if (axis == 0) {
|
|
75
|
+ // x
|
|
76
|
+ if (phase == 0) {
|
|
77
|
+ state = step_homing_x_fast;
|
|
78
|
+ stepper_x.setSpeed(X_HOMING_DIR * X_AXIS_MOVEMENT_DIR * XY_FAST_HOME_SPEED * XY_STEPS_PER_MM);
|
|
79
|
+ } else if (phase == 1) {
|
|
80
|
+ state = step_homing_x_back;
|
|
81
|
+ stepper_x.setSpeed(-1.0 * X_HOMING_DIR * X_AXIS_MOVEMENT_DIR * XY_FAST_HOME_SPEED * XY_STEPS_PER_MM);
|
|
82
|
+ } else if (phase == 2) {
|
|
83
|
+ state = step_homing_x_slow;
|
|
84
|
+ stepper_x.setSpeed(X_HOMING_DIR * X_AXIS_MOVEMENT_DIR * XY_SLOW_HOME_SPEED * XY_STEPS_PER_MM);
|
|
85
|
+ }
|
|
86
|
+ } else if (axis == 1) {
|
|
87
|
+ // y
|
|
88
|
+ if (phase == 0) {
|
|
89
|
+ state = step_homing_y_fast;
|
|
90
|
+ stepper_y.setSpeed(Y_HOMING_DIR * Y_AXIS_MOVEMENT_DIR * XY_FAST_HOME_SPEED * XY_STEPS_PER_MM);
|
|
91
|
+ } else if (phase == 1) {
|
|
92
|
+ state = step_homing_y_back;
|
|
93
|
+ stepper_y.setSpeed(-1.0 * Y_HOMING_DIR * Y_AXIS_MOVEMENT_DIR * XY_FAST_HOME_SPEED * XY_STEPS_PER_MM);
|
|
94
|
+ } else if (phase == 2) {
|
|
95
|
+ state = step_homing_y_slow;
|
|
96
|
+ stepper_y.setSpeed(Y_HOMING_DIR * Y_AXIS_MOVEMENT_DIR * XY_SLOW_HOME_SPEED * XY_STEPS_PER_MM);
|
|
97
|
+ }
|
|
98
|
+ } else if (axis == 2) {
|
|
99
|
+ // z
|
|
100
|
+ if (phase == 0) {
|
|
101
|
+ state = step_homing_z_fast;
|
|
102
|
+ stepper_z.setSpeed(Z_HOMING_DIR * Z_AXIS_MOVEMENT_DIR * Z_FAST_HOME_SPEED * Z_STEPS_PER_MM);
|
|
103
|
+ } else if (phase == 1) {
|
|
104
|
+ state = step_homing_z_back;
|
|
105
|
+ stepper_z.setSpeed(-1.0 * Z_HOMING_DIR * Z_AXIS_MOVEMENT_DIR * Z_FAST_HOME_SPEED * Z_STEPS_PER_MM);
|
|
106
|
+ } else if (phase == 2) {
|
|
107
|
+ state = step_homing_z_slow;
|
|
108
|
+ stepper_z.setSpeed(Z_HOMING_DIR * Z_AXIS_MOVEMENT_DIR * Z_SLOW_HOME_SPEED * Z_STEPS_PER_MM);
|
|
109
|
+ }
|
|
110
|
+ } else if (axis == 3) {
|
|
111
|
+ // e
|
|
112
|
+ if (phase == 0) {
|
|
113
|
+ state = step_homing_e_fast;
|
|
114
|
+ stepper_e.setSpeed(E_HOMING_DIR * E_AXIS_MOVEMENT_DIR * E_FAST_HOME_SPEED * E_STEPS_PER_MM);
|
|
115
|
+ } else if (phase == 1) {
|
|
116
|
+ state = step_homing_e_back;
|
|
117
|
+ stepper_e.setSpeed(-1.0 * E_HOMING_DIR * E_AXIS_MOVEMENT_DIR * E_FAST_HOME_SPEED * E_STEPS_PER_MM);
|
|
118
|
+ } else if (phase == 2) {
|
|
119
|
+ state = step_homing_e_slow;
|
|
120
|
+ stepper_e.setSpeed(E_HOMING_DIR * E_AXIS_MOVEMENT_DIR * E_SLOW_HOME_SPEED * E_STEPS_PER_MM);
|
|
121
|
+ }
|
|
122
|
+ } else {
|
|
123
|
+ Serial.println(F("home_init error: invalid axis"));
|
|
124
|
+ }
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+static bool steppers_home_switch(int axis) {
|
|
128
|
+ if (axis == 0) {
|
|
129
|
+ return digitalRead(X_ENDSTOP_PIN) == LOW;
|
|
130
|
+ } else if (axis == 1) {
|
|
131
|
+ return digitalRead(Y_ENDSTOP_PIN) == LOW;
|
|
132
|
+ } else if (axis == 2) {
|
|
133
|
+ return digitalRead(Z_ENDSTOP_PIN) == LOW;
|
|
134
|
+ } else if (axis == 3) {
|
|
135
|
+ return digitalRead(E_ENDSTOP_PIN) == LOW;
|
|
136
|
+ } else {
|
|
137
|
+ Serial.println(F("home_switch error: invalid axis"));
|
|
138
|
+ }
|
|
139
|
+ return true;
|
|
140
|
+}
|
|
141
|
+
|
51
|
142
|
bool steppers_run(void) {
|
52
|
143
|
if (state == step_homing_x_fast) {
|
|
144
|
+ if (steppers_home_switch(0)) {
|
|
145
|
+ steppers_initiate_home(0, 1);
|
|
146
|
+ } else {
|
|
147
|
+ stepper_x.runSpeed();
|
|
148
|
+ }
|
53
|
149
|
} else if (state == step_homing_x_back) {
|
|
150
|
+ unsigned long end_time = steppers_home_move_start_time + XY_HOME_BACK_OFF_TIME;
|
|
151
|
+ if ((!steppers_home_switch(0)) && (millis() >= end_time)) {
|
|
152
|
+ steppers_initiate_home(0, 2);
|
|
153
|
+ } else {
|
|
154
|
+ stepper_x.runSpeed();
|
|
155
|
+ }
|
54
|
156
|
} else if (state == step_homing_x_slow) {
|
|
157
|
+ if (steppers_home_switch(0)) {
|
|
158
|
+ steppers_initiate_home(1, 0);
|
|
159
|
+ } else {
|
|
160
|
+ stepper_x.runSpeed();
|
|
161
|
+ }
|
55
|
162
|
} else if (state == step_homing_y_fast) {
|
|
163
|
+ if (steppers_home_switch(1)) {
|
|
164
|
+ steppers_initiate_home(1, 1);
|
|
165
|
+ } else {
|
|
166
|
+ stepper_y.runSpeed();
|
|
167
|
+ }
|
56
|
168
|
} else if (state == step_homing_y_back) {
|
|
169
|
+ unsigned long end_time = steppers_home_move_start_time + XY_HOME_BACK_OFF_TIME;
|
|
170
|
+ if ((!steppers_home_switch(1)) && (millis() >= end_time)) {
|
|
171
|
+ steppers_initiate_home(1, 2);
|
|
172
|
+ } else {
|
|
173
|
+ stepper_y.runSpeed();
|
|
174
|
+ }
|
57
|
175
|
} else if (state == step_homing_y_slow) {
|
|
176
|
+ if (steppers_home_switch(1)) {
|
|
177
|
+ steppers_initiate_home(3, 0);
|
|
178
|
+ } else {
|
|
179
|
+ stepper_y.runSpeed();
|
|
180
|
+ }
|
58
|
181
|
} else if (state == step_homing_z_fast) {
|
|
182
|
+ if (steppers_home_switch(2)) {
|
|
183
|
+ steppers_initiate_home(2, 1);
|
|
184
|
+ } else {
|
|
185
|
+ stepper_z.runSpeed();
|
|
186
|
+ }
|
59
|
187
|
} else if (state == step_homing_z_back) {
|
|
188
|
+ unsigned long end_time = steppers_home_move_start_time + Z_HOME_BACK_OFF_TIME;
|
|
189
|
+ if ((!steppers_home_switch(2)) && (millis() >= end_time)) {
|
|
190
|
+ steppers_initiate_home(2, 2);
|
|
191
|
+ } else {
|
|
192
|
+ stepper_z.runSpeed();
|
|
193
|
+ }
|
60
|
194
|
} else if (state == step_homing_z_slow) {
|
|
195
|
+ if (steppers_home_switch(2)) {
|
|
196
|
+ steppers_initiate_home(0, 0);
|
|
197
|
+ } else {
|
|
198
|
+ stepper_z.runSpeed();
|
|
199
|
+ }
|
61
|
200
|
} else if (state == step_homing_e_fast) {
|
|
201
|
+ if (steppers_home_switch(3)) {
|
|
202
|
+ steppers_initiate_home(3, 1);
|
|
203
|
+ } else {
|
|
204
|
+ stepper_e.runSpeed();
|
|
205
|
+ }
|
62
|
206
|
} else if (state == step_homing_e_back) {
|
|
207
|
+ unsigned long end_time = steppers_home_move_start_time + E_HOME_BACK_OFF_TIME;
|
|
208
|
+ if ((!steppers_home_switch(3)) && (millis() >= end_time)) {
|
|
209
|
+ steppers_initiate_home(3, 2);
|
|
210
|
+ } else {
|
|
211
|
+ stepper_e.runSpeed();
|
|
212
|
+ }
|
63
|
213
|
} else if (state == step_homing_e_slow) {
|
64
|
|
- }
|
|
214
|
+ if (steppers_home_switch(3)) {
|
|
215
|
+ state = step_homed;
|
|
216
|
+ return false;
|
|
217
|
+ } else {
|
|
218
|
+ stepper_z.runSpeed();
|
|
219
|
+ }
|
|
220
|
+ } else if (state != step_disabled) {
|
|
221
|
+ for (int i = 0; i < 4; i++) {
|
|
222
|
+ if (steppers_home_switch(i)) {
|
|
223
|
+ Serial.print(F("ERROR: endstop hit on "));
|
|
224
|
+ Serial.println(i);
|
65
|
225
|
|
66
|
|
- boolean x = stepper_x.run();
|
67
|
|
- boolean y = stepper_y.run();
|
68
|
|
- boolean z = stepper_z.run();
|
69
|
|
- boolean e = stepper_e.run();
|
|
226
|
+ // TODO proper error handling?
|
|
227
|
+ lcd_clear();
|
|
228
|
+ lcd_set_heading("ERROR");
|
|
229
|
+ lcd_set_text("Endstop hit. Aborting!");
|
|
230
|
+ while (1) { }
|
|
231
|
+ }
|
|
232
|
+ }
|
|
233
|
+ boolean x = stepper_x.run();
|
|
234
|
+ boolean y = stepper_y.run();
|
|
235
|
+ boolean z = stepper_z.run();
|
|
236
|
+ boolean e = stepper_e.run();
|
|
237
|
+
|
|
238
|
+ return x || y || z || e;
|
|
239
|
+ }
|
70
|
240
|
|
71
|
|
- return x || y || z || e;
|
|
241
|
+ return true;
|
72
|
242
|
}
|
73
|
243
|
|
74
|
244
|
bool steppers_homed(void) {
|
|
@@ -76,8 +246,7 @@ bool steppers_homed(void) {
|
76
|
246
|
}
|
77
|
247
|
|
78
|
248
|
void steppers_start_homing(void) {
|
79
|
|
- state = step_homing_x_fast;
|
80
|
|
-
|
|
249
|
+ steppers_initiate_home(2, 0);
|
81
|
250
|
}
|
82
|
251
|
|
83
|
252
|
static int steppers_move_axis(AccelStepper &axis, long pos) {
|
|
@@ -91,45 +260,90 @@ static int steppers_move_axis(AccelStepper &axis, long pos) {
|
91
|
260
|
state = step_not_homed;
|
92
|
261
|
}
|
93
|
262
|
|
|
263
|
+ if (!steppers_homed()) {
|
|
264
|
+ Serial.println(F("WARNING: un-homed move!"));
|
|
265
|
+ }
|
|
266
|
+
|
94
|
267
|
axis.moveTo(pos);
|
|
268
|
+ return 0;
|
95
|
269
|
}
|
96
|
270
|
|
97
|
271
|
int steppers_move_x(long pos) {
|
98
|
272
|
Serial.print(F("Moving X to "));
|
99
|
273
|
Serial.print(pos);
|
100
|
274
|
Serial.print(F(" mm ("));
|
101
|
|
- Serial.print(pos * XY_STEPS_PER_MM);
|
|
275
|
+ Serial.print(pos * XY_STEPS_PER_MM * X_AXIS_MOVEMENT_DIR);
|
102
|
276
|
Serial.println(F(" steps)"));
|
103
|
277
|
|
104
|
|
- return steppers_move_axis(stepper_x, pos * XY_STEPS_PER_MM);
|
|
278
|
+ if (pos < X_AXIS_MIN) {
|
|
279
|
+ Serial.println(F("Move below X_AXIS_MIN!"));
|
|
280
|
+ return -1;
|
|
281
|
+ }
|
|
282
|
+
|
|
283
|
+ if (pos > X_AXIS_MAX) {
|
|
284
|
+ Serial.println(F("Move above X_AXIS_MAX!"));
|
|
285
|
+ return -1;
|
|
286
|
+ }
|
|
287
|
+
|
|
288
|
+ return steppers_move_axis(stepper_x, pos * XY_STEPS_PER_MM * X_AXIS_MOVEMENT_DIR);
|
105
|
289
|
}
|
106
|
290
|
|
107
|
291
|
int steppers_move_y(long pos) {
|
108
|
292
|
Serial.print(F("Moving Y to "));
|
109
|
293
|
Serial.print(pos);
|
110
|
294
|
Serial.print(F(" mm ("));
|
111
|
|
- Serial.print(pos * XY_STEPS_PER_MM);
|
|
295
|
+ Serial.print(pos * XY_STEPS_PER_MM * Y_AXIS_MOVEMENT_DIR);
|
112
|
296
|
Serial.println(F(" steps)"));
|
113
|
297
|
|
114
|
|
- return steppers_move_axis(stepper_y, pos * XY_STEPS_PER_MM);
|
|
298
|
+ if (pos < Y_AXIS_MIN) {
|
|
299
|
+ Serial.println(F("Move below Y_AXIS_MIN!"));
|
|
300
|
+ return -1;
|
|
301
|
+ }
|
|
302
|
+
|
|
303
|
+ if (pos > Y_AXIS_MAX) {
|
|
304
|
+ Serial.println(F("Move above Y_AXIS_MAX!"));
|
|
305
|
+ return -1;
|
|
306
|
+ }
|
|
307
|
+
|
|
308
|
+ return steppers_move_axis(stepper_y, pos * XY_STEPS_PER_MM * Y_AXIS_MOVEMENT_DIR);
|
115
|
309
|
}
|
116
|
310
|
|
117
|
311
|
int steppers_move_z(long pos) {
|
118
|
312
|
Serial.print(F("Moving Z to "));
|
119
|
313
|
Serial.print(pos);
|
120
|
314
|
Serial.print(F(" mm ("));
|
121
|
|
- Serial.print(pos * Z_STEPS_PER_MM);
|
|
315
|
+ Serial.print(pos * Z_STEPS_PER_MM * Z_AXIS_MOVEMENT_DIR);
|
122
|
316
|
Serial.println(F(" steps)"));
|
123
|
317
|
|
124
|
|
- return steppers_move_axis(stepper_z, pos * Z_STEPS_PER_MM);
|
|
318
|
+ if (pos < Z_AXIS_MIN) {
|
|
319
|
+ Serial.println(F("Move below Z_AXIS_MIN!"));
|
|
320
|
+ return -1;
|
|
321
|
+ }
|
|
322
|
+
|
|
323
|
+ if (pos > Z_AXIS_MAX) {
|
|
324
|
+ Serial.println(F("Move above Z_AXIS_MAX!"));
|
|
325
|
+ return -1;
|
|
326
|
+ }
|
|
327
|
+
|
|
328
|
+ return steppers_move_axis(stepper_z, pos * Z_STEPS_PER_MM * Z_AXIS_MOVEMENT_DIR);
|
125
|
329
|
}
|
126
|
330
|
|
127
|
331
|
int steppers_move_e(long pos) {
|
128
|
332
|
Serial.print(F("Moving E to "));
|
129
|
333
|
Serial.print(pos);
|
130
|
334
|
Serial.print(F(" mm ("));
|
131
|
|
- Serial.print(pos * E_STEPS_PER_MM);
|
|
335
|
+ Serial.print(pos * E_STEPS_PER_MM * E_AXIS_MOVEMENT_DIR);
|
132
|
336
|
Serial.println(F(" steps)"));
|
133
|
337
|
|
134
|
|
- return steppers_move_axis(stepper_e, pos * E_STEPS_PER_MM);
|
|
338
|
+ if (pos < E_AXIS_MIN) {
|
|
339
|
+ Serial.println(F("Move below E_AXIS_MIN!"));
|
|
340
|
+ return -1;
|
|
341
|
+ }
|
|
342
|
+
|
|
343
|
+ if (pos > E_AXIS_MAX) {
|
|
344
|
+ Serial.println(F("Move above E_AXIS_MAX!"));
|
|
345
|
+ return -1;
|
|
346
|
+ }
|
|
347
|
+
|
|
348
|
+ return steppers_move_axis(stepper_e, pos * E_STEPS_PER_MM * E_AXIS_MOVEMENT_DIR);
|
135
|
349
|
}
|