# Q1_Pro
# V4.4.16 2024-3-14
# update: variable change M106 & M141 & M107
# V4.4.16 2024-3-26
# update: variable change [homing_override]->add M204 S10000
# V4.4.20 2024-6-17
# update: Z_DOUDONG CLEAR_NOZZLE get_zoffset rewrite
#         move_subzoffset added 
# V4.4.21 2024-6-22
# update: Enable chamber exhaust fan by default when chamber temp not set

[gcode_macro test_zoffset]
gcode:
    G28
    get_zoffset
    G1 X{120 - printer.probe["x_offset"]} Y{120 - printer.probe["y_offset"]} F9000
    G1 Z10 F600
    probe
    M400
    G4 P5000
    G1 Z10 F600

[gcode_macro move_subzoffset]
gcode:
     
    G1 Z{printer.probe.last_z_result} F600

[gcode_macro get_zoffset]
gcode:
    G1 Z10 F600
    Z_DOUDONG
    ;G4 P500
    QIDI_PROBE_PIN_1
    M204 S30
    G4 P500
    probe probe_speed=5 lift_speed=5 samples=5 sample_retract_dist=5
    move_subzoffset
    M114
    SET_KINEMATIC_POSITION Z=-0.07
    G1 Z30 F600
    QIDI_PROBE_PIN_2
    M204 S10000

[gcode_macro save_zoffset]
gcode:
    {% if printer.gcode_move.homing_origin.z < 0.5 %}
       SAVE_VARIABLE VARIABLE=z_offset VALUE={printer.gcode_move.homing_origin.z}
    {% endif %}

[gcode_macro set_zoffset]        
gcode:
    {% set z = printer.save_variables.variables.z_offset %}
    SET_GCODE_OFFSET Z={z} MOVE=0

[gcode_macro CLEAR_NOZZLE_PLR]
gcode:
    {% set hotendtemp = params.HOTEND|default(250)|int %}
    {% if (printer.gcode_move.position.z ) < 35 %}
        G90
        G1 Z35 F900
    {% else %}
        G91
        G1 Z5 F900 
        G90
    {% endif %}
    G1 X97 F15000
    G1 Y243 F15000
    G1 Y254 F800

    M109 S{hotendtemp}

    G92 E0
    G1 E80 F400
    M106 S255

    G1 X97 F500
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 Y240 F800

[gcode_macro CLEAR_NOZZLE]
gcode:
    {% set hotendtemp = params.HOTEND|int %}
    {% if (printer.gcode_move.position.z ) < 35 %}
        G1 Z35 F900
    {% else %}
        G91
        G1 Z5 F900 
        G90
    {% endif %}
    G1 X97 F15000
    G1 Y243 F15000
    G1 Y254 F800

    M109 S{hotendtemp}

    G92 E0
    G1 E80 F400
    M106 S255
    M400
    M106 P2 S0
    M104 S0
    TEMPERATURE_WAIT SENSOR=extruder MAXIMUM={hotendtemp-20}

    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000

    G1 X65 F500
    G1 X88 F500
    G1 X65 F500
    G1 X88 F500
    G1 X65 F500
    G1 X88 F500
    G1 X65 F500
    G1 X88 F500
    G1 X65 F500 
    G1 X97 F500
    
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    G1 X85 F2000
    G1 X97 F2000
    
    M400
    M118 Nozzle cleared
    G1 Y240 F800
    G1 Y120 F15000
    G1 X230 F15000
    TEMPERATURE_WAIT SENSOR=extruder MAXIMUM=140
    M107
    M106 P2 S0
    M400
    M118 Nozzle cooled

[gcode_macro PRINT_START]
gcode:
    AUTOTUNE_SHAPERS

    {% set bedtemp = params.BED|int %}
    {% set hotendtemp = params.HOTEND|int %}
    {% set chambertemp = params.CHAMBER|default(0)|int %}
    set_zoffset
    {% if chambertemp == 0 %}
        M106 P3 S255
    {% endif %}
    M104 S0
    M140 S{bedtemp}    
    M141 S{chambertemp}
    G28    
    CLEAR_NOZZLE HOTEND={hotendtemp}

    M190 S{bedtemp}   
