(author: Chris P)
(version: 0.3)
(date: 04/25/19)

(Probe top right edge angle for y,z 0,0 position and edge angle)
(Start probe position is over right edge of stock, set edge width,)
(step off width distance and within max z distance)
(ensure all settings have been set properly according to help diagrams)

o<probe_right_edge_angle> sub

  (uses NGCGUI style arg spec)
  (number after "=" in comment is default value)
  #<probe_tool_number> = #1    (=99)
  #<max_z_distance> = #2       (=0.5000)
  #<max_xy_distance> = #3      (=0.5000)
  #<xy_clearance> = #4         (=0.1000)
  #<z_clearance> = #5          (=0.1000)
  #<step_off_width> = #6       (=0.5000)
  #<extra_probe_depth> = #7    (=0.0000)
  #<probe_slow_fr> = #8        (=0.0)
  #<probe_fast_fr> = #9        (=10.0)
  #<calibration_offset> = #10  (=0.0000)
  #<x_hint> = #11              (=1.0000)
  #<y_hint> = #12              (=1.0000)
  #<diameter_hint> = #13       (=1.0000)
  #<edge_width> = #14          (=0.5000)
  #<probe_mode> = #15          (=0)
  #<wco_rotation> = #16        (=0)

  (Cancel G92 offsets)
  G92.1

  #<workspace_x> = #[5201 + [20 * #5220]]

  (Probe Tool Safety Check)
  o<110> if [#5400 NE #<probe_tool_number>]
  (MSG, Specified probe tool #<probe_tool_number> not in spindle, aborting)
   o<Probe_right_edge_angle> return
  o<110> 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>]

  #<y_start_position> = #5421

  (Probe X Positioning Move, Step Off Width)
  G91
  G0 X[#<step_off_width>]

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

  (Call sub "probe_x_minus" to Probe x- side of Workpiece)
  o<probe_x_minus> call [#1][#3][#4][#8][#9][#10]

  #<x_minus_zero_edge_start> = #<_value>

  (value returned safety check, aborts if no value returned)
  o<120> if [#<probe_mode> EQ 1 AND #<_value_returned> NE 1]
    (MSG, Missing X Sub returned edge parameter, aborting)
    o<Probe_right_edge_angle> return
  o<120> endif

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

  (Call sub "probe_x_minus" to Probe x- side of Workpiece)
  o<probe_x_minus> call [#1][#3][#4][#8][#9][#10]

  #<x_minus_zero_edge_end> = #<_value>

  (value returned safety check, aborts if no value returned)
  o<130> if [#<probe_mode> EQ 1 AND #<_value_returned> NE 1]
    (MSG, Missing X Sub returned edge parameter, aborting)
    o<Probe_right_edge_angle> return
  o<130> endif

  #<edge_delta> = [#<x_minus_zero_edge_start> - #<x_minus_zero_edge_end>]

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

  M68 E4 Q[#<edge_delta>]

  M68 E3 Q[#<edge_angle>]

  (Probe Completion Move to Z Clearance Plane, and Y Zero)
  G91
  G0 Z[#<z_probe_stack>]
  G90
  G0 X[#<x_minus_zero_edge_start>] Y[#<y_start_position>]

  (probe mode rules for WCO, Rotation and probe position measuring only)
  o<140> if [#<probe_mode> EQ 0 AND #<wco_rotation> EQ 0]
    (Record Zero in selected axes and WCO)
    G10 L2 P#5220 X[#<x_minus_zero_edge_start> + #<workspace_x>] Y[#<y_start_position>]
    o<Probe_right_edge_angle> return
  o<140> endif

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

o<probe_right_edge_angle> endsub

M2 (end program)

