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

(Probe Corner X minus Edge Angle for x,y,z 0,0,0 position)
(Start probe position is over front right corner of stock)
(inside the step off width square and within max z distance)
(ensure all settings have been set properly according to help diagrams)

o<probe_corner_x_minus_edge_angle> sub

  (uses NGCGUI style arg spec)
  (number after "=" in comment is default value)
  #<probe_tool_number> = #3014    (=99)
  #<probe_slow_fr> = #3015        (=0)
  #<probe_fast_fr> = #3016        (=10.0)
  #<probe_traverse_fr> = #3017    (=50)
  #<max_xy_distance> = #3018      (=0.5000)
  #<xy_clearance> = #3019         (=0.1000)
  #<max_z_distance> = #3020       (=0.5000)
  #<z_clearance> = #3021          (=0.1000)
  #<extra_probe_depth> = #3022    (=0.0000)  
  #<step_off_width> = #3023       (=0.5000)
  #<edge_width> = #3024           (=0.5000)
  #<probe_mode> = #3030           (=0)
  #<wco_rotation> = #3031         (=0)
  #<calibration_offset> = #3032   (=0)

  (Cancel G92 offsets)
  G92.1

  (Cancel Feedrate Override)
  M50 P0
  
  (set rotation offset to zero for new probe cycle)
  G10 L2 P[#5220] R0

  #<workspace_x> = #[5201 + [20 * #5220]]
  #<workspace_y> = #[5202 + [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_corner_x_minus_edge_angle> return
  o100 endif

  (Probe Diameter)
  #<probe_diameter> = #5410

  (Probing depth from the z probed clearance height, used for z moves)
  #<z_probe_stack> = [#<z_clearance> + #<probe_diameter> + #<extra_probe_depth>]

  #<z1x> = #5420

  #<z1y> = #5421

  (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 X Positioning Move, Step Off Width)
  G91
  F[#<probe_traverse_fr>]
  G38.3 y-[#<step_off_width>]

  (Probe X Positioning Move, to Probing Depth)
  F[#<probe_fast_fr>]
  G38.3 Z-[#<z_probe_stack>]

  #<p1x> = #5420

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

  #<p1y> = #<_value>

  (value returned safety check, aborts if no value returned)
  o110 if [#<probe_mode> EQ 1 AND #<_value_returned> NE 1]
      (DEBUG, Missing Y Sub returned edge parameter, aborting)
      M50 P1
      o<probe_corner_x_minus_edge_angle> return
  o110 endif

  (Probe Y Positioning Move, up to Z clearance plane)
  G91
  F[#<probe_traverse_fr>]
  G38.3 Z[#<z_probe_stack>]

  (Probe Y Positioning Move, Traverse Workpiece at Z Clearance plane)
  F[#<probe_traverse_fr>]
  G38.3 X[#<step_off_width>] Y[#<step_off_width>]

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

  #<p2y> = #5421

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

  #<p2x> = #<_value>

  (value returned safety check, aborts if no value returned)
  o120 if [#<probe_mode> EQ 1 AND #<_value_returned> NE 1]
      (DEBUG, Missing X Sub returned edge parameter, aborting)
      M50 P1
      o<probe_corner_x_minus_edge_angle> return
  o120 endif

  (edge width move to edge second probing point)
  G91
  F[#<probe_fast_fr>]
  G38.3 Y[#<edge_width>]

  #<p3y> = #5421

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

  #<p3x> = #<_value>

  (value returned safety check, aborts if no value returned)
  o130 if [#<probe_mode> EQ 1 AND #<_value_returned> NE 1]
      (DEBUG, Missing X Sub returned edge parameter, aborting)
      M50 P1
      o<probe_corner_x_minus_edge_angle> return
  o130 endif

  #<edge_delta> = [#<p2x> - #<p3x>]

  #<edge_angle> = [ATAN [#<edge_delta>] / [#<edge_width>]]

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

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

  #<c1xr> = [#<z1x> + [#<p2x> - #<z1x>] * COS[-#<edge_angle>] - [#<p2y> - #<z1y>] * SIN[-#<edge_angle>]]

  #<c1yr> = [#<z1y> + [#<p1x> - #<z1x>] * SIN[-#<edge_angle>] + [#<p1y> - #<z1y>] * COS[-#<edge_angle>]]

  #<c1x> = [#<z1x> + [#<c1xr> - #<z1x>] * COS[#<edge_angle>] - [#<c1yr> - #<z1y>] * SIN[#<edge_angle>]]

  #<c1y> = [#<z1y> + [#<c1xr> - #<z1x>] * SIN[#<edge_angle>] + [#<c1yr> - #<z1y>] * COS[#<edge_angle>]]

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

  (probe mode rules for WCO,Rotation and probe position measuring only)
  o140 if [#<probe_mode> EQ 0 AND #<wco_rotation> EQ 0]
      (Record Zero in selected axes and WCO)
      G10 L2 P#5220 X[#<c1x> + #<workspace_x>] Y[#<c1y> + #<workspace_y>]
  o140 endif

  (probe mode rules for WCO,Rotation and probe position measuring only)
  o150 if [#<probe_mode> EQ 0 AND #<wco_rotation> EQ 1]
      (Record Zero in selected axes and WCO)
      G10 L2 P#5220 X[#<c1x> + #<workspace_x>] Y[#<c1y> + #<workspace_y>] R[#<edge_angle>]
  o150 endif

  (Reinstate Feedrate Override)
  M50 P1

o<probe_corner_x_minus_edge_angle> endsub

M2 (end program)