#    Z_TILT_ADJUST
    G29
    G0 Z50 F600
    G0 X0 Y0  F6000
    
    M191 S{chambertemp}
    M109 S{hotendtemp}
    M204 S10000

    ENABLE_ALL_SENSOR
    save_last_file

[gcode_macro ENABLE_ALL_SENSOR]
gcode:
    ENABLE_FILAMENT_WIDTH_SENSOR
    RESET_FILAMENT_WIDTH_SENSOR
    query_filament_width
    SET_FILAMENT_SENSOR SENSOR=fila ENABLE=1

[gcode_macro DISABLE_ALL_SENSOR]
gcode:
    SET_FILAMENT_SENSOR SENSOR=fila ENABLE=0
    DISABLE_FILAMENT_WIDTH_SENSOR

[gcode_macro AUTOTUNE_SHAPERS]
variable_autotune_shapers: 'ei'
gcode:

[gcode_macro M84]
rename_existing:M84.1
gcode:
    M84.1
	SET_STEPPER_ENABLE STEPPER=stepper_x enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_y enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_z enable=1
    SET_STEPPER_ENABLE STEPPER=stepper_z1 enable=1
	SET_STEPPER_ENABLE STEPPER=extruder enable=0

[gcode_macro DETECT_INTERRUPTION]
gcode:
    {% set was_interrupted = printer.save_variables.variables.was_interrupted %}
    {% if was_interrupted %}
    M118 Detected unexpected interruption during the last print. Do you want to resume printing? (Do not move the extruder before resuming.)
    M118 Yes: RESUME_INTERRUPTED
    M118 No: CLEAR_LAST_FILE
    {% endif %}
    
[delayed_gcode PRINTER_INIT]
initial_duration:0.2
gcode:
	SET_STEPPER_ENABLE STEPPER=stepper_z enable=1
    SET_STEPPER_ENABLE STEPPER=stepper_z1 enable=1
    BED_MESH_CLEAR      
    SET_FILAMENT_SENSOR SENSOR=fila ENABLE=0

[homing_override]
axes:xyz
gcode: 
    {% set HOME_CUR = 1 %}
    {% set driver_config = printer.configfile.settings['tmc2240 stepper_x'] %}
    {% set RUN_CUR = driver_config.run_current %}
    {% set HOLD_CUR = driver_config.hold_current %}
    M204 S10000
    M220 S100
	{% if params.X is defined %}
	    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR * 0.7} 
        G28 X
		SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR}     
        BEEP I=1 DUR=100       
        G1 X10 F1200
    {% endif %}

    {% if params.Y is defined %}
		SET_TMC_CURRENT STEPPER=stepper_y CURRENT={HOME_CUR * 0.9} 
		G28 Y
		SET_TMC_CURRENT STEPPER=stepper_y CURRENT={HOME_CUR}  
        BEEP I=1 DUR=100          
       G1 Y10 F1200
    {% endif %}

    {% if params.Z is defined %}
        SET_KINEMATIC_POSITION Z={printer.toolhead.axis_maximum.z-5}
        QIDI_PROBE_PIN_2
        probe samples=2
        SET_KINEMATIC_POSITION Z=1.9
        G1 Z10 F600
        Z_DOUDONG
        QIDI_PROBE_PIN_1
        probe probe_speed=10
        SET_KINEMATIC_POSITION Z=-0.1
        G1 Z30 F480
    {% endif %}

    {% if params.X is undefined %}
    {% if params.Y is undefined %}
    {% if params.Z is undefined %}
        SET_KINEMATIC_POSITION X=0
        SET_KINEMATIC_POSITION Y=0
        SET_KINEMATIC_POSITION Z={printer.toolhead.axis_maximum.z-10}
        G91
        G1 Z7 F600	
        G1 X10 F2400
        G1 Y10 F2400
        G4 P2000
    
       SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR * 0.8} 
        G28 X
    	SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR} 
        BEEP I=1 DUR=100  
        G1 X10 F1200
    
    	SET_TMC_CURRENT STEPPER=stepper_y CURRENT={HOME_CUR * 0.9} 
    	G28 Y
    	SET_TMC_CURRENT STEPPER=stepper_y CURRENT={HOME_CUR} 
        BEEP I=1 DUR=100        
        G1 Y10 F1200

        SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR * 0.8} 
        G28 X
    	SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR} 
        BEEP I=1 DUR=100  
        G1 X10 F1200

        SET_KINEMATIC_POSITION Z={printer.toolhead.axis_maximum.z-10}

        G90
        G1 X120 Y120 F7800
        G91
        QIDI_PROBE_PIN_2
        G28 Z
        G1 Z30  F600
    {% endif %}
    {% endif %}
    {% endif %}
    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={RUN_CUR} 
    SET_TMC_CURRENT STEPPER=stepper_y CURRENT={RUN_CUR} 
    M204 S10000
    G90
    QIDI_PROBE_PIN_2

