浏览代码

Merge pull request #4738 from thinkyhead/rc_ensure_floats

Optimize stepper ISRs, plus cleanup, shorthand
Scott Lahteine 8 年前
父节点
当前提交
c3caa42630
共有 6 个文件被更改,包括 97 次插入59 次删除
  1. 6
    1
      Marlin/Conditionals_post.h
  2. 12
    17
      Marlin/Marlin_main.cpp
  3. 5
    7
      Marlin/planner.cpp
  4. 2
    2
      Marlin/planner.h
  5. 71
    31
      Marlin/stepper.cpp
  6. 1
    1
      Marlin/stepper.h

+ 6
- 1
Marlin/Conditionals_post.h 查看文件

@@ -35,11 +35,14 @@
35 35
   #endif
36 36
 
37 37
   /**
38
-   * Axis lengths
38
+   * Axis lengths and center
39 39
    */
40 40
   #define X_MAX_LENGTH (X_MAX_POS - (X_MIN_POS))
41 41
   #define Y_MAX_LENGTH (Y_MAX_POS - (Y_MIN_POS))
42 42
   #define Z_MAX_LENGTH (Z_MAX_POS - (Z_MIN_POS))
43
+  #define X_CENTER float((X_MIN_POS + X_MAX_POS) * 0.5)
44
+  #define Y_CENTER float((Y_MIN_POS + Y_MAX_POS) * 0.5)
45
+  #define Z_CENTER float((Z_MIN_POS + Z_MAX_POS) * 0.5)
43 46
 
44 47
   /**
45 48
    * CoreXY and CoreXZ
@@ -127,6 +130,8 @@
127 130
    */
128 131
   #define HAS_PROBING_PROCEDURE (ENABLED(AUTO_BED_LEVELING_FEATURE) || ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST))
129 132
 
133
+  #define HOMING_Z_WITH_PROBE (HAS_BED_PROBE && Z_HOME_DIR < 0 && ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN))
134
+
130 135
   // Boundaries for probing based on set limits
131 136
   #define MIN_PROBE_X (max(X_MIN_POS, X_MIN_POS + X_PROBE_OFFSET_FROM_EXTRUDER))
132 137
   #define MAX_PROBE_X (min(X_MAX_POS, X_MAX_POS + X_PROBE_OFFSET_FROM_EXTRUDER))

+ 12
- 17
Marlin/Marlin_main.cpp 查看文件

