瀏覽代碼

Merge pull request #10614 from thinkyhead/bf2_synced_planner_set_position

[2.0.x] Improve sync of planner / stepper position, asynchronous G92
Scott Lahteine 7 年之前
父節點
當前提交
f30241bda5
No account linked to committer's email address

+ 5
- 5
Marlin/src/feature/I2CPositionEncoder.cpp 查看文件

@@ -358,7 +358,7 @@ bool I2CPositionEncoder::test_axis() {
358 358
 
359 359
   stepper.synchronize();
360 360
 
361
-  planner.buffer_line(startCoord[X_AXIS],startCoord[Y_AXIS],startCoord[Z_AXIS],
361
+  planner.buffer_line(startCoord[X_AXIS], startCoord[Y_AXIS], startCoord[Z_AXIS],
362 362
                       stepper.get_axis_position_mm(E_AXIS), feedrate, 0);
363 363
   stepper.synchronize();
364 364
 
@@ -415,10 +415,10 @@ void I2CPositionEncoder::calibrate_steps_mm(const uint8_t iter) {
415 415
   startCoord[encoderAxis] = startDistance;
416 416
   endCoord[encoderAxis] = endDistance;
417 417
 
418
-  LOOP_L_N(i, iter) {
419
-    stepper.synchronize();
418
+  stepper.synchronize();
420 419
 
421
-    planner.buffer_line(startCoord[X_AXIS],startCoord[Y_AXIS],startCoord[Z_AXIS],
420
+  LOOP_L_N(i, iter) {
421
+    planner.buffer_line(startCoord[X_AXIS], startCoord[Y_AXIS], startCoord[Z_AXIS],
422 422
                         stepper.get_axis_position_mm(E_AXIS), feedrate, 0);
423 423
     stepper.synchronize();
424 424
 
@@ -427,7 +427,7 @@ void I2CPositionEncoder::calibrate_steps_mm(const uint8_t iter) {
427 427
 
428 428
     //do_blocking_move_to(endCoord[X_AXIS],endCoord[Y_AXIS],endCoord[Z_AXIS]);
429 429
 
430
-    planner.buffer_line(endCoord[X_AXIS],endCoord[Y_AXIS],endCoord[Z_AXIS],
430
+    planner.buffer_line(endCoord[X_AXIS], endCoord[Y_AXIS], endCoord[Z_AXIS],
431 431
                         stepper.get_axis_position_mm(E_AXIS), feedrate, 0);
432 432
     stepper.synchronize();
433 433
 

+ 21
- 24
Marlin/src/feature/fwretract.cpp 查看文件

@@ -108,7 +108,7 @@ void FWRetract::retract(const bool retracting
108 108
     // G11 priority to recover the long retract if activated
109 109
     if (!retracting) swapping = retracted_swap[active_extruder];
110 110
   #else
111
-    const bool swapping = false;
111
+    constexpr bool swapping = false;
112 112
   #endif
113 113
 
114 114
   /* // debugging
@@ -118,62 +118,57 @@ void FWRetract::retract(const bool retracting
118 118
     for (uint8_t i = 0; i < EXTRUDERS; ++i) {
119 119
       SERIAL_ECHOPAIR("retracted[", i);
120 120
       SERIAL_ECHOLNPAIR("] ", retracted[i]);
121
-      SERIAL_ECHOPAIR("retracted_swap[", i);
122
-      SERIAL_ECHOLNPAIR("] ", retracted_swap[i]);
121
+      #if EXTRUDERS > 1
122
+        SERIAL_ECHOPAIR("retracted_swap[", i);
123
+        SERIAL_ECHOLNPAIR("] ", retracted_swap[i]);
124
+      #endif
123 125
     }
124 126
     SERIAL_ECHOLNPAIR("current_position[z] ", current_position[Z_AXIS]);
125 127
     SERIAL_ECHOLNPAIR("current_position[e] ", current_position[E_AXIS]);
126 128
     SERIAL_ECHOLNPAIR("hop_amount ", hop_amount);
127 129
   //*/
128 130
 
129
-  const float old_feedrate_mm_s = feedrate_mm_s;
131
+  const float old_feedrate_mm_s = feedrate_mm_s,
132
+              renormalize = RECIPROCAL(planner.e_factor[active_extruder]),
133
+              base_retract = swapping ? swap_retract_length : retract_length,
134
+              old_z = current_position[Z_AXIS],
135
+              old_e = current_position[E_AXIS];
130 136
 
131 137
   // The current position will be the destination for E and Z moves
132 138
   set_destination_from_current();
133
-  stepper.synchronize();  // Wait for buffered moves to complete
134
-
135
-  const float renormalize = 1.0 / planner.e_factor[active_extruder];
136 139
 
137 140
   if (retracting) {
138 141
     // Retract by moving from a faux E position back to the current E position
139 142
     feedrate_mm_s = retract_feedrate_mm_s;
140
-    current_position[E_AXIS] += (swapping ? swap_retract_length : retract_length) * renormalize;
141
-    sync_plan_position_e();
142
-    prepare_move_to_destination();  // set_current_to_destination
143
+    destination[E_AXIS] -= base_retract * renormalize;
144
+    prepare_move_to_destination();                        // set_current_to_destination
143 145
 
144 146
     // Is a Z hop set, and has the hop not yet been done?
145
-    // No double zlifting
146
-    // Feedrate to the max
147 147
     if (retract_zlift > 0.01 && !hop_amount) {            // Apply hop only once
148
-      const float old_z = current_position[Z_AXIS];
149 148
       hop_amount += retract_zlift;                        // Add to the hop total (again, only once)
150 149
       destination[Z_AXIS] += retract_zlift;               // Raise Z by the zlift (M207 Z) amount
151 150
       feedrate_mm_s = planner.max_feedrate_mm_s[Z_AXIS];  // Maximum Z feedrate
152 151
       prepare_move_to_destination();                      // Raise up, set_current_to_destination
153
-      current_position[Z_AXIS] = old_z;                   // Spoof the Z position in the planner
154
-      SYNC_PLAN_POSITION_KINEMATIC();
155 152
     }
156 153
   }
157 154
   else {
158 155
     // If a hop was done and Z hasn't changed, undo the Z hop
159 156
     if (hop_amount) {
160
-      current_position[Z_AXIS] += hop_amount;             // Set actual Z (due to the prior hop)
161
-      SYNC_PLAN_POSITION_KINEMATIC();                     // Spoof the Z position in the planner
157
+      destination[Z_AXIS] -= hop_amount;                  // Move back down by the total hop amount
162 158
       feedrate_mm_s = planner.max_feedrate_mm_s[Z_AXIS];  // Z feedrate to max
163 159
       prepare_move_to_destination();                      // Lower Z, set_current_to_destination
164 160
       hop_amount = 0.0;                                   // Clear the hop amount
165 161
     }
166 162
 
167
-    // A retract multiplier has been added here to get faster swap recovery
163
+    destination[E_AXIS] += (base_retract + (swapping ? swap_retract_recover_length : retract_recover_length)) * renormalize;
168 164
     feedrate_mm_s = swapping ? swap_retract_recover_feedrate_mm_s : retract_recover_feedrate_mm_s;
169
-
170
-    current_position[E_AXIS] -= (swapping ? swap_retract_length + swap_retract_recover_length
171
-                                          : retract_length + retract_recover_length) * renormalize;
172
-    sync_plan_position_e();
173 165
     prepare_move_to_destination();                        // Recover E, set_current_to_destination
174 166
   }
175 167
 
176 168
   feedrate_mm_s = old_feedrate_mm_s;                      // Restore original feedrate
169
+  current_position[Z_AXIS] = old_z;                       // Restore Z and E positions
170
+  current_position[E_AXIS] = old_e;
171
+  SYNC_PLAN_POSITION_KINEMATIC();                         // As if the move never took place
177 172
 
178 173
   retracted[active_extruder] = retracting;                // Active extruder now retracted / recovered
179 174
 
@@ -189,8 +184,10 @@ void FWRetract::retract(const bool retracting
189 184
     for (uint8_t i = 0; i < EXTRUDERS; ++i) {
190 185
       SERIAL_ECHOPAIR("retracted[", i);
191 186
       SERIAL_ECHOLNPAIR("] ", retracted[i]);
192
-      SERIAL_ECHOPAIR("retracted_swap[", i);
193
-      SERIAL_ECHOLNPAIR("] ", retracted_swap[i]);
187
+      #if EXTRUDERS > 1
188
+        SERIAL_ECHOPAIR("retracted_swap[", i);
189
+        SERIAL_ECHOLNPAIR("] ", retracted_swap[i]);
190
+      #endif
194 191
     }
195 192
     SERIAL_ECHOLNPAIR("current_position[z] ", current_position[Z_AXIS]);
196 193
     SERIAL_ECHOLNPAIR("current_position[e] ", current_position[E_AXIS]);

+ 4
- 4
Marlin/src/feature/pause.cpp 查看文件

@@ -121,8 +121,8 @@ static void do_pause_e_move(const float &length, const float &fr) {
121 121
   set_destination_from_current();
122 122
   destination[E_AXIS] += length / planner.e_factor[active_extruder];
123 123
   planner.buffer_line_kinematic(destination, fr, active_extruder);
124
-  stepper.synchronize();
125 124
   set_current_from_destination();
125
+  stepper.synchronize();
126 126
 }
127 127
 
128 128
 /**
@@ -366,12 +366,12 @@ bool pause_print(const float &retract, const point_t &park_point, const float &u
366 366
   #endif
367 367
   print_job_timer.pause();
368 368
 
369
-  // Wait for synchronize steppers
370
-  stepper.synchronize();
371
-
372 369
   // Save current position
373 370
   COPY(resume_position, current_position);
374 371
 
372
+  // Wait for buffered blocks to complete
373
+  stepper.synchronize();
374
+
375 375
   // Initial retract before move to filament change position
376 376
   if (retract && thermalManager.hotEnoughToExtrude(active_extruder))
377 377
     do_pause_e_move(retract, PAUSE_PARK_RETRACT_FEEDRATE);

+ 1
- 9
Marlin/src/gcode/bedlevel/G26.cpp 查看文件

@@ -240,8 +240,6 @@ void move_to(const float &rx, const float &ry, const float &z, const float &e_de
240 240
     destination[E_AXIS] = current_position[E_AXIS];
241 241
 
242 242
     G26_line_to_destination(feed_value);
243
-
244
-    stepper.synchronize();
245 243
     set_destination_from_current();
246 244
   }
247 245
 
@@ -256,8 +254,6 @@ void move_to(const float &rx, const float &ry, const float &z, const float &e_de
256 254
   destination[E_AXIS] += e_delta;
257 255
 
258 256
   G26_line_to_destination(feed_value);
259
-
260
-  stepper.synchronize();
261 257
   set_destination_from_current();
262 258
 }
263 259
 
@@ -499,13 +495,11 @@ inline bool prime_nozzle() {
499 495
           if (Total_Prime >= EXTRUDE_MAXLENGTH) return G26_ERR;
500 496
         #endif
501 497
         G26_line_to_destination(planner.max_feedrate_mm_s[E_AXIS] / 15.0);
502
-
498
+        set_destination_from_current();
503 499
         stepper.synchronize();    // Without this synchronize, the purge is more consistent,
504 500
                                   // but because the planner has a buffer, we won't be able
505 501
                                   // to stop as quickly. So we put up with the less smooth
506 502
                                   // action to give the user a more responsive 'Stop'.
507
-        set_destination_from_current();
508
-        idle();
509 503
       }
510 504
 
511 505
       wait_for_release();
@@ -526,7 +520,6 @@ inline bool prime_nozzle() {
526 520
     set_destination_from_current();
527 521
     destination[E_AXIS] += g26_prime_length;
528 522
     G26_line_to_destination(planner.max_feedrate_mm_s[E_AXIS] / 15.0);
529
-    stepper.synchronize();
530 523
     set_destination_from_current();
531 524
     retract_filament(destination);
532 525
   }
@@ -700,7 +693,6 @@ void GcodeSuite::G26() {
700 693
 
701 694
   if (current_position[Z_AXIS] < Z_CLEARANCE_BETWEEN_PROBES) {
702 695
     do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
703
-    stepper.synchronize();
704 696
     set_current_from_destination();
705 697
   }
706 698
 

+ 1
- 1
Marlin/src/gcode/bedlevel/abl/G29.cpp 查看文件

@@ -949,8 +949,8 @@ void GcodeSuite::G29() {
949 949
       #if ENABLED(DEBUG_LEVELING_FEATURE)
950 950
         if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPAIR("Z Probe End Script: ", Z_PROBE_END_SCRIPT);
951 951
       #endif
952
-      enqueue_and_echo_commands_P(PSTR(Z_PROBE_END_SCRIPT));
953 952
       stepper.synchronize();
953
+      enqueue_and_echo_commands_P(PSTR(Z_PROBE_END_SCRIPT));
954 954
     #endif
955 955
 
956 956
     // Auto Bed Leveling is complete! Enable if possible.

+ 0
- 1
Marlin/src/gcode/control/M80_M81.cpp 查看文件

@@ -108,7 +108,6 @@ void GcodeSuite::M81() {
108 108
   safe_delay(1000); // Wait 1 second before switching off
109 109
 
110 110
   #if HAS_SUICIDE
111
-    stepper.synchronize();
112 111
     suicide();
113 112
   #elif HAS_POWER_SWITCH
114 113
     PSU_OFF();

+ 2
- 6
Marlin/src/gcode/geometry/G92.cpp 查看文件

@@ -33,8 +33,6 @@
33 33
  */
34 34
 void GcodeSuite::G92() {
35 35
 
36
-  stepper.synchronize();
37
-
38 36
   #if ENABLED(CNC_COORDINATE_SYSTEMS)
39 37
     switch (parser.subcode) {
40 38
       case 1:
@@ -94,10 +92,8 @@ void GcodeSuite::G92() {
94 92
       COPY(coordinate_system[active_coordinate_system], position_shift);
95 93
   #endif
96 94
 
97
-  if (didXYZ)
98
-    SYNC_PLAN_POSITION_KINEMATIC();
99
-  else if (didE)
100
-    sync_plan_position_e();
95
+  if    (didXYZ) SYNC_PLAN_POSITION_KINEMATIC();
96
+  else if (didE) sync_plan_position_e();
101 97
 
102 98
   report_current_position();
103 99
 }

+ 2
- 2
Marlin/src/gcode/host/M114.cpp 查看文件

@@ -43,8 +43,6 @@
43 43
 
44 44
   void report_current_position_detail() {
45 45
 
46
-    stepper.synchronize();
47
-
48 46
     SERIAL_PROTOCOLPGM("\nLogical:");
49 47
     const float logical[XYZ] = {
50 48
       LOGICAL_X_POSITION(current_position[X_AXIS]),
@@ -79,6 +77,8 @@
79 77
       report_xyz(delta);
80 78
     #endif
81 79
 
80
+    stepper.synchronize();
81
+
82 82
     SERIAL_PROTOCOLPGM("Stepper:");
83 83
     LOOP_XYZE(i) {
84 84
       SERIAL_CHAR(' ');

+ 2
- 2
Marlin/src/gcode/lcd/M0_M1.cpp 查看文件

@@ -58,6 +58,8 @@ void GcodeSuite::M0_M1() {
58 58
 
59 59
   const bool has_message = !hasP && !hasS && args && *args;
60 60
 
61
+  stepper.synchronize();
62
+
61 63
   #if ENABLED(ULTIPANEL)
62 64
 
63 65
     if (has_message)
@@ -81,8 +83,6 @@ void GcodeSuite::M0_M1() {
81 83
   KEEPALIVE_STATE(PAUSED_FOR_USER);
82 84
   wait_for_user = true;
83 85
 
84
-  stepper.synchronize();
85
-
86 86
   if (ms > 0) {
87 87
     ms += millis();  // wait until this time for a click
88 88
     while (PENDING(millis(), ms) && wait_for_user) idle();

+ 4
- 4
Marlin/src/module/motion.cpp 查看文件

@@ -396,13 +396,13 @@ void do_blocking_move_to(const float rx, const float ry, const float rz, const f
396 396
 
397 397
   #endif
398 398
 
399
-  stepper.synchronize();
400
-
401 399
   feedrate_mm_s = old_feedrate_mm_s;
402 400
 
403 401
   #if ENABLED(DEBUG_LEVELING_FEATURE)
404 402
     if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< do_blocking_move_to");
405 403
   #endif
404
+
405
+  stepper.synchronize();
406 406
 }
407 407
 void do_blocking_move_to_x(const float &rx, const float &fr_mm_s/*=0.0*/) {
408 408
   do_blocking_move_to(rx, current_position[Y_AXIS], current_position[Z_AXIS], fr_mm_s);
@@ -881,8 +881,8 @@ float soft_endstop_min[XYZ] = { X_MIN_BED, Y_MIN_BED, Z_MIN_POS },
881 881
               current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS],
882 882
               planner.max_feedrate_mm_s[X_AXIS], 1
883 883
             );
884
-            SYNC_PLAN_POSITION_KINEMATIC();
885 884
             stepper.synchronize();
885
+            SYNC_PLAN_POSITION_KINEMATIC();
886 886
             extruder_duplication_enabled = true;
887 887
             active_extruder_parked = false;
888 888
             #if ENABLED(DEBUG_LEVELING_FEATURE)
@@ -1106,7 +1106,7 @@ static void do_homing_move(const AxisEnum axis, const float distance, const floa
1106 1106
     planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], current_position[E_AXIS], fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder);
1107 1107
   #else
1108 1108
     sync_plan_position();
1109
-    current_position[axis] = distance;
1109
+    current_position[axis] = distance; // Set delta/cartesian axes directly
1110 1110
     planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder);
1111 1111
   #endif
1112 1112
 

+ 36
- 22
Marlin/src/module/planner.cpp 查看文件

@@ -1382,15 +1382,9 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE]
1382 1382
   const float esteps_float = de * e_factor[extruder];
1383 1383
   const int32_t esteps = abs(esteps_float) + 0.5;
1384 1384
 
1385
-  // Calculate the buffer head after we push this byte
1386
-  const uint8_t next_buffer_head = next_block_index(block_buffer_head);
1387
-
1388
-  // If the buffer is full: good! That means we are well ahead of the robot.
1389
-  // Rest here until there is room in the buffer.
1390
-  while (block_buffer_tail == next_buffer_head) idle();
1391
-
1392
-  // Prepare to set up new block
1393
-  block_t* block = &block_buffer[block_buffer_head];
1385
+  // Wait for the next available block
1386
+  uint8_t next_buffer_head;
1387
+  block_t * const block = get_next_free_block(next_buffer_head);
1394 1388
 
1395 1389
   // Clear all flags, including the "busy" bit
1396 1390
   block->flag = 0x00;
@@ -2033,6 +2027,26 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE]
2033 2027
 } // _buffer_steps()
2034 2028
 
2035 2029
 /**
2030
+ * Planner::buffer_sync_block
2031
+ * Add a block to the buffer that just updates the position
2032
+ */
2033
+void Planner::buffer_sync_block() {
2034
+  // Wait for the next available block
2035
+  uint8_t next_buffer_head;
2036
+  block_t * const block = get_next_free_block(next_buffer_head);
2037
+
2038
+  block->steps[A_AXIS] = position[A_AXIS];
2039
+  block->steps[B_AXIS] = position[B_AXIS];
2040
+  block->steps[C_AXIS] = position[C_AXIS];
2041
+  block->steps[E_AXIS] = position[E_AXIS];
2042
+
2043
+  block->flag = BLOCK_FLAG_SYNC_POSITION;
2044
+
2045
+  block_buffer_head = next_buffer_head;
2046
+  stepper.wake_up();
2047
+} // buffer_sync_block()
2048
+
2049
+/**
2036 2050
  * Planner::buffer_segment
2037 2051
  *
2038 2052
  * Add a new linear movement to the buffer in axis units.
@@ -2160,19 +2174,19 @@ void Planner::_set_position_mm(const float &a, const float &b, const float &c, c
2160 2174
   #else
2161 2175
     #define _EINDEX E_AXIS
2162 2176
   #endif
2163
-  const int32_t na = position[A_AXIS] = LROUND(a * axis_steps_per_mm[A_AXIS]),
2164
-                nb = position[B_AXIS] = LROUND(b * axis_steps_per_mm[B_AXIS]),
2165
-                nc = position[C_AXIS] = LROUND(c * axis_steps_per_mm[C_AXIS]),
2166
-                ne = position[E_AXIS] = LROUND(e * axis_steps_per_mm[_EINDEX]);
2177
+  position[A_AXIS] = LROUND(a * axis_steps_per_mm[A_AXIS]),
2178
+  position[B_AXIS] = LROUND(b * axis_steps_per_mm[B_AXIS]),
2179
+  position[C_AXIS] = LROUND(c * axis_steps_per_mm[C_AXIS]),
2180
+  position[E_AXIS] = LROUND(e * axis_steps_per_mm[_EINDEX]);
2167 2181
   #if HAS_POSITION_FLOAT
2168
-    position_float[X_AXIS] = a;
2169
-    position_float[Y_AXIS] = b;
2170
-    position_float[Z_AXIS] = c;
2182
+    position_float[A_AXIS] = a;
2183
+    position_float[B_AXIS] = b;
2184
+    position_float[C_AXIS] = c;
2171 2185
     position_float[E_AXIS] = e;
2172 2186
   #endif
2173
-  stepper.set_position(na, nb, nc, ne);
2174 2187
   previous_nominal_speed = 0.0; // Resets planner junction speeds. Assumes start from rest.
2175 2188
   ZERO(previous_speed);
2189
+  buffer_sync_block();
2176 2190
 }
2177 2191
 
2178 2192
 void Planner::set_position_mm_kinematic(const float (&cart)[XYZE]) {
@@ -2220,23 +2234,23 @@ void Planner::set_position_mm(const AxisEnum axis, const float &v) {
2220 2234
   #if HAS_POSITION_FLOAT
2221 2235
     position_float[axis] = v;
2222 2236
   #endif
2223
-  stepper.set_position(axis, position[axis]);
2224 2237
   previous_speed[axis] = 0.0;
2238
+  buffer_sync_block();
2225 2239
 }
2226 2240
 
2227 2241
 // Recalculate the steps/s^2 acceleration rates, based on the mm/s^2
2228 2242
 void Planner::reset_acceleration_rates() {
2229 2243
   #if ENABLED(DISTINCT_E_FACTORS)
2230
-    #define HIGHEST_CONDITION (i < E_AXIS || i == E_AXIS + active_extruder)
2244
+    #define AXIS_CONDITION (i < E_AXIS || i == E_AXIS + active_extruder)
2231 2245
   #else
2232
-    #define HIGHEST_CONDITION true
2246
+    #define AXIS_CONDITION true
2233 2247
   #endif
2234 2248
   uint32_t highest_rate = 1;
2235 2249
   LOOP_XYZE_N(i) {
2236 2250
     max_acceleration_steps_per_s2[i] = max_acceleration_mm_per_s2[i] * axis_steps_per_mm[i];
2237
-    if (HIGHEST_CONDITION) NOLESS(highest_rate, max_acceleration_steps_per_s2[i]);
2251
+    if (AXIS_CONDITION) NOLESS(highest_rate, max_acceleration_steps_per_s2[i]);
2238 2252
   }
2239
-  cutoff_long = 4294967295UL / highest_rate;
2253
+  cutoff_long = 4294967295UL / highest_rate; // 0xFFFFFFFFUL
2240 2254
 }
2241 2255
 
2242 2256
 // Recalculate position, steps_to_mm if axis_steps_per_mm changes!

+ 28
- 4
Marlin/src/module/planner.h 查看文件

@@ -57,14 +57,18 @@ enum BlockFlagBit : char {
57 57
   BLOCK_BIT_BUSY,
58 58
 
59 59
   // The block is segment 2+ of a longer move
60
-  BLOCK_BIT_CONTINUED
60
+  BLOCK_BIT_CONTINUED,
61
+
62
+  // Sync the stepper counts from the block
63
+  BLOCK_BIT_SYNC_POSITION
61 64
 };
62 65
 
63 66
 enum BlockFlag : char {
64 67
   BLOCK_FLAG_RECALCULATE          = _BV(BLOCK_BIT_RECALCULATE),
65 68
   BLOCK_FLAG_NOMINAL_LENGTH       = _BV(BLOCK_BIT_NOMINAL_LENGTH),
66 69
   BLOCK_FLAG_BUSY                 = _BV(BLOCK_BIT_BUSY),
67
-  BLOCK_FLAG_CONTINUED            = _BV(BLOCK_BIT_CONTINUED)
70
+  BLOCK_FLAG_CONTINUED            = _BV(BLOCK_BIT_CONTINUED),
71
+  BLOCK_FLAG_SYNC_POSITION        = _BV(BLOCK_BIT_SYNC_POSITION)
68 72
 };
69 73
 
70 74
 /**
@@ -422,6 +426,20 @@ class Planner {
422 426
 
423 427
     #endif
424 428
 
429
+
430
+    /**
431
+     * Planner::get_next_free_block
432
+     *
433
+     * - Get the next head index (passed by reference)
434
+     * - Wait for a space to open up in the planner
435
+     * - Return the head block
436
+     */
437
+    FORCE_INLINE static block_t* get_next_free_block(uint8_t &next_buffer_head) {
438
+      next_buffer_head = next_block_index(block_buffer_head);
439
+      while (block_buffer_tail == next_buffer_head) idle(); // while (is_full)
440
+      return &block_buffer[block_buffer_head];
441
+    }
442
+
425 443
     /**
426 444
      * Planner::_buffer_steps
427 445
      *
@@ -440,6 +458,12 @@ class Planner {
440 458
     );
441 459
 
442 460
     /**
461
+     * Planner::buffer_sync_block
462
+     * Add a block to the buffer that just updates the position
463
+     */
464
+    static void buffer_sync_block();
465
+
466
+    /**
443 467
      * Planner::buffer_segment
444 468
      *
445 469
      * Add a new linear movement to the buffer in axis units.
@@ -518,7 +542,7 @@ class Planner {
518 542
     static void set_position_mm_kinematic(const float (&cart)[XYZE]);
519 543
     static void set_position_mm(const AxisEnum axis, const float &v);
520 544
     FORCE_INLINE static void set_z_position_mm(const float &z) { set_position_mm(Z_AXIS, z); }
521
-    FORCE_INLINE static void set_e_position_mm(const float &e) { set_position_mm(AxisEnum(E_AXIS), e); }
545
+    FORCE_INLINE static void set_e_position_mm(const float &e) { set_position_mm(E_AXIS, e); }
522 546
 
523 547
     /**
524 548
      * Sync from the stepper positions. (e.g., after an interrupted move)
@@ -528,7 +552,7 @@ class Planner {
528 552
     /**
529 553
      * Does the buffer have any blocks queued?
530 554
      */
531
-    static bool has_blocks_queued() { return (block_buffer_head != block_buffer_tail); }
555
+    FORCE_INLINE static bool has_blocks_queued() { return (block_buffer_head != block_buffer_tail); }
532 556
 
533 557
     /**
534 558
      * "Discard" the block and "release" the memory.

+ 22
- 31
Marlin/src/module/stepper.cpp 查看文件

@@ -109,10 +109,10 @@ int16_t Stepper::cleaning_buffer_counter = 0;
109 109
   bool Stepper::locked_z_motor = false, Stepper::locked_z2_motor = false;
110 110
 #endif
111 111
 
112
-long Stepper::counter_X = 0,
113
-     Stepper::counter_Y = 0,
114
-     Stepper::counter_Z = 0,
115
-     Stepper::counter_E = 0;
112
+int32_t Stepper::counter_X = 0,
113
+        Stepper::counter_Y = 0,
114
+        Stepper::counter_Z = 0,
115
+        Stepper::counter_E = 0;
116 116
 
117 117
 volatile uint32_t Stepper::step_events_completed = 0; // The number of step events executed in the current block
118 118
 
@@ -159,7 +159,7 @@ volatile int32_t Stepper::count_position[NUM_AXIS] = { 0 };
159 159
 volatile signed char Stepper::count_direction[NUM_AXIS] = { 1, 1, 1, 1 };
160 160
 
161 161
 #if ENABLED(MIXING_EXTRUDER)
162
-  long Stepper::counter_m[MIXING_STEPPERS];
162
+  int32_t Stepper::counter_m[MIXING_STEPPERS];
163 163
 #endif
164 164
 
165 165
 uint8_t Stepper::step_loops, Stepper::step_loops_nominal;
@@ -169,7 +169,7 @@ hal_timer_t Stepper::OCR1A_nominal;
169 169
   hal_timer_t Stepper::acc_step_rate; // needed for deceleration start point
170 170
 #endif
171 171
 
172
-volatile long Stepper::endstops_trigsteps[XYZ];
172
+volatile int32_t Stepper::endstops_trigsteps[XYZ];
173 173
 
174 174
 #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
175 175
   #define LOCKED_X_MOTOR  locked_x_motor
@@ -1217,6 +1217,16 @@ void Stepper::isr() {
1217 1217
     // Anything in the buffer?
1218 1218
     if ((current_block = planner.get_current_block())) {
1219 1219
 
1220
+      // Sync block? Sync the stepper counts and return
1221
+      while (TEST(current_block->flag, BLOCK_BIT_SYNC_POSITION)) {
1222
+        _set_position(
1223
+          current_block->steps[A_AXIS], current_block->steps[B_AXIS],
1224
+          current_block->steps[C_AXIS], current_block->steps[E_AXIS]
1225
+        );
1226
+        planner.discard_current_block();
1227
+        if (!(current_block = planner.get_current_block())) return;
1228
+      }
1229
+
1220 1230
       // Initialize the trapezoid generator from the current block.
1221 1231
       static int8_t last_extruder = -1;
1222 1232
 
@@ -1976,12 +1986,7 @@ void Stepper::synchronize() { while (planner.has_blocks_queued() || cleaning_buf
1976 1986
  * This allows get_axis_position_mm to correctly
1977 1987
  * derive the current XYZ position later on.
1978 1988
  */
1979
-void Stepper::set_position(const long &a, const long &b, const long &c, const long &e) {
1980
-
1981
-  synchronize(); // Bad to set stepper counts in the middle of a move
1982
-
1983
-  CRITICAL_SECTION_START;
1984
-
1989
+void Stepper::_set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e) {
1985 1990
   #if CORE_IS_XY
1986 1991
     // corexy positioning
1987 1992
     // these equations follow the form of the dA and dB equations on http://www.corexy.com/theory.html
@@ -2004,29 +2009,15 @@ void Stepper::set_position(const long &a, const long &b, const long &c, const lo
2004 2009
     count_position[Y_AXIS] = b;
2005 2010
     count_position[Z_AXIS] = c;
2006 2011
   #endif
2007
-
2008
-  count_position[E_AXIS] = e;
2009
-  CRITICAL_SECTION_END;
2010
-}
2011
-
2012
-void Stepper::set_position(const AxisEnum &axis, const long &v) {
2013
-  CRITICAL_SECTION_START;
2014
-  count_position[axis] = v;
2015
-  CRITICAL_SECTION_END;
2016
-}
2017
-
2018
-void Stepper::set_e_position(const long &e) {
2019
-  CRITICAL_SECTION_START;
2020 2012
   count_position[E_AXIS] = e;
2021
-  CRITICAL_SECTION_END;
2022 2013
 }
2023 2014
 
2024 2015
 /**
2025 2016
  * Get a stepper's position in steps.
2026 2017
  */
2027
-long Stepper::position(const AxisEnum axis) {
2018
+int32_t Stepper::position(const AxisEnum axis) {
2028 2019
   CRITICAL_SECTION_START;
2029
-  const long count_pos = count_position[axis];
2020
+  const int32_t count_pos = count_position[axis];
2030 2021
   CRITICAL_SECTION_END;
2031 2022
   return count_pos;
2032 2023
 }
@@ -2095,9 +2086,9 @@ void Stepper::endstop_triggered(const AxisEnum axis) {
2095 2086
 
2096 2087
 void Stepper::report_positions() {
2097 2088
   CRITICAL_SECTION_START;
2098
-  const long xpos = count_position[X_AXIS],
2099
-             ypos = count_position[Y_AXIS],
2100
-             zpos = count_position[Z_AXIS];
2089
+  const int32_t xpos = count_position[X_AXIS],
2090
+                ypos = count_position[Y_AXIS],
2091
+                zpos = count_position[Z_AXIS];
2101 2092
   CRITICAL_SECTION_END;
2102 2093
 
2103 2094
   #if CORE_IS_XY || CORE_IS_XZ || IS_DELTA || IS_SCARA

+ 31
- 8
Marlin/src/module/stepper.h 查看文件

@@ -94,7 +94,7 @@ class Stepper {
94 94
     #endif
95 95
 
96 96
     // Counter variables for the Bresenham line tracer
97
-    static long counter_X, counter_Y, counter_Z, counter_E;
97
+    static int32_t counter_X, counter_Y, counter_Z, counter_E;
98 98
     static volatile uint32_t step_events_completed; // The number of step events executed in the current block
99 99
 
100 100
     #if ENABLED(BEZIER_JERK_CONTROL)
@@ -137,8 +137,8 @@ class Stepper {
137 137
       static hal_timer_t acc_step_rate; // needed for deceleration start point
138 138
     #endif
139 139
 
140
-    static volatile long endstops_trigsteps[XYZ];
141
-    static volatile long endstops_stepsTotal, endstops_stepsDone;
140
+    static volatile int32_t endstops_trigsteps[XYZ];
141
+    static volatile int32_t endstops_stepsTotal, endstops_stepsDone;
142 142
 
143 143
     //
144 144
     // Positions of stepper motors, in step units
@@ -154,7 +154,7 @@ class Stepper {
154 154
     // Mixing extruder mix counters
155 155
     //
156 156
     #if ENABLED(MIXING_EXTRUDER)
157
-      static long counter_m[MIXING_STEPPERS];
157
+      static int32_t counter_m[MIXING_STEPPERS];
158 158
       #define MIXING_STEPPERS_LOOP(VAR) \
159 159
         for (uint8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++) \
160 160
           if (current_block->mix_event_count[VAR])
@@ -191,9 +191,32 @@ class Stepper {
191 191
     //
192 192
     // Set the current position in steps
193 193
     //
194
-    static void set_position(const long &a, const long &b, const long &c, const long &e);
195
-    static void set_position(const AxisEnum &a, const long &v);
196
-    static void set_e_position(const long &e);
194
+    static void _set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e);
195
+
196
+    FORCE_INLINE static void _set_position(const AxisEnum a, const int32_t &v) { count_position[a] = v; }
197
+
198
+    FORCE_INLINE static void set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e) {
199
+      synchronize();
200
+      CRITICAL_SECTION_START;
201
+      _set_position(a, b, c, e);
202
+      CRITICAL_SECTION_END;
203
+    }
204
+
205
+    static void set_position(const AxisEnum a, const int32_t &v) {
206
+      synchronize();
207
+      CRITICAL_SECTION_START;
208
+      count_position[a] = v;
209
+      CRITICAL_SECTION_END;
210
+    }
211
+
212
+    FORCE_INLINE static void _set_e_position(const int32_t &e) { count_position[E_AXIS] = e; }
213
+
214
+    static void set_e_position(const int32_t &e) {
215
+      synchronize();
216
+      CRITICAL_SECTION_START;
217
+      count_position[E_AXIS] = e;
218
+      CRITICAL_SECTION_END;
219
+    }
197 220
 
198 221
     //
199 222
     // Set direction bits for all steppers
@@ -203,7 +226,7 @@ class Stepper {
203 226
     //
204 227
     // Get the position of a stepper, in steps
205 228
     //
206
-    static long position(const AxisEnum axis);
229
+    static int32_t position(const AxisEnum axis);
207 230
 
208 231
     //
209 232
     // Report the positions of the steppers, in steps

Loading…
取消
儲存