[gcode_macro SHAPER_CALIBRATE]
rename_existing: RESHAPER_CALIBRATE
gcode:
    RESHAPER_CALIBRATE FREQ_START=20 FREQ_END=150
    
[gcode_macro PRINT_END]
gcode:
    SET_IDLE_TIMEOUT TIMEOUT={printer.configfile.settings.idle_timeout.timeout}         ; set timeout back to configured value
    CLEAR_PAUSE
    save_zoffset
    M106 P2 S0
    M106 P0 S0
    M106 P3 S0
    
    M104 S0
    M140 S0
    M141 S0

    M220 S100
    M221 S100
    SET_STEPPER_ENABLE STEPPER=stepper_x enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_y enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_z enable=1
    SET_STEPPER_ENABLE STEPPER=stepper_z1 enable=1
	SET_STEPPER_ENABLE STEPPER=extruder enable=0
	
    DISABLE_ALL_SENSOR
    SET_GCODE_OFFSET Z=0 MOVE=0
    BED_MESH_CLEAR
    G31
    CLEAR_LAST_FILE
    BEEP I=2 DUR=500
       
[gcode_macro CANCEL_PRINT]
rename_existing: BASE_CANCEL_PRINT
gcode:
    {% if (printer.gcode_move.position.z) < 150 %}       ; check that zhop doesn't exceed z max
        G1 Z150 F480                                       
    {% endif %}

    G1  X0 Y0 F7800

    SET_IDLE_TIMEOUT TIMEOUT={printer.configfile.settings.idle_timeout.timeout}         ; set timeout back to configured value
    CLEAR_PAUSE
    save_zoffset
    M106 P2 S0
    M106 P0 S0
    M106 P3 S0
    
    M104 S0
    M140 S0
    M141 S0

    M220 S100
    M221 S100
    SET_STEPPER_ENABLE STEPPER=stepper_x enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_y enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_z enable=1
    SET_STEPPER_ENABLE STEPPER=stepper_z1 enable=1
	SET_STEPPER_ENABLE STEPPER=extruder enable=0
	
    DISABLE_ALL_SENSOR
    SET_GCODE_OFFSET Z=0 MOVE=0
    BED_MESH_CLEAR
    G31
    BEEP I=2 DUR=500

    SDCARD_RESET_FILE
    BASE_CANCEL_PRINT
    CLEAR_LAST_FILE

[gcode_macro PAUSE]
rename_existing: BASE_PAUSE
gcode:
    {% set z = params.Z|default(35)|int %}                                                   ; z hop amount
    
    {% if printer['pause_resume'].is_paused|int == 0 %}     
        SET_GCODE_VARIABLE MACRO=RESUME VARIABLE=zhop VALUE={z}                              ; set z hop variable for reference in resume macro
        SET_GCODE_VARIABLE MACRO=RESUME VARIABLE=etemp VALUE={printer['extruder'].target}    ; set hotend temp variable for reference in resume macro

        DISABLE_FILAMENT_WIDTH_SENSOR
        SET_FILAMENT_SENSOR SENSOR=fila ENABLE=0                                             ; disable filament sensor       
        SAVE_GCODE_STATE NAME=PAUSE                                                          ; save current print position for resume                
        BASE_PAUSE                                                                           ; pause print
        G92 E0
        G1 E-5 F1800
        {% if (printer.gcode_move.position.z ) < z %}
            G91                                                                              ; relative positioning
            G1 Z{z} F900                                                                     ; raise Z up by z hop amount
        {% else %}
            G91                                                                              ; relative positioning
            G1 Z{5} F900 
            SET_GCODE_VARIABLE MACRO=RESUME VARIABLE=zhop VALUE=0
        {% endif %}
        SAVE_GCODE_STATE NAME=PAUSEPARK2
        G90                                                                                  ; absolute positioning
        G1 X97 F9000
        G1 Y253 F9000                                     ; park toolhead at front center
        SAVE_GCODE_STATE NAME=PAUSEPARK                                                      ; save parked position in case toolhead is moved during the pause (otherwise the return zhop can error) 
        M104 S0                                                                              ; turn off hotend
        SET_IDLE_TIMEOUT TIMEOUT=86400                                                       ; set timeout to 24 hours
        SET_STEPPER_ENABLE STEPPER=extruder enable=0
    {% endif %}

