Browse Source

LIN_ADVANCE bug fix and optimization

.) long to int: Extruder stalls at 10kHz / 20kHz step limits with long.
.) Take the delta_adv_steps calculation out of the step_loops loop. Wasted calculation performance if done inside.
.) >> 2 replaced by 3: Is divide by 8. Reason: Timer 0 runs at 16/8=2MHz, Timer 1 at 16/64=0.25MHz. ==> 2/0.25=8.
Sebastianv650 8 years ago
parent
commit
a448cedbc5
2 changed files with 22 additions and 23 deletions
  1. 21
    22
      Marlin/stepper.cpp
  2. 1
    1
      Marlin/stepper.h

+ 21
- 22
Marlin/stepper.cpp View File

95
   volatile unsigned char Stepper::eISR_Rate = 200; // Keep the ISR at a low rate until needed
95
   volatile unsigned char Stepper::eISR_Rate = 200; // Keep the ISR at a low rate until needed
96
 
96
 
97
   #if ENABLED(LIN_ADVANCE)
97
   #if ENABLED(LIN_ADVANCE)
98
-    volatile long Stepper::e_steps[E_STEPPERS];
98
+    volatile int Stepper::e_steps[E_STEPPERS];
99
     int Stepper::extruder_advance_k = LIN_ADVANCE_K,
99
     int Stepper::extruder_advance_k = LIN_ADVANCE_K,
100
         Stepper::final_estep_rate,
100
         Stepper::final_estep_rate,
101
         Stepper::current_estep_rate[E_STEPPERS],
101
         Stepper::current_estep_rate[E_STEPPERS],
391
 
391
 
392
       #if ENABLED(MIXING_EXTRUDER)
392
       #if ENABLED(MIXING_EXTRUDER)
393
         // Step mixing steppers proportionally
393
         // Step mixing steppers proportionally
394
-        bool dir = motor_direction(E_AXIS);
394
+        const bool dir = motor_direction(E_AXIS);
395
         MIXING_STEPPERS_LOOP(j) {
395
         MIXING_STEPPERS_LOOP(j) {
396
           counter_m[j] += current_block->steps[E_AXIS];
396
           counter_m[j] += current_block->steps[E_AXIS];
397
           if (counter_m[j] > 0) {
397
           if (counter_m[j] > 0) {
401
         }
401
         }
402
       #endif
402
       #endif
403
 
403
 
404
-      if (current_block->use_advance_lead) {
405
-        int delta_adv_steps = (((long)extruder_advance_k * current_estep_rate[TOOL_E_INDEX]) >> 9) - current_adv_steps[TOOL_E_INDEX];
406
-        #if ENABLED(MIXING_EXTRUDER)
407
-          // Mixing extruders apply advance lead proportionally
408
-          MIXING_STEPPERS_LOOP(j) {
409
-            int steps = delta_adv_steps * current_block->step_event_count / current_block->mix_event_count[j];
410
-            e_steps[j] += steps;
411
-            current_adv_steps[j] += steps;
412
-          }
413
-        #else
414
-          // For most extruders, advance the single E stepper
415
-          e_steps[TOOL_E_INDEX] += delta_adv_steps;
416
-          current_adv_steps[TOOL_E_INDEX] += delta_adv_steps;
417
-        #endif
418
-      }
419
-
420
     #elif ENABLED(ADVANCE)
404
     #elif ENABLED(ADVANCE)
421
 
405
 
422
       // Always count the unified E axis
406
       // Always count the unified E axis
432
       #if ENABLED(MIXING_EXTRUDER)
416
       #if ENABLED(MIXING_EXTRUDER)
433
 
417
 
434
         // Step mixing steppers proportionally
418
         // Step mixing steppers proportionally
435
-        bool dir = motor_direction(E_AXIS);
419
+        const bool dir = motor_direction(E_AXIS);
436
         MIXING_STEPPERS_LOOP(j) {
420
         MIXING_STEPPERS_LOOP(j) {
437
           counter_m[j] += current_block->steps[E_AXIS];
421
           counter_m[j] += current_block->steps[E_AXIS];
438
           if (counter_m[j] > 0) {
422
           if (counter_m[j] > 0) {
536
     }
520
     }
537
   }
521
   }
538
 
522
 
523
+  #if ENABLED(LIN_ADVANCE)
524
+    if (current_block->use_advance_lead) {
525
+      int delta_adv_steps = (((long)extruder_advance_k * current_estep_rate[TOOL_E_INDEX]) >> 9) - current_adv_steps[TOOL_E_INDEX];
526
+      current_adv_steps[TOOL_E_INDEX] += delta_adv_steps;
527
+      #if ENABLED(MIXING_EXTRUDER)
528
+        // Mixing extruders apply advance lead proportionally
529
+        MIXING_STEPPERS_LOOP(j)
530
+          e_steps[j] += delta_adv_steps * current_block->step_event_count / current_block->mix_event_count[j];
531
+      #else
532
+        // For most extruders, advance the single E stepper
533
+        e_steps[TOOL_E_INDEX] += delta_adv_steps;
534
+      #endif
535
+   }
536
+  #endif
537
+  
539
   #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
538
   #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
540
     // If we have esteps to execute, fire the next advance_isr "now"
539
     // If we have esteps to execute, fire the next advance_isr "now"
541
     if (e_steps[TOOL_E_INDEX]) OCR0A = TCNT0 + 2;
540
     if (e_steps[TOOL_E_INDEX]) OCR0A = TCNT0 + 2;
593
     #endif // ADVANCE or LIN_ADVANCE
592
     #endif // ADVANCE or LIN_ADVANCE
594
 
593
 
595
     #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
594
     #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
596
-      eISR_Rate = (timer >> 2) * step_loops / abs(e_steps[TOOL_E_INDEX]);
595
+      eISR_Rate = (timer >> 3) * step_loops / abs(e_steps[TOOL_E_INDEX]); //>> 3 is divide by 8. Reason: Timer 0 runs at 16/8=2MHz, Timer 1 at 16/64=0.25MHz. ==> 2/0.25=8.
597
     #endif
596
     #endif
598
   }
597
   }
