From ac5ff1d802ec87230f5946868feee3229dea8556 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Thu, 3 May 2018 20:55:00 -0500
Subject: [PATCH] Adjust usage of stepper.synchronize

---
 Marlin/G26_Mesh_Validation_Tool.cpp | 13 +++----------
 Marlin/I2CPositionEncoder.cpp       | 10 +++++-----
 Marlin/Marlin_main.cpp              | 29 ++++++++++++++---------------
 3 files changed, 22 insertions(+), 30 deletions(-)

diff --git a/Marlin/G26_Mesh_Validation_Tool.cpp b/Marlin/G26_Mesh_Validation_Tool.cpp
index a605aaa903..365cd903aa 100644
--- a/Marlin/G26_Mesh_Validation_Tool.cpp
+++ b/Marlin/G26_Mesh_Validation_Tool.cpp
@@ -204,8 +204,6 @@
       destination[E_AXIS] = current_position[E_AXIS];
 
       G26_line_to_destination(feed_value);
-
-      stepper.synchronize();
       set_destination_from_current();
     }
 
@@ -220,8 +218,6 @@
     destination[E_AXIS] += e_delta;
 
     G26_line_to_destination(feed_value);
-
-    stepper.synchronize();
     set_destination_from_current();
   }
 
@@ -267,13 +263,12 @@
             if (Total_Prime >= EXTRUDE_MAXLENGTH) return G26_ERR;
           #endif
           G26_line_to_destination(planner.max_feedrate_mm_s[E_AXIS] / 15.0);
-
+          set_destination_from_current();
           stepper.synchronize();    // Without this synchronize, the purge is more consistent,
                                     // but because the planner has a buffer, we won't be able
                                     // to stop as quickly. So we put up with the less smooth
                                     // action to give the user a more responsive 'Stop'.
-          set_destination_from_current();
-          idle();
+
           SERIAL_FLUSH(); // Prevent host M105 buffer overrun.
         }
 
@@ -295,7 +290,6 @@
       set_destination_from_current();
       destination[E_AXIS] += g26_prime_length;
       G26_line_to_destination(planner.max_feedrate_mm_s[E_AXIS] / 15.0);
-      stepper.synchronize();
       set_destination_from_current();
       retract_filament(destination);
     }
@@ -703,7 +697,6 @@
 
     if (current_position[Z_AXIS] < Z_CLEARANCE_BETWEEN_PROBES) {
       do_blocking_move_to_z(Z_CLEARANCE_BETWEEN_PROBES);
-      stepper.synchronize();
       set_current_from_destination();
     }
 
@@ -738,7 +731,7 @@
       lcd_external_control = true;
     #endif
 
