|
@@ -56,9 +56,9 @@ static long counter_x, // Counter variables for the bresenham line tracer
|
56
|
56
|
volatile static unsigned long step_events_completed; // The number of step events executed in the current block
|
57
|
57
|
#ifdef ADVANCE
|
58
|
58
|
static long advance_rate, advance, final_advance = 0;
|
59
|
|
- static short old_advance = 0;
|
|
59
|
+ static long old_advance = 0;
|
60
|
60
|
#endif
|
61
|
|
-static short e_steps;
|
|
61
|
+static long e_steps;
|
62
|
62
|
static unsigned char busy = false; // TRUE when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler.
|
63
|
63
|
static long acceleration_time, deceleration_time;
|
64
|
64
|
//static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
|
|
@@ -79,13 +79,20 @@ static bool old_y_max_endstop=false;
|
79
|
79
|
static bool old_z_min_endstop=false;
|
80
|
80
|
static bool old_z_max_endstop=false;
|
81
|
81
|
|
|
82
|
+static bool check_endstops = true;
|
|
83
|
+
|
82
|
84
|
volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
|
83
|
85
|
volatile char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
|
84
|
86
|
|
85
|
87
|
//===========================================================================
|
86
|
88
|
//=============================functions ============================
|
87
|
89
|
//===========================================================================
|
88
|
|
-
|
|
90
|
+
|
|
91
|
+#ifdef ENDSTOPS_ONLY_FOR_HOMING
|
|
92
|
+ #define CHECK_ENDSTOPS if(check_endstops)
|
|
93
|
+#else
|
|
94
|
+ #define CHECK_ENDSTOPS
|
|
95
|
+#endif
|
89
|
96
|
|
90
|
97
|
// intRes = intIn1 * intIn2 >> 16
|
91
|
98
|
// uses:
|
|
@@ -191,6 +198,11 @@ void endstops_hit_on_purpose()
|
191
|
198
|
endstop_z_hit=false;
|
192
|
199
|
}
|
193
|
200
|
|
|
201
|
+void enable_endstops(bool check)
|
|
202
|
+{
|
|
203
|
+ check_endstops = check;
|
|
204
|
+}
|
|
205
|
+
|
194
|
206
|
// __________________________
|
195
|
207
|
// /| |\ _________________ ^
|
196
|
208
|
// / | | \ /| |\ |
|
|
@@ -254,6 +266,9 @@ FORCE_INLINE void trapezoid_generator_reset() {
|
254
|
266
|
#ifdef ADVANCE
|
255
|
267
|
advance = current_block->initial_advance;
|
256
|
268
|
final_advance = current_block->final_advance;
|
|
269
|
+ // Do E steps + advance steps
|
|
270
|
+ e_steps += ((advance >>8) - old_advance);
|
|
271
|
+ old_advance = advance >>8;
|
257
|
272
|
#endif
|
258
|
273
|
deceleration_time = 0;
|
259
|
274
|
// step_rate to timer interval
|
|
@@ -261,6 +276,17 @@ FORCE_INLINE void trapezoid_generator_reset() {
|
261
|
276
|
acceleration_time = calc_timer(acc_step_rate);
|
262
|
277
|
OCR1A = acceleration_time;
|
263
|
278
|
OCR1A_nominal = calc_timer(current_block->nominal_rate);
|
|
279
|
+
|
|
280
|
+// SERIAL_ECHO_START;
|
|
281
|
+// SERIAL_ECHOPGM("advance :");
|
|
282
|
+// SERIAL_ECHO(current_block->advance/256.0);
|
|
283
|
+// SERIAL_ECHOPGM("advance rate :");
|
|
284
|
+// SERIAL_ECHO(current_block->advance_rate/256.0);
|
|
285
|
+// SERIAL_ECHOPGM("initial advance :");
|
|
286
|
+// SERIAL_ECHO(current_block->initial_advance/256.0);
|
|
287
|
+// SERIAL_ECHOPGM("final advance :");
|
|
288
|
+// SERIAL_ECHOLN(current_block->final_advance/256.0);
|
|
289
|
+
|
264
|
290
|
}
|
265
|
291
|
|
266
|
292
|
// "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
|
|
@@ -295,82 +321,100 @@ ISR(TIMER1_COMPA_vect)
|
295
|
321
|
if ((out_bits & (1<<X_AXIS)) != 0) { // -direction
|
296
|
322
|
WRITE(X_DIR_PIN, INVERT_X_DIR);
|
297
|
323
|
count_direction[X_AXIS]=-1;
|
298
|
|
- #if X_MIN_PIN > -1
|
299
|
|
- bool x_min_endstop=(READ(X_MIN_PIN) != X_ENDSTOPS_INVERTING);
|
300
|
|
- if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
|
301
|
|
- endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
|
302
|
|
- endstop_x_hit=true;
|
303
|
|
- step_events_completed = current_block->step_event_count;
|
304
|
|
- }
|
305
|
|
- old_x_min_endstop = x_min_endstop;
|
306
|
|
- #endif
|
|
324
|
+ CHECK_ENDSTOPS
|
|
325
|
+ {
|
|
326
|
+ #if X_MIN_PIN > -1
|
|
327
|
+ bool x_min_endstop=(READ(X_MIN_PIN) != X_ENDSTOPS_INVERTING);
|
|
328
|
+ if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
|
|
329
|
+ endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
|
|
330
|
+ endstop_x_hit=true;
|
|
331
|
+ step_events_completed = current_block->step_event_count;
|
|
332
|
+ }
|
|
333
|
+ old_x_min_endstop = x_min_endstop;
|
|
334
|
+ #endif
|
|
335
|
+ }
|
307
|
336
|
}
|
308
|
337
|
else { // +direction
|
309
|
338
|
WRITE(X_DIR_PIN,!INVERT_X_DIR);
|
310
|
339
|
count_direction[X_AXIS]=1;
|
311
|
|
- #if X_MAX_PIN > -1
|
312
|
|
- bool x_max_endstop=(READ(X_MAX_PIN) != X_ENDSTOPS_INVERTING);
|
313
|
|
- if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
|
314
|
|
- endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
|
315
|
|
- endstop_x_hit=true;
|
316
|
|
- step_events_completed = current_block->step_event_count;
|
317
|
|
- }
|
318
|
|
- old_x_max_endstop = x_max_endstop;
|
319
|
|
- #endif
|
|
340
|
+ CHECK_ENDSTOPS
|
|
341
|
+ {
|
|
342
|
+ #if X_MAX_PIN > -1
|
|
343
|
+ bool x_max_endstop=(READ(X_MAX_PIN) != X_ENDSTOPS_INVERTING);
|
|
344
|
+ if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
|
|
345
|
+ endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
|
|
346
|
+ endstop_x_hit=true;
|
|
347
|
+ step_events_completed = current_block->step_event_count;
|
|
348
|
+ }
|
|
349
|
+ old_x_max_endstop = x_max_endstop;
|
|
350
|
+ #endif
|
|
351
|
+ }
|
320
|
352
|
}
|
321
|
353
|
|
322
|
354
|
if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
|
323
|
355
|
WRITE(Y_DIR_PIN,INVERT_Y_DIR);
|
324
|
356
|
count_direction[Y_AXIS]=-1;
|
325
|
|
- #if Y_MIN_PIN > -1
|
326
|
|
- bool y_min_endstop=(READ(Y_MIN_PIN) != Y_ENDSTOPS_INVERTING);
|
327
|
|
- if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
|
328
|
|
- endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
|
329
|
|
- endstop_y_hit=true;
|
330
|
|
- step_events_completed = current_block->step_event_count;
|
331
|
|
- }
|
332
|
|
- old_y_min_endstop = y_min_endstop;
|
333
|
|
- #endif
|
|
357
|
+ CHECK_ENDSTOPS
|
|
358
|
+ {
|
|
359
|
+ #if Y_MIN_PIN > -1
|
|
360
|
+ bool y_min_endstop=(READ(Y_MIN_PIN) != Y_ENDSTOPS_INVERTING);
|
|
361
|
+ if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
|
|
362
|
+ endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
|
|
363
|
+ endstop_y_hit=true;
|
|
364
|
+ step_events_completed = current_block->step_event_count;
|
|
365
|
+ }
|
|
366
|
+ old_y_min_endstop = y_min_endstop;
|
|
367
|
+ #endif
|
|
368
|
+ }
|
334
|
369
|
}
|
335
|
370
|
else { // +direction
|
336
|
371
|
WRITE(Y_DIR_PIN,!INVERT_Y_DIR);
|
337
|
372
|
count_direction[Y_AXIS]=1;
|
338
|
|
- #if Y_MAX_PIN > -1
|
339
|
|
- bool y_max_endstop=(READ(Y_MAX_PIN) != Y_ENDSTOPS_INVERTING);
|
340
|
|
- if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
|
341
|
|
- endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
|
342
|
|
- endstop_y_hit=true;
|
343
|
|
- step_events_completed = current_block->step_event_count;
|
344
|
|
- }
|
345
|
|
- old_y_max_endstop = y_max_endstop;
|
346
|
|
- #endif
|
|
373
|
+ CHECK_ENDSTOPS
|
|
374
|
+ {
|
|
375
|
+ #if Y_MAX_PIN > -1
|
|
376
|
+ bool y_max_endstop=(READ(Y_MAX_PIN) != Y_ENDSTOPS_INVERTING);
|
|
377
|
+ if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
|
|
378
|
+ endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
|
|
379
|
+ endstop_y_hit=true;
|
|
380
|
+ step_events_completed = current_block->step_event_count;
|
|
381
|
+ }
|
|
382
|
+ old_y_max_endstop = y_max_endstop;
|
|
383
|
+ #endif
|
|
384
|
+ }
|
347
|
385
|
}
|
348
|
386
|
|
349
|
387
|
if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
|
350
|
388
|
WRITE(Z_DIR_PIN,INVERT_Z_DIR);
|
351
|
389
|
count_direction[Z_AXIS]=-1;
|
352
|
|
- #if Z_MIN_PIN > -1
|
353
|
|
- bool z_min_endstop=(READ(Z_MIN_PIN) != Z_ENDSTOPS_INVERTING);
|
354
|
|
- if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
|
355
|
|
- endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
|
356
|
|
- endstop_z_hit=true;
|
357
|
|
- step_events_completed = current_block->step_event_count;
|
358
|
|
- }
|
359
|
|
- old_z_min_endstop = z_min_endstop;
|
360
|
|
- #endif
|
|
390
|
+ CHECK_ENDSTOPS
|
|
391
|
+ {
|
|
392
|
+ #if Z_MIN_PIN > -1
|
|
393
|
+ bool z_min_endstop=(READ(Z_MIN_PIN) != Z_ENDSTOPS_INVERTING);
|
|
394
|
+ if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
|
|
395
|
+ endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
|
|
396
|
+ endstop_z_hit=true;
|
|
397
|
+ step_events_completed = current_block->step_event_count;
|
|
398
|
+ }
|
|
399
|
+ old_z_min_endstop = z_min_endstop;
|
|
400
|
+ #endif
|
|
401
|
+ }
|
361
|
402
|
}
|
362
|
403
|
else { // +direction
|
363
|
404
|
WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
|
364
|
|
- count_direction[Z_AXIS]=1;
|
365
|
|
- #if Z_MAX_PIN > -1
|
366
|
|
- bool z_max_endstop=(READ(Z_MAX_PIN) != Z_ENDSTOPS_INVERTING);
|
367
|
|
- if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
|
368
|
|
- endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
|
369
|
|
- endstop_z_hit=true;
|
370
|
|
- step_events_completed = current_block->step_event_count;
|
371
|
|
- }
|
372
|
|
- old_z_max_endstop = z_max_endstop;
|
373
|
|
- #endif
|
|
405
|
+ count_direction[Z_AXIS]=1;
|
|
406
|
+ CHECK_ENDSTOPS
|
|
407
|
+ {
|
|
408
|
+ #if Z_MAX_PIN > -1
|
|
409
|
+ bool z_max_endstop=(READ(Z_MAX_PIN) != Z_ENDSTOPS_INVERTING);
|
|
410
|
+ if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
|
|
411
|
+ endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
|
|
412
|
+ endstop_z_hit=true;
|
|
413
|
+ step_events_completed = current_block->step_event_count;
|
|
414
|
+ }
|
|
415
|
+ old_z_max_endstop = z_max_endstop;
|
|
416
|
+ #endif
|
|
417
|
+ }
|
374
|
418
|
}
|
375
|
419
|
|
376
|
420
|
#ifndef ADVANCE
|
|
@@ -383,6 +427,9 @@ ISR(TIMER1_COMPA_vect)
|
383
|
427
|
count_direction[E_AXIS]=-1;
|
384
|
428
|
}
|
385
|
429
|
#endif //!ADVANCE
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
386
|
433
|
for(int8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
|
387
|
434
|
MSerial.checkRx(); // Check for serial chars.
|
388
|
435
|
|
|
@@ -391,19 +438,12 @@ ISR(TIMER1_COMPA_vect)
|
391
|
438
|
if (counter_e > 0) {
|
392
|
439
|
counter_e -= current_block->step_event_count;
|
393
|
440
|
if ((out_bits & (1<<E_AXIS)) != 0) { // - direction
|
394
|
|
- CRITICAL_SECTION_START;
|
395
|
441
|
e_steps--;
|
396
|
|
- CRITICAL_SECTION_END;
|
397
|
442
|
}
|
398
|
443
|
else {
|
399
|
|
- CRITICAL_SECTION_START;
|
400
|
444
|
e_steps++;
|
401
|
|
- CRITICAL_SECTION_END;
|
402
|
445
|
}
|
403
|
446
|
}
|
404
|
|
- // Do E steps + advance steps
|
405
|
|
- e_steps += ((advance >> 16) - old_advance);
|
406
|
|
- old_advance = advance >> 16;
|
407
|
447
|
#endif //ADVANCE
|
408
|
448
|
|
409
|
449
|
counter_x += current_block->steps_x;
|
|
@@ -462,6 +502,11 @@ ISR(TIMER1_COMPA_vect)
|
462
|
502
|
for(int8_t i=0; i < step_loops; i++) {
|
463
|
503
|
advance += advance_rate;
|
464
|
504
|
}
|
|
505
|
+ //if(advance > current_block->advance) advance = current_block->advance;
|
|
506
|
+ // Do E steps + advance steps
|
|
507
|
+ e_steps += ((advance >>8) - old_advance);
|
|
508
|
+ old_advance = advance >>8;
|
|
509
|
+
|
465
|
510
|
#endif
|
466
|
511
|
}
|
467
|
512
|
else if (step_events_completed > (unsigned long int)current_block->decelerate_after) {
|
|
@@ -486,8 +531,10 @@ ISR(TIMER1_COMPA_vect)
|
486
|
531
|
for(int8_t i=0; i < step_loops; i++) {
|
487
|
532
|
advance -= advance_rate;
|
488
|
533
|
}
|
489
|
|
- if(advance < final_advance)
|
490
|
|
- advance = final_advance;
|
|
534
|
+ if(advance < final_advance) advance = final_advance;
|
|
535
|
+ // Do E steps + advance steps
|
|
536
|
+ e_steps += ((advance >>8) - old_advance);
|
|
537
|
+ old_advance = advance >>8;
|
491
|
538
|
#endif //ADVANCE
|
492
|
539
|
}
|
493
|
540
|
else {
|
|
@@ -508,7 +555,7 @@ ISR(TIMER1_COMPA_vect)
|
508
|
555
|
// Timer 0 is shared with millies
|
509
|
556
|
ISR(TIMER0_COMPA_vect)
|
510
|
557
|
{
|
511
|
|
- old_OCR0A += 25; // ~10kHz interrupt
|
|
558
|
+ old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
|
512
|
559
|
OCR0A = old_OCR0A;
|
513
|
560
|
// Set E direction (Depends on E direction + advance)
|
514
|
561
|
for(unsigned char i=0; i<4;) {
|
|
@@ -520,7 +567,7 @@ ISR(TIMER1_COMPA_vect)
|
520
|
567
|
e_steps++;
|
521
|
568
|
WRITE(E_STEP_PIN, HIGH);
|
522
|
569
|
}
|
523
|
|
- if (e_steps > 0) {
|
|
570
|
+ else if (e_steps > 0) {
|
524
|
571
|
WRITE(E_DIR_PIN,!INVERT_E_DIR);
|
525
|
572
|
e_steps--;
|
526
|
573
|
WRITE(E_STEP_PIN, HIGH);
|
|
@@ -649,6 +696,13 @@ void st_init()
|
649
|
696
|
e_steps = 0;
|
650
|
697
|
TIMSK0 |= (1<<OCIE0A);
|
651
|
698
|
#endif //ADVANCE
|
|
699
|
+
|
|
700
|
+ #ifdef ENDSTOPS_ONLY_FOR_HOMING
|
|
701
|
+ enable_endstops(false);
|
|
702
|
+ #else
|
|
703
|
+ enable_endstops(true);
|
|
704
|
+ #endif
|
|
705
|
+
|
652
|
706
|
sei();
|
653
|
707
|
}
|
654
|
708
|
|