Browse Source

Merge pull request #4980 from thinkyhead/rc_lin_update

LIN_ADVANCE bug fix and optimization
Scott Lahteine 8 years ago
parent
commit
bbeaca5839
5 changed files with 64 additions and 37 deletions
  1. 2
    3
      Marlin/Marlin_main.cpp
  2. 40
    27
      Marlin/stepper.cpp
  3. 1
    1
      Marlin/stepper.h
  4. 20
    5
      Marlin/temperature.cpp
  5. 1
    1
      Marlin/ultralcd.cpp

+ 2
- 3
Marlin/Marlin_main.cpp View File

4899
       for (uint8_t j = 0; j <= n; j++) sum += sample_set[j];
4899
       for (uint8_t j = 0; j <= n; j++) sum += sample_set[j];
4900
       mean = sum / (n + 1);
4900
       mean = sum / (n + 1);
4901
 
4901
 
4902
-      if(sample_set[n] < min) min = sample_set[n];
4903
-      if(sample_set[n] > max) max = sample_set[n];
4902
+      NOMORE(min, sample_set[n]);
4903
+      NOLESS(max, sample_set[n]);
4904
 
4904
 
4905
       /**
4905
       /**
4906
        * Now, use that mean to calculate the standard deviation for the
4906
        * Now, use that mean to calculate the standard deviation for the
4956
     SERIAL_PROTOCOLPGM("Standard Deviation: ");
4956
     SERIAL_PROTOCOLPGM("Standard Deviation: ");
4957
     SERIAL_PROTOCOL_F(sigma, 6);
4957
     SERIAL_PROTOCOL_F(sigma, 6);
4958
     SERIAL_EOL;
4958
     SERIAL_EOL;
4959
-
4960
     SERIAL_EOL;
4959
     SERIAL_EOL;
4961
 
4960
 
4962
     clean_up_after_endstop_or_probe_move();
4961
     clean_up_after_endstop_or_probe_move();

+ 40
- 27
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],
311
   #endif // !ADVANCE && !LIN_ADVANCE
311
   #endif // !ADVANCE && !LIN_ADVANCE
312
 }
312
 }
313
 
313
 
314
-// "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
315
-// It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
314
+/**
315
+ * Stepper Driver Interrupt
316
+ *
317
+ * Directly pulses the stepper motors at high frequency.
318
+ * Timer 1 runs at a base frequency of 2MHz, with this ISR using OCR1A compare mode.
319
+ *
320
+ * OCR1A   Frequency
321
+ *     1     2 MHz
322
+ *    50    40 KHz
323
+ *   100    20 KHz - capped max rate
324
+ *   200    10 KHz - nominal max rate
325
+ *  2000     1 KHz - sleep rate
326
+ *  4000   500  Hz - init rate
327
+ */
316
 ISR(TIMER1_COMPA_vect) { Stepper::isr(); }
328
 ISR(TIMER1_COMPA_vect) { Stepper::isr(); }
317
 
329
 
318
 void Stepper::isr() {
330
 void Stepper::isr() {
323
       if ((cleaning_buffer_counter == 1) && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
335
       if ((cleaning_buffer_counter == 1) && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
324
     #endif
336
     #endif
325
     cleaning_buffer_counter--;
337
     cleaning_buffer_counter--;
326
-    OCR1A = 200;
338
+    OCR1A = 200; // Run at max speed - 10 KHz
327
     return;
339
     return;
328
   }
340
   }
329
 
341
 
348
       #if ENABLED(Z_LATE_ENABLE)
360
       #if ENABLED(Z_LATE_ENABLE)
349
         if (current_block->steps[Z_AXIS] > 0) {
361
         if (current_block->steps[Z_AXIS] > 0) {
350
           enable_z();
362
           enable_z();
351
-          OCR1A = 2000; //1ms wait
363
+          OCR1A = 2000; // Run at slow speed - 1 KHz
352
           return;
364
           return;
353
         }
365
         }
354
       #endif
366
       #endif
358
       // #endif
370
       // #endif
359
     }
371
     }
360
     else {
372
     else {
361
-      OCR1A = 2000; // 1kHz.
373
+      OCR1A = 2000; // Run at slow speed - 1 KHz
362
       return;
374
       return;
363
     }
375
     }
364
   }
376
   }
391
 
403
 
392
       #if ENABLED(MIXING_EXTRUDER)
404
       #if ENABLED(MIXING_EXTRUDER)
393
         // Step mixing steppers proportionally
405
         // Step mixing steppers proportionally
