From 85ca0214ea297982e660861a8cfbd7cbcbb53f0d Mon Sep 17 00:00:00 2001 From: smallwhiteawhite <1368582937@qq.com> Date: Fri, 27 Nov 2026 21:50:04 -0800 Subject: [PATCH] rs274ngc: avoid NaN arc center for R0 within tolerance in arc_data_r() In Interp::arc_data_r(), when a radius-format arc has R0 and a chord within tolerance (half_length <= tolerance), the tolerance check passes and the semicircle adjustment previously shrank half_length to abs_radius (0.0), causing asin(0.0 / 0.0) to produce NaN center coordinates in ARC_FEED. Grow abs_radius to half_length instead of shrinking half_length to abs_radius, avoiding the division by zero and placing the arc center at the chord midpoint (matching R=half_length while preserving backward compatibility with existing G-code). Add a regression test (tests/interp/good/good-arc.r0.ngc) verifying that G2 with R0 within tolerance emits a finite midpoint center. Suggested-by: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> Fixes #4919 --- src/emc/rs274ngc/interp_arc.cc | 4 ++-- tests/interp/good/good-arc.r0.ngc | 5 +++++ tests/interp/good/good-arc.r0.ngc.expected | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/interp/good/good-arc.r0.ngc create mode 100644 tests/interp/good/good-arc.r0.ngc.expected diff --git a/src/emc/rs274ngc/interp_arc.cc b/src/emc/rs274ngc/interp_arc.cc index 961f670e09c..65c526944ea 100644 --- a/src/emc/rs274ngc/interp_arc.cc +++ b/src/emc/rs274ngc/interp_arc.cc @@ -374,8 +374,8 @@ int Interp::arc_data_r(int move, //!< either G_2 (cw arc) or G_3 (ccw arc) half_length = hypot((mid_x - end_x), (mid_y - end_y)); CHKS(((half_length - abs_radius) > tolerance), NCE_ARC_RADIUS_TOO_SMALL_TO_REACH_END_POINT); - if ((half_length / abs_radius) > (1 - TINY)) - half_length = abs_radius; /* allow a small error for semicircle */ + if (half_length > abs_radius * (1 - TINY)) + abs_radius = half_length; /* allow a small error for semicircle, R0 too */ /* check needed before calling asin */ if (((move == G_2) && (radius > 0)) || ((move == G_3) && (radius < 0))) theta = atan2((end_y - current_y), (end_x - current_x)) - M_PI_2l; diff --git a/tests/interp/good/good-arc.r0.ngc b/tests/interp/good/good-arc.r0.ngc new file mode 100644 index 00000000000..9b653ebcf5e --- /dev/null +++ b/tests/interp/good/good-arc.r0.ngc @@ -0,0 +1,5 @@ +g21 +f1 +g0x0y0z0 +g2 x0.001 y0 r0 +m2 diff --git a/tests/interp/good/good-arc.r0.ngc.expected b/tests/interp/good/good-arc.r0.ngc.expected new file mode 100644 index 00000000000..d309d8275cd --- /dev/null +++ b/tests/interp/good/good-arc.r0.ngc.expected @@ -0,0 +1,19 @@ + 1 N..... USE_LENGTH_UNITS(CANON_UNITS_MM) + 2 N..... SET_G5X_OFFSET(1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000) + 3 N..... SET_G92_OFFSET(0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000) + 4 N..... SET_XY_ROTATION(0.0000) + 5 N..... SET_FEED_REFERENCE(CANON_XYZ) + 6 N..... ON_RESET() + 7 N..... USE_LENGTH_UNITS(CANON_UNITS_MM) + 8 N..... SET_FEED_RATE(1.0000) + 9 N..... STRAIGHT_TRAVERSE(0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000) + 10 N..... ARC_FEED(0.0010, 0.0000, 0.0005, 0.0000, -1, 0.0000, 0.0000, 0.0000, 0.0000) + 11 N..... SET_G5X_OFFSET(1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000) + 12 N..... SET_XY_ROTATION(0.0000) + 13 N..... SET_FEED_MODE(0, 0) + 14 N..... SET_FEED_RATE(0.0000) + 15 N..... STOP_SPINDLE_TURNING(0) + 16 N..... SET_SPINDLE_MODE(0 0.0000) + 17 N..... PROGRAM_END() + 18 N..... ON_RESET() + 19 N..... ON_RESET()