Configuring RosettaCNC M6 Tool Change Macros with G43

Tom Garrett8 min read
Motion ControlOther ManufacturerTutorial / How-to
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Overview: The Six Tool Change Modes

RosettaCNC handles M6 through a selectable tool change strategy rather than a single hard-coded routine. Three strategies are pre-built inside the CNC, and three route execution into a user-editable macro (subprogram) so the integrator controls every motion and handshake.

Mode Behaviour Typical use
None M6 is ignored entirely; no motion, no prompt Single-tool jobs. T/M6 lines remain in the file and can still be used to segment and colour long programs (e.g. marble roughing/finishing blocks) in the viewer
Manual Pre-built manual change driven by setup parameters Machines without ATC: define change position, movement sequence, whether to use tool-table data or re-measure length each time
Automatic Pre-built ATC sequence managed by the CNC/PLC Machines with a carousel/rack changer using the standard handshake
Custom macro User subprogram executes the whole change Non-standard sequences, extra I/O, dust shoe lift, probe cycles
Custom macro - manual User subprogram plus the built-in manual logic Manual change with added site-specific steps
Custom macro - automatic User subprogram plus the built-in automatic (PLC) logic ATC where the macro handles safe positioning and state, and the PLC does the mechanical swap
Selection matters before you write code. If you pick a pure Custom macro mode, the macro is responsible for everything, including the mechanical change. If you pick Custom macro - automatic, the macro delegates the mechanical change to the CNC/PLC via M106. Confirm which mode the machine is configured for before commissioning the macro below.

Anatomy of the M6 Custom Macro

The macro is called whenever M6 appears in the main program or is issued from MDI. The tool number requested by the T word is passed in as argument #1.

; User defined tool change subprogram
; called when M6 is executed from program or MDI
;
; Arguments
; =========
; #1   tool id to change

; store actual state group 0
#4101=[#5101]
; store actual state M3, M4, M5
#4151=#5151
; store actual state M7, M9
#4153=#5153
; store actual state M8, M9
#4154=#5154

; disable spindle, flood & mist
m5
m9

g53 g0 z0
m109 p"Insert tool T#1" q2
g4 p1

; call default PLC tool change management
m106