394
-        bool dir = motor_direction(E_AXIS);
406
+        const bool dir = motor_direction(E_AXIS);
395
         MIXING_STEPPERS_LOOP(j) {
407
         MIXING_STEPPERS_LOOP(j) {
396
           counter_m[j] += current_block->steps[E_AXIS];
408
           counter_m[j] += current_block->steps[E_AXIS];
397
           if (counter_m[j] > 0) {
409
           if (counter_m[j] > 0) {
401
         }
413
         }
402
       #endif
414
       #endif
403
 
415
 
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)
416
     #elif ENABLED(ADVANCE)
421
 
417
 
422
       // Always count the unified E axis
418
       // Always count the unified E axis
432
       #if ENABLED(MIXING_EXTRUDER)
428
       #if ENABLED(MIXING_EXTRUDER)
433
 
429
 
434
         // Step mixing steppers proportionally
430
         // Step mixing steppers proportionally
435
-        bool dir = motor_direction(E_AXIS);
431
+        const bool dir = motor_direction(E_AXIS);
436
         MIXING_STEPPERS_LOOP(j) {
432
         MIXING_STEPPERS_LOOP(j) {
437
           counter_m[j] += current_block->steps[E_AXIS];
433
           counter_m[j] += current_block->steps[E_AXIS];
438
           if (counter_m[j] > 0) {
434
           if (counter_m[j] > 0) {
536
     }
532
     }
537
   }
533
   }
538
 
534
 
535
+  #if ENABLED(LIN_ADVANCE)
536
+    if (current_block->use_advance_lead) {
537
+      int delta_adv_steps = (((long)extruder_advance_k * current_estep_rate[TOOL_E_INDEX]) >> 9) - current_adv_steps[TOOL_E_INDEX];
538
+      current_adv_steps[TOOL_E_INDEX] += delta_adv_steps;
539
+      #if ENABLED(MIXING_EXTRUDER)
540
+        // Mixing extruders apply advance lead proportionally
541
+        MIXING_STEPPERS_LOOP(j)
542
+          e_steps[j] += delta_adv_steps * current_block->step_event_count / current_block->mix_event_count[j];
543
+      #else
544
+        // For most extruders, advance the single E stepper
545
+        e_steps[TOOL_E_INDEX] += delta_adv_steps;
546
+      #endif
547
+   }
548
+  #endif
549
+  
539
   #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
550
   #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
540
     // If we have esteps to execute, fire the next advance_isr "now"
551
     // If we have esteps to execute, fire the next advance_isr "now"
541
     if (e_steps[TOOL_E_INDEX]) OCR0A = TCNT0 + 2;
552
     if (e_steps[TOOL_E_INDEX]) OCR0A = TCNT0 + 2;
593
     #endif // ADVANCE or LIN_ADVANCE
604
     #endif // ADVANCE or LIN_ADVANCE
594
 
605
 
595
     #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
606
     #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
596
-      eISR_Rate = (timer >> 2) * step_loops / abs(e_steps[TOOL_E_INDEX]);
607
+      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
608
     #endif
598
   }
609
   }
599
   else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
610
   else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
643
     #endif // ADVANCE or LIN_ADVANCE
654
     #endif // ADVANCE or LIN_ADVANCE
644
 
655
 
645
     #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
656
     #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
646
-      eISR_Rate = (timer >> 2) * step_loops / abs(e_steps[TOOL_E_INDEX]);
657
+      eISR_Rate = (timer >> 3) * step_loops / abs(e_steps[TOOL_E_INDEX]);
647
     #endif
658
     #endif
648
   }
659
   }
