(author: Chris P, TooTall18)
(version: 0.2)
(date: 06/27/24)

o<tool_touch_off> sub

#<fast_probe_fr> = #3004    (set from probe screen fast probe feed rate)
#<slow_probe_fr> = #3005    (set from probe screen slow probe feedrate)
#<traverse_fr> = #3006    (set from probe screen traverse probe feedrate)
#<z_max_travel> = #3007    (max z distance the tool travels before erroring out if not contact is made)
#<xy_max_travel> = #3008    (max xy distance the tool travels before erroring out if not contact is made)
#<retract_distance> = #3009    (distance the tool retracts after making contact during fast feed mode)
#<spindle_zero_height> = #3010    (G53 distance from home to spindle nose triggering point on touch plate)
#<tool_diameter_probe_mode> = #3011    (activates the tool diameter probe subroutine section)
#<tool_diameter_offset_mode> = #3012    (activates the tool diameter offset position for probe subroutine section)
#<tool_setter_offset_direction> = #3013    (sets tool setter offset direction to move tool)
#<tool_diameter> = #5410    (current tool's diameter used for offseting probe position in x axis)

G92.1    (Cancel G92 offset)

#<tool_touch_x_coords> = #5181
#<tool_touch_y_coords> = #5182
#<tool_touch_z_coords> = #5183

#<tool_radius_offset> = [#<tool_diameter> / 2]
#<left_offset_probing_position> = [#<tool_touch_x_coords> - #<tool_radius_offset>]
#<right_offset_probing_position> = [#<tool_touch_x_coords> + #<tool_radius_offset>]
#<front_offset_probing_position> = [#<tool_touch_y_coords> - #<tool_radius_offset>]
#<back_offset_probing_position> = [#<tool_touch_y_coords> + #<tool_radius_offset>]

o100 if [#<tool_diameter_offset_mode> EQ 1]
    o101 if [#<tool_setter_offset_direction> EQ 0]
        #<tool_touch_x_coords> = #<left_offset_probing_position>
    o101 else if [#<tool_setter_offset_direction> EQ 1]
        #<tool_touch_x_coords> = #<right_offset_probing_position>
    o101 else if [#<tool_setter_offset_direction> EQ 2]
        #<tool_touch_y_coords> = #<front_offset_probing_position>
    o101 else if [#<tool_setter_offset_direction> EQ 3]
        #<tool_touch_y_coords> = #<back_offset_probing_position>
    o101 endif
o100 endif

o110 if [2 EQ 2]
    G49
o110 endif

G90    (set absolute coordinates)
G53 G1 F[#<traverse_fr>] Z0    (move to z0 home position)
G53 G1 F[#<traverse_fr>] X#<tool_touch_x_coords> Y#<tool_touch_y_coords>
G53 G1 F[#<traverse_fr>] Z#<tool_touch_z_coords>

#<offset_z> = #5422         ;Stores the offset of the current Z coordinate.

G91
F #<fast_probe_fr>
G38.2 Z-[#<z_max_travel>]    (fast tool probe)
#<z_fast_probe> = #5063    (save probe result of fast probe to parameters)
G1 F[#<traverse_fr>] Z[#<retract_distance>]    (retract tool retract distance amount)

(Slow Probe Rule, if Slow Probe FR is set to 0, Slow Probe is Bypassed)
o120 if [#<slow_probe_fr> GT 0]
    (Initiate Slow Z- Probe)
    G91
    F[#<slow_probe_fr>]    (set probe slow feedrate)
    G38.2 Z-[#<retract_distance> * 2]    (slow tool probe)
    #<z_slow_probe> = #5063
    G90
    G1 F[#<traverse_fr>] Z[#<z_slow_probe> + #<retract_distance>]
o120 endif

o130 if [#5070 EQ 1]    (verify probe event was succesful)
    #<z_slow_probe> = #5063    (save slow probe result to parameters)
o130 else
    (MSG,Tool Length Offset Probe Failed)
o130 endif

(Tool Diameter Probe Mode Section, User must define this section as needed)
o140 if [#<tool_diameter_probe_mode> EQ 1]
    (MSG, Tool Diameter Probing is Not Defined in Subroutine)
o140 endif

o150 if [3 EQ 3]
    G49
o150 endif

G90    (set absolute coordinates)
G53 G1 F[#<traverse_fr>] Z0 (Send Spindle to home zero position)

(define new tool length offset parameters)
#<new_tool_length_offset> = [ABS[#<spindle_zero_height> + #5063 - #<offset_z>]]

G10 L1 P #5400 Z [#<new_tool_length_offset>]  (5400 = tool number)

T #5400 G43  H #5400    (enable tool length offset)

o<tool_touch_off> endsub

M2 (end program)
