Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Automatic Bed Leveling with Conditional Gcode Iterations

    Scheduled Pinned Locked Moved
    Gcode meta commands
    5
    79
    6.5k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mwolterundefined
      mwolter
      last edited by mwolter

      @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
      
      Phaedruxundefined Kolbiundefined 2 Replies Last reply Reply Quote 4
      • Phaedruxundefined
        Phaedrux Moderator @mwolter
        last edited by

        @mwolter Beautiful.

        Z-Bot CoreXY Build | Thingiverse Profile

        1 Reply Last reply Reply Quote 0
        • Kolbiundefined
          Kolbi @mwolter
          last edited by Kolbi

          @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
          
          ;
          
          mwolterundefined 1 Reply Last reply Reply Quote 0
          • fcwiltundefined
            fcwilt
            last edited by fcwilt

            @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

            Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

            mwolterundefined 1 Reply Last reply Reply Quote 1
            • mwolterundefined
              mwolter @fcwilt
              last edited by mwolter

              @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.

              fcwiltundefined 1 Reply Last reply Reply Quote 0
              • mwolterundefined
                mwolter @Kolbi
                last edited by

                @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
                
                Kolbiundefined 1 Reply Last reply Reply Quote 0
                • Kolbiundefined
                  Kolbi @mwolter
                  last edited by Kolbi

                  @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
                   
                  ;
                  
                  1 Reply Last reply Reply Quote 0
                  • Kolbiundefined
                    Kolbi
                    last edited by

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

                     continue
                    
                     
                    ;
                    
                    mwolterundefined 1 Reply Last reply Reply Quote 0
                    • mwolterundefined
                      mwolter @Kolbi
                      last edited by

                      @Kolbi nope

                      1 Reply Last reply Reply Quote 0
                      • mwolterundefined
                        mwolter
                        last edited by mwolter

                        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
                        
                        1 Reply Last reply Reply Quote 0
                        • Kolbiundefined
                          Kolbi
                          last edited by Kolbi

                          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 
                          
                          1 Reply Last reply Reply Quote 1
                          • fcwiltundefined
                            fcwilt @mwolter
                            last edited by

                            @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

                            Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

                            mwolterundefined 1 Reply Last reply Reply Quote 0
                            • mwolterundefined
                              mwolter @fcwilt
                              last edited by

                              @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.

                              fcwiltundefined 1 Reply Last reply Reply Quote 0
                              • Kolbiundefined
                                Kolbi @Phaedrux
                                last edited by

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

                                Now you're cooking with gas!

                                Definitely cooking with gas now!

                                1 Reply Last reply Reply Quote 0
                                • fcwiltundefined
                                  fcwilt @mwolter
                                  last edited by fcwilt

                                  @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

                                  Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

                                  mwolterundefined Phaedruxundefined 2 Replies Last reply Reply Quote 1
                                  • mwolterundefined
                                    mwolter @fcwilt
                                    last edited by

                                    This post is deleted!
                                    1 Reply Last reply Reply Quote 0
                                    • Phaedruxundefined
                                      Phaedrux Moderator @fcwilt
                                      last edited by

                                      @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

                                      Z-Bot CoreXY Build | Thingiverse Profile

                                      mwolterundefined 1 Reply Last reply Reply Quote 1
                                      • mwolterundefined
                                        mwolter @Phaedrux
                                        last edited by

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

                                        Phaedruxundefined A Former User? fcwiltundefined 3 Replies Last reply Reply Quote 1
                                        • Phaedruxundefined
                                          Phaedrux Moderator @mwolter
                                          last edited by

                                          @mwolter That's a good question, and I'm not sure off the top of my head. Would have to test to see if it sticks in that format or not.

                                          Z-Bot CoreXY Build | Thingiverse Profile

                                          1 Reply Last reply Reply Quote 0
                                          • Kolbiundefined
                                            Kolbi
                                            last edited by

                                            Another thing I noticed is repeatability with PindaV2 probe is not as consistent as BLTouchV3.1... So being that I use a bondtech bmg mosquito, I've made this modified version to scrap the Pinda and welcome the BLTouch. Will get it sorted on the printer and test it fully this weekend.
                                            BMGm-BLTouch.JPG BMGm-BLTouch-Actual.jpg

                                            mwolterundefined 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Unless otherwise noted, all forum content is licensed under CC-BY-SA