From b246dd8525d603277667b1f2ba499271911eae93 Mon Sep 17 00:00:00 2001 From: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> Date: Wed, 24 Jun 2026 09:30:06 +0800 Subject: [PATCH] task: reset canon length units on program open G20/G21 from a previous run persists in canon.lengthUnits, causing FROM_PROG_LEN() to convert differently on re-run for G-code commands that appear before the G20/G21 line. This shows up as wrong unit scaling (off by 25.4x) on the lead-in moves of the second run, producing jagged velocity in the scope. Reset canon.lengthUnits to the machine default in emcTaskPlanOpen before reopening the interpreter. Fixes #4194 --- src/emc/task/emctask.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/emc/task/emctask.cc b/src/emc/task/emctask.cc index b598bd12b52..0ac5c22f3d1 100644 --- a/src/emc/task/emctask.cc +++ b/src/emc/task/emctask.cc @@ -14,6 +14,7 @@ ********************************************************************/ #include +#include // fabs() #include // rtapi_strlcpy() #include // struct stat #include // stat() @@ -548,6 +549,17 @@ int emcTaskPlanOpen(const char *file) emcStatus->task.readLine = 0; } + // Reset canon length units to machine default before re-opening. + // Without this, G20/G21 from the previous run persists in + // canon.lengthUnits, causing FROM_PROG_LEN() to convert differently + // on re-run for G-code commands that appear before G20/G21. + double units = GET_EXTERNAL_LENGTH_UNITS(); + if (fabs(units - 1.0 / 25.4) < 1.0e-3) { + USE_LENGTH_UNITS(CANON_UNITS_INCHES); + } else if (fabs(units - 1.0) < 1.0e-3) { + USE_LENGTH_UNITS(CANON_UNITS_MM); + } + int retval = interp.open(file); if (retval > INTERP_MIN_ERROR) { print_interp_error(retval);