@@ -1586,7 +1586,7 @@ static void set_axis_is_at_home(AxisEnum axis) {
1586 1586
 
1587 1587
     if (axis == Z_AXIS) {
1588 1588
       #if HAS_BED_PROBE && Z_HOME_DIR < 0
1589
-        #if DISABLED(Z_MIN_PROBE_ENDSTOP)
1589
+        #if HOMING_Z_WITH_PROBE
1590 1590
           current_position[Z_AXIS] -= zprobe_zoffset;
1591 1591
           #if ENABLED(DEBUG_LEVELING_FEATURE)
1592 1592
             if (DEBUGGING(LEVELING)) {
@@ -2049,8 +2049,8 @@ static void clean_up_after_endstop_or_probe_move() {
2049 2049
     #endif
2050 2050
   #endif
2051 2051
 
2052
-  #define DEPLOY_PROBE() set_probe_deployed( true )
2053
-  #define STOW_PROBE() set_probe_deployed( false )
2052
+  #define DEPLOY_PROBE() set_probe_deployed(true)
2053
+  #define STOW_PROBE() set_probe_deployed(false)
2054 2054
 
2055 2055
   // returns false for ok and true for failure
2056 2056
   static bool set_probe_deployed(bool deploy) {
@@ -2073,8 +2073,8 @@ static void clean_up_after_endstop_or_probe_move() {
2073 2073
       if (axis_unhomed_error(true, true,  true )) { stop(); return true; }
2074 2074
     #endif
2075 2075
 
2076
-    float oldXpos = current_position[X_AXIS]; // save x position
2077
-    float oldYpos = current_position[Y_AXIS]; // save y position
2076
+    float oldXpos = current_position[X_AXIS],
2077
+          oldYpos = current_position[Y_AXIS];
2078 2078
 
2079 2079
     #ifdef _TRIGGERED_WHEN_STOWED_TEST
2080 2080
 
@@ -2430,10 +2430,10 @@ static void do_homing_move(AxisEnum axis, float where, float fr_mm_s = 0.0) {
2430 2430
 #define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
2431 2431
 
2432 2432
 static void homeaxis(AxisEnum axis) {
2433
-  #define HOMEAXIS_DO(LETTER) \
2434
-    ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
2433
+  #define CAN_HOME(A) \
2434
+    (axis == A##_AXIS && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
2435 2435
 
2436
-  if (!(axis == X_AXIS ? HOMEAXIS_DO(X) : axis == Y_AXIS ? HOMEAXIS_DO(Y) : axis == Z_AXIS ? HOMEAXIS_DO(Z) : false)) return;
2436
+  if (!CAN_HOME(X) && !CAN_HOME(Y) && !CAN_HOME(Z)) return;
2437 2437
 
2438 2438
   #if ENABLED(DEBUG_LEVELING_FEATURE)
2439 2439
     if (DEBUGGING(LEVELING)) {
@@ -2449,7 +2449,7 @@ static void homeaxis(AxisEnum axis) {
2449 2449
     home_dir(axis);
2450 2450
 
2451 2451
   // Homing Z towards the bed? Deploy the Z probe or endstop.
2452
-  #if HAS_BED_PROBE && Z_HOME_DIR < 0 && DISABLED(Z_MIN_PROBE_ENDSTOP)
2452
+  #if HOMING_Z_WITH_PROBE
2453 2453
     if (axis == Z_AXIS) {
2454 2454
       #if ENABLED(DEBUG_LEVELING_FEATURE)
2455 2455
         if (DEBUGGING(LEVELING)) SERIAL_ECHOPGM("> ");
@@ -2532,7 +2532,7 @@ static void homeaxis(AxisEnum axis) {
2532 2532
   #endif
2533 2533
 
2534 2534
   // Put away the Z probe
2535
-  #if HAS_BED_PROBE && Z_HOME_DIR < 0 && DISABLED(Z_MIN_PROBE_ENDSTOP)
2535
+  #if HOMING_Z_WITH_PROBE
2536 2536
     if (axis == Z_AXIS) {
2537 2537
       #if ENABLED(DEBUG_LEVELING_FEATURE)
2538 2538
         if (DEBUGGING(LEVELING)) SERIAL_ECHOPGM("> ");
@@ -3104,9 +3104,7 @@ inline void gcode_G28() {
3104 3104
         #if ENABLED(Z_SAFE_HOMING)
3105 3105
 
3106 3106
           #if ENABLED(DEBUG_LEVELING_FEATURE)
3107
-            if (DEBUGGING(LEVELING)) {
3108
-              SERIAL_ECHOLNPGM("> Z_SAFE_HOMING >>>");
3109
-            }
3107
+            if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("> Z_SAFE_HOMING >>>");
3110 3108
           #endif
3111 3109
 
3112 3110
           if (home_all_axis) {
@@ -3127,10 +3125,7 @@ inline void gcode_G28() {
3127 3125
             destination[Z_AXIS] = current_position[Z_AXIS]; // Z is already at the right height
3128 3126
 
3129 3127
             #if ENABLED(DEBUG_LEVELING_FEATURE)
3130
-              if (DEBUGGING(LEVELING)) {
3131
-                DEBUG_POS("> Z_SAFE_HOMING > home_all_axis", current_position);
3132
-                DEBUG_POS("> Z_SAFE_HOMING > home_all_axis", destination);
3133
-              }
3128
+              if (DEBUGGING(LEVELING)) DEBUG_POS("> Z_SAFE_HOMING > home_all_axis", destination);
3134 3129
             #endif
3135 3130
 
3136 3131
             // Move in the XY plane

+ 5
- 7
Marlin/planner.cpp 查看文件

@@ -203,9 +203,8 @@ void Planner::calculate_trapezoid_for_block(block_t* block, float entry_factor,
203 203
 
204 204
 
205 205
 // The kernel called by recalculate() when scanning the plan from last to first entry.
206
-void Planner::reverse_pass_kernel(block_t* previous, block_t* current, block_t* next) {
206
+void Planner::reverse_pass_kernel(block_t* current, block_t* next) {
207 207
   if (!current) return;
208
-  UNUSED(previous);
209 208
 
210 209
   if (next) {
211 210
     // If entry speed is already at the maximum entry speed, no need to recheck. Block is cruising.
@@ -250,15 +249,14 @@ void Planner::reverse_pass() {
250 249
       block[2] = block[1];
251 250
       block[1] = block[0];
252 251
       block[0] = &block_buffer[b];
253
-      reverse_pass_kernel(block[0], block[1], block[2]);
252
+      reverse_pass_kernel(block[1], block[2]);
254 253
     }
255 254
   }
256 255
 }
257 256
 
258 257
 // The kernel called by recalculate() when scanning the plan from first to last entry.
259
-void Planner::forward_pass_kernel(block_t* previous, block_t* current, block_t* next) {
258
+void Planner::forward_pass_kernel(block_t* previous, block_t* current) {
260 259
   if (!previous) return;
261
-  UNUSED(next);
262 260
 
263 261
   // If the previous block is an acceleration block, but it is not long enough to complete the
264 262
   // full speed change within the block, we need to adjust the entry speed accordingly. Entry
@@ -288,9 +286,9 @@ void Planner::forward_pass() {
288 286
     block[0] = block[1];
289 287
     block[1] = block[2];
290 288
     block[2] = &block_buffer[b];
291
-    forward_pass_kernel(block[0], block[1], block[2]);
289
+    forward_pass_kernel(block[0], block[1]);
292 290
   }
293
-  forward_pass_kernel(block[1], block[2], NULL);
291
+  forward_pass_kernel(block[1], block[2]);
294 292
 }
295 293
 
296 294
 /**

+ 2
- 2
Marlin/planner.h 查看文件

@@ -320,8 +320,8 @@ class Planner {
320 320
 
321 321
     static void calculate_trapezoid_for_block(block_t* block, float entry_factor, float exit_factor);
322 322
 
323
-    static void reverse_pass_kernel(block_t* previous, block_t* current, block_t* next);
324
-    static void forward_pass_kernel(block_t* previous, block_t* current, block_t* next);
323
+    static void reverse_pass_kernel(block_t* current, block_t* next);
324
+    static void forward_pass_kernel(block_t* previous, block_t* current);
325 325
 
326 326
     static void reverse_pass();
327 327
     static void forward_pass();

+ 71
- 31
Marlin/stepper.cpp 查看文件

@@ -87,7 +87,7 @@ long  Stepper::counter_X = 0,
87 87
       Stepper::counter_Z = 0,
88 88
       Stepper::counter_E = 0;
89 89
 
90
-volatile unsigned long Stepper::step_events_completed = 0; // The number of step events executed in the current block
90
+volatile uint32_t Stepper::step_events_completed = 0; // The number of step events executed in the current block
91 91
 
92 92
 #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
93 93
 
@@ -372,6 +372,7 @@ void Stepper::isr() {
372 372
     ) endstops.update();
373 373
 
374 374
     // Take multiple steps per interrupt (For high speed moves)
375
+    bool all_steps_done = false;
375 376
     for (int8_t i = 0; i < step_loops; i++) {
376 377
       #ifndef USBCON
377 378
         customizedSerial.checkRx(); // Check for serial chars.
@@ -385,7 +386,7 @@ void Stepper::isr() {
385 386
           #if DISABLED(MIXING_EXTRUDER)
386 387
             // Don't step E here for mixing extruder
387 388
             count_position[E_AXIS] += count_direction[E_AXIS];
388
-            e_steps[TOOL_E_INDEX] += motor_direction(E_AXIS) ? -1 : 1;
389
+            motor_direction(E_AXIS) ? --e_steps[TOOL_E_INDEX] : ++e_steps[TOOL_E_INDEX];
389 390
           #endif
390 391
         }
391 392
 
@@ -449,10 +450,12 @@ void Stepper::isr() {
449 450
       #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
450 451
       #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
451 452
 
453
+      // Advance the Bresenham counter; start a pulse if the axis needs a step
452 454
       #define PULSE_START(AXIS) \
453 455
         _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
454 456
         if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
455 457
 
458
+      // Stop an active pulse, reset the Bresenham counter, update the position
456 459
       #define PULSE_STOP(AXIS) \
457 460
         if (_COUNTER(AXIS) > 0) { \
458 461
           _COUNTER(AXIS) -= current_block->step_event_count; \
@@ -460,6 +463,7 @@ void Stepper::isr() {
460 463
           _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
461 464
         }
462 465
 
466
+      // If a minimum pulse time was specified get the CPU clock
463 467
       #if MINIMUM_STEPPER_PULSE > 0
464 468
         static uint32_t pulse_start;
465 469
         pulse_start = TCNT0;
@@ -475,6 +479,7 @@ void Stepper::isr() {
475 479
         PULSE_START(Z);
476 480
       #endif
477 481
 
482
+      // For non-advance use linear interpolation for E also
478 483
       #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
479 484
         #if ENABLED(MIXING_EXTRUDER)
480 485
           // Keep updating the single E axis
@@ -491,6 +496,7 @@ void Stepper::isr() {
491 496
         #endif
492 497
       #endif // !ADVANCE && !LIN_ADVANCE
493 498
 
499
+      // For a minimum pulse time wait before stopping pulses
494 500
       #if MINIMUM_STEPPER_PULSE > 0
495 501
         #define CYCLES_EATEN_BY_CODE 10
496 502
         while ((uint32_t)(TCNT0 - pulse_start) < (MINIMUM_STEPPER_PULSE * (F_CPU / 1000000UL)) - CYCLES_EATEN_BY_CODE) { /* nada */ }
@@ -524,18 +530,20 @@ void Stepper::isr() {
524 530
         #endif
525 531
       #endif // !ADVANCE && !LIN_ADVANCE
526 532
 
527
-      step_events_completed++;
528
-      if (step_events_completed >= current_block->step_event_count) break;
533
+      if (++step_events_completed >= current_block->step_event_count) {
534
+        all_steps_done = true;
535
+        break;
536
+      }
529 537
     }
530 538
 
531 539
     #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
532
-      // If we have esteps to execute, fire the next ISR "now"
540
+      // If we have esteps to execute, fire the next advance_isr "now"
533 541
       if (e_steps[TOOL_E_INDEX]) OCR0A = TCNT0 + 2;
534 542
     #endif
535 543
 
536 544
     // Calculate new timer value
537
-    unsigned short timer, step_rate;
538
-    if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
545
+    uint16_t timer, step_rate;
546
+    if (step_events_completed <= (uint32_t)current_block->accelerate_until) {
539 547
 
540 548
       MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
541 549
       acc_step_rate += current_block->initial_rate;
@@ -551,14 +559,14 @@ void Stepper::isr() {
551 559
       #if ENABLED(LIN_ADVANCE)
552 560
 
553 561
         if (current_block->use_advance_lead)
554
-          current_estep_rate[TOOL_E_INDEX] = ((unsigned long)acc_step_rate * current_block->e_speed_multiplier8) >> 8;
562
+          current_estep_rate[TOOL_E_INDEX] = ((uint32_t)acc_step_rate * current_block->e_speed_multiplier8) >> 8;
555 563
 
556 564
         if (current_block->use_advance_lead) {
557 565
           #if ENABLED(MIXING_EXTRUDER)
558 566
             MIXING_STEPPERS_LOOP(j)
559
-              current_estep_rate[j] = ((unsigned long)acc_step_rate * current_block->e_speed_multiplier8 * current_block->step_event_count / current_block->mix_event_count[j]) >> 8;
567
+              current_estep_rate[j] = ((uint32_t)acc_step_rate * current_block->e_speed_multiplier8 * current_block->step_event_count / current_block->mix_event_count[j]) >> 8;
560 568
           #else
561
-            current_estep_rate[TOOL_E_INDEX] = ((unsigned long)acc_step_rate * current_block->e_speed_multiplier8) >> 8;
569
+            current_estep_rate[TOOL_E_INDEX] = ((uint32_t)acc_step_rate * current_block->e_speed_multiplier8) >> 8;
562 570
           #endif
563 571
         }
564 572
 
@@ -588,10 +596,10 @@ void Stepper::isr() {
588 596
         eISR_Rate = (timer >> 2) * step_loops / abs(e_steps[TOOL_E_INDEX]);
589 597
       #endif
590 598
     }
591
-    else if (step_events_completed > (unsigned long)current_block->decelerate_after) {
599
+    else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
592 600
       MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
593 601
 
594
-      if (step_rate <= acc_step_rate) { // Still decelerating?
602
+      if (step_rate < acc_step_rate) { // Still decelerating?
595 603
         step_rate = acc_step_rate - step_rate;
596 604
         NOLESS(step_rate, current_block->final_rate);
597 605
       }
@@ -608,9 +616,9 @@ void Stepper::isr() {
608 616
         if (current_block->use_advance_lead) {
609 617
           #if ENABLED(MIXING_EXTRUDER)
610 618
             MIXING_STEPPERS_LOOP(j)
611
-              current_estep_rate[j] = ((unsigned long)step_rate * current_block->e_speed_multiplier8 * current_block->step_event_count / current_block->mix_event_count[j]) >> 8;
619
+              current_estep_rate[j] = ((uint32_t)step_rate * current_block->e_speed_multiplier8 * current_block->step_event_count / current_block->mix_event_count[j]) >> 8;
612 620
           #else
613
-            current_estep_rate[TOOL_E_INDEX] = ((unsigned long)step_rate * current_block->e_speed_multiplier8) >> 8;
621
+            current_estep_rate[TOOL_E_INDEX] = ((uint32_t)step_rate * current_block->e_speed_multiplier8) >> 8;
614 622
           #endif
615 623
         }
616 624
 
@@ -654,10 +662,10 @@ void Stepper::isr() {
654 662
       step_loops = step_loops_nominal;
655 663
     }
656 664
 
657
-    OCR1A = (OCR1A < (TCNT1 + 16)) ? (TCNT1 + 16) : OCR1A;
665
+    NOLESS(OCR1A, TCNT1 + 16);
658 666
 
659 667
     // If current block is finished, reset pointer
660
-    if (step_events_completed >= current_block->step_event_count) {
668
+    if (all_steps_done) {
661 669
       current_block = NULL;
662 670
       planner.discard_current_block();
663 671
     }
@@ -675,29 +683,61 @@ void Stepper::isr() {
675 683
     old_OCR0A += eISR_Rate;
676 684
     OCR0A = old_OCR0A;
677 685
 
678
-    #define STEP_E_ONCE(INDEX) \
679
-      if (e_steps[INDEX] != 0) { \
680
-        E## INDEX ##_STEP_WRITE(INVERT_E_STEP_PIN); \
681
-        if (e_steps[INDEX] < 0) { \
682
-          E## INDEX ##_DIR_WRITE(INVERT_E## INDEX ##_DIR); \
683
-          e_steps[INDEX]++; \
684
-        } \
685
-        else { \
686
-          E## INDEX ##_DIR_WRITE(!INVERT_E## INDEX ##_DIR); \
687
-          e_steps[INDEX]--; \
688
-        } \
686
+    #define SET_E_STEP_DIR(INDEX) \
687
+      E## INDEX ##_DIR_WRITE(e_steps[INDEX] <= 0 ? INVERT_E## INDEX ##_DIR : !INVERT_E## INDEX ##_DIR)
688
+
689
+    #define START_E_PULSE(INDEX) \
690
+      if (e_steps[INDEX]) E## INDEX ##_STEP_WRITE(INVERT_E_STEP_PIN)
691
+
692
+    #define STOP_E_PULSE(INDEX) \
693
+      if (e_steps[INDEX]) { \
694
+        e_steps[INDEX] < 0 ? ++e_steps[INDEX] : --e_steps[INDEX]; \
689 695
         E## INDEX ##_STEP_WRITE(!INVERT_E_STEP_PIN); \
690 696
       }
691 697
 
698
+    SET_E_STEP_DIR(0);
699
+    #if E_STEPPERS > 1
700
+      SET_E_STEP_DIR(1);
701
+      #if E_STEPPERS > 2
702
+        SET_E_STEP_DIR(2);
703
+        #if E_STEPPERS > 3
704
+          SET_E_STEP_DIR(3);
705
+        #endif
706
+      #endif
707
+    #endif
708
+
692 709
     // Step all E steppers that have steps
693 710
     for (uint8_t i = 0; i < step_loops; i++) {
694
-      STEP_E_ONCE(0);
711
+
712
+      #if MINIMUM_STEPPER_PULSE > 0
713
+        static uint32_t pulse_start;
714
+        pulse_start = TCNT0;
715
+      #endif
716
+
717
+      START_E_PULSE(0);
718
+      #if E_STEPPERS > 1
719
+        START_E_PULSE(1);
720
+        #if E_STEPPERS > 2
721
+          START_E_PULSE(2);
722
+          #if E_STEPPERS > 3
723
+            START_E_PULSE(3);
724
+          #endif
725
+        #endif
726
+      #endif
727
+
728
+      // For a minimum pulse time wait before stopping pulses
729
+      #if MINIMUM_STEPPER_PULSE > 0
730
+        #define CYCLES_EATEN_BY_E 10
731
+        while ((uint32_t)(TCNT0 - pulse_start) < (MINIMUM_STEPPER_PULSE * (F_CPU / 1000000UL)) - CYCLES_EATEN_BY_E) { /* nada */ }
732
+      #endif
733
+
734
+      STOP_E_PULSE(0);
695 735
       #if E_STEPPERS > 1
696
-        STEP_E_ONCE(1);
736
+        STOP_E_PULSE(1);
697 737
         #if E_STEPPERS > 2
698
-          STEP_E_ONCE(2);
738
+          STOP_E_PULSE(2);
699 739
           #if E_STEPPERS > 3
700
-            STEP_E_ONCE(3);
740
+            STOP_E_PULSE(3);
701 741
           #endif
702 742
         #endif
703 743
       #endif

+ 1
- 1
Marlin/stepper.h 查看文件

@@ -102,7 +102,7 @@ class Stepper {
102 102
 
103 103
     // Counter variables for the Bresenham line tracer
104 104
     static long counter_X, counter_Y, counter_Z, counter_E;
105
-    static volatile unsigned long step_events_completed; // The number of step events executed in the current block
105
+    static volatile uint32_t step_events_completed; // The number of step events executed in the current block
106 106
 
107 107
     #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
108 108
       static unsigned char old_OCR0A;

正在加载...
取消
保存