Skip to content

Instantly share code, notes, and snippets.

@zachallaun
Last active January 8, 2024 20:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachallaun/8f5ff5f107e48ca8461715d5b6039968 to your computer and use it in GitHub Desktop.
Save zachallaun/8f5ff5f107e48ca8461715d5b6039968 to your computer and use it in GitHub Desktop.
A cncjs macro for fixed-location tool changes (WIP)
; This macro allows you to use a fixed machine location for a tool change and probe. It works by
; probing twice, once to capture the position of the tool you're changing away from, and again to
; set the adjusted WCS offset of the tool you're changing to.
; Using the macro in practice:
; 1) Insert an M6 tool change command in your program, which will pause cncjs and allow you to
; execute this macro.
; 2) The router retracts to a safe height and moves to a pre-defined location for tool changes and
; probing, then pauses.
; 3) Turn off your router, set up your probe, and continue. This will probe to capture the position
; of your current tool and then retract back up to the tool change height and pause.
; 4) Change out your tool, set up your probe, and continue. This will probe the new tool. When it
; reaches the probe, the end of the tool will be in precisely the same position as the original
; tool, but the Z position will be different, so we set the current Z offset to be the previously
; captured offset.
; 5) Remove the probe and continue. The spindle will move back to the location it encountered
; the M6.
; 6) Turn your router back on and resume execution of your program.
%wait
G21 ; Metric
; User-defined variables (***METRIC***)
%SAFE_HEIGHT = -1 ; Height needed to clear everything (negative number, distance below Z limit)
%TOOL_PROBE_X = -580 ; Machine coords
%TOOL_PROBE_Y = -564 ; Machine coords
%TOOL_PROBE_Z = -10 ; Machine coords
%PROBE_DISTANCE = 100
%PROBE_FAST_FEEDRATE = 200 ; mm/min
%PROBE_SLOW_FEEDRATE = 20 ; mm/min
%wait
; Keep a backup of current work position
%X0=posx, Y0=posy, Z0=posz
; Save modal state
; * Work Coordinate System: G54, G55, G56, G57, G58, G59
; * Plane: G17, G18, G19
; * Units: G20, G21
; * Distance Mode: G90, G91
; * Feed Rate Mode: G93, G94
; * Spindle State: M3, M4, M5
; * Coolant State: M7, M8, M9
%WCS = modal.wcs
%PLANE = modal.plane
%UNITS = modal.units
%DISTANCE = modal.distance
%FEEDRATE = modal.feedrate
%SPINDLE = modal.spindle
%COOLANT = modal.coolant
M5 ; Stop spindle
G90
G53 Z[SAFE_HEIGHT]
G53 X[TOOL_PROBE_X] Y[TOOL_PROBE_Y]
%wait
; Set up for probing
M0
G53 Z[TOOL_PROBE_Z]
G91
G38.2 Z-[PROBE_DISTANCE] F[PROBE_FAST_FEEDRATE]
G0 Z1
G38.2 Z-2 F[PROBE_SLOW_FEEDRATE]
G90
%ORIGINAL_TOOL = posz
%wait
G91
G0 Z5
G90
G53 Z[SAFE_HEIGHT]
%wait
; Manual tool change & probing
M0
G53 Z[TOOL_PROBE_Z]
G91
G38.2 Z-[PROBE_DISTANCE] F[PROBE_FAST_FEEDRATE]
G0 Z1
G38.2 Z-2 F[PROBE_SLOW_FEEDRATE]
G90
%wait
; Update Z offset for new tool to be that of original tool
G10 L20 Z[ORIGINAL_TOOL]
%wait
G91
G0 Z5
G90
G53 Z[SAFE_HEIGHT]
%wait
; Cleanup (e.g. remove touch plate, wires, etc)
M0
; Go to previous work position
G0 X[X0] Y[Y0]
G0 Z[Z0]
; Restore modal state
[WCS] [PLANE] [UNITS] [DISTANCE] [FEEDRATE] [SPINDLE] [COOLANT]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment