|
@@ -21,7 +21,7 @@
|
21
|
21
|
*/
|
22
|
22
|
|
23
|
23
|
/**
|
24
|
|
- * stepper.cpp - stepper motor driver: executes motion plans using stepper motors
|
|
24
|
+ * stepper.cpp - A singleton object to execute motion plans using stepper motors
|
25
|
25
|
* Marlin Firmware
|
26
|
26
|
*
|
27
|
27
|
* Derived from Grbl
|
|
@@ -46,6 +46,7 @@
|
46
|
46
|
|
47
|
47
|
#include "Marlin.h"
|
48
|
48
|
#include "stepper.h"
|
|
49
|
+#include "endstops.h"
|
49
|
50
|
#include "planner.h"
|
50
|
51
|
#include "temperature.h"
|
51
|
52
|
#include "ultralcd.h"
|
|
@@ -57,85 +58,7 @@
|
57
|
58
|
#include <SPI.h>
|
58
|
59
|
#endif
|
59
|
60
|
|
60
|
|
-//===========================================================================
|
61
|
|
-//============================= public variables ============================
|
62
|
|
-//===========================================================================
|
63
|
|
-block_t* current_block; // A pointer to the block currently being traced
|
64
|
|
-
|
65
|
|
-#if ENABLED(HAS_Z_MIN_PROBE)
|
66
|
|
- volatile bool z_probe_is_active = false;
|
67
|
|
-#endif
|
68
|
|
-
|
69
|
|
-//===========================================================================
|
70
|
|
-//============================= private variables ===========================
|
71
|
|
-//===========================================================================
|
72
|
|
-//static makes it impossible to be called from outside of this file by extern.!
|
73
|
|
-
|
74
|
|
-// Variables used by The Stepper Driver Interrupt
|
75
|
|
-static unsigned char out_bits = 0; // The next stepping-bits to be output
|
76
|
|
-static unsigned int cleaning_buffer_counter;
|
77
|
|
-
|
78
|
|
-#if ENABLED(Z_DUAL_ENDSTOPS)
|
79
|
|
- static bool performing_homing = false,
|
80
|
|
- locked_z_motor = false,
|
81
|
|
- locked_z2_motor = false;
|
82
|
|
-#endif
|
83
|
|
-
|
84
|
|
-// Counter variables for the Bresenham line tracer
|
85
|
|
-static long counter_x, counter_y, counter_z, counter_e;
|
86
|
|
-volatile static unsigned long step_events_completed; // The number of step events executed in the current block
|
87
|
|
-
|
88
|
|
-#if ENABLED(ADVANCE)
|
89
|
|
- static long advance_rate, advance, final_advance = 0;
|
90
|
|
- static long old_advance = 0;
|
91
|
|
- static long e_steps[4];
|
92
|
|
-#endif
|
93
|
|
-
|
94
|
|
-static long acceleration_time, deceleration_time;
|
95
|
|
-//static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
|
96
|
|
-static unsigned short acc_step_rate; // needed for deceleration start point
|
97
|
|
-static uint8_t step_loops;
|
98
|
|
-static uint8_t step_loops_nominal;
|
99
|
|
-static unsigned short OCR1A_nominal;
|
100
|
|
-
|
101
|
|
-volatile long endstops_trigsteps[3] = { 0 };
|
102
|
|
-volatile long endstops_stepsTotal, endstops_stepsDone;
|
103
|
|
-static volatile char endstop_hit_bits = 0; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT value
|
104
|
|
-
|
105
|
|
-#if DISABLED(Z_DUAL_ENDSTOPS)
|
106
|
|
- static byte
|
107
|
|
-#else
|
108
|
|
- static uint16_t
|
109
|
|
-#endif
|
110
|
|
- old_endstop_bits = 0; // use X_MIN, X_MAX... Z_MAX, Z_MIN_PROBE, Z2_MIN, Z2_MAX
|
111
|
|
-
|
112
|
|
-#if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
|
113
|
|
- bool abort_on_endstop_hit = false;
|
114
|
|
-#endif
|
115
|
|
-
|
116
|
|
-#if HAS_MOTOR_CURRENT_PWM
|
117
|
|
- #ifndef PWM_MOTOR_CURRENT
|
118
|
|
- #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
|
119
|
|
- #endif
|
120
|
|
- const int motor_current_setting[3] = PWM_MOTOR_CURRENT;
|
121
|
|
-#endif
|
122
|
|
-
|
123
|
|
-static bool check_endstops = true;
|
124
|
|
-static bool check_endstops_global =
|
125
|
|
- #if ENABLED(ENDSTOPS_ONLY_FOR_HOMING)
|
126
|
|
- false
|
127
|
|
- #else
|
128
|
|
- true
|
129
|
|
- #endif
|
130
|
|
-;
|
131
|
|
-
|
132
|
|
-volatile long count_position[NUM_AXIS] = { 0 }; // Positions of stepper motors, in step units
|
133
|
|
-volatile signed char count_direction[NUM_AXIS] = { 1 };
|
134
|
|
-
|
135
|
|
-
|
136
|
|
-//===========================================================================
|
137
|
|
-//================================ functions ================================
|
138
|
|
-//===========================================================================
|
|
61
|
+Stepper stepper; // Singleton
|
139
|
62
|
|
140
|
63
|
#if ENABLED(DUAL_X_CARRIAGE)
|
141
|
64
|
#define X_APPLY_DIR(v,ALWAYS) \
|
|
@@ -173,12 +96,12 @@ volatile signed char count_direction[NUM_AXIS] = { 1 };
|
173
|
96
|
#define Z_APPLY_STEP(v,Q) \
|
174
|
97
|
if (performing_homing) { \
|
175
|
98
|
if (Z_HOME_DIR > 0) {\
|
176
|
|
- if (!(TEST(old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
|
177
|
|
- if (!(TEST(old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
|
|
99
|
+ if (!(TEST(endstops.old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
|
|
100
|
+ if (!(TEST(endstops.old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
|
178
|
101
|
} \
|
179
|
102
|
else { \
|
180
|
|
- if (!(TEST(old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
|
181
|
|
- if (!(TEST(old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
|
|
103
|
+ if (!(TEST(endstops.old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
|
|
104
|
+ if (!(TEST(endstops.old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
|
182
|
105
|
} \
|
183
|
106
|
} \
|
184
|
107
|
else { \
|
|
@@ -195,31 +118,6 @@ volatile signed char count_direction[NUM_AXIS] = { 1 };
|
195
|
118
|
|
196
|
119
|
#define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
|
197
|
120
|
|
198
|
|
-// intRes = intIn1 * intIn2 >> 16
|
199
|
|
-// uses:
|
200
|
|
-// r26 to store 0
|
201
|
|
-// r27 to store the byte 1 of the 24 bit result
|
202
|
|
-#define MultiU16X8toH16(intRes, charIn1, intIn2) \
|
203
|
|
- asm volatile ( \
|
204
|
|
- "clr r26 \n\t" \
|
205
|
|
- "mul %A1, %B2 \n\t" \
|
206
|
|
- "movw %A0, r0 \n\t" \
|
207
|
|
- "mul %A1, %A2 \n\t" \
|
208
|
|
- "add %A0, r1 \n\t" \
|
209
|
|
- "adc %B0, r26 \n\t" \
|
210
|
|
- "lsr r0 \n\t" \
|
211
|
|
- "adc %A0, r26 \n\t" \
|
212
|
|
- "adc %B0, r26 \n\t" \
|
213
|
|
- "clr r1 \n\t" \
|
214
|
|
- : \
|
215
|
|
- "=&r" (intRes) \
|
216
|
|
- : \
|
217
|
|
- "d" (charIn1), \
|
218
|
|
- "d" (intIn2) \
|
219
|
|
- : \
|
220
|
|
- "r26" \
|
221
|
|
- )
|
222
|
|
-
|
223
|
121
|
// intRes = longIn1 * longIn2 >> 24
|
224
|
122
|
// uses:
|
225
|
123
|
// r26 to store 0
|
|
@@ -281,312 +179,38 @@ volatile signed char count_direction[NUM_AXIS] = { 1 };
|
281
|
179
|
#define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
|
282
|
180
|
#define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
|
283
|
181
|
|
284
|
|
-void enable_endstops(bool check) { check_endstops = check; }
|
285
|
|
-
|
286
|
|
-void enable_endstops_globally(bool check) { check_endstops_global = check_endstops = check; }
|
287
|
|
-
|
288
|
|
-void endstops_not_homing() { check_endstops = check_endstops_global; }
|
289
|
|
-
|
290
|
|
-void endstops_hit_on_purpose() { endstop_hit_bits = 0; }
|
291
|
|
-
|
292
|
|
-void checkHitEndstops() {
|
293
|
|
- if (endstop_hit_bits) {
|
294
|
|
- #if ENABLED(ULTRA_LCD)
|
295
|
|
- char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
|
296
|
|
- #define _SET_STOP_CHAR(A,C) (chr## A = C)
|
297
|
|
- #else
|
298
|
|
- #define _SET_STOP_CHAR(A,C) ;
|
299
|
|
- #endif
|
300
|
|
-
|
301
|
|
- #define _ENDSTOP_HIT_ECHO(A,C) do{ \
|
302
|
|
- SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", endstops_trigsteps[A ##_AXIS] / axis_steps_per_unit[A ##_AXIS]); \
|
303
|
|
- _SET_STOP_CHAR(A,C); }while(0)
|
304
|
|
-
|
305
|
|
- #define _ENDSTOP_HIT_TEST(A,C) \
|
306
|
|
- if (TEST(endstop_hit_bits, A ##_MIN) || TEST(endstop_hit_bits, A ##_MAX)) \
|
307
|
|
- _ENDSTOP_HIT_ECHO(A,C)
|
308
|
|
-
|
309
|
|
- SERIAL_ECHO_START;
|
310
|
|
- SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
|
311
|
|
- _ENDSTOP_HIT_TEST(X, 'X');
|
312
|
|
- _ENDSTOP_HIT_TEST(Y, 'Y');
|
313
|
|
- _ENDSTOP_HIT_TEST(Z, 'Z');
|
314
|
|
-
|
315
|
|
- #if ENABLED(Z_MIN_PROBE_ENDSTOP)
|
316
|
|
- #define P_AXIS Z_AXIS
|
317
|
|
- if (TEST(endstop_hit_bits, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
|
318
|
|
- #endif
|
319
|
|
- SERIAL_EOL;
|
320
|
|
-
|
321
|
|
- #if ENABLED(ULTRA_LCD)
|
322
|
|
- char msg[3 * strlen(MSG_LCD_ENDSTOPS) + 8 + 1]; // Room for a UTF 8 string
|
323
|
|
- sprintf_P(msg, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
|
324
|
|
- lcd_setstatus(msg);
|
325
|
|
- #endif
|
326
|
|
-
|
327
|
|
- endstops_hit_on_purpose();
|
328
|
|
-
|
329
|
|
- #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
|
330
|
|
- if (abort_on_endstop_hit) {
|
331
|
|
- card.sdprinting = false;
|
332
|
|
- card.closefile();
|
333
|
|
- quickStop();
|
334
|
|
- disable_all_heaters(); // switch off all heaters.
|
335
|
|
- }
|
336
|
|
- #endif
|
337
|
|
- }
|
338
|
|
-}
|
339
|
|
-
|
340
|
|
-// Check endstops - Called from ISR!
|
341
|
|
-inline void update_endstops() {
|
342
|
|
-
|
343
|
|
- #if ENABLED(Z_DUAL_ENDSTOPS)
|
344
|
|
- uint16_t
|
345
|
|
- #else
|
346
|
|
- byte
|
347
|
|
- #endif
|
348
|
|
- current_endstop_bits = 0;
|
349
|
|
-
|
350
|
|
- #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
|
351
|
|
- #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
|
352
|
|
- #define _AXIS(AXIS) AXIS ##_AXIS
|
353
|
|
- #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
|
354
|
|
- #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
|
355
|
|
-
|
356
|
|
- // SET_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
|
357
|
|
- #define SET_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
|
358
|
|
- // COPY_BIT: copy the value of COPY_BIT to BIT in bits
|
359
|
|
- #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
|
360
|
|
- // TEST_ENDSTOP: test the old and the current status of an endstop
|
361
|
|
- #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits, ENDSTOP) && TEST(old_endstop_bits, ENDSTOP))
|
362
|
|
-
|
363
|
|
- #if ENABLED(COREXY) || ENABLED(COREXZ)
|
364
|
|
-
|
365
|
|
- #define _SET_TRIGSTEPS(AXIS) do { \
|
366
|
|
- float axis_pos = count_position[_AXIS(AXIS)]; \
|
367
|
|
- if (_AXIS(AXIS) == A_AXIS) \
|
368
|
|
- axis_pos = (axis_pos + count_position[CORE_AXIS_2]) / 2; \
|
369
|
|
- else if (_AXIS(AXIS) == CORE_AXIS_2) \
|
370
|
|
- axis_pos = (count_position[A_AXIS] - axis_pos) / 2; \
|
371
|
|
- endstops_trigsteps[_AXIS(AXIS)] = axis_pos; \
|
372
|
|
- } while(0)
|
373
|
|
-
|
374
|
|
- #else
|
375
|
|
-
|
376
|
|
- #define _SET_TRIGSTEPS(AXIS) endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]
|
377
|
|
-
|
378
|
|
- #endif // COREXY || COREXZ
|
379
|
|
-
|
380
|
|
- #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
|
381
|
|
- SET_ENDSTOP_BIT(AXIS, MINMAX); \
|
382
|
|
- if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && current_block->steps[_AXIS(AXIS)] > 0) { \
|
383
|
|
- _SET_TRIGSTEPS(AXIS); \
|
384
|
|
- _ENDSTOP_HIT(AXIS); \
|
385
|
|
- step_events_completed = current_block->step_event_count; \
|
386
|
|
- } \
|
387
|
|
- } while(0)
|
388
|
|
-
|
389
|
|
- #if ENABLED(COREXY) || ENABLED(COREXZ)
|
390
|
|
- // Head direction in -X axis for CoreXY and CoreXZ bots.
|
391
|
|
- // If Delta1 == -Delta2, the movement is only in Y or Z axis
|
392
|
|
- if ((current_block->steps[A_AXIS] != current_block->steps[CORE_AXIS_2]) || (TEST(out_bits, A_AXIS) == TEST(out_bits, CORE_AXIS_2))) {
|
393
|
|
- if (TEST(out_bits, X_HEAD))
|
394
|
|
- #else
|
395
|
|
- if (TEST(out_bits, X_AXIS)) // stepping along -X axis (regular Cartesian bot)
|
396
|
|
- #endif
|
397
|
|
- { // -direction
|
398
|
|
- #if ENABLED(DUAL_X_CARRIAGE)
|
399
|
|
- // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
|
400
|
|
- if ((current_block->active_extruder == 0 && X_HOME_DIR == -1) || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
|
401
|
|
- #endif
|
402
|
|
- {
|
403
|
|
- #if HAS_X_MIN
|
404
|
|
- UPDATE_ENDSTOP(X, MIN);
|
405
|
|
- #endif
|
406
|
|
- }
|
407
|
|
- }
|
408
|
|
- else { // +direction
|
409
|
|
- #if ENABLED(DUAL_X_CARRIAGE)
|
410
|
|
- // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
|
411
|
|
- if ((current_block->active_extruder == 0 && X_HOME_DIR == 1) || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
|
412
|
|
- #endif
|
413
|
|
- {
|
414
|
|
- #if HAS_X_MAX
|
415
|
|
- UPDATE_ENDSTOP(X, MAX);
|
416
|
|
- #endif
|
417
|
|
- }
|
418
|
|
- }
|
419
|
|
- #if ENABLED(COREXY) || ENABLED(COREXZ)
|
420
|
|
- }
|
421
|
|
- #endif
|
422
|
|
-
|
423
|
|
- #if ENABLED(COREXY)
|
424
|
|
- // Head direction in -Y axis for CoreXY bots.
|
425
|
|
- // If DeltaX == DeltaY, the movement is only in X axis
|
426
|
|
- if ((current_block->steps[A_AXIS] != current_block->steps[B_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, B_AXIS))) {
|
427
|
|
- if (TEST(out_bits, Y_HEAD))
|
428
|
|
- #else
|
429
|
|
- if (TEST(out_bits, Y_AXIS)) // -direction
|
430
|
|
- #endif
|
431
|
|
- { // -direction
|
432
|
|
- #if HAS_Y_MIN
|
433
|
|
- UPDATE_ENDSTOP(Y, MIN);
|
434
|
|
- #endif
|
435
|
|
- }
|
436
|
|
- else { // +direction
|
437
|
|
- #if HAS_Y_MAX
|
438
|
|
- UPDATE_ENDSTOP(Y, MAX);
|
439
|
|
- #endif
|
440
|
|
- }
|
441
|
|
- #if ENABLED(COREXY)
|
442
|
|
- }
|
443
|
|
- #endif
|
444
|
|
-
|
445
|
|
- #if ENABLED(COREXZ)
|
446
|
|
- // Head direction in -Z axis for CoreXZ bots.
|
447
|
|
- // If DeltaX == DeltaZ, the movement is only in X axis
|
448
|
|
- if ((current_block->steps[A_AXIS] != current_block->steps[C_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, C_AXIS))) {
|
449
|
|
- if (TEST(out_bits, Z_HEAD))
|
450
|
|
- #else
|
451
|
|
- if (TEST(out_bits, Z_AXIS))
|
452
|
|
- #endif
|
453
|
|
- { // z -direction
|
454
|
|
- #if HAS_Z_MIN
|
455
|
|
-
|
456
|
|
- #if ENABLED(Z_DUAL_ENDSTOPS)
|
457
|
|
- SET_ENDSTOP_BIT(Z, MIN);
|
458
|
|
- #if HAS_Z2_MIN
|
459
|
|
- SET_ENDSTOP_BIT(Z2, MIN);
|
460
|
|
- #else
|
461
|
|
- COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
|
462
|
|
- #endif
|
463
|
|
-
|
464
|
|
- byte z_test = TEST_ENDSTOP(Z_MIN) | (TEST_ENDSTOP(Z2_MIN) << 1); // bit 0 for Z, bit 1 for Z2
|
465
|
|
-
|
466
|
|
- if (z_test && current_block->steps[Z_AXIS] > 0) { // z_test = Z_MIN || Z2_MIN
|
467
|
|
- endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
|
468
|
|
- SBI(endstop_hit_bits, Z_MIN);
|
469
|
|
- if (!performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
|
470
|
|
- step_events_completed = current_block->step_event_count;
|
471
|
|
- }
|
472
|
|
- #else // !Z_DUAL_ENDSTOPS
|
473
|
|
-
|
474
|
|
- #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
|
475
|
|
- if (z_probe_is_active) UPDATE_ENDSTOP(Z, MIN);
|
476
|
|
- #else
|
477
|
|
- UPDATE_ENDSTOP(Z, MIN);
|
478
|
|
- #endif
|
479
|
|
- #endif // !Z_DUAL_ENDSTOPS
|
480
|
|
- #endif
|
481
|
|
-
|
482
|
|
- #if ENABLED(Z_MIN_PROBE_ENDSTOP) && DISABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
|
483
|
|
- if (z_probe_is_active) {
|
484
|
|
- UPDATE_ENDSTOP(Z, MIN_PROBE);
|
485
|
|
- if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
|
486
|
|
- }
|
487
|
|
- #endif
|
488
|
|
- }
|
489
|
|
- else { // z +direction
|
490
|
|
- #if HAS_Z_MAX
|
491
|
|
-
|
492
|
|
- #if ENABLED(Z_DUAL_ENDSTOPS)
|
493
|
|
-
|
494
|
|
- SET_ENDSTOP_BIT(Z, MAX);
|
495
|
|
- #if HAS_Z2_MAX
|
496
|
|
- SET_ENDSTOP_BIT(Z2, MAX);
|
497
|
|
- #else
|
498
|
|
- COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
|
499
|
|
- #endif
|
500
|
|
-
|
501
|
|
- byte z_test = TEST_ENDSTOP(Z_MAX) | (TEST_ENDSTOP(Z2_MAX) << 1); // bit 0 for Z, bit 1 for Z2
|
502
|
|
-
|
503
|
|
- if (z_test && current_block->steps[Z_AXIS] > 0) { // t_test = Z_MAX || Z2_MAX
|
504
|
|
- endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
|
505
|
|
- SBI(endstop_hit_bits, Z_MIN);
|
506
|
|
- if (!performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
|
507
|
|
- step_events_completed = current_block->step_event_count;
|
508
|
|
- }
|
509
|
|
-
|
510
|
|
- #else // !Z_DUAL_ENDSTOPS
|
511
|
|
-
|
512
|
|
- UPDATE_ENDSTOP(Z, MAX);
|
513
|
|
-
|
514
|
|
- #endif // !Z_DUAL_ENDSTOPS
|
515
|
|
- #endif // Z_MAX_PIN
|
516
|
|
- }
|
517
|
|
- #if ENABLED(COREXZ)
|
518
|
|
- }
|
519
|
|
- #endif
|
520
|
|
- old_endstop_bits = current_endstop_bits;
|
521
|
|
-}
|
522
|
|
-
|
523
|
|
-// __________________________
|
524
|
|
-// /| |\ _________________ ^
|
525
|
|
-// / | | \ /| |\ |
|
526
|
|
-// / | | \ / | | \ s
|
527
|
|
-// / | | | | | \ p
|
528
|
|
-// / | | | | | \ e
|
529
|
|
-// +-----+------------------------+---+--+---------------+----+ e
|
530
|
|
-// | BLOCK 1 | BLOCK 2 | d
|
531
|
|
-//
|
532
|
|
-// time ----->
|
533
|
|
-//
|
534
|
|
-// The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
|
535
|
|
-// first block->accelerate_until step_events_completed, then keeps going at constant speed until
|
536
|
|
-// step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
|
537
|
|
-// The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
|
538
|
|
-
|
539
|
|
-void st_wake_up() {
|
|
182
|
+/**
|
|
183
|
+ * __________________________
|
|
184
|
+ * /| |\ _________________ ^
|
|
185
|
+ * / | | \ /| |\ |
|
|
186
|
+ * / | | \ / | | \ s
|
|
187
|
+ * / | | | | | \ p
|
|
188
|
+ * / | | | | | \ e
|
|
189
|
+ * +-----+------------------------+---+--+---------------+----+ e
|
|
190
|
+ * | BLOCK 1 | BLOCK 2 | d
|
|
191
|
+ *
|
|
192
|
+ * time ----->
|
|
193
|
+ *
|
|
194
|
+ * The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
|
|
195
|
+ * first block->accelerate_until step_events_completed, then keeps going at constant speed until
|
|
196
|
+ * step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
|
|
197
|
+ * The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
|
|
198
|
+ */
|
|
199
|
+void Stepper::wake_up() {
|
540
|
200
|
// TCNT1 = 0;
|
541
|
201
|
ENABLE_STEPPER_DRIVER_INTERRUPT();
|
542
|
202
|
}
|
543
|
203
|
|
544
|
|
-FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
|
545
|
|
- unsigned short timer;
|
546
|
|
-
|
547
|
|
- NOMORE(step_rate, MAX_STEP_FREQUENCY);
|
548
|
|
-
|
549
|
|
- if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
|
550
|
|
- step_rate = (step_rate >> 2) & 0x3fff;
|
551
|
|
- step_loops = 4;
|
552
|
|
- }
|
553
|
|
- else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
|
554
|
|
- step_rate = (step_rate >> 1) & 0x7fff;
|
555
|
|
- step_loops = 2;
|
556
|
|
- }
|
557
|
|
- else {
|
558
|
|
- step_loops = 1;
|
559
|
|
- }
|
560
|
|
-
|
561
|
|
- NOLESS(step_rate, F_CPU / 500000);
|
562
|
|
- step_rate -= F_CPU / 500000; // Correct for minimal speed
|
563
|
|
- if (step_rate >= (8 * 256)) { // higher step rate
|
564
|
|
- unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
|
565
|
|
- unsigned char tmp_step_rate = (step_rate & 0x00ff);
|
566
|
|
- unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
|
567
|
|
- MultiU16X8toH16(timer, tmp_step_rate, gain);
|
568
|
|
- timer = (unsigned short)pgm_read_word_near(table_address) - timer;
|
569
|
|
- }
|
570
|
|
- else { // lower step rates
|
571
|
|
- unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
|
572
|
|
- table_address += ((step_rate) >> 1) & 0xfffc;
|
573
|
|
- timer = (unsigned short)pgm_read_word_near(table_address);
|
574
|
|
- timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
|
575
|
|
- }
|
576
|
|
- if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
|
577
|
|
- return timer;
|
578
|
|
-}
|
579
|
|
-
|
580
|
204
|
/**
|
581
|
205
|
* Set the stepper direction of each axis
|
582
|
206
|
*
|
583
|
207
|
* X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY
|
584
|
208
|
* X_AXIS=A_AXIS and Z_AXIS=C_AXIS for COREXZ
|
585
|
209
|
*/
|
586
|
|
-void set_stepper_direction() {
|
|
210
|
+void Stepper::set_directions() {
|
587
|
211
|
|
588
|
212
|
#define SET_STEP_DIR(AXIS) \
|
589
|
|
- if (TEST(out_bits, AXIS ##_AXIS)) { \
|
|
213
|
+ if (motor_direction(AXIS ##_AXIS)) { \
|
590
|
214
|
AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \
|
591
|
215
|
count_direction[AXIS ##_AXIS] = -1; \
|
592
|
216
|
} \
|
|
@@ -600,7 +224,7 @@ void set_stepper_direction() {
|
600
|
224
|
SET_STEP_DIR(Z); // C
|
601
|
225
|
|
602
|
226
|
#if DISABLED(ADVANCE)
|
603
|
|
- if (TEST(out_bits, E_AXIS)) {
|
|
227
|
+ if (motor_direction(E_AXIS)) {
|
604
|
228
|
REV_E_DIR();
|
605
|
229
|
count_direction[E_AXIS] = -1;
|
606
|
230
|
}
|
|
@@ -611,49 +235,11 @@ void set_stepper_direction() {
|
611
|
235
|
#endif //!ADVANCE
|
612
|
236
|
}
|
613
|
237
|
|
614
|
|
-// Initializes the trapezoid generator from the current block. Called whenever a new
|
615
|
|
-// block begins.
|
616
|
|
-FORCE_INLINE void trapezoid_generator_reset() {
|
617
|
|
-
|
618
|
|
- static int8_t last_extruder = -1;
|
619
|
|
-
|
620
|
|
- if (current_block->direction_bits != out_bits || current_block->active_extruder != last_extruder) {
|
621
|
|
- out_bits = current_block->direction_bits;
|
622
|
|
- last_extruder = current_block->active_extruder;
|
623
|
|
- set_stepper_direction();
|
624
|
|
- }
|
625
|
|
-
|
626
|
|
- #if ENABLED(ADVANCE)
|
627
|
|
- advance = current_block->initial_advance;
|
628
|
|
- final_advance = current_block->final_advance;
|
629
|
|
- // Do E steps + advance steps
|
630
|
|
- e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
|
631
|
|
- old_advance = advance >>8;
|
632
|
|
- #endif
|
633
|
|
- deceleration_time = 0;
|
634
|
|
- // step_rate to timer interval
|
635
|
|
- OCR1A_nominal = calc_timer(current_block->nominal_rate);
|
636
|
|
- // make a note of the number of step loops required at nominal speed
|
637
|
|
- step_loops_nominal = step_loops;
|
638
|
|
- acc_step_rate = current_block->initial_rate;
|
639
|
|
- acceleration_time = calc_timer(acc_step_rate);
|
640
|
|
- OCR1A = acceleration_time;
|
641
|
|
-
|
642
|
|
- // SERIAL_ECHO_START;
|
643
|
|
- // SERIAL_ECHOPGM("advance :");
|
644
|
|
- // SERIAL_ECHO(current_block->advance/256.0);
|
645
|
|
- // SERIAL_ECHOPGM("advance rate :");
|
646
|
|
- // SERIAL_ECHO(current_block->advance_rate/256.0);
|
647
|
|
- // SERIAL_ECHOPGM("initial advance :");
|
648
|
|
- // SERIAL_ECHO(current_block->initial_advance/256.0);
|
649
|
|
- // SERIAL_ECHOPGM("final advance :");
|
650
|
|
- // SERIAL_ECHOLN(current_block->final_advance/256.0);
|
651
|
|
-}
|
652
|
|
-
|
653
|
238
|
// "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
|
654
|
239
|
// It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
|
655
|
|
-ISR(TIMER1_COMPA_vect) {
|
|
240
|
+ISR(TIMER1_COMPA_vect) { stepper.isr(); }
|
656
|
241
|
|
|
242
|
+void Stepper::isr() {
|
657
|
243
|
if (cleaning_buffer_counter) {
|
658
|
244
|
current_block = NULL;
|
659
|
245
|
plan_discard_current_block();
|
|
@@ -672,8 +258,8 @@ ISR(TIMER1_COMPA_vect) {
|
672
|
258
|
if (current_block) {
|
673
|
259
|
current_block->busy = true;
|
674
|
260
|
trapezoid_generator_reset();
|
675
|
|
- counter_x = -(current_block->step_event_count >> 1);
|
676
|
|
- counter_y = counter_z = counter_e = counter_x;
|
|
261
|
+ counter_X = -(current_block->step_event_count >> 1);
|
|
262
|
+ counter_Y = counter_Z = counter_E = counter_X;
|
677
|
263
|
step_events_completed = 0;
|
678
|
264
|
|
679
|
265
|
#if ENABLED(Z_LATE_ENABLE)
|
|
@@ -697,9 +283,9 @@ ISR(TIMER1_COMPA_vect) {
|
697
|
283
|
|
698
|
284
|
// Update endstops state, if enabled
|
699
|
285
|
#if ENABLED(HAS_Z_MIN_PROBE)
|
700
|
|
- if (check_endstops || z_probe_is_active) update_endstops();
|
|
286
|
+ if (endstops.enabled || endstops.z_probe_enabled) endstops.update();
|
701
|
287
|
#else
|
702
|
|
- if (check_endstops) update_endstops();
|
|
288
|
+ if (endstops.enabled) endstops.update();
|
703
|
289
|
#endif
|
704
|
290
|
|
705
|
291
|
// Take multiple steps per interrupt (For high speed moves)
|
|
@@ -709,48 +295,47 @@ ISR(TIMER1_COMPA_vect) {
|
709
|
295
|
#endif
|
710
|
296
|
|
711
|
297
|
#if ENABLED(ADVANCE)
|
712
|
|
- counter_e += current_block->steps[E_AXIS];
|
713
|
|
- if (counter_e > 0) {
|
714
|
|
- counter_e -= current_block->step_event_count;
|
715
|
|
- e_steps[current_block->active_extruder] += TEST(out_bits, E_AXIS) ? -1 : 1;
|
|
298
|
+ counter_E += current_block->steps[E_AXIS];
|
|
299
|
+ if (counter_E > 0) {
|
|
300
|
+ counter_E -= current_block->step_event_count;
|
|
301
|
+ e_steps[current_block->active_extruder] += motor_direction(E_AXIS) ? -1 : 1;
|
716
|
302
|
}
|
717
|
303
|
#endif //ADVANCE
|
718
|
304
|
|
719
|
|
- #define _COUNTER(axis) counter_## axis
|
|
305
|
+ #define _COUNTER(AXIS) counter_## AXIS
|
720
|
306
|
#define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
|
721
|
307
|
#define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
|
722
|
308
|
|
723
|
|
- #define STEP_ADD(axis, AXIS) \
|
724
|
|
- _COUNTER(axis) += current_block->steps[_AXIS(AXIS)]; \
|
725
|
|
- if (_COUNTER(axis) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
|
|
309
|
+ #define STEP_ADD(AXIS) \
|
|
310
|
+ _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
|
|
311
|
+ if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
|
726
|
312
|
|
727
|
|
- STEP_ADD(x,X);
|
728
|
|
- STEP_ADD(y,Y);
|
729
|
|
- STEP_ADD(z,Z);
|
|
313
|
+ STEP_ADD(X);
|
|
314
|
+ STEP_ADD(Y);
|
|
315
|
+ STEP_ADD(Z);
|
730
|
316
|
#if DISABLED(ADVANCE)
|
731
|
|
- STEP_ADD(e,E);
|
|
317
|
+ STEP_ADD(E);
|
732
|
318
|
#endif
|
733
|
319
|
|
734
|
|
- #define STEP_IF_COUNTER(axis, AXIS) \
|
735
|
|
- if (_COUNTER(axis) > 0) { \
|
736
|
|
- _COUNTER(axis) -= current_block->step_event_count; \
|
|
320
|
+ #define STEP_IF_COUNTER(AXIS) \
|
|
321
|
+ if (_COUNTER(AXIS) > 0) { \
|
|
322
|
+ _COUNTER(AXIS) -= current_block->step_event_count; \
|
737
|
323
|
count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
|
738
|
324
|
_APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
|
739
|
325
|
}
|
740
|
326
|
|
741
|
|
- STEP_IF_COUNTER(x, X);
|
742
|
|
- STEP_IF_COUNTER(y, Y);
|
743
|
|
- STEP_IF_COUNTER(z, Z);
|
|
327
|
+ STEP_IF_COUNTER(X);
|
|
328
|
+ STEP_IF_COUNTER(Y);
|
|
329
|
+ STEP_IF_COUNTER(Z);
|
744
|
330
|
#if DISABLED(ADVANCE)
|
745
|
|
- STEP_IF_COUNTER(e, E);
|
|
331
|
+ STEP_IF_COUNTER(E);
|
746
|
332
|
#endif
|
747
|
333
|
|
748
|
334
|
step_events_completed++;
|
749
|
335
|
if (step_events_completed >= current_block->step_event_count) break;
|
750
|
336
|
}
|
751
|
337
|
// Calculate new timer value
|
752
|
|
- unsigned short timer;
|
753
|
|
- unsigned short step_rate;
|
|
338
|
+ unsigned short timer, step_rate;
|
754
|
339
|
if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
|
755
|
340
|
|
756
|
341
|
MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
|
|
@@ -817,10 +402,11 @@ ISR(TIMER1_COMPA_vect) {
|
817
|
402
|
}
|
818
|
403
|
|
819
|
404
|
#if ENABLED(ADVANCE)
|
820
|
|
- unsigned char old_OCR0A;
|
821
|
405
|
// Timer interrupt for E. e_steps is set in the main routine;
|
822
|
406
|
// Timer 0 is shared with millies
|
823
|
|
- ISR(TIMER0_COMPA_vect) {
|
|
407
|
+ ISR(TIMER0_COMPA_vect) { stepper.advance_isr(); }
|
|
408
|
+
|
|
409
|
+ void Stepper::advance_isr() {
|
824
|
410
|
old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
|
825
|
411
|
OCR0A = old_OCR0A;
|
826
|
412
|
|
|
@@ -852,9 +438,10 @@ ISR(TIMER1_COMPA_vect) {
|
852
|
438
|
#endif
|
853
|
439
|
}
|
854
|
440
|
}
|
|
441
|
+
|
855
|
442
|
#endif // ADVANCE
|
856
|
443
|
|
857
|
|
-void st_init() {
|
|
444
|
+void Stepper::init() {
|
858
|
445
|
digipot_init(); //Initialize Digipot Motor Current
|
859
|
446
|
microstep_init(); //Initialize Microstepping Pins
|
860
|
447
|
|
|
@@ -944,70 +531,10 @@ void st_init() {
|
944
|
531
|
if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
|
945
|
532
|
#endif
|
946
|
533
|
|
947
|
|
- //endstops and pullups
|
948
|
|
-
|
949
|
|
- #if HAS_X_MIN
|
950
|
|
- SET_INPUT(X_MIN_PIN);
|
951
|
|
- #if ENABLED(ENDSTOPPULLUP_XMIN)
|
952
|
|
- WRITE(X_MIN_PIN,HIGH);
|
953
|
|
- #endif
|
954
|
|
- #endif
|
955
|
|
-
|
956
|
|
- #if HAS_Y_MIN
|
957
|
|
- SET_INPUT(Y_MIN_PIN);
|
958
|
|
- #if ENABLED(ENDSTOPPULLUP_YMIN)
|
959
|
|
- WRITE(Y_MIN_PIN,HIGH);
|
960
|
|
- #endif
|
961
|
|
- #endif
|
962
|
|
-
|
963
|
|
- #if HAS_Z_MIN
|
964
|
|
- SET_INPUT(Z_MIN_PIN);
|
965
|
|
- #if ENABLED(ENDSTOPPULLUP_ZMIN)
|
966
|
|
- WRITE(Z_MIN_PIN,HIGH);
|
967
|
|
- #endif
|
968
|
|
- #endif
|
969
|
|
-
|
970
|
|
- #if HAS_Z2_MIN
|
971
|
|
- SET_INPUT(Z2_MIN_PIN);
|
972
|
|
- #if ENABLED(ENDSTOPPULLUP_ZMIN)
|
973
|
|
- WRITE(Z2_MIN_PIN,HIGH);
|
974
|
|
- #endif
|
975
|
|
- #endif
|
976
|
|
-
|
977
|
|
- #if HAS_X_MAX
|
978
|
|
- SET_INPUT(X_MAX_PIN);
|
979
|
|
- #if ENABLED(ENDSTOPPULLUP_XMAX)
|
980
|
|
- WRITE(X_MAX_PIN,HIGH);
|
981
|
|
- #endif
|
982
|
|
- #endif
|
983
|
|
-
|
984
|
|
- #if HAS_Y_MAX
|
985
|
|
- SET_INPUT(Y_MAX_PIN);
|
986
|
|
- #if ENABLED(ENDSTOPPULLUP_YMAX)
|
987
|
|
- WRITE(Y_MAX_PIN,HIGH);
|
988
|
|
- #endif
|
989
|
|
- #endif
|
990
|
|
-
|
991
|
|
- #if HAS_Z_MAX
|
992
|
|
- SET_INPUT(Z_MAX_PIN);
|
993
|
|
- #if ENABLED(ENDSTOPPULLUP_ZMAX)
|
994
|
|
- WRITE(Z_MAX_PIN,HIGH);
|
995
|
|
- #endif
|
996
|
|
- #endif
|
997
|
|
-
|
998
|
|
- #if HAS_Z2_MAX
|
999
|
|
- SET_INPUT(Z2_MAX_PIN);
|
1000
|
|
- #if ENABLED(ENDSTOPPULLUP_ZMAX)
|
1001
|
|
- WRITE(Z2_MAX_PIN,HIGH);
|
1002
|
|
- #endif
|
1003
|
|
- #endif
|
1004
|
|
-
|
1005
|
|
- #if HAS_Z_PROBE && ENABLED(Z_MIN_PROBE_ENDSTOP) // Check for Z_MIN_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used.
|
1006
|
|
- SET_INPUT(Z_MIN_PROBE_PIN);
|
1007
|
|
- #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
|
1008
|
|
- WRITE(Z_MIN_PROBE_PIN,HIGH);
|
1009
|
|
- #endif
|
1010
|
|
- #endif
|
|
534
|
+ //
|
|
535
|
+ // Init endstops and pullups here
|
|
536
|
+ //
|
|
537
|
+ endstops.init();
|
1011
|
538
|
|
1012
|
539
|
#define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
|
1013
|
540
|
#define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
|
|
@@ -1083,17 +610,17 @@ void st_init() {
|
1083
|
610
|
SBI(TIMSK0, OCIE0A);
|
1084
|
611
|
#endif //ADVANCE
|
1085
|
612
|
|
1086
|
|
- enable_endstops(true); // Start with endstops active. After homing they can be disabled
|
|
613
|
+ endstops.enable(true); // Start with endstops active. After homing they can be disabled
|
1087
|
614
|
sei();
|
1088
|
615
|
|
1089
|
|
- set_stepper_direction(); // Init directions to out_bits = 0
|
|
616
|
+ set_directions(); // Init directions to last_direction_bits = 0
|
1090
|
617
|
}
|
1091
|
618
|
|
1092
|
619
|
|
1093
|
620
|
/**
|
1094
|
621
|
* Block until all buffered steps are executed
|
1095
|
622
|
*/
|
1096
|
|
-void st_synchronize() { while (blocks_queued()) idle(); }
|
|
623
|
+void Stepper::synchronize() { while (blocks_queued()) idle(); }
|
1097
|
624
|
|
1098
|
625
|
/**
|
1099
|
626
|
* Set the stepper positions directly in steps
|
|
@@ -1101,10 +628,10 @@ void st_synchronize() { while (blocks_queued()) idle(); }
|
1101
|
628
|
* The input is based on the typical per-axis XYZ steps.
|
1102
|
629
|
* For CORE machines XYZ needs to be translated to ABC.
|
1103
|
630
|
*
|
1104
|
|
- * This allows st_get_axis_position_mm to correctly
|
|
631
|
+ * This allows get_axis_position_mm to correctly
|
1105
|
632
|
* derive the current XYZ position later on.
|
1106
|
633
|
*/
|
1107
|
|
-void st_set_position(const long& x, const long& y, const long& z, const long& e) {
|
|
634
|
+void Stepper::set_position(const long& x, const long& y, const long& z, const long& e) {
|
1108
|
635
|
CRITICAL_SECTION_START;
|
1109
|
636
|
|
1110
|
637
|
#if ENABLED(COREXY)
|
|
@@ -1129,7 +656,7 @@ void st_set_position(const long& x, const long& y, const long& z, const long& e)
|
1129
|
656
|
CRITICAL_SECTION_END;
|
1130
|
657
|
}
|
1131
|
658
|
|
1132
|
|
-void st_set_e_position(const long& e) {
|
|
659
|
+void Stepper::set_e_position(const long& e) {
|
1133
|
660
|
CRITICAL_SECTION_START;
|
1134
|
661
|
count_position[E_AXIS] = e;
|
1135
|
662
|
CRITICAL_SECTION_END;
|
|
@@ -1138,7 +665,7 @@ void st_set_e_position(const long& e) {
|
1138
|
665
|
/**
|
1139
|
666
|
* Get a stepper's position in steps.
|
1140
|
667
|
*/
|
1141
|
|
-long st_get_position(AxisEnum axis) {
|
|
668
|
+long Stepper::position(AxisEnum axis) {
|
1142
|
669
|
CRITICAL_SECTION_START;
|
1143
|
670
|
long count_pos = count_position[axis];
|
1144
|
671
|
CRITICAL_SECTION_END;
|
|
@@ -1149,7 +676,7 @@ long st_get_position(AxisEnum axis) {
|
1149
|
676
|
* Get an axis position according to stepper position(s)
|
1150
|
677
|
* For CORE machines apply translation from ABC to XYZ.
|
1151
|
678
|
*/
|
1152
|
|
-float st_get_axis_position_mm(AxisEnum axis) {
|
|
679
|
+float Stepper::get_axis_position_mm(AxisEnum axis) {
|
1153
|
680
|
float axis_steps;
|
1154
|
681
|
#if ENABLED(COREXY) | ENABLED(COREXZ)
|
1155
|
682
|
if (axis == X_AXIS || axis == CORE_AXIS_2) {
|
|
@@ -1162,19 +689,19 @@ float st_get_axis_position_mm(AxisEnum axis) {
|
1162
|
689
|
axis_steps = (pos1 + ((axis == X_AXIS) ? pos2 : -pos2)) / 2.0f;
|
1163
|
690
|
}
|
1164
|
691
|
else
|
1165
|
|
- axis_steps = st_get_position(axis);
|
|
692
|
+ axis_steps = position(axis);
|
1166
|
693
|
#else
|
1167
|
|
- axis_steps = st_get_position(axis);
|
|
694
|
+ axis_steps = position(axis);
|
1168
|
695
|
#endif
|
1169
|
696
|
return axis_steps / axis_steps_per_unit[axis];
|
1170
|
697
|
}
|
1171
|
698
|
|
1172
|
|
-void finishAndDisableSteppers() {
|
1173
|
|
- st_synchronize();
|
|
699
|
+void Stepper::finish_and_disable() {
|
|
700
|
+ synchronize();
|
1174
|
701
|
disable_all_steppers();
|
1175
|
702
|
}
|
1176
|
703
|
|
1177
|
|
-void quickStop() {
|
|
704
|
+void Stepper::quick_stop() {
|
1178
|
705
|
cleaning_buffer_counter = 5000;
|
1179
|
706
|
DISABLE_STEPPER_DRIVER_INTERRUPT();
|
1180
|
707
|
while (blocks_queued()) plan_discard_current_block();
|
|
@@ -1182,11 +709,62 @@ void quickStop() {
|
1182
|
709
|
ENABLE_STEPPER_DRIVER_INTERRUPT();
|
1183
|
710
|
}
|
1184
|
711
|
|
|
712
|
+void Stepper::endstop_triggered(AxisEnum axis) {
|
|
713
|
+
|
|
714
|
+ #if ENABLED(COREXY) || ENABLED(COREXZ)
|
|
715
|
+
|
|
716
|
+ float axis_pos = count_position[axis];
|
|
717
|
+ if (axis == A_AXIS)
|
|
718
|
+ axis_pos = (axis_pos + count_position[CORE_AXIS_2]) / 2;
|
|
719
|
+ else if (axis == CORE_AXIS_2)
|
|
720
|
+ axis_pos = (count_position[A_AXIS] - axis_pos) / 2;
|
|
721
|
+ endstops_trigsteps[axis] = axis_pos;
|
|
722
|
+
|
|
723
|
+ #else // !COREXY && !COREXZ
|
|
724
|
+
|
|
725
|
+ endstops_trigsteps[axis] = count_position[axis];
|
|
726
|
+
|
|
727
|
+ #endif // !COREXY && !COREXZ
|
|
728
|
+
|
|
729
|
+ kill_current_block();
|
|
730
|
+}
|
|
731
|
+
|
|
732
|
+void Stepper::report_positions() {
|
|
733
|
+ CRITICAL_SECTION_START;
|
|
734
|
+ long xpos = count_position[X_AXIS],
|
|
735
|
+ ypos = count_position[Y_AXIS],
|
|
736
|
+ zpos = count_position[Z_AXIS];
|
|
737
|
+ CRITICAL_SECTION_END;
|
|
738
|
+
|
|
739
|
+ #if ENABLED(COREXY) || ENABLED(COREXZ)
|
|
740
|
+ SERIAL_PROTOCOLPGM(MSG_COUNT_A);
|
|
741
|
+ #else
|
|
742
|
+ SERIAL_PROTOCOLPGM(MSG_COUNT_X);
|
|
743
|
+ #endif
|
|
744
|
+ SERIAL_PROTOCOL(xpos);
|
|
745
|
+
|
|
746
|
+ #if ENABLED(COREXY) || ENABLED(COREXZ)
|
|
747
|
+ SERIAL_PROTOCOLPGM(" B:");
|
|
748
|
+ #else
|
|
749
|
+ SERIAL_PROTOCOLPGM(" Y:");
|
|
750
|
+ #endif
|
|
751
|
+ SERIAL_PROTOCOL(ypos);
|
|
752
|
+
|
|
753
|
+ #if ENABLED(COREXZ) || ENABLED(COREXZ)
|
|
754
|
+ SERIAL_PROTOCOLPGM(" C:");
|
|
755
|
+ #else
|
|
756
|
+ SERIAL_PROTOCOLPGM(" Z:");
|
|
757
|
+ #endif
|
|
758
|
+ SERIAL_PROTOCOL(zpos);
|
|
759
|
+
|
|
760
|
+ SERIAL_EOL;
|
|
761
|
+}
|
|
762
|
+
|
1185
|
763
|
#if ENABLED(BABYSTEPPING)
|
1186
|
764
|
|
1187
|
765
|
// MUST ONLY BE CALLED BY AN ISR,
|
1188
|
766
|
// No other ISR should ever interrupt this!
|
1189
|
|
- void babystep(const uint8_t axis, const bool direction) {
|
|
767
|
+ void Stepper::babystep(const uint8_t axis, const bool direction) {
|
1190
|
768
|
|
1191
|
769
|
#define _ENABLE(axis) enable_## axis()
|
1192
|
770
|
#define _READ_DIR(AXIS) AXIS ##_DIR_READ
|
|
@@ -1256,10 +834,14 @@ void quickStop() {
|
1256
|
834
|
|
1257
|
835
|
#endif //BABYSTEPPING
|
1258
|
836
|
|
|
837
|
+/**
|
|
838
|
+ * Software-controlled Stepper Motor Current
|
|
839
|
+ */
|
|
840
|
+
|
1259
|
841
|
#if HAS_DIGIPOTSS
|
1260
|
842
|
|
1261
|
843
|
// From Arduino DigitalPotControl example
|
1262
|
|
- void digitalPotWrite(int address, int value) {
|
|
844
|
+ void Stepper::digitalPotWrite(int address, int value) {
|
1263
|
845
|
digitalWrite(DIGIPOTSS_PIN, LOW); // take the SS pin low to select the chip
|
1264
|
846
|
SPI.transfer(address); // send in the address and value via SPI:
|
1265
|
847
|
SPI.transfer(value);
|
|
@@ -1269,8 +851,7 @@ void quickStop() {
|
1269
|
851
|
|
1270
|
852
|
#endif //HAS_DIGIPOTSS
|
1271
|
853
|
|
1272
|
|
-// Initialize Digipot Motor Current
|
1273
|
|
-void digipot_init() {
|
|
854
|
+void Stepper::digipot_init() {
|
1274
|
855
|
#if HAS_DIGIPOTSS
|
1275
|
856
|
const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
|
1276
|
857
|
|
|
@@ -1299,7 +880,7 @@ void digipot_init() {
|
1299
|
880
|
#endif
|
1300
|
881
|
}
|
1301
|
882
|
|
1302
|
|
-void digipot_current(uint8_t driver, int current) {
|
|
883
|
+void Stepper::digipot_current(uint8_t driver, int current) {
|
1303
|
884
|
#if HAS_DIGIPOTSS
|
1304
|
885
|
const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
|
1305
|
886
|
digitalPotWrite(digipot_ch[driver], current);
|
|
@@ -1322,7 +903,7 @@ void digipot_current(uint8_t driver, int current) {
|
1322
|
903
|
#endif
|
1323
|
904
|
}
|
1324
|
905
|
|
1325
|
|
-void microstep_init() {
|
|
906
|
+void Stepper::microstep_init() {
|
1326
|
907
|
#if HAS_MICROSTEPS_E1
|
1327
|
908
|
pinMode(E1_MS1_PIN, OUTPUT);
|
1328
|
909
|
pinMode(E1_MS2_PIN, OUTPUT);
|
|
@@ -1343,7 +924,11 @@ void microstep_init() {
|
1343
|
924
|
#endif
|
1344
|
925
|
}
|
1345
|
926
|
|
1346
|
|
-void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
|
|
927
|
+/**
|
|
928
|
+ * Software-controlled Microstepping
|
|
929
|
+ */
|
|
930
|
+
|
|
931
|
+void Stepper::microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
|
1347
|
932
|
if (ms1 >= 0) switch (driver) {
|
1348
|
933
|
case 0: digitalWrite(X_MS1_PIN, ms1); break;
|
1349
|
934
|
case 1: digitalWrite(Y_MS1_PIN, ms1); break;
|
|
@@ -1364,7 +949,7 @@ void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
|
1364
|
949
|
}
|
1365
|
950
|
}
|
1366
|
951
|
|
1367
|
|
-void microstep_mode(uint8_t driver, uint8_t stepping_mode) {
|
|
952
|
+void Stepper::microstep_mode(uint8_t driver, uint8_t stepping_mode) {
|
1368
|
953
|
switch (stepping_mode) {
|
1369
|
954
|
case 1: microstep_ms(driver, MICROSTEP1); break;
|
1370
|
955
|
case 2: microstep_ms(driver, MICROSTEP2); break;
|
|
@@ -1374,7 +959,7 @@ void microstep_mode(uint8_t driver, uint8_t stepping_mode) {
|
1374
|
959
|
}
|
1375
|
960
|
}
|
1376
|
961
|
|
1377
|
|
-void microstep_readings() {
|
|
962
|
+void Stepper::microstep_readings() {
|
1378
|
963
|
SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
|
1379
|
964
|
SERIAL_PROTOCOLPGM("X: ");
|
1380
|
965
|
SERIAL_PROTOCOL(digitalRead(X_MS1_PIN));
|
|
@@ -1396,7 +981,7 @@ void microstep_readings() {
|
1396
|
981
|
}
|
1397
|
982
|
|
1398
|
983
|
#if ENABLED(Z_DUAL_ENDSTOPS)
|
1399
|
|
- void In_Homing_Process(bool state) { performing_homing = state; }
|
1400
|
|
- void Lock_z_motor(bool state) { locked_z_motor = state; }
|
1401
|
|
- void Lock_z2_motor(bool state) { locked_z2_motor = state; }
|
|
984
|
+ void Stepper::set_homing_flag(bool state) { performing_homing = state; }
|
|
985
|
+ void Stepper::set_z_lock(bool state) { locked_z_motor = state; }
|
|
986
|
+ void Stepper::set_z2_lock(bool state) { locked_z2_motor = state; }
|
1402
|
987
|
#endif
|