[gcode_macro RESUME]
rename_existing: BASE_RESUME
variable_zhop: 0
variable_etemp: 0
gcode:
    {% set e = params.E|default(5)|int %}                                          ; hotend prime amount (in mm)
    
    {% if printer['pause_resume'].is_paused|int == 1 %}
        SET_IDLE_TIMEOUT TIMEOUT={printer.configfile.settings.idle_timeout.timeout}  ; set timeout back to configured value
        {% if etemp > 0 %}
            M109 S{etemp|int}                                                        ; wait for hotend to heat back up
        {% endif %}
        M83                                                                          ; relative extruder positioning
            G1 X97  F9000
            G1 Y253 F9000                            
            G91
            G1  E{e} F300                                                ; prime nozzle by E, lower Z back down
            G90
            G1 X85 F3000
            G1 X98 F3000
            G1 X85 F3000
            G1 X98 F3000
            G1 X85 F3000
            G1 X98 F3000
            G1 X85 F3000
            G1 X98 F3000
            G1 X85 F3000
            G1 X98 F3000
            G1 X85 F3000
            G1 X98 F3000
            G1 X65 F500
            G1 Y200 F3000
        RESTORE_GCODE_STATE NAME=PAUSEPARK2 MOVE=1 MOVE_SPEED=200                            
        RESTORE_GCODE_STATE NAME=PAUSE MOVE=1 MOVE_SPEED=10                         ; restore position
        BASE_RESUME       
        ENABLE_FILAMENT_WIDTH_SENSOR
        RESET_FILAMENT_WIDTH_SENSOR
        query_filament_width                                                           ; resume print
        SET_FILAMENT_SENSOR SENSOR=fila ENABLE=1                          ; enable filament sensor
    {% endif %}

[gcode_macro BEEP]
gcode:
    {% set i = params.I|default(1)|int %}        ; Iterations (number of times to beep).
    {% set dur = params.DUR|default(100)|int %}  ; Duration/wait of each beep in ms. Default 100ms.

    {% if printer["output_pin sound"].value|int == 1 %}
        {% for iteration in range(i|int) %}
            SET_PIN PIN=beeper VALUE=1
            G4 P{dur}
            SET_PIN PIN=beeper VALUE=0
    		G4 P{dur}
        {% endfor %}
    {% endif %}

[gcode_macro M141]
gcode:
    {% if printer["heater_generic chamber"] is defined %}
        {% set s = params.S|float %}
        SET_HEATER_TEMPERATURE HEATER=chamber TARGET={([s, 60]|min)}
    {% endif %}

[gcode_macro M191]
gcode:
    #Parameters
    {% if printer["heater_generic chamber"] is defined %}
        {% set s = params.S|float %}
    
        M141 {% for p in params %}{'%s%s' % (p, params[p])}{% endfor %}  
        {% if s != 0 %}
            TEMPERATURE_WAIT SENSOR="heater_generic chamber" MINIMUM={([s, 60]|min)-2} #MAXIMUM={s+1}   
        {% endif %}
    {% endif %}

[gcode_macro M106]
gcode:
    {% set p = params.P|default(0)|int %}

    {% if p == 2 %}
    {% if params.S is defined %}
        SET_FAN_SPEED FAN=auxiliary_cooling_fan SPEED={(params.S|float / 255.0)}
    {% else %}
        SET_FAN_SPEED FAN=auxiliary_cooling_fan SPEED=1
    {% endif %}
    {% endif %} 

    {% if p == 0 %}
    {% if params.S is defined %}
        SET_FAN_SPEED FAN=cooling_fan SPEED={(params.S|float / 255.0)}
    {% else %}
        SET_FAN_SPEED FAN=cooling_fan SPEED=1
    {% endif %}
    {% endif %} 

    {% if p == 3 %}
    {% if params.S is defined %}
        SET_FAN_SPEED FAN=chamber_circulation_fan SPEED={(params.S|float / 255.0)}
    {% else %}
        SET_FAN_SPEED FAN=chamber_circulation_fan SPEED=1
    {% endif %}
    {% endif %} 

