Преглед на файлове

Merge pull request #4980 from thinkyhead/rc_lin_update

LIN_ADVANCE bug fix and optimization
Scott Lahteine преди 8 години
родител
ревизия
bbeaca5839
променени са 5 файла, в които са добавени 64 реда и са изтрити 37 реда
  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 Целия файл

@@ -4899,8 +4899,8 @@ inline void gcode_M42() {
4899 4899
       for (uint8_t j = 0; j <= n; j++) sum += sample_set[j];
4900 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 4906
        * Now, use that mean to calculate the standard deviation for the
@@ -4956,7 +4956,6 @@ inline void gcode_M42() {
4956 4956
     SERIAL_PROTOCOLPGM("Standard Deviation: ");
4957 4957
     SERIAL_PROTOCOL_F(sigma, 6);
4958 4958
     SERIAL_EOL;
4959
-
4960 4959
     SERIAL_EOL;
4961 4960
 
4962 4961
     clean_up_after_endstop_or_probe_move();

+ 40
- 27
Marlin/stepper.cpp Целия файл

@@ -95,7 +95,7 @@ volatile uint32_t Stepper::step_events_completed = 0; // The number of step even
95 95
   volatile unsigned char Stepper::eISR_Rate = 200; // Keep the ISR at a low rate until needed
96 96
 
97 97
   #if ENABLED(LIN_ADVANCE)
98
-    volatile long Stepper::e_steps[E_STEPPERS];
98
+    volatile int Stepper::e_steps[E_STEPPERS];
99 99
     int Stepper::extruder_advance_k = LIN_ADVANCE_K,
100 100
         Stepper::final_estep_rate,
101 101
         Stepper::current_estep_rate[E_STEPPERS],
@@ -311,8 +311,20 @@ void Stepper::set_directions() {
311 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 328
 ISR(TIMER1_COMPA_vect) { Stepper::isr(); }
317 329
 
318 330
 void Stepper::isr() {
@@ -323,7 +335,7 @@ void Stepper::isr() {
323 335
       if ((cleaning_buffer_counter == 1) && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
324 336
     #endif
325 337
     cleaning_buffer_counter--;
326
-    OCR1A = 200;
338
+    OCR1A = 200; // Run at max speed - 10 KHz
327 339
     return;
328 340
   }
329 341
 
@@ -348,7 +360,7 @@ void Stepper::isr() {
348 360
       #if ENABLED(Z_LATE_ENABLE)
349 361
         if (current_block->steps[Z_AXIS] > 0) {
350 362
           enable_z();
351
-          OCR1A = 2000; //1ms wait
363
+          OCR1A = 2000; // Run at slow speed - 1 KHz
352 364
           return;
353 365
         }
354 366
       #endif
@@ -358,7 +370,7 @@ void Stepper::isr() {
358 370
       // #endif
359 371
     }
360 372
     else {
361
-      OCR1A = 2000; // 1kHz.
373
+      OCR1A = 2000; // Run at slow speed - 1 KHz
362 374
       return;
363 375
     }
364 376
   }
@@ -391,7 +403,7 @@ void Stepper::isr() {
391 403
 
392 404
       #if ENABLED(MIXING_EXTRUDER)
393 405
         // Step mixing steppers proportionally
394
-        bool dir = motor_direction(E_AXIS);
406
+        const bool dir = motor_direction(E_AXIS);
395 407
         MIXING_STEPPERS_LOOP(j) {
396 408
           counter_m[j] += current_block->steps[E_AXIS];
397 409
           if (counter_m[j] > 0) {
@@ -401,22 +413,6 @@ void Stepper::isr() {
401 413
         }
402 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 416
     #elif ENABLED(ADVANCE)
421 417
 
422 418
       // Always count the unified E axis
@@ -432,7 +428,7 @@ void Stepper::isr() {
432 428
       #if ENABLED(MIXING_EXTRUDER)
433 429
 
434 430
         // Step mixing steppers proportionally
435
-        bool dir = motor_direction(E_AXIS);
431
+        const bool dir = motor_direction(E_AXIS);
436 432
         MIXING_STEPPERS_LOOP(j) {
437 433
           counter_m[j] += current_block->steps[E_AXIS];
438 434
           if (counter_m[j] > 0) {
@@ -536,6 +532,21 @@ void Stepper::isr() {
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 550
   #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
540 551
     // If we have esteps to execute, fire the next advance_isr "now"
541 552
     if (e_steps[TOOL_E_INDEX]) OCR0A = TCNT0 + 2;
@@ -593,7 +604,7 @@ void Stepper::isr() {
593 604
     #endif // ADVANCE or LIN_ADVANCE
594 605
 
595 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 608
     #endif
598 609
   }
599 610
   else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
@@ -643,7 +654,7 @@ void Stepper::isr() {
643 654
     #endif // ADVANCE or LIN_ADVANCE
644 655
 
645 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 658
     #endif
648 659
   }
649 660
   else {
@@ -653,7 +664,7 @@ void Stepper::isr() {
653 664
       if (current_block->use_advance_lead)
654 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 669
     #endif
659 670
 
@@ -904,6 +915,7 @@ void Stepper::init() {
904 915
   // output mode = 00 (disconnected)
905 916
   TCCR1A &= ~(3 << COM1A0);
906 917
   TCCR1A &= ~(3 << COM1B0);
918
+
907 919
   // Set the timer pre-scaler
908 920
   // Generally we use a divider of 8, resulting in a 2MHz timer
909 921
   // frequency on a 16MHz MCU. If you are going to change this, be
@@ -911,6 +923,7 @@ void Stepper::init() {
911 923
   // create_speed_lookuptable.py
912 924
   TCCR1B = (TCCR1B & ~(0x07 << CS10)) | (2 << CS10);
913 925
 
926
+  // Init Stepper ISR to 122 Hz for quick starting
914 927
   OCR1A = 0x4000;
915 928
   TCNT1 = 0;
916 929
   ENABLE_STEPPER_DRIVER_INTERRUPT();

+ 1
- 1
Marlin/stepper.h Целия файл

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

+ 20
- 5
Marlin/temperature.cpp Целия файл

@@ -1371,7 +1371,7 @@ void Temperature::set_current_temp_raw() {
1371 1371
  * Timer 0 is shared with millies so don't change the prescaler.
1372 1372
  *
1373 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 1375
  * in OCR0B above (128 or halfway between OVFs).
1376 1376
  *
1377 1377
  *  - Manage PWM to all the heaters and fan
@@ -1485,9 +1485,16 @@ void Temperature::isr() {
1485 1485
       #endif
1486 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 1496
     pwm_count += _BV(SOFT_PWM_SCALE);
1490
-    pwm_count &= 0x7f;
1497
+    pwm_count &= 0x7F;
1491 1498
 
1492 1499
   #else // SLOW_PWM_HEATERS
1493 1500
 
@@ -1586,10 +1593,18 @@ void Temperature::isr() {
1586 1593
       #endif
1587 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 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 1608
     if ((pwm_count % 64) == 0) {
1594 1609
       slow_pwm_count++;
1595 1610
       slow_pwm_count &= 0x7f;

+ 1
- 1
Marlin/ultralcd.cpp Целия файл

@@ -223,7 +223,7 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to
223 223
     static int8_t _countedItems = 0; \
224 224
     int8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \
225 225
     if (_countedItems > 0 && encoderLine >= _countedItems - LIMIT) { \
226
-      encoderLine = _countedItems - LIMIT; \
226
+      encoderLine = max(0, _countedItems - LIMIT); \
227 227
       encoderPosition = encoderLine * (ENCODER_STEPS_PER_MENU_ITEM); \
228 228
     }
229 229
 

Loading…
Отказ
Запис