-//  debug_current_and_destination(PSTR("Starting G26 Mesh Validation Pattern."));
+    //debug_current_and_destination(PSTR("Starting G26 Mesh Validation Pattern."));
 
     /**
      * Pre-generate radius offset values at 30 degree intervals to reduce CPU load.
diff --git a/Marlin/I2CPositionEncoder.cpp b/Marlin/I2CPositionEncoder.cpp
index 95e0f1b725..433c917b4f 100644
--- a/Marlin/I2CPositionEncoder.cpp
+++ b/Marlin/I2CPositionEncoder.cpp
@@ -358,7 +358,7 @@
 
     stepper.synchronize();
 
-    planner.buffer_line(startCoord[X_AXIS],startCoord[Y_AXIS],startCoord[Z_AXIS],
+    planner.buffer_line(startCoord[X_AXIS], startCoord[Y_AXIS], startCoord[Z_AXIS],
                         stepper.get_axis_position_mm(E_AXIS), feedrate, 0);
     stepper.synchronize();
 
@@ -415,10 +415,10 @@
     startCoord[encoderAxis] = startDistance;
     endCoord[encoderAxis] = endDistance;
 
-    LOOP_L_N(i, iter) {
-      stepper.synchronize();
+    stepper.synchronize();
 
-      planner.buffer_line(startCoord[X_AXIS],startCoord[Y_AXIS],startCoord[Z_AXIS],
+    LOOP_L_N(i, iter) {
+      planner.buffer_line(startCoord[X_AXIS], startCoord[Y_AXIS], startCoord[Z_AXIS],
                           stepper.get_axis_position_mm(E_AXIS), feedrate, 0);
       stepper.synchronize();
 
@@ -427,7 +427,7 @@
 
       //do_blocking_move_to(endCoord[X_AXIS],endCoord[Y_AXIS],endCoord[Z_AXIS]);
 
-      planner.buffer_line(endCoord[X_AXIS],endCoord[Y_AXIS],endCoord[Z_AXIS],
+      planner.buffer_line(endCoord[X_AXIS], endCoord[Y_AXIS], endCoord[Z_AXIS],
                           stepper.get_axis_position_mm(E_AXIS), feedrate, 0);
       stepper.synchronize();
 
diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 3791aae687..a3f48f0848 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -2984,7 +2984,7 @@ static void do_homing_move(const AxisEnum axis, const float distance, const floa
     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);
   #else
     sync_plan_position();
-    current_position[axis] = distance;
+    current_position[axis] = distance; // Set delta/cartesian axes directly
     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);
   #endif
 
@@ -5337,8 +5337,8 @@ void home_all_axes() { gcode_G28(true); }
         #if ENABLED(DEBUG_LEVELING_FEATURE)
           if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPAIR("Z Probe End Script: ", Z_PROBE_END_SCRIPT);
         #endif
-        enqueue_and_echo_commands_P(PSTR(Z_PROBE_END_SCRIPT));
         stepper.synchronize();
+        enqueue_and_echo_commands_P(PSTR(Z_PROBE_END_SCRIPT));
       #endif
 
       // Auto Bed Leveling is complete! Enable if possible.
@@ -6134,9 +6134,8 @@ void home_all_axes() { gcode_G28(true); }
       }
     #endif
 
-    stepper.synchronize();  // wait until the machine is idle
-
     // Move until destination reached or target hit
+    stepper.synchronize();
     endstops.enable(true);
     G38_move = true;
     G38_endstop_hit = false;
@@ -6158,13 +6157,13 @@ void home_all_axes() { gcode_G28(true); }
         LOOP_XYZ(i) destination[i] += retract_mm[i];
         endstops.enable(false);
         prepare_move_to_destination();
-        stepper.synchronize();
 
         feedrate_mm_s /= 4;
 
         // Bump the target more slowly
         LOOP_XYZ(i) destination[i] -= retract_mm[i] * 2;
 
+        stepper.synchronize();
         endstops.enable(true);
         G38_move = true;
         prepare_move_to_destination();
@@ -6346,6 +6345,8 @@ inline void gcode_G92() {
 
     const bool has_message = !hasP && !hasS && args && *args;
 
+    stepper.synchronize();
+
     #if ENABLED(ULTIPANEL)
 
       if (has_message)
@@ -6369,8 +6370,6 @@ inline void gcode_G92() {
     KEEPALIVE_STATE(PAUSED_FOR_USER);
     wait_for_user = true;
 
-    stepper.synchronize();
-
     if (ms > 0) {
       ms += millis();  // wait until this time for a click
       while (PENDING(millis(), ms) && wait_for_user) idle();
@@ -6521,8 +6520,8 @@ inline void gcode_M17() {
     set_destination_from_current();
     destination[E_AXIS] += length / planner.e_factor[active_extruder];
     planner.buffer_line_kinematic(destination, fr, active_extruder);
-    stepper.synchronize();
     set_current_from_destination();
+    stepper.synchronize();
   }
 
   static float resume_position[XYZE];
@@ -6810,12 +6809,12 @@ inline void gcode_M17() {
     #endif
     print_job_timer.pause();
 
-    // Wait for synchronize steppers
-    stepper.synchronize();
-
     // Save current position
     COPY(resume_position, current_position);
 
+    // Wait for synchronize steppers
+    stepper.synchronize();
+
     // Initial retract before move to filament change position
     if (retract && thermalManager.hotEnoughToExtrude(active_extruder))
       do_pause_e_move(retract, PAUSE_PARK_RETRACT_FEEDRATE);
@@ -8505,7 +8504,6 @@ inline void gcode_M81() {
   safe_delay(1000); // Wait 1 second before switching off
 
   #if HAS_SUICIDE
-    stepper.synchronize();
     suicide();
   #elif HAS_POWER_SWITCH
     PSU_OFF();
@@ -8641,8 +8639,6 @@ void report_current_position() {
 
   void report_current_position_detail() {
 
-    stepper.synchronize();
-
     SERIAL_PROTOCOLPGM("\nLogical:");
     const float logical[XYZ] = {
       LOGICAL_X_POSITION(current_position[X_AXIS]),
@@ -8677,6 +8673,8 @@ void report_current_position() {
       report_xyz(delta);
     #endif
 
+    stepper.synchronize();
+
     SERIAL_PROTOCOLPGM("Stepper:");
     LOOP_XYZE(i) {
       SERIAL_CHAR(' ');
@@ -13366,8 +13364,8 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) {
               current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS],
               planner.max_feedrate_mm_s[X_AXIS], 1
             );
-            SYNC_PLAN_POSITION_KINEMATIC();
             stepper.synchronize();
+            SYNC_PLAN_POSITION_KINEMATIC();
             extruder_duplication_enabled = true;
             active_extruder_parked = false;
             #if ENABLED(DEBUG_LEVELING_FEATURE)
@@ -13975,6 +13973,7 @@ void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
       planner.buffer_line_kinematic(current_position, MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED), active_extruder);
       current_position[E_AXIS] = olde;
       planner.set_e_position_mm(olde);
+
       stepper.synchronize();
       #if ENABLED(SWITCHING_EXTRUDER)
         E0_ENABLE_WRITE(oldstatus);
-- 
GitLab