Automatic Bed Leveling with Conditional Gcode Iterations



  • This is a long and winding thread on my attempt to figure out the best way to construct beg.g for my independent controlled Z axis printer. Bottom line up front, two efficient ways were created to do such.

    ; bed.g
    ; called to perform automatic bed compensation via G32
    ; 4 Passes performed. Reccomend 2 passes as minimum.
     
    M561                                             ; clear any bed transform
    G28                                              ; home
     
    while iterations <= 3                            ; perform 4 passes
        G30 P0 X25 Y100 Z-99999                      ; probe near a leadscrew, half way along Y axis
        G30 P1 X235 Y100 Z-99999 S2                  ; probe near a leadscrew and calibrate 2 motors
        G90                                          ; back to absolute mode
     
    G1 H2 Z8 F2600                                   ; raise head 8mm to ensure it is above the Z probe trigger height
    G1 X104 Y100 F6000                               ; put head over the centre of the bed, or wherever you want to probe
    G30  
    

    and

    ; bed.g
    ; Called to perform automatic bed compensation via G32
    ; 2 passes performed, and then 5 additional passes until set deviation is obtained - else failure message and beep
     
    M561                                                      ; Clear any bed transform
    G28                                                       ; Home
     
    while iterations <=1                                      ; Do minimum of 2 passes
       G30 P0 X25 Y100 Z-99999                                ; Probe near a leadscrew, half way along Y axis
       G30 P1 X235 Y100 Z-99999 S2                            ; Probe near a leadscrew and calibrate 2 motors
       G90
    
    while move.calibration.initial.deviation >= 0.002         ; perform additional tramming if previous deviation was over 0.002mm 
       if iterations = 5                                      ; Perform 5 checks
          M300 S3000 P500                                     ; Sound alert, required deviation could not be achieved
          abort "!!! ABORTED !!! Failed to achieve < 0.002 deviation within 5 movements. Current deviation is " ^ move.calibration.initial.deviation ^ "mm."
       G30 P0 X25 Y100 Z-99999                                ; Probe near a leadscrew, half way along Y axis
       G30 P1 X235 Y100 Z-99999 S2                            ; Probe near a leadscrew and calibrate 2 motors
       G90                                                    ; Back to absolute mode
    
    echo "Gantry deviation of " ^ move.calibration.initial.deviation ^ "mm obtained."
    G1 H2 Z8 F2600                                            ; Raise head 8mm to ensure it is above the Z probe trigger height
    G1 X104 Y100 F6000                                        ; Put head over the centre of the bed, or wherever you want to probe
    G30 
    

    -Original post follows:
    I was looking into the M30 command & bed.g file, in regards to leveling the gantry, and had a question. I usually find I have to run the below (G30 set twice as shown) to get to a low disparity, such as:

    G30 P0 X25 Y100 Z-99999                          ; probe near a leadscrew, half way along Y axis
    G30 P1 X235 Y100 Z-99999 S2                      ; probe near a leadscrew and calibrate 2 motors
    G90
    G30 P0 X25 Y100 Z-99999                          ; probe near a leadscrew, half way along Y axis
    G30 P1 X235 Y100 Z-99999 S2                      ; probe near a leadscrew and calibrate 2 motors
    G90                                              ; back to absolute mode
    

    This works fine but could the same, or possible more accurate, be accomplished like this?

    G30 P0 X25 Y100 Z-99999                          ; probe near a leadscrew, half way along Y axis
    G30 P1 X235 Y100 Z-99999                         ; probe near a leadscrew and calibrate 2 motors
    G30 P2 X25 Y100 Z-99999                          ; probe near a leadscrew, half way along Y axis
    G30 P3 X235 Y100 Z-99999 S4                      ; probe near a leadscrew and calibrate 2 motors
    G90                                              ; back to absolute mode
    

    Cheers,
    Kolbi


  • Moderator

    If that did work, I don't think it would be more accurate. Plus I don't think that would work as expected.

    Doubling up the passes in bed.g isn't uncommon. It will really depend on how out of true the tilt is and how close to the lead screws you can probe.



  • Thanks @Phaedrux, was just wondering if there was a better way to skin the cat.


  • Moderator

    I'd probably do it 3 times for good measure and then at the end probe the center of the bed with G30 to re-establish Z0. But that's just me.



  • @Phaedrux hehe, this is my bed.g:

    ; bed.g
    ; called to perform automatic bed compensation via G32
    ; 4 Passes performed. Reccomend 2 passes as minimum.
    
    M561                                             ; clear any bed transform
    G28 W                                            ; home
    
    G30 P0 X25 Y100 Z-99999                          ; probe near a leadscrew, half way along Y axis
    G30 P1 X235 Y100 Z-99999 S2                      ; probe near a leadscrew and calibrate 2 motors
    G90                                              ; back to absolute mode
    
    G30 P0 X25 Y100 Z-99999                          ; probe near a leadscrew, half way along Y axis
    G30 P1 X235 Y100 Z-99999 S2                      ; probe near a leadscrew and calibrate 2 motors
    G90                                              ; back to absolute mode
    
    G30 P0 X25 Y100 Z-99999                          ; probe near a leadscrew, half way along Y axis
    G30 P1 X235 Y100 Z-99999 S2                      ; probe near a leadscrew and calibrate 2 motors
    G90                                              ; back to absolute mode
    
    G30 P0 X25 Y100 Z-99999                          ; probe near a leadscrew, half way along Y axis
    G30 P1 X235 Y100 Z-99999 S2                      ; probe near a leadscrew and calibrate 2 motors
    G1 H2 Z8 F2600                                   ; raise head 4mm to ensure it is above the Z probe trigger height
    G90                                              ; back to absolute mode
    
    G1 X104 Y100 F6000                               ; put head over the centre of the bed, or wherever you want to probe
    G30                                              ; lower head, stop when probe triggered and set Z to trigger height
    


  • @Phaedrux A cleaner way to do it:

    ; bed.g
    ; called to perform automatic bed compensation via G32
    ; 4 Passes performed. Reccomend 2 passes as minimum.
    
    M561                                             ; clear any bed transform
    G28                                              ; home
    
    while iterations <= 3                            ; perform 4 passes
        G30 P0 X25 Y100 Z-99999                      ; probe near a leadscrew, half way along Y axis
        G30 P1 X235 Y100 Z-99999 S2                  ; probe near a leadscrew and calibrate 2 motors
        G90                                          ; back to absolute mode
    
    G1 H2 Z8 F2600                                   ; raise head 8mm to ensure it is above the Z probe trigger height
    G1 X104 Y100 F6000                               ; put head over the centre of the bed, or wherever you want to probe
    G30  
    

  • Moderator

    Now you're cooking with gas!



  • @Kolbi

    Since you're delving into conditional gcode you could look at something like this which only continues probing if necessary. Apologize for the length of this post but I included all sub macros for completeness. homexy.g also uses a virtual tool to indicate homing is in progress to avoid unnecessary Z axis movement. In case you're wondering, the sub macros are used to reduce duplicate code and unnecessarily updating several macros.

    if !move.axes[0].homed || !move.axes[1].homed	; If the printer hasn't been homed, home it
     M98 P"0:/sys/homeyx.g"	; home y and x
    ;
    M98 P"0:/sys/homez.g"			; always home z
    ;
    M561					; clear any bed transform
    M558 P9 H5 F120 T24000			; increase dive height incase bed is severely out of alignment
    M98 P"bed_threeScrews.g"		; perform bed tramming
    echo "Bed Tramming Cycle: 1 - Difference was " ^ move.calibration.initial.deviation ^ "mm"
    ;
    M98 P"0:/macros/Utilities/Config/bltouch_config"	; Apply default BLTouch config
    ;
    while move.calibration.initial.deviation >= 0.01	; perform additional tramming if previous deviation was over 0.01mm
     if iterations = 5
      abort "Too many auto tramming attempts"
     M98 P"bed_threeScrews.g"				; perform bed tramming
     echo "Bed Tramming Cycle: " ^ iterations + 2 ^ " - Difference was " ^ move.calibration.initial.deviation ^ "mm"
     continue
    ;
    G28 Z							; home z
    

    bltouch_config

    M558 P9 C"^io7.in" H2 F120 T24000 	; set Z probe type to bltouch and the dive height + speeds
    ;
    M98 P"0:/macros/Utilities/Config/bltouch_offset_config"
    

    bltouch_offset_config

    ;
    ; G31 - +Z Nozzle closer to bed, -Z Nozzle further from bed
    ;
    G31 X0 Y-20.23 Z2.1 P500		; 200710 mosquito liquid
    

    bed_threeScrews.g

    G30 P0 X10 Y18 Z-99999			; probe near a front left leadscrew
    G30 P1 X10 Y300 Z-99999			; probe near a back left leadscrew
    G30 P2 X320 Y166 Z-99999 S3		; probe near a right leadscrew and level
    

    homexy.g

    ; homeyx.g
    ; called to home x and y axes
    ;
    ; Warning: do not home individual axes by calling G28 X or G28 Y. It will cause errors. Instead run the macro file directly. IE M98 P"0:/sys/homex.g"
    ;
    M563 P9 S"HomeAll"		; create tool 9 to indicate homeall is in progress
    ;
    G91              		; relative positioning
    G1 Z5 F6000 H2   		; lift Z relative to current position
    M98 P"0:/sys/homey.g"		; home y
    M98 P"0:/sys/homex.g"		; home x
    G90             		; absolute positioning
    ;
    M563 P9 D-1 H-1			; delete tool 9 to indicate homeall is done
    

    homex.g

    if #tools != 10	; not homeall?
      G91              ; relative positioning
      G1 Z5 F6000 H2   ; lift Z relative to current position
    ;
    G1 H1 X357 F6000 ; move quickly to X axis endstop and stop there (first pass)
    G1 X-5 F3600     ; go back a few mm
    G1 H1 X357 F360  ; move slowly to X axis endstop once more (second pass)
    ;
    if #tools != 10	; not homeall?
      G1 Z-5 F6000 H2  ; lower Z again
      G90              ; absolute positioning
    

    homez.g

    if #tools != 10	; not homeall?
      G91			; relative positioning
      G1 Z5 F6000 H2	; lift Z relative to current position
      G90			; absolute positioning
    ;
    ; Home Z - inline with autolevel axis
    G1 X165 Y193 F24000	; go to middle of the bed probe point and home Z. BLTouch on right
    M558 F1200		; Set probing at faster feed rate
    G30			; quickly home Z by probing the bed
    M400			; clear buffer
    M98 P"0:/macros/Utilities/Config/bltouch_config"	; Return bltouch to defaults
    G30			; slowly home Z by probing the bed
    M400			; clear buffer
    

  • Moderator

    @mwolter Beautiful.



  • @mwolter Nice, thanks for that.

    Being away from my printer at the moment, but I would guess the following would be a correct implementation for my dual Z setup:
    !!! Please see my final code, this is not correct !!!

    ; bed.g
    M561                                                    ; clear any bed transform
    G28                                                     ; home
    while move.calibration.initial.deviation >= 0.01	; perform additional tramming if previous deviation was over 0.01mm
    
     if iterations = 5                                      ; perform 5 checks
      abort "!!!Failed to achieve < 0.01 deviation within 5 movements. Current deviation is " ^ move.calibration.initial.deviation ^ "mm."
    
        G30 P0 X25 Y100 Z-99999                             ; probe near a leadscrew, half way along Y axis
        G30 P1 X235 Y100 Z-99999 S2                         ; probe near a leadscrew and calibrate 2 motors
        G90                                                 ; back to absolute mode
    
     echo "Bed Tramming Cycle: " ^ iterations + 2 ^ " - Difference was " ^ move.calibration.initial.deviation ^ "mm"
     continue
    
    ;
    


  • @mwolter said in Automatic Bed Leveling with Conditional Gcode Iterations:

    G1 Z{move.axes[2].userPosition+20} F1200

    Hi,

    Why that command as opposed to G91 G1 Z20?

    And why did you implement homing such that you could not safely use G28 X or G28 Y?

    It would not be hard to structure it so you could.

    Thanks.

    Frederick



  • @fcwilt Using that command I can be sure it raised 20mm from the previous height. Probably overkill 🙂 It was a quick add before posting this and thought it would help with changing the magnetic bed but it makes for an odd extra movement of the bed between performing the homez and bed leveling. I'll rework the macros to utilize the virtual homeall tool to skip the unnecessary bed movement and update the macros above.



  • @Kolbi There are too many spaces before the G30 commands and it would cause issues. Would recommend doing one leveling pass before the while command. I haven't tested the following code but it should be close.

    M561                                         ; clear any bed transform
    G28                                          ; home
    G30 P0 X25 Y100 Z-99999                      ; probe near a leadscrew, half way along Y axis
    G30 P1 X235 Y100 Z-99999 S2                  ; probe near a leadscrew and calibrate 2 motors
    echo "Bed Tramming Cycle: 1 - Difference was " ^ move.calibration.initial.deviation ^ "mm"
    
    while move.calibration.initial.deviation >= 0.01	; perform additional tramming if previous deviation was over 0.01mm
     if iterations = 5
      abort "Too many auto tramming attempts"
     G30 P0 X25 Y100 Z-99999                      ; probe near a leadscrew, half way along Y axis
     G30 P1 X235 Y100 Z-99999 S2                  ; probe near a leadscrew and calibrate 2 motors
     echo "Bed Tramming Cycle: " ^ iterations + 2 ^ " - Difference was " ^ move.calibration.initial.deviation ^ "mm"
     continue
    G28 Z					; home z
    


  • @mwolter Thanks for covering my 6, I just caught that and was typing this:
    !!! Please see my final code, this is not correct !!!

    ; bed.g
    ; Called to perform automatic bed compensation via G32
    M561                                                    ; clear any bed transform
    G28                                                     ; home
    
    G30 P0 X25 Y100 Z-99999                                 ; probe near a leadscrew, half way along Y axis
    G30 P1 X235 Y100 Z-99999 S2                             ; probe near a leadscrew and calibrate 2 motors
    
    while move.calibration.initial.deviation >= 0.01	; perform additional tramming if previous deviation was over 0.01mm
     
     if iterations = 5                                      ; perform 5 checks
      abort "!!!Failed to achieve < 0.01 deviation within 5 movements. Current deviation is " ^ move.calibration.initial.deviation ^ "mm."
     
     G30 P0 X25 Y100 Z-99999                                ; probe near a leadscrew, half way along Y axis
     G30 P1 X235 Y100 Z-99999 S2                            ; probe near a leadscrew and calibrate 2 motors
     G90                                                    ; back to absolute mode
     
     echo "Gantry deviation of " ^ move.calibration.initial.deviation ^ "mm obtained within " ^ iterations + 2 ^ " tramming cycles."
     continue
     
    ;
    


  • @mwolter I guess the trailing code is not a hard requirement? By trailing code I'm referring to this:

     continue
    
     
    ;
    


  • @Kolbi nope



  • Removed the following and added it to homeall.g since I typically do a home all before changing the bed.

    G1 Z{move.axes[2].userPosition+20} F1200		; move z up 20mm for magnetic bed replacement
    

    homeall.g

    ; homeall.g
    ; called to home all axes (G28)
    ;
    ; Warning: do not home individual axes by calling G28 X or G28 Y. It will cause errors. Instead run the macro file directly. IE M98 P"0:/sys/homex.g"
    ;
    M563 P9 S"HomeAll"		; create tool 9 to indicate homeall is in progress
    ;
    G91              		; relative positioning
    G1 Z5 F6000 H2   		; lift Z relative to current position
    M98 P"0:/sys/homey.g"	; home y
    M98 P"0:/sys/homex.g"	; home x
    G90             		; absolute positioning
    ;
    M98 P"0:/sys/homez.g"	; home z
    ;
    M563 P9 D-1 H-1			; delete tool 9 to indicate homeall is done
    ;
    G1 Z{move.axes[2].userPosition+50} F1200		; move z up 50mm for magnetic bed replacement
    


  • Thanks @mwolter!

    For brevity, here is my final working version:

    ; bed.g
    ; Called to perform automatic bed compensation via G32
    
    M561                                                      ; Clear any bed transform
    G28                                                       ; Home
    
    while iterations <=1                                      ; Do minimum of 2 passes
       G30 P0 X25 Y100 Z-99999                                ; Probe near a leadscrew, half way along Y axis
       G30 P1 X235 Y100 Z-99999 S2                            ; Probe near a leadscrew and calibrate 2 motors
       G90
    
    while move.calibration.initial.deviation >= 0.002         ; perform additional tramming if previous deviation was over 0.002mm 
       if iterations = 5                                      ; Perform 5 checks
          M300 S3000 P500                                     ; Sound alert, required deviation could not be achieved
          abort "!!! ABORTED !!! Failed to achieve < 0.002 deviation within 5 movements. Current deviation is " ^ move.calibration.initial.deviation ^ "mm."
       G30 P0 X25 Y100 Z-99999                                ; Probe near a leadscrew, half way along Y axis
       G30 P1 X235 Y100 Z-99999 S2                            ; Probe near a leadscrew and calibrate 2 motors
       G90                                                    ; Back to absolute mode
    
    echo "Gantry deviation of " ^ move.calibration.initial.deviation ^ "mm obtained."
    G1 H2 Z8 F2600                                            ; Raise head 8mm to ensure it is above the Z probe trigger height
    G1 X104 Y100 F6000                                        ; Put head over the centre of the bed, or wherever you want to probe
    G30 
    


  • @mwolter said in Automatic Bed Leveling with Conditional Gcode Iterations:

    @fcwilt Using that command I can be sure it raised 20mm from the previous height.

    Hi,

    Thanks for the reply.

    Why do you think G91 G1 Z20 doesn't do that same thing?

    Frederick



  • @fcwilt
    It’s just a different way of doing it and like I mentioned, this way I can be sure it’s raised 20mm from its current position. The G30 command actually sets Z to about 3m. So G1 Z20 will only raise Z by 17mm.



  • @Phaedrux said in Automatic Bed Leveling with Conditional Gcode Iterations:

    Now you're cooking with gas!

    Definitely cooking with gas now!



  • @mwolter said in Automatic Bed Leveling with Conditional Gcode Iterations:
    The G30 command actually sets Z to about 3m. So G1 Z20 will only raise Z by 17mm.

    I think you will find that a G91 G1 Z20 will raise Z by 20 unless it doesn't have that much room left on the axis.

    If a G30 sets Z to 3 then a G91 G1 Z20 will take Z to 23.

    At least that is my experience.

    Good luck.

    Frederick



  • This post is deleted!

  • Moderator

    @fcwilt said in Automatic Bed Leveling with Conditional Gcode Iterations:

    I think you will find that a G91 G1 Z20 will raise Z by 20 unless it doesn't have that much room left of the axis.

    Correct. G90 moves to an absolute point, G91 is relative to the current position.

    https://duet3d.dozuki.com/Wiki/Gcode#Section_G90_Set_to_Absolute_Positioning



  • @Phaedrux If the command G90 G1 Z20 is used in one line, does G91 have to be used to return to absolute mode?


Log in to reply