Browse Source

Merge pull request #5115 from thinkyhead/rc_buffer_line_wait_later

Optimize buffer_line by calculating before wait-for-free-block
Scott Lahteine 7 years ago
parent
commit
8a4c51f313
3 changed files with 112 additions and 95 deletions
  1. 66
    58
      Marlin/planner.cpp
  2. 45
    36
      Marlin/planner.h
  3. 1
    1
      Marlin/stepper.cpp

+ 66
- 58
Marlin/planner.cpp View File

@@ -180,7 +180,7 @@ void Planner::calculate_trapezoid_for_block(block_t* const block, const float &e
180 180
   // block->decelerate_after = accelerate_steps+plateau_steps;
181 181
 
182 182
   CRITICAL_SECTION_START;  // Fill variables used by the stepper in a critical section
183
-  if (!block->busy) { // Don't update variables if block is busy.
183
+  if (!TEST(block->flag, BLOCK_BIT_BUSY)) { // Don't update variables if block is busy.
184 184
     block->accelerate_until = accelerate_steps;
185 185
     block->decelerate_after = accelerate_steps + plateau_steps;
186 186
     block->initial_rate = initial_rate;
@@ -212,10 +212,10 @@ void Planner::reverse_pass_kernel(block_t* const current, const block_t *next) {
212 212
   if (current->entry_speed != max_entry_speed) {
213 213
     // If nominal length true, max junction speed is guaranteed to be reached. Only compute
214 214
     // for max allowable speed if block is decelerating and nominal length is false.
215
-    current->entry_speed = ((current->flag & BLOCK_FLAG_NOMINAL_LENGTH) || max_entry_speed <= next->entry_speed)
215
+    current->entry_speed = (TEST(current->flag, BLOCK_BIT_NOMINAL_LENGTH) || max_entry_speed <= next->entry_speed)
216 216
       ? max_entry_speed
217 217
       : min(max_entry_speed, max_allowable_speed(-current->acceleration, next->entry_speed, current->millimeters));
218
-    current->flag |= BLOCK_FLAG_RECALCULATE;
218
+    SBI(current->flag, BLOCK_BIT_RECALCULATE);
219 219
   }
220 220
 }
221 221
 
@@ -237,7 +237,7 @@ void Planner::reverse_pass() {
237 237
 
238 238
     uint8_t b = BLOCK_MOD(block_buffer_head - 3);
239 239
     while (b != tail) {
240
-      if (block[0] && (block[0]->flag & BLOCK_FLAG_START_FROM_FULL_HALT)) break;
240
+      if (block[0] && TEST(block[0]->flag, BLOCK_BIT_START_FROM_FULL_HALT)) break;
241 241
       b = prev_block_index(b);
242 242
       block[2] = block[1];
243 243
       block[1] = block[0];
@@ -255,14 +255,14 @@ void Planner::forward_pass_kernel(const block_t* previous, block_t* const curren
255 255
   // full speed change within the block, we need to adjust the entry speed accordingly. Entry
256 256
   // speeds have already been reset, maximized, and reverse planned by reverse planner.
257 257
   // If nominal length is true, max junction speed is guaranteed to be reached. No need to recheck.
258
-  if (!(previous->flag & BLOCK_FLAG_NOMINAL_LENGTH)) {
258
+  if (!TEST(previous->flag, BLOCK_BIT_NOMINAL_LENGTH)) {
259 259
     if (previous->entry_speed < current->entry_speed) {
260 260
       float entry_speed = min(current->entry_speed,
261 261
                                max_allowable_speed(-previous->acceleration, previous->entry_speed, previous->millimeters));
262 262
       // Check for junction speed change
263 263
       if (current->entry_speed != entry_speed) {
264 264
         current->entry_speed = entry_speed;
265
-        current->flag |= BLOCK_FLAG_RECALCULATE;
265
+        SBI(current->flag, BLOCK_BIT_RECALCULATE);
266 266
       }
267 267
     }
268 268
   }
@@ -298,11 +298,11 @@ void Planner::recalculate_trapezoids() {
298 298
     next = &block_buffer[block_index];
299 299
     if (current) {
300 300
       // Recalculate if current block entry or exit junction speed has changed.
301
-      if ((current->flag & BLOCK_FLAG_RECALCULATE) || (next->flag & BLOCK_FLAG_RECALCULATE)) {
301
+      if (TEST(current->flag, BLOCK_BIT_RECALCULATE) || TEST(next->flag, BLOCK_BIT_RECALCULATE)) {
302 302
         // NOTE: Entry and exit factors always > 0 by all previous logic operations.
303 303
         float nom = current->nominal_speed;
304 304
         calculate_trapezoid_for_block(current, current->entry_speed / nom, next->entry_speed / nom);
305
-        current->flag &= ~BLOCK_FLAG_RECALCULATE; // Reset current only to ensure next trapezoid is computed
305
+        CBI(current->flag, BLOCK_BIT_RECALCULATE); // Reset current only to ensure next trapezoid is computed
306 306
       }
307 307
     }
308 308
     block_index = next_block_index(block_index);
@@ -311,7 +311,7 @@ void Planner::recalculate_trapezoids() {
311 311
   if (next) {
312 312
     float nom = next->nominal_speed;
313 313
     calculate_trapezoid_for_block(next, next->entry_speed / nom, (MINIMUM_PLANNER_SPEED) / nom);
314
-    next->flag &= ~BLOCK_FLAG_RECALCULATE;
314
+    CBI(next->flag, BLOCK_BIT_RECALCULATE);
315 315
   }
316 316
 }
317 317
 
@@ -594,12 +594,6 @@ void Planner::check_axes_activity() {
594 594
  *  extruder    - target extruder
595 595
  */
596 596
 void Planner::_buffer_line(const float &a, const float &b, const float &c, const float &e, float fr_mm_s, const uint8_t extruder) {
597
-  // Calculate the buffer head after we push this byte
598
-  int next_buffer_head = next_block_index(block_buffer_head);
599
-
600
-  // If the buffer is full: good! That means we are well ahead of the robot.
601
-  // Rest here until there is room in the buffer.
602
-  while (block_buffer_tail == next_buffer_head) idle();
603 597
 
604 598
   // The target position of the tool in absolute steps
605 599
   // Calculate target position in absolute steps
@@ -662,11 +656,50 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
662 656
     }
663 657
   #endif
664 658
 
659
+  // Compute direction bit-mask for this block
660
+  uint8_t dm = 0;
661
+  #if ENABLED(COREXY)
662
+    if (da < 0) SBI(dm, X_HEAD); // Save the real Extruder (head) direction in X Axis
663
+    if (db < 0) SBI(dm, Y_HEAD); // ...and Y
664
+    if (dc < 0) SBI(dm, Z_AXIS);
665
+    if (da + db < 0) SBI(dm, A_AXIS); // Motor A direction
666
+    if (da - db < 0) SBI(dm, B_AXIS); // Motor B direction
667
+  #elif ENABLED(COREXZ)
668
+    if (da < 0) SBI(dm, X_HEAD); // Save the real Extruder (head) direction in X Axis
669
+    if (db < 0) SBI(dm, Y_AXIS);
670
+    if (dc < 0) SBI(dm, Z_HEAD); // ...and Z
671
+    if (da + dc < 0) SBI(dm, A_AXIS); // Motor A direction
672
+    if (da - dc < 0) SBI(dm, C_AXIS); // Motor C direction
673
+  #elif ENABLED(COREYZ)
674
+    if (da < 0) SBI(dm, X_AXIS);
675
+    if (db < 0) SBI(dm, Y_HEAD); // Save the real Extruder (head) direction in Y Axis
676
+    if (dc < 0) SBI(dm, Z_HEAD); // ...and Z
677
+    if (db + dc < 0) SBI(dm, B_AXIS); // Motor B direction
678
+    if (db - dc < 0) SBI(dm, C_AXIS); // Motor C direction
679
+  #else
680
+    if (da < 0) SBI(dm, X_AXIS);
681
+    if (db < 0) SBI(dm, Y_AXIS);
682
+    if (dc < 0) SBI(dm, Z_AXIS);
683
+  #endif
684
+  if (de < 0) SBI(dm, E_AXIS);
685
+
686
+  int32_t esteps = labs(de) * volumetric_multiplier[extruder] * flow_percentage[extruder] * 0.01 + 0.5;
687
+
688
+  // Calculate the buffer head after we push this byte
689
+  int next_buffer_head = next_block_index(block_buffer_head);
690
+
691
+  // If the buffer is full: good! That means we are well ahead of the robot.
692
+  // Rest here until there is room in the buffer.
693
+  while (block_buffer_tail == next_buffer_head) idle();
694
+
665 695
   // Prepare to set up new block
666 696
   block_t* block = &block_buffer[block_buffer_head];
667 697
 
668
-  // Mark block as not busy (Not executed by the stepper interrupt)
669
-  block->busy = false;
698
+  // Clear all flags, including the "busy" bit
699
+  block->flag = 0;
700
+
701
+  // Set direction bits
702
+  block->direction_bits = dm;
670 703
 
671 704
   // Number of steps for each axis
672 705
   #if ENABLED(COREXY)
@@ -692,15 +725,12 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
692 725
     block->steps[Z_AXIS] = labs(dc);
693 726
   #endif
694 727
 
695
-  block->steps[E_AXIS] = labs(de) * volumetric_multiplier[extruder] * flow_percentage[extruder] * 0.01 + 0.5;
696
-  block->step_event_count = MAX4(block->steps[X_AXIS], block->steps[Y_AXIS], block->steps[Z_AXIS], block->steps[E_AXIS]);
728
+  block->steps[E_AXIS] = esteps;
729
+  block->step_event_count = MAX4(block->steps[X_AXIS], block->steps[Y_AXIS], block->steps[Z_AXIS], esteps);
697 730
 
698 731
   // Bail if this is a zero-length block
699 732
   if (block->step_event_count < MIN_STEPS_PER_SEGMENT) return;
700 733
 
701
-  // Clear the block flags
702
-  block->flag = 0;
703
-
704 734
   // For a mixing extruder, get a magnified step_event_count for each
705 735
   #if ENABLED(MIXING_EXTRUDER)
706 736
     for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
@@ -716,34 +746,6 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
716 746
     block->e_to_p_pressure = baricuda_e_to_p_pressure;
717 747
   #endif
718 748
 
719
-  // Compute direction bit-mask for this block
720
-  uint8_t dm = 0;
721
-  #if ENABLED(COREXY)
722
-    if (da < 0) SBI(dm, X_HEAD); // Save the real Extruder (head) direction in X Axis
723
-    if (db < 0) SBI(dm, Y_HEAD); // ...and Y
724
-    if (dc < 0) SBI(dm, Z_AXIS);
725
-    if (da + db < 0) SBI(dm, A_AXIS); // Motor A direction
726
-    if (da - db < 0) SBI(dm, B_AXIS); // Motor B direction
727
-  #elif ENABLED(COREXZ)
728
-    if (da < 0) SBI(dm, X_HEAD); // Save the real Extruder (head) direction in X Axis
729
-    if (db < 0) SBI(dm, Y_AXIS);
730
-    if (dc < 0) SBI(dm, Z_HEAD); // ...and Z
731
-    if (da + dc < 0) SBI(dm, A_AXIS); // Motor A direction
732
-    if (da - dc < 0) SBI(dm, C_AXIS); // Motor C direction
733
-  #elif ENABLED(COREYZ)
734
-    if (da < 0) SBI(dm, X_AXIS);
735
-    if (db < 0) SBI(dm, Y_HEAD); // Save the real Extruder (head) direction in Y Axis
736
-    if (dc < 0) SBI(dm, Z_HEAD); // ...and Z
737
-    if (db + dc < 0) SBI(dm, B_AXIS); // Motor B direction
738
-    if (db - dc < 0) SBI(dm, C_AXIS); // Motor C direction
739
-  #else
740
-    if (da < 0) SBI(dm, X_AXIS);
741
-    if (db < 0) SBI(dm, Y_AXIS);
742
-    if (dc < 0) SBI(dm, Z_AXIS);
743
-  #endif
744
-  if (de < 0) SBI(dm, E_AXIS);
745
-  block->direction_bits = dm;
746
-
747 749
   block->active_extruder = extruder;
748 750
 
749 751
   //enable active axes
@@ -761,6 +763,12 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
761 763
       enable_z();
762 764
     }
763 765
     if (block->steps[Y_AXIS]) enable_y();
766
+  #elif ENABLED(COREYZ)
767
+    if (block->steps[B_AXIS] || block->steps[C_AXIS]) {
768
+      enable_y();
769
+      enable_z();
770
+    }
771
+    if (block->steps[X_AXIS]) enable_x();
764 772
   #else
765 773
     if (block->steps[X_AXIS]) enable_x();
766 774
     if (block->steps[Y_AXIS]) enable_y();
@@ -770,7 +778,7 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
770 778
   #endif
771 779
 
772 780
   // Enable extruder(s)
773
-  if (block->steps[E_AXIS]) {
781
+  if (esteps) {
774 782
 
775 783
     #if ENABLED(DISABLE_INACTIVE_EXTRUDER) // Enable only the selected extruder
776 784
 
@@ -839,7 +847,7 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
839 847
     #endif
840 848
   }
841 849
 
842
-  if (block->steps[E_AXIS])
850
+  if (esteps)
843 851
     NOLESS(fr_mm_s, min_feedrate_mm_s);
844 852
   else
845 853
     NOLESS(fr_mm_s, min_travel_feedrate_mm_s);
@@ -1037,7 +1045,7 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
1037 1045
     }while(0)
1038 1046
 
1039 1047
     // Start with print or travel acceleration
1040
-    accel = ceil((block->steps[E_AXIS] ? acceleration : travel_acceleration) * steps_per_mm);
1048
+    accel = ceil((esteps ? acceleration : travel_acceleration) * steps_per_mm);
1041 1049
 
1042 1050
     // Limit acceleration per axis
1043 1051
     if (block->step_event_count <= cutoff_long){
@@ -1186,12 +1194,12 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
1186 1194
     if (previous_safe_speed > vmax_junction_threshold && safe_speed > vmax_junction_threshold) {
1187 1195
       // Not coasting. The machine will stop and start the movements anyway,
1188 1196
       // better to start the segment from start.
1189
-      block->flag |= BLOCK_FLAG_START_FROM_FULL_HALT;
1197
+      SBI(block->flag, BLOCK_BIT_START_FROM_FULL_HALT);
1190 1198
       vmax_junction = safe_speed;
1191 1199
     }
1192 1200
   }
1193 1201
   else {
1194
-    block->flag |= BLOCK_FLAG_START_FROM_FULL_HALT;
1202
+    SBI(block->flag, BLOCK_BIT_START_FROM_FULL_HALT);
1195 1203
     vmax_junction = safe_speed;
1196 1204
   }
1197 1205
 
@@ -1224,18 +1232,18 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
1224 1232
     // This leads to an enormous number of advance steps due to a huge e_acceleration.
1225 1233
     // The math is correct, but you don't want a retract move done with advance!
1226 1234
     // So this situation is filtered out here.
1227
-    if (!block->steps[E_AXIS] || (!block->steps[X_AXIS] && !block->steps[Y_AXIS]) || stepper.get_advance_k() == 0 || (uint32_t) block->steps[E_AXIS] == block->step_event_count) {
1235
+    if (!esteps || (!block->steps[X_AXIS] && !block->steps[Y_AXIS]) || stepper.get_advance_k() == 0 || (uint32_t)esteps == block->step_event_count) {
1228 1236
       block->use_advance_lead = false;
1229 1237
     }
1230 1238
     else {
1231 1239
       block->use_advance_lead = true;
1232
-      block->e_speed_multiplier8 = (block->steps[E_AXIS] << 8) / block->step_event_count;
1240
+      block->e_speed_multiplier8 = (esteps << 8) / block->step_event_count;
1233 1241
     }
1234 1242
 
1235 1243
   #elif ENABLED(ADVANCE)
1236 1244
 
1237 1245
     // Calculate advance rate
1238
-    if (!block->steps[E_AXIS] || (!block->steps[X_AXIS] && !block->steps[Y_AXIS] && !block->steps[Z_AXIS])) {
1246
+    if (!esteps || (!block->steps[X_AXIS] && !block->steps[Y_AXIS] && !block->steps[Z_AXIS])) {
1239 1247
       block->advance_rate = 0;
1240 1248
       block->advance = 0;
1241 1249
     }

+ 45
- 36
Marlin/planner.h View File

@@ -40,17 +40,27 @@
40 40
   #include "vector_3.h"
41 41
 #endif
42 42
 
43
-enum BlockFlag {
44
-    // Recalculate trapezoids on entry junction. For optimization.
45
-    BLOCK_FLAG_RECALCULATE          = _BV(0),
43
+enum BlockFlagBit {
44
+  // Recalculate trapezoids on entry junction. For optimization.
45
+  BLOCK_BIT_RECALCULATE,
46
+
47
+  // Nominal speed always reached.
48
+  // i.e., The segment is long enough, so the nominal speed is reachable if accelerating
49
+  // from a safe speed (in consideration of jerking from zero speed).
50
+  BLOCK_BIT_NOMINAL_LENGTH,
46 51
 
47
-    // Nominal speed always reached.
48
-    // i.e., The segment is long enough, so the nominal speed is reachable if accelerating
49
-    // from a safe speed (in consideration of jerking from zero speed).
50
-    BLOCK_FLAG_NOMINAL_LENGTH       = _BV(1),
52
+  // Start from a halt at the start of this block, respecting the maximum allowed jerk.
53
+  BLOCK_BIT_START_FROM_FULL_HALT,
51 54
 
52
-    // Start from a halt at the start of this block, respecting the maximum allowed jerk.
53
-    BLOCK_FLAG_START_FROM_FULL_HALT = _BV(2)
55
+  // The block is busy
56
+  BLOCK_BIT_BUSY
57
+};
58
+
59
+enum BlockFlag {
60
+  BLOCK_FLAG_RECALCULATE          = _BV(BLOCK_BIT_RECALCULATE),
61
+  BLOCK_FLAG_NOMINAL_LENGTH       = _BV(BLOCK_BIT_NOMINAL_LENGTH),
62
+  BLOCK_FLAG_START_FROM_FULL_HALT = _BV(BLOCK_BIT_START_FROM_FULL_HALT),
63
+  BLOCK_FLAG_BUSY                 = _BV(BLOCK_BIT_BUSY)
54 64
 };
55 65
 
56 66
 /**
@@ -64,57 +74,56 @@ enum BlockFlag {
64 74
  */
65 75
 typedef struct {
66 76
 
77
+  uint8_t flag;                             // Block flags (See BlockFlag enum above)
78
+
67 79
   unsigned char active_extruder;            // The extruder to move (if E move)
68 80
 
69
-  // Fields used by the bresenham algorithm for tracing the line
70
-  long steps[NUM_AXIS];                     // Step count along each axis
71
-  unsigned long step_event_count;           // The number of step events required to complete this block
81
+  // Fields used by the Bresenham algorithm for tracing the line
82
+  int32_t steps[NUM_AXIS];                  // Step count along each axis
83
+  uint32_t step_event_count;                // The number of step events required to complete this block
72 84
 
73 85
   #if ENABLED(MIXING_EXTRUDER)
74
-    unsigned long mix_event_count[MIXING_STEPPERS]; // Scaled step_event_count for the mixing steppers
86
+    uint32_t mix_event_count[MIXING_STEPPERS]; // Scaled step_event_count for the mixing steppers
75 87
   #endif
76 88
 
77
-  long accelerate_until,                    // The index of the step event on which to stop acceleration
78
-       decelerate_after,                    // The index of the step event on which to start decelerating
79
-       acceleration_rate;                   // The acceleration rate used for acceleration calculation
89
+  int32_t accelerate_until,                 // The index of the step event on which to stop acceleration
90
+          decelerate_after,                 // The index of the step event on which to start decelerating
91
+          acceleration_rate;                // The acceleration rate used for acceleration calculation
80 92
 
81
-  unsigned char direction_bits;             // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
93
+  uint8_t direction_bits;                   // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
82 94
 
83 95
   // Advance extrusion
84 96
   #if ENABLED(LIN_ADVANCE)
85 97
     bool use_advance_lead;
86
-    int e_speed_multiplier8; // Factorised by 2^8 to avoid float
98
+    int16_t e_speed_multiplier8; // Factorised by 2^8 to avoid float
87 99
   #elif ENABLED(ADVANCE)
88
-    long advance_rate;
89
-    volatile long initial_advance;
90
-    volatile long final_advance;
100
+    int32_t advance_rate;
101
+    volatile int32_t initial_advance;
102
+    volatile int32_t final_advance;
91 103
     float advance;
92 104
   #endif
93 105
 
94 106
   // Fields used by the motion planner to manage acceleration
95
-  float nominal_speed,                          // The nominal speed for this block in mm/sec
96
-        entry_speed,                            // Entry speed at previous-current junction in mm/sec
97
-        max_entry_speed,                        // Maximum allowable junction entry speed in mm/sec
98
-        millimeters,                            // The total travel of this block in mm
99
-        acceleration;                           // acceleration mm/sec^2
100
-  uint8_t flag;                                 // Block flags (See BlockFlag enum above)
107
+  float nominal_speed,                      // The nominal speed for this block in mm/sec
108
+        entry_speed,                        // Entry speed at previous-current junction in mm/sec
109
+        max_entry_speed,                    // Maximum allowable junction entry speed in mm/sec
110
+        millimeters,                        // The total travel of this block in mm
111
+        acceleration;                       // acceleration mm/sec^2
101 112
 
102 113
   // Settings for the trapezoid generator
103
-  uint32_t nominal_rate,                        // The nominal step rate for this block in step_events/sec
104
-           initial_rate,                        // The jerk-adjusted step rate at start of block
105
-           final_rate,                          // The minimal rate at exit
106
-           acceleration_steps_per_s2;           // acceleration steps/sec^2
114
+  uint32_t nominal_rate,                    // The nominal step rate for this block in step_events/sec
115
+           initial_rate,                    // The jerk-adjusted step rate at start of block
116
+           final_rate,                      // The minimal rate at exit
117
+           acceleration_steps_per_s2;       // acceleration steps/sec^2
107 118
 
108 119
   #if FAN_COUNT > 0
109
-    unsigned long fan_speed[FAN_COUNT];
120
+    uint32_t fan_speed[FAN_COUNT];
110 121
   #endif
111 122
 
112 123
   #if ENABLED(BARICUDA)
113
-    unsigned long valve_pressure, e_to_p_pressure;
124
+    uint32_t valve_pressure, e_to_p_pressure;
114 125
   #endif
115 126
 
116
-  volatile char busy;
117
-
118 127
 } block_t;
119 128
 
120 129
 #define BLOCK_MOD(n) ((n)&(BLOCK_BUFFER_SIZE-1))
@@ -341,7 +350,7 @@ class Planner {
341 350
     static block_t* get_current_block() {
342 351
       if (blocks_queued()) {
343 352
         block_t* block = &block_buffer[block_buffer_tail];
344
-        block->busy = true;
353
+        SBI(block->flag, BLOCK_BIT_BUSY);
345 354
         return block;
346 355
       }
347 356
       else

+ 1
- 1
Marlin/stepper.cpp View File

@@ -344,7 +344,7 @@ void Stepper::isr() {
344 344
     // Anything in the buffer?
345 345
     current_block = planner.get_current_block();
346 346
     if (current_block) {
347
-      current_block->busy = true;
347
+      SBI(current_block->flag, BLOCK_BIT_BUSY);
348 348
       trapezoid_generator_reset();
349 349
 
350 350
       // Initialize Bresenham counters to 1/2 the ceiling

Loading…
Cancel
Save