599
   else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
598
   else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
643
     #endif // ADVANCE or LIN_ADVANCE
642
     #endif // ADVANCE or LIN_ADVANCE
644
 
643
 
645
     #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
644
     #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
646
-      eISR_Rate = (timer >> 2) * step_loops / abs(e_steps[TOOL_E_INDEX]);
645
+      eISR_Rate = (timer >> 3) * step_loops / abs(e_steps[TOOL_E_INDEX]);
647
     #endif
646
     #endif
648
   }
647
   }
649
   else {
648
   else {
653
       if (current_block->use_advance_lead)
652
       if (current_block->use_advance_lead)
654
         current_estep_rate[TOOL_E_INDEX] = final_estep_rate;
653
         current_estep_rate[TOOL_E_INDEX] = final_estep_rate;
655
 
654
 
656
-      eISR_Rate = (OCR1A_nominal >> 2) * step_loops_nominal / abs(e_steps[TOOL_E_INDEX]);
655
+      eISR_Rate = (OCR1A_nominal >> 3) * step_loops_nominal / abs(e_steps[TOOL_E_INDEX]);
657
 
656
 
658
     #endif
657
     #endif
659
 
658
 

+ 1
- 1
Marlin/stepper.h View File

108
       static unsigned char old_OCR0A;
108
       static unsigned char old_OCR0A;
109
       static volatile unsigned char eISR_Rate;
109
       static volatile unsigned char eISR_Rate;
110
       #if ENABLED(LIN_ADVANCE)
110
       #if ENABLED(LIN_ADVANCE)
111
-        static volatile long e_steps[E_STEPPERS];
111
+        static volatile int e_steps[E_STEPPERS];
112
         static int extruder_advance_k;
112
         static int extruder_advance_k;
113
         static int final_estep_rate;
113
         static int final_estep_rate;
114
         static int current_estep_rate[E_STEPPERS]; // Actual extruder speed [steps/s]
114
         static int current_estep_rate[E_STEPPERS]; // Actual extruder speed [steps/s]

Loading…
Cancel
Save