; restore previous states
if [#4151 eq 3] then m3
if [#4151 eq 4] then m4
if [#4153 eq 7] then m7
if [#4154 eq 8] then m8
if [#4101 le 1] then g#4101

; enable offset xyz compensation
g43 h#1

Execution order, line by line

  1. Snapshot modal state. The current modal G-code of group 0 and the current spindle/coolant M-code states are copied from the read-only system variables into user variables so they survive the change.
  2. Make the machine safe. M5 stops the spindle, M9 kills flood and mist. Do this before any Z motion so nothing is spraying at the change position.
  3. Retract in machine coordinates. G53 G0 Z0 moves to machine Z zero independently of any active work offset (G54–G59) or Z shift. Using G53 here is the critical detail: a program-coordinate G0 Z0 would crash into the part on most setups.
  4. Prompt or handshake. M109 P"Insert tool T#1" Q2 displays the message with the requested tool number substituted and waits for operator acknowledgement. G4 P1 adds a dwell after the dialog closes so motion does not restart the instant the button is released.
  5. Perform the change. M106 hands off to the built-in PLC tool change management.
  6. Restore modal state. Conditional restore of spindle direction, coolant, and motion mode.
  7. Apply the length offset. G43 H#1 activates the tool length compensation for the tool just loaded.

System Variable Map Used by the Macro

Variable Direction Meaning / expected values
#1 In (argument) Tool ID requested by the T word preceding M6
#5101 Read Active modal G-code of group 0 (motion mode)
#5151 Read Active spindle state: 3 = M3, 4 = M4, 5 = M5
#5153 Read Mist state: 7 = M7, 9 = M9
#5154 Read Flood state: 8 = M8, 9 = M9
#4101, #4151, #4153, #4154 Write/Read Storage copies of the four states above, used by the restore block

Why the motion mode restore is guarded

if [#4101 le 1] then g#4101 restores only G0 and G1. That guard is intentional: re-issuing G2 or G3 as a bare modal word without I/J/K or R arguments is not meaningful, and canned-cycle or other group 0 modes above 1 should not be blindly re-armed after a tool change. If the interrupted block was an arc, the post-processed program will re-establish the correct mode on the next motion block anyway.

Coolant restore is split across two variables. Mist (#5153) and flood (#5154) both report 9 when off, so you cannot infer both states from one variable. Store and test them separately, exactly as shown, or you will silently drop one coolant channel after every change.

Automatic G43 H Application and How to Override It

Because the macro ends with g43 h#1, the length compensation for the newly loaded tool is applied automatically using the same number that was passed to M6. The practical effect on the part program:

; before - offset written by hand, mismatch possible
T5 M6
G43 H5 Z25.0

; after - macro applies G43 H5 itself
T5 M6
Z25.0

This removes a real class of crash: a typed H that does not match the T word, whether from a post-processor quirk or a keystroke error. The number can only ever be the tool actually requested.

Using a different offset than the tool number

The macro sets a modal state, so it can be overridden by the next G43 the program issues. To deliberately run tool 5 on a different offset, place an explicit G43 after the M6 line:

T5 M6            ; macro loads T5 and applies G43 H5
G43 H12          ; override: now using offset 12

Two cautions with this technique:

  • The override must come after M6, never before, otherwise the macro's trailing G43 H#1 overwrites it.
  • Any later M6 resets the offset to match its own tool number, so re-issue the override after every change if it must persist.

If you prefer full manual control of H on this machine, delete or comment the final g43 h#1 line — but then every part program must supply its own G43 H, and you lose the typo protection.

Commissioning and Verification

  1. Dry-run the retract with the spindle empty. Jog to a work position with a work offset active, then execute T1 M6 from MDI. Confirm the Z axis goes to machine zero, not to work zero.
  2. Verify the prompt string. The dialog should read "Insert tool T1" with the number substituted, proving #1 is being passed and expanded.
  3. Test modal restore. Start M3 and M8, then run M6. After the change, spindle should be running in the same direction and flood should be back on. Repeat with M4 and M7.
  4. Test the off case. With spindle and coolant off before M6, confirm nothing is switched on afterwards — the eq tests should all fail.
  5. Verify the offset. After the change, check the tool length compensation display shows the offset from the tool table row matching the requested tool. Touch off a known surface and confirm the Z readout.
  6. Test a first change from a cold start. With no offset previously active, run T2 M6 and confirm G43 H2 takes effect without an intervening motion block.
  7. Test the mid-program case. Run a two-tool program and confirm the interrupted motion mode resumes correctly, especially if the block before M6 was G1.
Before enabling the automatic mode on an ATC: confirm the M106 PLC sequence, the tool table pocket assignments, and the safe change position agree with each other. A macro that retracts to G53 Z0 but a changer that expects a different clearance height will fail on the first swap. Keep the E-stop within reach for the first several cycles.

Extending the Macro

Typical additions that belong inside this macro rather than in the part program:

  • Move X/Y to a fixed change position with G53 G0 X.. Y.. after the Z retract, when the changer or the operator needs clearance from the workpiece.
  • Insert a tool length measurement cycle after M106 and before G43, if the setup calls for re-measuring rather than trusting the tool table.
  • Raise a dust shoe or open a guard through auxiliary M-codes before the retract, and restore it in the same conditional style used for spindle and coolant.
  • Skip the entire sequence when the requested tool equals the tool already in the spindle, to avoid a pointless cycle on redundant M6 calls.

Keep the store/restore block symmetrical: anything you switch off at the top must be conditionally switched back on at the bottom, keyed off a stored variable rather than assumed.

Does RosettaCNC apply G43 automatically after M6?

Only if the tool change macro ends with g43 h#1. That line applies the length offset whose number matches the tool passed to M6, so the part program no longer needs its own G43 H line.

Can I use a tool offset number different from the tool number?

Yes. Issue an explicit G43 H<n> on the line after M6. It overrides the modal offset the macro just set. The next M6 will reset it back to the tool number.

Why does the macro use G53 G0 Z0 instead of G0 Z0?

G53 forces the move into machine coordinates, ignoring the active work offset. A plain G0 Z0 would move to work zero, which on most setups is at or inside the part.

Which variables hold the spindle and coolant state during a tool change?

#5151 reports 3, 4 or 5 for M3/M4/M5; #5153 reports 7 or 9 for mist; #5154 reports 8 or 9 for flood. The macro copies them to #4151, #4153 and #4154 before issuing M5 M9, then restores them conditionally.

Can RosettaCNC run a fully automatic ATC tool change?

Yes. Select the automatic mode for the built-in sequence, or the custom-macro-automatic mode to run your own macro that delegates the mechanical swap to the PLC through M106.

Back to blog