Browse Source

Fix G2/G3 rounding error (#15507)

Ed Williams 4 years ago
parent
commit
d8aeeb8ff6
1 changed files with 7 additions and 6 deletions
  1. 7
    6
      Marlin/src/gcode/motion/G2_G3.cpp

+ 7
- 6
Marlin/src/gcode/motion/G2_G3.cpp View File

@@ -285,12 +285,13 @@ void GcodeSuite::G2_G3(const bool clockwise) {
285 285
       if (r) {
286 286
         const xy_pos_t p1 = current_position, p2 = destination;
287 287
         if (p1 != p2) {
288
-          const xy_pos_t d = p2 - p1, m = (p1 + p2) * 0.5f;   // XY distance and midpoint
289
-          const float e = clockwise ^ (r < 0) ? -1 : 1,       // clockwise -1/1, counterclockwise 1/-1
290
-                      len = d.magnitude(),                    // Total move length
291
-                      h = SQRT((r - d * 0.5f) * (r + d * 0.5f)); // Distance to the arc pivot-point
292
-          const xy_pos_t s = { d.x, -d.y };                   // Inverse Slope of the perpendicular bisector
293
-          arc_offset = m + s * RECIPROCAL(len) * e * h - p1;  // The calculated offset
288
+          const xy_pos_t d2 = (p2 - p1) * 0.5f;          // XY vector to midpoint of move from current
289
+          const float e = clockwise ^ (r < 0) ? -1 : 1,  // clockwise -1/1, counterclockwise 1/-1
290
+                      len = d2.magnitude(),              // Distance to mid-point of move from current
291
+                      h2 = (r - len) * (r + len),        // factored to reduce rounding error
292
+                      h = (h2 >= 0) ? SQRT(h2) : 0.0f;   // Distance to the arc pivot-point from midpoint
293
+          const xy_pos_t s = { -d2.y, d2.x } / len;      // Unit vector along perpendicular bisector
294
+          arc_offset = d2 + s * e * h;                   // The calculated offset (mid-point if |r| <= len)
294 295
         }
295 296
       }
296 297
     }

Loading…
Cancel
Save