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

(Probe Ridge in Y Axis to find center and z 0 position)
(Start probe position is over center of stock in the Y axis)
(within max z distance, ensure all settings have been)
(set properly according to help diagrams)

o<probe_ridge_y> sub

  (uses NGCGUI style arg spec)
  #<probe_tool_number> = #3014
  #<probe_slow_fr> = #3015
  #<probe_fast_fr> = #3016
  #<probe_traverse_fr> = #3017
  #<max_xy_distance> = #3018
  #<xy_clearance> = #3019
  #<max_z_distance> = #3020
  #<z_clearance> = #3021
  #<extra_probe_depth> = #3022  
  #<step_off_width> = #3023
  #<edge_width> = #3024
  #<x_hint_ridge_valley> = #3028
  #<y_hint_ridge_valley> = #3029
  #<probe_mode> = #3030
  #<calibration_offset> = #3032

  (Cancel G92 offsets)
  G92.1

  (Cancel Feedrate Override)
  M50 P0

  #<workspace_y> = #[5202 + [20 * #5220]]
  #<workspace_z> = #[5203 + [20 * #5220]]

  (Probe Tool Safety Check)
  o100 if [#5400 NE #<probe_tool_number>]
      (DEBUG, Specified probe tool %d#<probe_tool_number> not in spindle, aborting)
      M50 P1
      o<probe_ridge_y> return
  o100 endif

  (Probe Diameter)
  #<probe_diameter> = #5410

  (Probe Radius)
  #<probe_radius> = [#<probe_diameter> / 2]

  (Probe Centerline Offset)
  #<probe_center_offset> = [#<probe_radius> - #<calibration_offset>]

  (remove probe tip diam and cal offset from probed width calculations)
  #<probe_diameter_offset> = [#<probe_diameter> - [#<calibration_offset> * 2]]

  #<z_probe_stack> = [#<z_clearance> + #<probe_diameter> + #<extra_probe_depth>]

  (Call sub "probe_z_minus_sub" to Probe top of Workpiece)
  o<probe_z_minus_sub> call [#3014][#3015][#3016][#3017][#3020][#3021][#3030]

  (Probe Y Positioning Move, Y hint/2 + Step Off Width)
  G91
  F[#<probe_traverse_fr>]
  G38.3 Y-[[#<y_hint_ridge_valley>/2] + #<step_off_width>]

  (Probe Y Positioning Move, to Probing Depth)
  F[#<probe_fast_fr>]
  Z-[#<z_probe_stack>]

  (Call sub "probe_y_plus" to Probe y+ side of Workpiece)
  o<probe_y_plus> call [#3014][#3015][#3016][#3017][#3018][#3019][#3032]

  #<y_plus_probed> = #5062

  (first side Probe Completion Move to Z Clearance Plane)
  G91
  F[#<probe_traverse_fr>]
  G38.3 Z[#<z_probe_stack>]

  (Positioning move in Y, Y Hint + Step Off Width at Rapid Speed)
  F[#<probe_traverse_fr>]
  G38.3 Y[#<y_hint_ridge_valley> + #<step_off_width>]

  (Probe Y Positioning Move, to Probing Depth)
  F[#<probe_fast_fr>]
  Z-[#<z_probe_stack>]

  (Call sub "probe_y_minus" to Probe y- side of Workpiece)
  o<probe_y_minus> call [#3014][#3015][#3016][#3017][#3018][#3019][#3032]

  #<y_minus_probed> = #5062

  (probed center calulation)
  #<y_center_probed> = [[#<y_plus_probed> + #<y_minus_probed>] / 2]

  (calculate Y Width Probed)
  #<raw_width> = [#<y_minus_probed> - #<y_plus_probed>]

  (Completed probed width calculations)
  #<y_probed_width> = [#<raw_width> - #<probe_diameter_offset>]

  #1003 = #<y_probed_width>
  (DEBUG, EVAL[vcp.getWidget{"y_probed_width"}.setValue{#1003}])

  #1004 = #<y_center_probed>
  (DEBUG, EVAL[vcp.getWidget{"y_center_probed"}.setValue{#1004}])

  (Probe Completion Move to Z Clearance Plane, and X Zero)
  G91
  F[#<probe_traverse_fr>]
  G38.3 Z[#<z_probe_stack>]
  G90
  F[#<probe_traverse_fr>]
  G38.3 Y[#<y_center_probed>]

  (probe mode rules for WCO or probe position measuring only)
  o110 if [#<probe_mode> EQ 0]
      (Record Y Zero in selected WCO)
      G10 L2 P#5220 Y[#<y_center_probed> + #<workspace_y>]
      (Probe Completion Move to Z Clearance Plane, and X Zero, Y Zero)
  o110 endif

  (Reinstate Feedrate Override)
  M50 P1

o<probe_ridge_y> endsub

M2 (end program)
