GCode Variables and Loops

Okay this one works, I had to add a X and Z home in the middle to get it right. To speed it up I set it to rapid down 80% for each probe so we could do a slower probe.

(PRINT, LowRider Z Leveling for FluidNC v3.9.1 or higher)

(UPDATE REQUIRED)
#<probeYPos> = 55 (Y position to probe at dependent upon condition of spoilboard)
#<probeXLeftPos> = 5 (Left X position to probe at dependent upon condition of spoilboard)
#<probeXRightPos> = 635 (Right X position to probe at, should be close to max X)
#<xTotalDistance> = 892 (Total X Distance - On LR3, this is from left bearing to Y rail)
#<zMotorLeft> = 0 (Left Z Motor - 0 if Z0 is on the left, 1 if Z1 is on the left)

(UPDATE OPTIONAL)
#<minPulloff> = 4.0 (Minimum pulloff value)
#<jogXYFeedrate> = 1000 (Feedrate for X and Y jogging)
#<jogZFeedrate> = 500 (Feedrate for Z jogging)
#<jogHeight> = 30 (Z height when jogging in X or Y)
#<probeDistance> = -110 (Max probe distance, should be negative)
#<probeFeedrate> = 100 (Feedrate when probing)
#<probeCount> = 3 (Number of times to probe on each side)

#<probeRapidDistance> = [#<probeDistance> * 0.8] 

(PRINT, Home All and Initialize)
$H
G21
G90
G94
G92 X0 Y0 Z0

(PRINT, Move to Initial Y Position %d#<probeYPos>)
G00 Y#<probeYPos> F#<jogXYFeedrate>
(PRINT, Move to Initial X Position %d#<probeXLeftPos>)
G00 X#<probeXLeftPos> F#<jogXYFeedrate>

M0 (PRINT, Attach Probe and Probe Left x%d#<probeCount>)
#<probeSum> = 0
o100 repeat [#<probeCount>]
  G0 Z#<probeRapidDistance> F#<jogZFeedrate>
  G38.2 Z#<probeDistance> F#<probeFeedrate>
  #<probeSum> = [#<probeSum> + #5063]
  (PRINT, Probe Left Z: #5063)
  $HZ F#<jogZFeedrate>
o100 endrepeat
#<probeLeftAvg> = [#<probeSum>/3]

M0 (PRINT, Detach Probe Before Moving to Z%d#<jogHeight>  and X%d#<probeXRightPos>)
$HZ F#<probeFeedrate>
$HX
G00 X#<probeXRightPos> F#<jogXYFeedrate>

M0 (PRINT, Re-attach Probe and Probe Right x%d#<probeCount>)

(MSG Probe Right)
#<probeSum> = 0
o101 repeat [#<probeCount>]
  G0 Z#<probeRapidDistance> F#<jogZFeedrate>
  G38.2 Z#<probeDistance> F#<probeFeedrate>
  #<probeSum> = [#<probeSum> + #5063]
  (PRINT, Probe Right Z: #5063)
  $HZ F#<jogZFeedrate>
o101 endrepeat
#<probeRightAvg> = [#<probeSum>/3]

(PRINT, Z Average: Left=#<probeLeftAvg>, Right=#<probeRightAvg>)

#<zDifference> = [ABS[ABS[#<probeLeftAvg>] - ABS[#<probeRightAvg>]]]
(PRINT, Z Difference: #<zDifference>)

#<xDistance> = [#<probeXRightPos> - #<probeXLeftPos>]
#<zOffsetChange> = [[#<zDifference> / #<xDistance>] * #<xTotalDistance>]

(Get current Z pulloff values)
#<z0Pulloff> = #</axes/z/motor0/pulloff_mm>
#<z1Pulloff> = #</axes/z/motor1/pulloff_mm>

(Calculate New Pulloff Values)
#<z0PulloffNew> = #<z0Pulloff>
#<z1PulloffNew> = #<z1Pulloff>

o102 if [[#<zMotorLeft> EQ 0] AND [#<probeLeftAvg> GT #<probeRightAvg>]]
  #<z1PulloffNew> = [#<z1PulloffNew> + #<zOffsetChange>]
o102 elseif [[#<zMotorLeft> EQ 1] AND [#<probeLeftAvg> GT #<probeRightAvg>]]
  #<z0PulloffNew> = [#<z0PulloffNew> + #<zOffsetChange>]
o102 elseif [[#<zMotorLeft> EQ 0] AND [#<probeRightAvg> GT #<probeLeftAvg>]]
  #<z0PulloffNew> = [#<z0PulloffNew> + #<zOffsetChange>]
o102 elseif [[#<zMotorLeft> EQ 1] AND [#<probeRightAvg> GT #<probeLeftAvg>]]
  #<z1PulloffNew> = [#<z1PulloffNew> + #<zOffsetChange>]
o102 endif

(PRINT, New Pulloff Values Prior to Normalizing: Z0=#<z0PulloffNew>, Z1=#<z1PulloffNew>)

(Normalize New Pulloff Values to Minimum Pulloff)
#<change> = 0
o103 if [#<z0PulloffNew> LT #<z1PulloffNew>]
  #<change> = [#<minPulloff> - #<z0PulloffNew>]
  #<z0PulloffNew> = #<minPulloff>
  #<z1PulloffNew> = [#<z1PulloffNew> + #<change>]
o103 elseif [#<z0PulloffNew> GT #<z1PulloffNew>]
  #<change> = [#<minPulloff> - #<z1PulloffNew>]
  #<z1PulloffNew> = #<minPulloff>
  #<z0PulloffNew> = [#<z0PulloffNew> + #<change>]
o103 endif

(PRINT, Z0 Pulloff: Current=#<z0Pulloff>, New=#<z0PulloffNew>)
(PRINT, Z1 Pulloff: Current=#<z1Pulloff>, New=#<z1PulloffNew>)

M0 (PRINT, Continue to Update Pulloff Values)

(Set Pulloff Values)
#</axes/z/motor0/pulloff_mm> = #<z0PulloffNew>
#</axes/z/motor1/pulloff_mm> = #<z1PulloffNew>

(Save Pulloff Values to Config File)
$CD=config.yaml

(PRINT, Z Leveling Complete)
1 Like