649
   else {
660
   else {
653
       if (current_block->use_advance_lead)
664
       if (current_block->use_advance_lead)
654
         current_estep_rate[TOOL_E_INDEX] = final_estep_rate;
665
         current_estep_rate[TOOL_E_INDEX] = final_estep_rate;
655
 
666
 
656
-      eISR_Rate = (OCR1A_nominal >> 2) * step_loops_nominal / abs(e_steps[TOOL_E_INDEX]);
667
+      eISR_Rate = (OCR1A_nominal >> 3) * step_loops_nominal / abs(e_steps[TOOL_E_INDEX]);
657
 
668
 
658
     #endif
669
     #endif
659
 
670
 
904
   // output mode = 00 (disconnected)
915
   // output mode = 00 (disconnected)
905
   TCCR1A &= ~(3 << COM1A0);
916
   TCCR1A &= ~(3 << COM1A0);
906
   TCCR1A &= ~(3 << COM1B0);
917
   TCCR1A &= ~(3 << COM1B0);
918
+
907
   // Set the timer pre-scaler
919
   // Set the timer pre-scaler
908
   // Generally we use a divider of 8, resulting in a 2MHz timer
920
   // Generally we use a divider of 8, resulting in a 2MHz timer
909
   // frequency on a 16MHz MCU. If you are going to change this, be
921
   // frequency on a 16MHz MCU. If you are going to change this, be
911
   // create_speed_lookuptable.py
923
   // create_speed_lookuptable.py
912
   TCCR1B = (TCCR1B & ~(0x07 << CS10)) | (2 << CS10);
924
   TCCR1B = (TCCR1B & ~(0x07 << CS10)) | (2 << CS10);
913
 
925
 
926
+  // Init Stepper ISR to 122 Hz for quick starting
914
   OCR1A = 0x4000;
927
   OCR1A = 0x4000;
915
   TCNT1 = 0;
928
   TCNT1 = 0;
916
   ENABLE_STEPPER_DRIVER_INTERRUPT();
929
   ENABLE_STEPPER_DRIVER_INTERRUPT();

+ 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]

+ 20
- 5
Marlin/temperature.cpp View File

1371
  * Timer 0 is shared with millies so don't change the prescaler.
1371
  * Timer 0 is shared with millies so don't change the prescaler.
1372
  *
1372
  *
1373
  * This ISR uses the compare method so it runs at the base
1373
  * This ISR uses the compare method so it runs at the base
1374
- * frequency (16 MHz / 256 = 62500 Hz), but at the TCNT0 set
1374
+ * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
1375
  * in OCR0B above (128 or halfway between OVFs).
1375
  * in OCR0B above (128 or halfway between OVFs).
1376
  *
1376
  *
1377
  *  - Manage PWM to all the heaters and fan
1377
  *  - Manage PWM to all the heaters and fan
1485
       #endif
1485
       #endif
1486
     #endif
1486
     #endif
1487
 
1487
 
1488
-    // 488.28 Hz (or 1:976.56, 2:1953.12, 3:3906.25, 4:7812.5, 5:7812.5 6:15625, 6:15625 7:31250)
1488
+    // SOFT_PWM_SCALE to frequency:
1489
+    //
1490
+    // 0: 16000000/64/256/128 =   7.6294 Hz
1491
+    // 1:                / 64 =  15.2588 Hz
1492
+    // 2:                / 32 =  30.5176 Hz
1493
+    // 3:                / 16 =  61.0352 Hz
1494
+    // 4:                /  8 = 122.0703 Hz
1495
+    // 5:                /  4 = 244.1406 Hz
1489
     pwm_count += _BV(SOFT_PWM_SCALE);
1496
     pwm_count += _BV(SOFT_PWM_SCALE);
1490
-    pwm_count &= 0x7f;
1497
+    pwm_count &= 0x7F;
1491
 
1498
 
1492
   #else // SLOW_PWM_HEATERS
1499
   #else // SLOW_PWM_HEATERS
1493
 
1500
 
1586
       #endif
1593
       #endif
1587
     #endif //FAN_SOFT_PWM
1594
     #endif //FAN_SOFT_PWM
1588
 
1595
 
1596
+    // SOFT_PWM_SCALE to frequency:
1597
+    //
1598
+    // 0: 16000000/64/256/128 =   7.6294 Hz
1599
+    // 1:                / 64 =  15.2588 Hz
1600
+    // 2:                / 32 =  30.5176 Hz
1601
+    // 3:                / 16 =  61.0352 Hz
1602
+    // 4:                /  8 = 122.0703 Hz
1603
+    // 5:                /  4 = 244.1406 Hz
1589
     pwm_count += _BV(SOFT_PWM_SCALE);
1604
     pwm_count += _BV(SOFT_PWM_SCALE);
1590
-    pwm_count &= 0x7f;
1605
+    pwm_count &= 0x7F;
1591
 
1606
 
1592
-    // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
1607
+    // increment slow_pwm_count only every 64 pwm_count (e.g., every 8s)
1593
     if ((pwm_count % 64) == 0) {
1608
     if ((pwm_count % 64) == 0) {
1594
       slow_pwm_count++;
1609
       slow_pwm_count++;
1595
       slow_pwm_count &= 0x7f;
1610
       slow_pwm_count &= 0x7f;

+ 1
- 1
Marlin/ultralcd.cpp View File

223
     static int8_t _countedItems = 0; \
223
     static int8_t _countedItems = 0; \
224
     int8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \
224
     int8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \
225
     if (_countedItems > 0 && encoderLine >= _countedItems - LIMIT) { \
225
     if (_countedItems > 0 && encoderLine >= _countedItems - LIMIT) { \
226
-      encoderLine = _countedItems - LIMIT; \
226
+      encoderLine = max(0, _countedItems - LIMIT); \
227
       encoderPosition = encoderLine * (ENCODER_STEPS_PER_MENU_ITEM); \
227
       encoderPosition = encoderLine * (ENCODER_STEPS_PER_MENU_ITEM); \
228
     }
228
     }
229
 
229
 

Loading…
Cancel
Save