GCode Variables and Loops

Ok, I’m more excited about this than I probably should be but I have a good use for this and it works.

Previously, I had created an extension I was still working on to perform Z leveling. It works well, but honestly an extension is a bit overkill. Now, I’ve recreated that functionality with a gcode file that will run on FluidNC 3.8.2 or higher regardless of WebUI version.

Anyways, check this out. You need to set the first 4 variables as appropriate for your build. It performs these steps:

  1. Home All
  2. Move to starting left probe position.
  3. Prompt to attach probe before continuing.
  4. Probe at left 3x and calculate average.
  5. Prompt to remove probe before moving to right position.
  6. Move to right probe position.
  7. Prompt to attach probe before probing at right position.
  8. Probe at right 3x and calculate average
  9. Calculate new pulloff values.
  10. Prompt before updating values and saving.
(LowRider Z Leveling for FluidNC v3.8.2 or higher)

(UPDATE REQUIRED)
#<probeYPos> = 50 (Y position to probe at dependent upon condition of spoilboard)
#<probeXLeftPos> = 0 (Left X position to probe at dependent upon condition of spoilboard)
#<probeXRightPos> = 880 (Right X position to probe at, should be close to max X)
#<xTotalDistance> = 1080 (Total X Distance - On LR3, this is from left bearing to Y rail)

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

(MSG Home All)
$H

(MSG Move to Initial Y Position)
G00 Y#<probeYPos> F#<jogXYFeedrate>
(MSG Move to Initial X Position)
G00 X#<probeXLeftPos> F#<jogXYFeedrate>

M0 (MSG Attach Probe and Probe Left)
#<probeSum> = 0
o100 repeat [#<probeCount>]
  G38.2 Z#<probeDistance> F#<probeFeedrate>
  #<probeSum> = [#<probeSum> + #5063]
  G00 Z10 F#<jogZFeedrate>
  D#5063 (Z Result of G38 Probe)
o100 endrepeat
#<probeLeftAvg> = [#<probeSum>/3]

M0 (MSG Detach Probe Before Move Up and Right)
G00 Z#<jogHeight> F#<jogZFeedrate>
G00 X#<probeXRightPos> F#<jogXYFeedrate>

M0 (MSG Re-attach Probe and Probe Right)

(MSG Probe Right)
#<probeSum> = 0
o101 repeat [#<probeCount>]
  G38.2 Z#<probeDistance> F#<probeFeedrate>
  #<probeSum> = [#<probeSum> + #5063]
  G00 Z10 F#<jogZFeedrate>
  D#5063 (Z Result of G38 Probe)
o101 endrepeat
#<probeRightAvg> = [#<probeSum>/3]

(MSG Average Left Z)
D#<probeLeftAvg>
(MSG Average Right Z)
D#<probeRightAvg>

#<zDifference> = [ABS[ABS[#<probeLeftAvg>] - ABS[#<probeRightAvg>]]]
#<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 [#<probeLeftAvg> GT #<probeRightAvg>]
  #<z1PulloffNew> = [#<z1PulloffNew> + #<zOffsetChange>]
o102 elseif [#<probeRightAvg> GT #<probeLeftAvg>]
  #<z0PulloffNew> = [#<z0PulloffNew> + #<zOffsetChange>]
o102 endif

(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

(MSG Old Pulloff Z0)
D#<z0Pulloff>
(MSG New Pulloff Z0)
D#<z0PulloffNew>

(MSG Old Pulloff Z1)
D#<z1Pulloff>
(MSG New Pulloff Z1)
D#<z1PulloffNew>

M0 (MSG Continue to Update Pulloff Values)

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

(MSG Save Pulloff Values)
$CD=config.yaml
11 Likes