|
@@ -89,13 +89,24 @@ long Stepper::counter_X = 0,
|
89
|
89
|
|
90
|
90
|
volatile unsigned long Stepper::step_events_completed = 0; // The number of step events executed in the current block
|
91
|
91
|
|
92
|
|
-#if ENABLED(ADVANCE)
|
|
92
|
+#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
|
|
93
|
+
|
93
|
94
|
unsigned char Stepper::old_OCR0A;
|
94
|
|
- long Stepper::final_advance = 0,
|
95
|
|
- Stepper::old_advance = 0,
|
96
|
|
- Stepper::e_steps[EXTRUDERS],
|
97
|
|
- Stepper::advance_rate,
|
98
|
|
- Stepper::advance;
|
|
95
|
+ volatile unsigned char Stepper::eISR_Rate = 200; // Keep the ISR at a low rate until needed
|
|
96
|
+
|
|
97
|
+ #if ENABLED(LIN_ADVANCE)
|
|
98
|
+ volatile int Stepper::e_steps[EXTRUDERS];
|
|
99
|
+ int Stepper::extruder_advance_k = LIN_ADVANCE_K,
|
|
100
|
+ Stepper::final_estep_rate,
|
|
101
|
+ Stepper::current_estep_rate[EXTRUDERS],
|
|
102
|
+ Stepper::current_adv_steps[EXTRUDERS];
|
|
103
|
+ #else
|
|
104
|
+ long Stepper::e_steps[EXTRUDERS],
|
|
105
|
+ Stepper::final_advance = 0,
|
|
106
|
+ Stepper::old_advance = 0,
|
|
107
|
+ Stepper::advance_rate,
|
|
108
|
+ Stepper::advance;
|
|
109
|
+ #endif
|
99
|
110
|
#endif
|
100
|
111
|
|
101
|
112
|
long Stepper::acceleration_time, Stepper::deceleration_time;
|
|
@@ -344,14 +355,32 @@ void Stepper::isr() {
|
344
|
355
|
customizedSerial.checkRx(); // Check for serial chars.
|
345
|
356
|
#endif
|
346
|
357
|
|
347
|
|
- #if ENABLED(ADVANCE)
|
|
358
|
+ #if ENABLED(LIN_ADVANCE)
|
|
359
|
+
|
|
360
|
+ counter_E += current_block->steps[E_AXIS];
|
|
361
|
+ if (counter_E > 0) {
|
|
362
|
+ counter_E -= current_block->step_event_count;
|
|
363
|
+ count_position[E_AXIS] += count_direction[E_AXIS];
|
|
364
|
+ e_steps[current_block->active_extruder] += motor_direction(E_AXIS) ? -1 : 1;
|
|
365
|
+ }
|
|
366
|
+
|
|
367
|
+ if (current_block->use_advance_lead) {
|
|
368
|
+ int delta_adv_steps; //Maybe a char would be enough?
|
|
369
|
+ delta_adv_steps = (((long)extruder_advance_k * current_estep_rate[current_block->active_extruder]) >> 9) - current_adv_steps[current_block->active_extruder];
|
|
370
|
+ e_steps[current_block->active_extruder] += delta_adv_steps;
|
|
371
|
+ current_adv_steps[current_block->active_extruder] += delta_adv_steps;
|
|
372
|
+ }
|
|
373
|
+
|
|
374
|
+ #elif ENABLED(ADVANCE)
|
|
375
|
+
|
348
|
376
|
counter_E += current_block->steps[E_AXIS];
|
349
|
377
|
if (counter_E > 0) {
|
350
|
378
|
counter_E -= current_block->step_event_count;
|
351
|
379
|
e_steps[current_block->active_extruder] += motor_direction(E_AXIS) ? -1 : 1;
|
352
|
380
|
}
|
353
|
|
- #endif //ADVANCE
|
354
|
381
|
|
|
382
|
+ #endif // ADVANCE or LIN_ADVANCE
|
|
383
|
+
|
355
|
384
|
#define _COUNTER(AXIS) counter_## AXIS
|
356
|
385
|
#define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
|
357
|
386
|
#define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
|
|
@@ -363,7 +392,7 @@ void Stepper::isr() {
|
363
|
392
|
STEP_ADD(X);
|
364
|
393
|
STEP_ADD(Y);
|
365
|
394
|
STEP_ADD(Z);
|
366
|
|
- #if DISABLED(ADVANCE)
|
|
395
|
+ #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
|
367
|
396
|
STEP_ADD(E);
|
368
|
397
|
#endif
|
369
|
398
|
|
|
@@ -377,13 +406,19 @@ void Stepper::isr() {
|
377
|
406
|
STEP_IF_COUNTER(X);
|
378
|
407
|
STEP_IF_COUNTER(Y);
|
379
|
408
|
STEP_IF_COUNTER(Z);
|
380
|
|
- #if DISABLED(ADVANCE)
|
|
409
|
+ #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
|
381
|
410
|
STEP_IF_COUNTER(E);
|
382
|
411
|
#endif
|
383
|
412
|
|
384
|
413
|
step_events_completed++;
|
385
|
414
|
if (step_events_completed >= current_block->step_event_count) break;
|
386
|
415
|
}
|
|
416
|
+
|
|
417
|
+ #if ENABLED(LIN_ADVANCE)
|
|
418
|
+ // If we have esteps to execute, fire the next ISR "now"
|
|
419
|
+ if (e_steps[current_block->active_extruder]) OCR0A = TCNT0 + 2;
|
|
420
|
+ #endif
|
|
421
|
+
|
387
|
422
|
// Calculate new timer value
|
388
|
423
|
unsigned short timer, step_rate;
|
389
|
424
|
if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
|
|
@@ -399,7 +434,12 @@ void Stepper::isr() {
|
399
|
434
|
OCR1A = timer;
|
400
|
435
|
acceleration_time += timer;
|
401
|
436
|
|
402
|
|
- #if ENABLED(ADVANCE)
|
|
437
|
+ #if ENABLED(LIN_ADVANCE)
|
|
438
|
+
|
|
439
|
+ if (current_block->use_advance_lead)
|
|
440
|
+ current_estep_rate[current_block->active_extruder] = ((unsigned long)acc_step_rate * current_block->e_speed_multiplier8) >> 8;
|
|
441
|
+
|
|
442
|
+ #elif ENABLED(ADVANCE)
|
403
|
443
|
|
404
|
444
|
advance += advance_rate * step_loops;
|
405
|
445
|
//NOLESS(advance, current_block->advance);
|
|
@@ -408,7 +448,11 @@ void Stepper::isr() {
|
408
|
448
|
e_steps[current_block->active_extruder] += ((advance >> 8) - old_advance);
|
409
|
449
|
old_advance = advance >> 8;
|
410
|
450
|
|
411
|
|
- #endif //ADVANCE
|
|
451
|
+ #endif // ADVANCE or LIN_ADVANCE
|
|
452
|
+
|
|
453
|
+ #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
|
|
454
|
+ eISR_Rate = (timer >> 2) / abs(e_steps[current_block->active_extruder]);
|
|
455
|
+ #endif
|
412
|
456
|
}
|
413
|
457
|
else if (step_events_completed > (unsigned long)current_block->decelerate_after) {
|
414
|
458
|
MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
|
|
@@ -424,8 +468,14 @@ void Stepper::isr() {
|
424
|
468
|
timer = calc_timer(step_rate);
|
425
|
469
|
OCR1A = timer;
|
426
|
470
|
deceleration_time += timer;
|
|
471
|
+
|
|
472
|
+ #if ENABLED(LIN_ADVANCE)
|
|
473
|
+
|
|
474
|
+ if (current_block->use_advance_lead)
|
|
475
|
+ current_estep_rate[current_block->active_extruder] = ((unsigned long)step_rate * current_block->e_speed_multiplier8) >> 8;
|
|
476
|
+
|
|
477
|
+ #elif ENABLED(ADVANCE)
|
427
|
478
|
|
428
|
|
- #if ENABLED(ADVANCE)
|
429
|
479
|
advance -= advance_rate * step_loops;
|
430
|
480
|
NOLESS(advance, final_advance);
|
431
|
481
|
|
|
@@ -433,9 +483,24 @@ void Stepper::isr() {
|
433
|
483
|
uint32_t advance_whole = advance >> 8;
|
434
|
484
|
e_steps[current_block->active_extruder] += advance_whole - old_advance;
|
435
|
485
|
old_advance = advance_whole;
|
436
|
|
- #endif //ADVANCE
|
|
486
|
+
|
|
487
|
+ #endif // ADVANCE or LIN_ADVANCE
|
|
488
|
+
|
|
489
|
+ #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
|
|
490
|
+ eISR_Rate = (timer >> 2) / abs(e_steps[current_block->active_extruder]);
|
|
491
|
+ #endif
|
437
|
492
|
}
|
438
|
493
|
else {
|
|
494
|
+
|
|
495
|
+ #if ENABLED(LIN_ADVANCE)
|
|
496
|
+
|
|
497
|
+ if (current_block->use_advance_lead)
|
|
498
|
+ current_estep_rate[current_block->active_extruder] = final_estep_rate;
|
|
499
|
+
|
|
500
|
+ eISR_Rate = (OCR1A_nominal >> 2) / abs(e_steps[current_block->active_extruder]);
|
|
501
|
+
|
|
502
|
+ #endif
|
|
503
|
+
|
439
|
504
|
OCR1A = OCR1A_nominal;
|
440
|
505
|
// ensure we're running at the correct step rate, even if we just came off an acceleration
|
441
|
506
|
step_loops = step_loops_nominal;
|
|
@@ -451,13 +516,15 @@ void Stepper::isr() {
|
451
|
516
|
}
|
452
|
517
|
}
|
453
|
518
|
|
454
|
|
-#if ENABLED(ADVANCE)
|
|
519
|
+#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
|
|
520
|
+
|
455
|
521
|
// Timer interrupt for E. e_steps is set in the main routine;
|
456
|
522
|
// Timer 0 is shared with millies
|
457
|
523
|
ISR(TIMER0_COMPA_vect) { Stepper::advance_isr(); }
|
458
|
524
|
|
459
|
525
|
void Stepper::advance_isr() {
|
460
|
|
- old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
|
|
526
|
+
|
|
527
|
+ old_OCR0A += eISR_Rate;
|
461
|
528
|
OCR0A = old_OCR0A;
|
462
|
529
|
|
463
|
530
|
#define STEP_E_ONCE(INDEX) \
|
|
@@ -474,22 +541,21 @@ void Stepper::isr() {
|
474
|
541
|
E## INDEX ##_STEP_WRITE(!INVERT_E_STEP_PIN); \
|
475
|
542
|
}
|
476
|
543
|
|
477
|
|
- // Step all E steppers that have steps, up to 4 steps per interrupt
|
478
|
|
- for (unsigned char i = 0; i < 4; i++) {
|
479
|
|
- STEP_E_ONCE(0);
|
480
|
|
- #if EXTRUDERS > 1
|
481
|
|
- STEP_E_ONCE(1);
|
482
|
|
- #if EXTRUDERS > 2
|
483
|
|
- STEP_E_ONCE(2);
|
484
|
|
- #if EXTRUDERS > 3
|
485
|
|
- STEP_E_ONCE(3);
|
486
|
|
- #endif
|
|
544
|
+ // Step all E steppers that have steps
|
|
545
|
+ STEP_E_ONCE(0);
|
|
546
|
+ #if EXTRUDERS > 1
|
|
547
|
+ STEP_E_ONCE(1);
|
|
548
|
+ #if EXTRUDERS > 2
|
|
549
|
+ STEP_E_ONCE(2);
|
|
550
|
+ #if EXTRUDERS > 3
|
|
551
|
+ STEP_E_ONCE(3);
|
487
|
552
|
#endif
|
488
|
553
|
#endif
|
489
|
|
- }
|
|
554
|
+ #endif
|
|
555
|
+
|
490
|
556
|
}
|
491
|
557
|
|
492
|
|
-#endif // ADVANCE
|
|
558
|
+#endif // ADVANCE or LIN_ADVANCE
|
493
|
559
|
|
494
|
560
|
void Stepper::init() {
|
495
|
561
|
|
|
@@ -656,14 +722,28 @@ void Stepper::init() {
|
656
|
722
|
TCNT1 = 0;
|
657
|
723
|
ENABLE_STEPPER_DRIVER_INTERRUPT();
|
658
|
724
|
|
659
|
|
- #if ENABLED(ADVANCE)
|
|
725
|
+ #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
|
|
726
|
+
|
|
727
|
+ #if ENABLED(LIN_ADVANCE)
|
|
728
|
+
|
|
729
|
+ for (int i = 0; i < EXTRUDERS; i++) {
|
|
730
|
+ e_steps[i] = 0;
|
|
731
|
+ current_adv_steps[i] = 0;
|
|
732
|
+ }
|
|
733
|
+
|
|
734
|
+ #elif ENABLED(ADVANCE)
|
|
735
|
+
|
|
736
|
+ for (uint8_t i = 0; i < EXTRUDERS; i++) e_steps[i] = 0;
|
|
737
|
+
|
|
738
|
+ #endif
|
|
739
|
+
|
660
|
740
|
#if defined(TCCR0A) && defined(WGM01)
|
661
|
741
|
CBI(TCCR0A, WGM01);
|
662
|
742
|
CBI(TCCR0A, WGM00);
|
663
|
743
|
#endif
|
664
|
|
- for (uint8_t i = 0; i < EXTRUDERS; i++) e_steps[i] = 0;
|
665
|
744
|
SBI(TIMSK0, OCIE0A);
|
666
|
|
- #endif //ADVANCE
|
|
745
|
+
|
|
746
|
+ #endif // ADVANCE or LIN_ADVANCE
|
667
|
747
|
|
668
|
748
|
endstops.enable(true); // Start with endstops active. After homing they can be disabled
|
669
|
749
|
sei();
|
|
@@ -1040,3 +1120,14 @@ void Stepper::microstep_readings() {
|
1040
|
1120
|
SERIAL_PROTOCOLLN(digitalRead(E1_MS2_PIN));
|
1041
|
1121
|
#endif
|
1042
|
1122
|
}
|
|
1123
|
+
|
|
1124
|
+#if ENABLED(LIN_ADVANCE)
|
|
1125
|
+
|
|
1126
|
+ void Stepper::advance_M905() {
|
|
1127
|
+ if (code_seen('K')) extruder_advance_k = code_value();
|
|
1128
|
+ SERIAL_ECHO_START;
|
|
1129
|
+ SERIAL_ECHOPAIR("Advance factor: ", extruder_advance_k);
|
|
1130
|
+ SERIAL_EOL;
|
|
1131
|
+ }
|
|
1132
|
+
|
|
1133
|
+#endif // LIN_ADVANCE
|