Custom G-Code for Z-Axis Screw Lubrication

I need someone to check my homework, please. I’m attempting to write a custom set of instructions for the purpose of lubricating the z-axis screws. My thought was that I’d run a custom code to find z0 and run the bed up and down a few times pausing in between passes. Here’s what I came up with. I’m terrified of running the bed into the nozzle and was hoping to see if others with a better understanding of the code can help to see if there are any issues.

;======== X2D Z-axis Lead Screw Maintenance ========
;===== 2026/05/09 =====
;===== 3 full cycles with pause between each pass ===

;===== step 1: reset machine state =================
M400 ; wait for buffer to clear
M17 D ; reset all motor currents to default
M220 S100 ; reset feedrate override to 100%
M201.2 K1.0 ; reset acceleration magnitude
M1002 set_gcode_claim_speed_level : 0
M975 S1 ; turn on input shaping
M982.2 S1 ; turn on cog noise reduction
G29.2 S0 ; disable ABL to avoid interference
M211 X0 Y0 Z0 ; disable software endstops
G90 ; absolute positioning
M83 ; extruder relative mode (safety)
;===== step 1: reset machine state =================

;===== step 2: drop bed down safely ================
; Drop bed DOWN first regardless of starting position
; This creates clearance between bed and nozzle
; before any homing or upward movement occurs
G91 ; relative positioning
M17 S ; save motor state
M17 Z0.4 ; lower Z motor current for safe movement
G1 Z-50 F300 ; drop bed DOWN 50mm slowly
M400 ; wait for move to complete
M17 R ; restore motor current
G90 ; back to absolute positioning
;===== step 2: drop bed down safely ================

;===== step 3: avoid end stop ======================
; Bambu safe pre-home sequence - handles unknown
; Z position safely before homing begins
G91
G380 S2 Z22 F1200 ; back away from bottom endstop
G380 S2 Z-12 F1200 ; controlled return approach
G90
;===== step 3: avoid end stop ======================

;===== step 4: full homing sequence ================
; Must home X first, then center, then Z
; This is the exact sequence from X2D start gcode
M1002 gcode_claim_action : 13
G28 X T300 R ; home X first - R raises Z before moving
G150.1 F8000 ; move toolhead to wipe position
G150.3 ; move toolhead to safe park position
M972 S24 P0 ; pre-home hardware verification
G90
M83
G1 Y128 F30000 ; center Y axis
G1 X128 ; center X axis
G28 Z P0 T400 ; home Z - establishes Z=0 at endstop
M400 ; wait for homing to complete
;===== step 4: full homing sequence ================

;===== step 5: move to maintenance start position ==
M117 Starting maintenance routine…
G1 Z5 F300 ; move bed to bottom start position
M400
;===== step 5: move to maintenance start position ==

;===== step 6: maintenance passes ==================

; — Pass 1 Down —
M117 Pass 1/3 - Moving DOWN
M17 S
M17 Z0.4
G1 Z5 F300
M400
M17 R
M0 ; PAUSE - press resume to continue

; — Pass 1 Up —
M117 Pass 1/3 - Moving UP
M17 S
M17 Z0.4
G91 ; relative positioning for safe upper travel
G1 Z240 F300 ; move UP 240mm from Z5 = ~Z245 safely below ceiling
G90
M400
M17 R
M0 ; PAUSE - press resume to continue

; — Pass 2 Down —
M117 Pass 2/3 - Moving DOWN
M17 S
M17 Z0.4
G1 Z5 F300
M400
M17 R
M0 ; PAUSE - press resume to continue

; — Pass 2 Up —
M117 Pass 2/3 - Moving UP
M17 S
M17 Z0.4
G91
G1 Z240 F300
G90
M400
M17 R
M0 ; PAUSE - press resume to continue

; — Pass 3 Down —
M117 Pass 3/3 - Moving DOWN
M17 S
M17 Z0.4
G1 Z5 F300
M400
M17 R
M0 ; PAUSE - press resume to continue

; — Pass 3 Up —
M117 Pass 3/3 - Moving UP
M17 S
M17 Z0.4
G91
G1 Z240 F300
G90
M400
M17 R
; No pause after final pass - proceed to park

;===== step 6: maintenance passes ==================

