Browse Source

Replace block flag bools with flag bits

…and apply const to some method parameters
Scott Lahteine 8 years ago
parent
commit
8e1cc9332a
2 changed files with 51 additions and 48 deletions
  1. 25
    34
      Marlin/planner.cpp
  2. 26
    14
      Marlin/planner.h

+ 25
- 34
Marlin/planner.cpp View File

@@ -149,7 +149,7 @@ void Planner::init() {
149 149
  * Calculate trapezoid parameters, multiplying the entry- and exit-speeds
150 150
  * by the provided factors.
151 151
  */
152
-void Planner::calculate_trapezoid_for_block(block_t* block, float entry_factor, float exit_factor) {
152
+void Planner::calculate_trapezoid_for_block(block_t* const block, const float &entry_factor, const float &exit_factor) {
153 153
   uint32_t initial_rate = ceil(block->nominal_rate * entry_factor),
154 154
            final_rate = ceil(block->nominal_rate * exit_factor); // (steps per second)
155 155
 
@@ -203,29 +203,20 @@ 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* current, block_t* next) {
207
-  if (!current) return;
208
-
209
-  if (next) {
210
-    // If entry speed is already at the maximum entry speed, no need to recheck. Block is cruising.
211
-    // If not, block in state of acceleration or deceleration. Reset entry speed to maximum and
212
-    // check for maximum allowable speed reductions to ensure maximum possible planned speed.
213
-    float max_entry_speed = current->max_entry_speed;
214
-    if (current->entry_speed != max_entry_speed) {
215
-
216
-      // If nominal length true, max junction speed is guaranteed to be reached. Only compute
217
-      // for max allowable speed if block is decelerating and nominal length is false.
218
-      if (!current->nominal_length_flag && max_entry_speed > next->entry_speed) {
219
-        current->entry_speed = min(max_entry_speed,
220
-                                   max_allowable_speed(-current->acceleration, next->entry_speed, current->millimeters));
221
-      }
222
-      else {
223
-        current->entry_speed = max_entry_speed;
224
-      }
225
-      current->recalculate_flag = true;
226
-
227
-    }
228
-  } // Skip last block. Already initialized and set for recalculation.
206
+void Planner::reverse_pass_kernel(block_t* const current, const block_t *next) {
207
+  if (!current || !next) return;
208
+  // If entry speed is already at the maximum entry speed, no need to recheck. Block is cruising.
209
+  // If not, block in state of acceleration or deceleration. Reset entry speed to maximum and
210
+  // check for maximum allowable speed reductions to ensure maximum possible planned speed.
211
+  float max_entry_speed = current->max_entry_speed;
212
+  if (current->entry_speed != max_entry_speed) {
213
+    // If nominal length true, max junction speed is guaranteed to be reached. Only compute
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)
216
+      ? max_entry_speed
217
+      : min(max_entry_speed, max_allowable_speed(-current->acceleration, next->entry_speed, current->millimeters));
218
+    current->flag |= BLOCK_FLAG_RECALCULATE;
219
+  }
229 220
 }
230 221
 
231 222
 /**
@@ -255,21 +246,21 @@ void Planner::reverse_pass() {
255 246
 }
256 247
 
257 248
 // The kernel called by recalculate() when scanning the plan from first to last entry.
258
-void Planner::forward_pass_kernel(block_t* previous, block_t* current) {
249
+void Planner::forward_pass_kernel(const block_t* previous, block_t* const current) {
259 250
   if (!previous) return;
260 251
 
261 252
   // If the previous block is an acceleration block, but it is not long enough to complete the
262 253
   // full speed change within the block, we need to adjust the entry speed accordingly. Entry
263 254
   // speeds have already been reset, maximized, and reverse planned by reverse planner.
264 255
   // If nominal length is true, max junction speed is guaranteed to be reached. No need to recheck.
265
-  if (!previous->nominal_length_flag) {
256
+  if (!(previous->flag & BLOCK_FLAG_NOMINAL_LENGTH)) {
266 257
     if (previous->entry_speed < current->entry_speed) {
267 258
       float entry_speed = min(current->entry_speed,
268 259
                                max_allowable_speed(-previous->acceleration, previous->entry_speed, previous->millimeters));
269 260
       // Check for junction speed change
270 261
       if (current->entry_speed != entry_speed) {
271 262
         current->entry_speed = entry_speed;
272
-        current->recalculate_flag = true;
263
+        current->flag |= BLOCK_FLAG_RECALCULATE;
273 264
       }
274 265
     }
275 266
   }
@@ -298,19 +289,18 @@ void Planner::forward_pass() {
298 289
  */
299 290
 void Planner::recalculate_trapezoids() {
300 291
   int8_t block_index = block_buffer_tail;
301
-  block_t* current;
302
-  block_t* next = NULL;
292
+  block_t *current, *next = NULL;
303 293
 
304 294
   while (block_index != block_buffer_head) {
305 295
     current = next;
306 296
     next = &block_buffer[block_index];
307 297
     if (current) {
308 298
       // Recalculate if current block entry or exit junction speed has changed.
309
-      if (current->recalculate_flag || next->recalculate_flag) {
299
+      if ((current->flag & BLOCK_FLAG_RECALCULATE) || (next->flag & BLOCK_FLAG_RECALCULATE)) {
310 300
         // NOTE: Entry and exit factors always > 0 by all previous logic operations.
311 301
         float nom = current->nominal_speed;
312 302
         calculate_trapezoid_for_block(current, current->entry_speed / nom, next->entry_speed / nom);
313
-        current->recalculate_flag = false; // Reset current only to ensure next trapezoid is computed
303
+        current->flag &= ~BLOCK_FLAG_RECALCULATE; // Reset current only to ensure next trapezoid is computed
314 304
       }
315 305
     }
316 306
     block_index = next_block_index(block_index);
@@ -319,7 +309,7 @@ void Planner::recalculate_trapezoids() {
319 309
   if (next) {
320 310
     float nom = next->nominal_speed;
321 311
     calculate_trapezoid_for_block(next, next->entry_speed / nom, (MINIMUM_PLANNER_SPEED) / nom);
322
-    next->recalculate_flag = false;
312
+    next->flag &= ~BLOCK_FLAG_RECALCULATE;
323 313
   }
324 314
 }
325 315
 
@@ -1119,8 +1109,9 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
1119 1109
   // block nominal speed limits both the current and next maximum junction speeds. Hence, in both
1120 1110
   // the reverse and forward planners, the corresponding block junction speed will always be at the
1121 1111
   // the maximum junction speed and may always be ignored for any speed reduction checks.
1122
-  block->nominal_length_flag = (block->nominal_speed <= v_allowable);
1123
-  block->recalculate_flag = true; // Always calculate trapezoid for new block
1112
+  block->flag &= ~BLOCK_FLAG_NOMINAL_LENGTH;
1113
+  if (block->nominal_speed <= v_allowable) block->flag |= BLOCK_FLAG_NOMINAL_LENGTH;
1114
+  block->flag |= BLOCK_FLAG_RECALCULATE; // Always calculate trapezoid for new block
1124 1115
 
1125 1116
   // Update previous path unit_vector and nominal speed
1126 1117
   memcpy(previous_speed, current_speed, sizeof(previous_speed));

+ 26
- 14
Marlin/planner.h View File

@@ -40,6 +40,19 @@
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),
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_FLAG_NOMINAL_LENGTH       = _BV(1),
51
+
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)
54
+};
55
+
43 56
 /**
44 57
  * struct block_t
45 58
  *
@@ -79,19 +92,18 @@ typedef struct {
79 92
   #endif
80 93
 
81 94
   // Fields used by the motion planner to manage acceleration
82
-  float nominal_speed,                               // The nominal speed for this block in mm/sec
83
-        entry_speed,                                 // Entry speed at previous-current junction in mm/sec
84
-        max_entry_speed,                             // Maximum allowable junction entry speed in mm/sec
85
-        millimeters,                                 // The total travel of this block in mm
86
-        acceleration;                                // acceleration mm/sec^2
87
-  unsigned char recalculate_flag,                    // Planner flag to recalculate trapezoids on entry junction
88
-                nominal_length_flag;                 // Planner flag for nominal speed always reached
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)
89 101
 
90 102
   // Settings for the trapezoid generator
91
-  unsigned long nominal_rate,                        // The nominal step rate for this block in step_events/sec
92
-                initial_rate,                        // The jerk-adjusted step rate at start of block
93
-                final_rate,                          // The minimal rate at exit
94
-                acceleration_steps_per_s2;           // acceleration steps/sec^2
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
95 107
 
96 108
   #if FAN_COUNT > 0
97 109
     unsigned long fan_speed[FAN_COUNT];
@@ -379,10 +391,10 @@ class Planner {
379 391
       return sqrt(sq(target_velocity) - 2 * accel * distance);
380 392
     }
381 393
 
382
-    static void calculate_trapezoid_for_block(block_t* block, float entry_factor, float exit_factor);
394
+    static void calculate_trapezoid_for_block(block_t* const block, const float &entry_factor, const float &exit_factor);
383 395
 
384
-    static void reverse_pass_kernel(block_t* current, block_t* next);
385
-    static void forward_pass_kernel(block_t* previous, block_t* current);
396
+    static void reverse_pass_kernel(block_t* const current, const block_t *next);
397
+    static void forward_pass_kernel(const block_t *previous, block_t* const current);
386 398
 
387 399
     static void reverse_pass();
388 400
     static void forward_pass();

Loading…
Cancel
Save