en:software:mdurosettacncsoftwaregcode:macro_programming:user_tool_change_subprogram

Setting the Tool Change Type option to Custom Macro in the board settings the user can customise the tool change procedure. if the option is enabled the M6 command will look into the machine macro folder and execute the G-code file named tool_change.ngc. In this file the user can specify any supported G-code command to perform the tool change procedure as required by the specific machine.

When the tool_change.ngc is used it could be useful to consider using 4 M codes:

  • M61: Set the current tool
  • M106: Execute PLC internal tool change procedure
  • M107: Inform the PLC that the following commands are part of the user tool change macro. This is used for visualisation purposes and for axes limits checking.
  • M108: Inform the PLC that the user tool change macro has ended. This is used for visualisation purposes and for axes limits checking.
; (c) 2016-2019 by RosettaCNC Motion
; User defined tool change subprogram:
; will be called when M6 is called if the parameter "Tool Change Type"
; is set to one of the custom macro modes.
;
; Arguments
; =========
; #1 : tool id of the tool to be loaded (corresponds to #5132)
; #2 : slot of the tool to be loaded (corresponds to #5133)
; #3 : tool id of the tool in use (corresponds to #5134)
; #4 : slot of the tool in use (corresponds to #5135)
 
; Skip tool change if the tool to be loaded is already loaded
IF [#1 EQ #3] THEN M99
 
; Store actual the G code of the modal group 1: G0, G1, ...
#4101=#5101
; Store actual state M3, M4, M5
#4151=#5151
; Store actual state M7, M9
#4153=#5153
; Store actual state M8, M9
#4154=#5154
; Store current positions (X, Y, Z, A, B, C)
#4001=#5001
#4002=#5002
#4003=#5003
#4004=#5004
#4005=#5005
#4006=#5006
 
; Disable spindle, flood & mist
M5 M9
 
; Move upwards to a "safe position"
G53 G0 Z200
; Move to tool change position
G53 G0 X-100 Y-100
 
; Display the message to the user
M109 P"Insert tool T#1" Q2
G4 P1
 
; Call M61 or M106:
; - call M106 to use part of the RosettaCNC internal tool change procedure
; - call M61 if you have handled the tool change procedure entirely in your code
;   to inform RosettaCNC that the the new tool has been loaded
M61 Q#1
 
; Move back above the original position (keep Z to a "safety position")
G0 X#4001 Y#4002
 
; 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
 
; Enable tool offset compensation
G43 H#1
 
; Move Z back to the original position
G1 Z#4003
 
G#4101
 
M99
; (c) 2016-2019 by RosettaCNC Motion
; User defined tool change subprogram:
; will be called when M6 is called if the parameter "Tool Change Type"
; is set to one of the custom macro modes.
;
; Arguments
; =========
; #1 : tool id of the tool to be loaded (corresponds to #5132)
; #2 : slot of the tool to be loaded (corresponds to #5133)
; #3 : tool id of the tool in use (corresponds to #5134)
; #4 : slot of the tool in use (corresponds to #5135)
 
; Skip tool change if the tool to be loaded is already loaded
IF [#1 EQ #3] THEN M99
 
; Store actual the G code of the modal group 1: G0, G1, ...
#4101=#5101
; Store actual state M3, M4, M5
#4151=#5151
; Store actual state M7, M9
#4153=#5153
; Store actual state M8, M9
#4154=#5154
; Store current positions (X, Y, Z, A, B, C)
#4001=#5001
#4002=#5002
#4003=#5003
#4004=#5004
#4005=#5005
#4006=#5006
 
; Disable spindle, flood & mist
M5 M9
 
; Move upwards to a "safe position"
G53 G0 Z200
; Move to intermediate tool change position
G53 G0 X0 Y0
 
; Move to a position that depends on the active slot to drop the current tool
IF [#4 EQ 1] THEN G53 G1 X-10 Y0
IF [#4 EQ 2] THEN G53 G1 X-10 Y10
IF [#4 EQ 3] THEN G53 G1 X-10 Y20
IF [#4 EQ 4] THEN G53 G1 X-10 Y30
 
; Set aux1 output to drop the current tool
M62 P1
 
; Inform the PLC that the tool has been dropped
M61 Q0
 
; Move to intermediate tool change position
G53 G0 X0 Y0
 
; Move to a position that depends on the selected slot to load the tool
IF [#2 EQ 1] THEN G53 G1 X-10 Y0
IF [#2 EQ 2] THEN G53 G1 X-10 Y10
IF [#2 EQ 3] THEN G53 G1 X-10 Y20
IF [#2 EQ 4] THEN G53 G1 X-10 Y30
 
; reset the aux1 output to lock new tool
M63 P1
 
; Call M61 or M106:
; - call M106 to use part of the RosettaCNC internal tool change procedure
; - call M61 if you have handled the tool change procedure entirely in your code
;   to inform RosettaCNC that the the new tool has been loaded
M61 Q#1
 
; Move back above the original position (keep Z to a "safety position")
G0 X#4001 Y#4002
 
; 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
 
; Enable tool offset compensation
G43 H#1
 
; Move Z back to the original position
G1 Z#4003
 
G#4101
 
M99
; (c) 2016-2019 by RosettaCNC Motion
; User defined tool change subprogram:
; will be called when M6 is called if the parameter "Tool Change Type"
; is set to one of the custom macro modes.
;
; Arguments
; =========
; #1 : tool id of the tool to be loaded (corresponds to #5132)
; #2 : slot of the tool to be loaded (corresponds to #5133)
; #3 : tool id of the tool in use (corresponds to #5134)
; #4 : slot of the tool in use (corresponds to #5135)
 
; System parameters
; =========
; #6002: system parameter: Set to 1 if the tool change should be skipped if the same tool is already loaded.
; #6003: system parameter: Feed during tool change procedure
; #6010: system parameter: 0: perform tool change; 
;                          1: perform tool change and enable tool compensation from table;
;                          2: perform tool change, measure tool length and apply it
; #6011: system parameter: Tool change position X
; #6012: system parameter: Tool change position Y
; #6013: system parameter: Tool change position Z
; #6021: system parameter: Feed fast approaching sensor
; #6022: system parameter: Feed slow approaching sensor
; #6023: system parameter: Approaching sensor target Z (reached with fast feed #6021)
; #6024: system parameter: Sensor Z BCS position (reached with slow feed #6022)
; #6031: system parameter: Pre-measuring position X
; #6032: system parameter: Pre-measuring position Y
; #6033: system parameter: Pre-measuring position Z
 
IF [#6002 GE 1] THEN10
  ; Skip tool change if the tool to be loaded is already loaded
  IF [#1 EQ #3] THEN M99
END10
 
IF [[#6002 EQ #0] OR[#6003 EQ #0] OR [#6010 EQ #0] OR [#6011 EQ #0] OR [#6012 EQ #0] OR [#6013 EQ #0]] THEN10
  M109 P"One or more compulsory system parameters are not specified."  Q1
  M2
END10
 
IF [[#6021 EQ #0] OR [#6022 EQ #0] OR [#6023 EQ #0] OR [#6024 EQ #0] OR [#6031 EQ #0] OR [#6032 EQ #0] OR [#6033 EQ #0]] THEN10
  M109 P"One or more compulsory system parameters are not specified."  Q1
  M2
END10
 
; Store actual the G code of the modal group 1: G0, G1, ...
#4101=#5101
; Store current target feed
#4130=#5130
; Store actual state M3, M4, M5
#4151=#5151
; Store actual state M7, M9
#4153=#5153
; Store actual state M8, M9
#4154=#5154
; Store current positions (X, Y, Z, A, B, C)
#4001=#5001
#4002=#5002
#4003=#5003
#4004=#5004
#4005=#5005
#4006=#5006
; Measured tool offset Z
#4010=0
 
; Inform the PLC that the tool change procedure is starting
M107
 
M109 P"Tool change procedure" Q4
 
; Disable spindle, flood & mist
M5 M9
 
F#6003
; Move upwards to a "safe position"
G53 G1 Z#6013
; Move to tool change position
G53 G1 X#6011 Y#6012
 
IF [#1 NE #3] THEN10
  ; A different tool should be inserted. Display the message to the user.
  M109 P"Insert tool T#1 and press OK" Q2
  G4 P1
  M61 Q#1
END10
 
; Check if a G38.X is used. In that case convert to a G80 to prevent an error when setting G#4101.
IF [[#5101 GE 38] AND [#5101 LT 39]] THEN10
  #4101 = 80
END10
 
IF [#6010 EQ 2] THEN10
  ; Measure tool offset
  M109 P"Check tool length procedure" Q4
  G53 G1 Z#6033
  G53 G1 X#6031 Y#6032
  G53 F#6021 G38.3 Z#6023
  IF [#5700 EQ -2] THEN M109 P"Error updating piece position: Sensor already tripped!" Q1 M108
  ; Sensor should not be tripped during the fast approach
  IF [#5700 EQ 1] THEN M109 P"Error during tool measuring procedure: Sensor tripped during fast approach!" Q1 M108
  G53 F#6022 G38.3 Z#6024
  IF [#5700 EQ -2] THEN M109 P"Error updating piece position: Sensor already tripped!" Q1 M108
  IF [#5700 NE 1] THEN M109 P"Error during tool measuring procedure: Sensor not triggered!" Q1 M108
  ; Calculate the tool length offset as the difference between when the sensor was tripped and the sensor Z position.
  #4010 = [#5713 - #6024]
  ; Move upwards to a "safe position".
  F#6003
  G53 G1 Z#6013
  M109 P"Check tool length procedure has ended. Detected length is #4010." Q4
END10
 
; Move back above the original position (keep Z to a "safety position").
G1 X#4001 Y#4002
 
IF [#6010 EQ 1] THEN10
  ; Enable tool offset compensation considering the tool offset written in the tool table.
  G43 H#1
END10
IF [#6010 EQ 2] THEN10
  ; Enable tool length compensation.
  G43.1 Z#4010
END10
 
; 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
 
; Move Z back to the original position.
G1 Z#4003
 
; Restore original target feed
F#4130
; Restore original G code if it is possible (G0 or G1 or G2 or G3 or ...).
G#4101
 
; Reset HUD message.
M109 P"" Q4 
 
; Inform the PLC that the tool change procedure has ended.
M108
M99

Usage example.

G54 G49 F1000
; Reset G54 WCS offsets
G10 L2 P1 X0 Y0 Z0
; Ensure that the current position can be reached when the tool compensation is activated.
G0 X0 Y0 Z-20
T1
M6
; Tool length compensation is enabled if parameter #6010 is set to 1 or 2.
M109 P"Compensated tool length is #5422"
G1 X100
M2
  • Last modified: 2023/03/22 09:07
  • by 127.0.0.1