Skip to content

Commit b27451f

Browse files
committed
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.
1 parent c28a7c8 commit b27451f

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/emc/task/emctask.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
********************************************************************/
1515

1616
#include <stdlib.h>
17+
#include <math.h> // fabs()
1718
#include <rtapi_string.h> // rtapi_strlcpy()
1819
#include <sys/stat.h> // struct stat
1920
#include <unistd.h> // stat()
@@ -553,6 +554,17 @@ int emcTaskPlanOpen(const char *file)
553554
emcStatus->task.readLine = 0;
554555
}
555556

557+
// Reset canon length units to machine default before re-opening.
558+
// Without this, G20/G21 from the previous run persists in
559+
// canon.lengthUnits, causing FROM_PROG_LEN() to convert differently
560+
// on re-run for G-code commands that appear before G20/G21.
561+
double units = GET_EXTERNAL_LENGTH_UNITS();
562+
if (fabs(units - 1.0 / 25.4) < 1.0e-3) {
563+
USE_LENGTH_UNITS(CANON_UNITS_INCHES);
564+
} else if (fabs(units - 1.0) < 1.0e-3) {
565+
USE_LENGTH_UNITS(CANON_UNITS_MM);
566+
}
567+
556568
int retval = interp.open(file);
557569
if (retval > INTERP_MIN_ERROR) {
558570
print_interp_error(retval);

0 commit comments

Comments
 (0)