[gcode_macro M107]
gcode:  
    SET_FAN_SPEED FAN=cooling_fan SPEED=0

[gcode_macro M303]
gcode:
    {% if params.E is defined %}
     {% if params.S is defined %}
        {% if (params.E|int)==-1 %} 
         PID_CALIBRATE HEATER=heater_bed TARGET={params.S|int}
        {% endif %}
        {% if (params.E|int)==0 %}
         PID_CALIBRATE HEATER=extruder TARGET={params.S|int}
        {% endif %}
     {% endif %}
  {% endif %}

[gcode_macro M8029]
gcode:
     {% if params.D is defined %}
       {% if (params.D|int)==1 %} 
        ENABLE_FILAMENT_WIDTH_SENSOR
       {% endif %}
       {% if (params.D|int)==0 %} 
        DISABLE_FILAMENT_WIDTH_SENSOR
       {% endif %}
     {% endif %}
     
[gcode_macro M900]
gcode:
    {% if params.K is defined %} 
          SET_PRESSURE_ADVANCE ADVANCE={params.K}
    {% endif %}  
    {% if params.T is defined %}    
       SET_PRESSURE_ADVANCE SMOOTH_TIME={params.T}
    {% endif %} 

[gcode_macro M290]
gcode:
   SET_GCODE_OFFSET Z_ADJUST={params.Z}

[gcode_macro M901]
gcode:
    G28
    SHAPER_CALIBRATE
    M400
    M118 Input shaping complete
    SAVE_CONFIG
       
[gcode_macro M0]
gcode:
    PAUSE

[gcode_macro M25]
rename_existing: M9925
gcode:
    PAUSE

[gcode_macro RESPOND_INFO]
variable_S:0
gcode:
    {% if params.S is defined %}
        {% set s = params.S|int %}
        {% if s == 0 %}
            { action_respond_info("Nozzle cooled")  }
        {% endif %}
    {% endif %}

[gcode_macro M4027]
gcode:
    G1 X{120 - printer.probe["x_offset"]} Y{120 - printer.probe["y_offset"]} F9000
    probe
    save_meshoffset
    G1 z10 F600
    SET_GCODE_OFFSET Z=0 MOVE=1
    BED_MESH_CALIBRATE
    set_meshoffset
    G0 Z50 F600
    G0 X0 Y0 F9000
    M400
    M118 Bed mesh calibrate complete
    SAVE_CONFIG

[gcode_macro M4028]
gcode:
    G28
    get_zoffset
    M400
    M118 Position init complete
    
[gcode_macro M4029]
gcode:
    M104 S140
    SAVE_VARIABLE VARIABLE=z_offset VALUE=0
    G28 
    M400
    M118 Position init complete

    CLEAR_NOZZLE HOTEND=240

    G28
    get_zoffset

[gcode_macro M4030]
gcode:
    BED_MESH_CLEAR      
    SET_GCODE_OFFSET Z=0 MOVE=0
    G28
    get_zoffset
    G1 X120 Y120 F9000
    G1 Z0 F600

[gcode_macro M4031]
gcode:
    SET_TMC_CURRENT STEPPER=stepper_z CURRENT={printer.configfile.settings['tmc2209 stepper_z'].run_current * 0.8 }
    SET_TMC_CURRENT STEPPER=stepper_z1 CURRENT={printer.configfile.settings['tmc2209 stepper_z1'].run_current * 0.8 }
    REVERSE_HOMING
    SET_TMC_CURRENT STEPPER=stepper_z CURRENT={printer.configfile.settings['tmc2209 stepper_z'].run_current}
    SET_TMC_CURRENT STEPPER=stepper_z1 CURRENT={printer.configfile.settings['tmc2209 stepper_z1'].run_current}
    G91
    G1 Z-30 F600
    G90

    SET_TMC_CURRENT STEPPER=stepper_z CURRENT={printer.configfile.settings['tmc2209 stepper_z'].run_current * 0.8 }
    SET_TMC_CURRENT STEPPER=stepper_z1 CURRENT={printer.configfile.settings['tmc2209 stepper_z1'].run_current * 0.8 }
    REVERSE_HOMING
    SET_TMC_CURRENT STEPPER=stepper_z CURRENT={printer.configfile.settings['tmc2209 stepper_z'].run_current}
    SET_TMC_CURRENT STEPPER=stepper_z1 CURRENT={printer.configfile.settings['tmc2209 stepper_z1'].run_current}
    G91
    G1 Z-30 F600
    G90