;===== step 7: park at working height ==============
M117 Maintenance complete - parking…
M17 S ; save motor state
M17 Z0.4 ; lower Z motor current for safe descent
G1 Z120 F600 ; park at comfortable working height
G1 Z118 ; small settle move - mirrors end gcode pattern
M400 P100 ; short dwell to let movement settle
M17 R ; restore Z motor current
;===== step 7: park at working height ==============

;===== step 8: restore machine state ===============
G29.2 S1 ; re-enable ABL
M211 X1 Y1 Z1 ; re-enable software endstops
M220 S100 ; confirm feedrate at 100%
M201.2 K1.0 ; confirm acceleration at default
M73.2 R1.0 ; reset time magnitude display
M1002 set_gcode_claim_speed_level : 0
M1015.3 S0 ; disable clog detect
M1015.4 S0 K0 ; disable air printing detect
;===== step 8: restore machine state ===============

;===== step 9: finish sound ========================
M17
M400 S1
M1006 S1
M1006 A53 B10 L50 C53 D10 M50 E53 F10 N50
M1006 A57 B10 L50 C57 D10 M50 E57 F10 N50
M1006 A0 B15 L0 C0 D15 M0 E0 F15 N0
M1006 A53 B10 L50 C53 D10 M50 E53 F10 N50
M1006 A57 B10 L50 C57 D10 M50 E57 F10 N50
M1006 A0 B15 L0 C0 D15 M0 E0 F15 N0
M1006 A48 B10 L50 C48 D10 M50 E48 F10 N50
M1006 A0 B15 L0 C0 D15 M0 E0 F15 N0
M1006 A60 B10 L50 C60 D10 M50 E60 F10 N50
M1006 W
;===== step 9: finish sound ========================

M400
M18 ; disable all motors
;======== end maintenance ========

1 Like

You could open the g code in Bambu studio and in preview turn on “show toolhead moves” (I don’t recall the exact name in the app, sorry) and see how the code works. If it shows you going close to or below the bed surface, that’s a problem.

I assume you’re talking about showing the travel path? I might be doing it wrong, but I don’t think studio shows the paths prior to the prints. I’d love to be wrong about that, thanks for the suggestion though.

I did not check all of your gcode but I see 2 issues already: I don’t think M0 is implemented in Bambu firmware and using relative positioning is prone to errors (you commented: “G1 Z-50 F300 ; drop bed DOWN 50mm slowly” but a negative Z value brings the bed up not down).

I would stay with absolute positioning and after a home command to set Z at 0 (and X,Y at center) go from there. I would leave clearance on all axis (Y and Z from 10 minimum to 240 maximum and X from 30 minimum to 220 maximum to avoid issue whether current nozzle is left or right)

If M0 is now implemented, fine (is it new or you just guessing it exists?) If not implemented, you will have to set a fixed pause duration with M400 Ssomething) but why make the whole gcode this complicated.

You can leave the motors on while the gcode pauses, you really don’t need those M17 commands. I would just start with a home and then absolute positioning on each step you want Z to fall on (add a G1 Xsomething, Ysomething before to where you want your head so it does not interfere with your maintenance), end with a M18 to disable motors so you can park manually (I prefer this than to input purge or wipe commands to avoid messing up :))

Before the end, I would suggest you do 3 passes with rather high F speed from bottom to top to bottom and top again (from Z10 to Z240) to spread your lubricating oil.

I would just do:

G91; turn on relative positioning
G1 Z15 F1200; lower bed 15mm from current position
G28; Home
G90; Absolute positioning
M83; Put E ==extruder in relative mode==
G29.2 S0 ; turn OFF pos comp

G1 F300; set speed for all moves
G1 Z40; to clear the bed a good bit before moving the head
G1 Xsomething Ysomething; put the head somewhere

G1 Zxx: to whatever position bed down from home for start of lubrication
M400 Ssomething; pause for some seconds (or M0 if now implemented)

G1 Zyy; next location
M400 Ssomething

etc…

; ==optionally spread oil==

G1 F9600

G1 Z10

G1 Z240

G1 Z10

G1 Z240

G1 Z10

G1 Z240

G1 Z10

; ==end==
M400
M18

If you write your code to look like preprint moves then yes, the preprint moves don’t show up.

Write your gcode to look like the main body of a print gcode instead.

Thanks for the info! Great help.

1 Like