[gcode_macro M603]
description: unload filament
gcode:
    M118 Heat up complete
    G92 E0
    G0  E15 F400
    G4  P1000
    G92 E0
    G1  E-90 F800
    M400
    M118 Unload finish

[gcode_macro M604]
description: load filament
gcode:
    M118 Heat up complete
    M83
    G1 E80 F400
    M400
    M118 Load finish

[output_pin sound]
pin: gpio8
value:0

[gcode_macro beep_on]
gcode:
    SET_PIN PIN=sound VALUE=1
    
[gcode_macro beep_off]
gcode:
    SET_PIN PIN=sound VALUE=0

[gcode_arcs]
resolution: 1.0

[gcode_macro M109]
rename_existing: M99109
gcode:
    {% set s = params.S|float %}
    
    M104 {% for p in params %}{'%s%s' % (p, params[p])}{% endfor %}  ; Set hotend temp
    {% if s != 0 %}
        TEMPERATURE_WAIT SENSOR=extruder MINIMUM={s} MAXIMUM={s+1}   ; Wait for hotend temp (within 1 degree)
    {% endif %}

[exclude_object]

[gcode_macro G31]
gcode:
    SET_GCODE_VARIABLE MACRO=G29 VARIABLE=k VALUE=1

[gcode_macro G32]
gcode:
    SET_GCODE_VARIABLE MACRO=G29 VARIABLE=k VALUE=0

[gcode_macro set_meshoffset]
variable_zoffset:0
gcode:
    ADD_Z_OFFSET_TO_BED_MESH ZOFFSET={0 - zoffset}
    SET_GCODE_VARIABLE MACRO=set_meshoffset VARIABLE=zoffset VALUE=0
[gcode_macro save_meshoffset]
gcode:
    SET_GCODE_VARIABLE MACRO=set_meshoffset VARIABLE=zoffset VALUE={printer.probe.last_z_result}

[gcode_macro G29]
variable_k:1
gcode:
    BED_MESH_CLEAR
    {% if k|int==1 %}
        G28   
        get_zoffset
        G1 X{120 - printer.probe["x_offset"]} Y{120 - printer.probe["y_offset"]} F9000
        G1 Z10 F600
        probe
        save_meshoffset
        G1 z10 F600
        BED_MESH_CALIBRATE PROFILE=kamp
        SAVE_VARIABLE VARIABLE=profile_name VALUE='"kamp"'
        set_meshoffset        
        SAVE_CONFIG_QD
    {% else %}
        g28
        get_zoffset
        BED_MESH_PROFILE LOAD=default
        SAVE_VARIABLE VARIABLE=profile_name VALUE='"default"'
    {% endif %}

[gcode_macro M204]
rename_existing: M99204
gcode:
    {% if params.S is defined %}
        {% set s = params.S|float %}
    {% endif %}
    {% if params.P is defined %}
    {% if params.T is defined %}
        {% set s = [params.P|float ,params.T|float] | min %}
    {% endif %}
    {% endif %}

    SET_VELOCITY_LIMIT ACCEL={s}
    SET_VELOCITY_LIMIT ACCEL_TO_DECEL={s/2}

[gcode_macro Z_DOUDONG]
gcode:
    M204 S30
    G90
    G0 Z1
    G91
    G4 P3000
     SET_PIN PIN=ctlyd VALUE=1 
    {% for z in range(1,7) %}
        G1 Z10 F900
        G1 Z-10 F900
    {% endfor %}
    SET_PIN PIN=ctlyd VALUE=0
    G4 P1000
    G1 Z4
    G90
#    M204 S10000

