Skip to content

Commit

Permalink
start_end: Split out PRINT_START
Browse files Browse the repository at this point in the history
  • Loading branch information
jschuh committed May 19, 2023
1 parent f46c25e commit 85c3888
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 41 deletions.
59 changes: 43 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,22 @@ into the relevant sections.
#### Start G-code

```
M190 S0 ; Not needed in Prusa Slicer 2.6 and later
M109 S0 ; Not needed in Prusa Slicer 2.6 and later
PRINT_START EXTRUDER={first_layer_temperature[initial_tool]} BED=[first_layer_bed_temperature] MESH_MIN={first_layer_print_min[0]},{first_layer_print_min[1]} MESH_MAX={first_layer_print_max[0]},{first_layer_print_max[1]} LAYERS={total_layer_count} NOZZLE_SIZE={nozzle_diameter[0]}
M190 S0 ; Remove this if autoemit_temperature_commands is off in Prusa Slicer 2.6 and later
M109 S0 ; Remove this if autoemit_temperature_commands is off in Prusa Slicer 2.6 and later
_PRINT_START_PHASE_INIT EXTRUDER={first_layer_temperature[initial_tool]} BED=[first_layer_bed_temperature] MESH_MIN={first_layer_print_min[0]},{first_layer_print_min[1]} MESH_MAX={first_layer_print_max[0]},{first_layer_print_max[1]} LAYERS={total_layer_count} NOZZLE_SIZE={nozzle_diameter[0]}
; Insert custom gcode here.
_PRINT_START_PHASE_PREHEAT
; Insert custom gcode here.
_PRINT_START_PHASE_PROBING
; Insert custom gcode here.
_PRINT_START_PHASE_EXTRUDER
; Insert custom gcode here.
_PRINT_START_PHASE_PURGE
; This is the place to put slicer purge lines if you haven't set a non-zero
; variable_start_purge_length to have START_PRINT automatically purge (e.g. if
; using a Mosaic Palette, which requires the slicer to generate the purge).
; variable_start_purge_length to have START_PRINT automatically calculate and
; perform the purge (e.g. if using a Mosaic Palette, which requires the slicer
; to generate the purge).
```

#### End G-code
Expand Down Expand Up @@ -307,11 +316,20 @@ configuration steps listed below.
```
M190 S0
M109 S0
PRINT_START EXTRUDER={material_print_temperature_layer_0} BED={material_bed_temperature_layer_0} NOZZLE_SIZE={machine_nozzle_size}
_PRINT_START_PHASE_INIT EXTRUDER={material_print_temperature_layer_0} BED={material_bed_temperature_layer_0} NOZZLE_SIZE={machine_nozzle_size}
; Insert custom gcode here.
_PRINT_START_PHASE_PREHEAT
; Insert custom gcode here.
_PRINT_START_PHASE_PROBING
; Insert custom gcode here.
_PRINT_START_PHASE_EXTRUDER
; Insert custom gcode here.
_PRINT_START_PHASE_PURGE
; This is the place to put slicer purge lines if you haven't set a non-zero
; variable_start_purge_length to have START_PRINT automatically purge (e.g. if
; using a Mosaic Palette, which requires the slicer to generate the purge).
; variable_start_purge_length to have START_PRINT automatically calculate and
; perform the purge (e.g. if using a Mosaic Palette, which requires the slicer
; to generate the purge).
```

#### End G-code
Expand Down Expand Up @@ -891,14 +909,6 @@ These are the customization options you can add to your
probing that use the nozzle directly. When this value is provided
`variable_start_extruder_preheat_scale` is ignored.

* `variable_start_gcode_before_print` *(default: None)* - Optional user-supplied
gcode run after any leveling operations are complete and the bed, extruder,
and chamber are all stabilized at their target temperatures. Immediately after
this gcode executes the purge line will be printed (if specified) and then the
file from the virtual sdcard will begin printing. This is a useful to add any
probe docking commands, loading from a multi-material unit, or other
operations that must occur before any filament is extruded.

* `variable_start_level_bed_at_temp` *(default: True if `bed_mesh` configured
)* - If true the `PRINT_START` macro will run [`BED_MESH_CALIBRATE_FAST`](
#bed-mesh-improvements) after the bed has stabilized at its target
Expand Down Expand Up @@ -960,6 +970,23 @@ gcode:
dockable probe you may choose to wrap `BED_MESH_CALIBRATE` with the
appropriate docking/undocking commands.

#### `PRINT_START` Phases

The recommended slicer start gcode breaks `PRINT_START` into the phases below.
This approach allows for pausing or cancelling, and inserting custom gcode
between the phases (e.g. to set status LEDs, deploy/dock probes, load filament).
The phases are described in order below:

* `_PRINT_START_PHASE_INIT` - Initializes the `PRINT_START` settings, and begins
heating the bed and chamber.
* `_PRINT_START_PHASE_PREHEAT` - Stabilizes the bed and (if applicable) chamber
at the target temperatures. Also homes the axes while heating is in progress.
* `_PRINT_START_PHASE_PROBING` - Performs probing operations, including mesh
bed calibration, quad gantry leveling, etc.
* `_PRINT_START_PHASE_EXTRUDER` - Stabilizes the extruder at the target
temperature.
* `_PRINT_START_PHASE_PURGE` - Extrudes a purge line in front of the print area.

#### `PRINT_END`

Parks the printhead, shuts down heaters, fans, etc, and performs general state
Expand Down
10 changes: 7 additions & 3 deletions globals.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ variable_start_end_park_y: 0.0
variable_start_extruder_preheat_scale: 0.5
# If non-zero the extruder will stabilize at this temp before probing the bed.
variable_start_extruder_probing_temp: 0
# Optional gcode run after all prep is complete, immediately before purge/print.
variable_start_gcode_before_print: ''
# Set to rehome Z in PRINT_START after bed temp stabilizes; False to disable.
variable_start_home_z_at_temp: True
# Set to level bed in PRINT_START after bed temp stabilizes; False to disable.
Expand Down Expand Up @@ -115,7 +113,13 @@ gcode:
"start_extruder_set_target_before_level" :
"The start_extruder_set_target_before_level option has been removed. "
"Use the start_extruder_probing_temp option instead to set a fixed "
"probing temperature."
"probing temperature.",
"start_gcode_before_print" :
"The start_gcode_before_print option has been removed. The same effect "
"can now be achieved by inserting custom gcode before the "
"_PRINT_START_PHASE_PURGE line in your slicer's start gcode, or by "
"wrapping the _PRINT_START_PHASE_PURGE macro. See the Slicer "
"Configuration section in the readme for more information."
} %}
{% set output = [] %}
{% for s in required_sections %}
Expand Down
141 changes: 119 additions & 22 deletions start_end.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,81 @@
# This file may be distributed under the terms of the GNU GPLv3 license.

[gcode_macro print_start]
description: Inserted by slicer at start of print.
description: Inserted by slicer at start of print. Rather than using this macro
directly you should configure your slicer as instructed in the readme.
Usage: PRINT_START BED=<temp> EXTRUDER=<temp> [CHAMBER=<temp>]
[MESH_MIN=<x,y>] [MESH_MAX=<x,y>] [LAYERS=<num>]
[NOZZLE_SIZE=<mm>]
gcode:
CHECK_KM_CONFIG # Need this in case startup errors were missed.
{action_respond_info(
"This file is using an old The PRINT_START format. This print will run "
"fine, but you should update your slicer config to take advantage of the "
"phased PRINT_START macros. The slicer documentation is here:\n"
"https://github.com/jschuh/klipper-macros\x23slicer-configuration"
)}

M107 # Turn off part cooling fan in case it was on.
_PRINT_START_PHASE_INIT {rawparams}
_PRINT_START_PHASE_PREHEAT
_PRINT_START_PHASE_PROBING
_PRINT_START_PHASE_EXTRUDER
_PRINT_START_PHASE_PURGE

[gcode_macro _print_start_phase_init]
description: Inserted by slicer at start of print. Initializes PRINT_START
phases.
Usage: See PRINT_START.
gcode:
CHECK_KM_CONFIG # Need this in case startup errors were missed.
SET_GCODE_VARIABLE MACRO=print_end VARIABLE=was_cancelled VALUE="{False}"
CLEAR_PAUSE
{% set BED = params.BED|default(params.BED_TEMP)|float %}
{% set EXTRUDER = params.EXTRUDER|default(params.EXTRUDER_TEMP)|float %}
{% set CHAMBER = params.CHAMBER|default(0)|float %}
{% set settings = printer["gcode_macro print_start_set"].settings %}
{% set MESH_MIN = params.MESH_MIN|default(settings.MESH_MIN)|default(None) %}
{% set MESH_MAX = params.MESH_MAX|default(settings.MESH_MAX)|default(None) %}
# Clear the print settings after they are no longer needed.
SET_GCODE_VARIABLE MACRO=print_start_set VARIABLE=settings VALUE="{{}}"
{% set LAYERS = params.LAYERS|default(settings.LAYERS)|default(0)|int %}
{% set NOZZLE_SIZE = params.NOZZLE_SIZE|default(settings.NOZZLE_SIZE)|
default(printer.configfile.settings.extruder.nozzle_diameter)|float %}
{% if 'PRINT_START_PHASE' in settings %}
{ action_raise_error("Error in PRINT_START_PHASE: init") }
{% endif %}
{% set km = printer["gcode_macro _km_globals"] %}

{% if not params.BED %}
{% set dummy = params.__setitem__('BED', params.BED_TEMP|int) %}
{% endif %}
{% if not params.EXTRUDER %}
{% set dummy = params.__setitem__('EXTRUDER', params.EXTRUDER_TEMP|int) %}
{% endif %}

# Stash all the params for use by the other phases.
PRINT_START_SET PRINT_START_PHASE="init" {% for k in params %}{' %s=\"%s\"' %
(k,params[k]|replace('\\','\\\\')|replace('\'','\\\'')|replace('\"','\\\"'))
}{% endfor %}

M107 # Turn off part cooling fan in case it was on.
CLEAR_PAUSE
# Kick off the longest preheats in the init.
M140 S{params.BED}
{% if params.CHAMBER %}M141 S{params.CHAMBER|int}{% endif %}
BED_MESH_CLEAR
# Load a saved mesh if configured.
{% if km.start_try_saved_surface_mesh and printer.bed_mesh %}
BED_MESH_CLEAR
LOAD_SURFACE_MESH
{% endif %}
{% set actions_at_temp = km.start_level_bed_at_temp or
PRINT_START_SET PRINT_START_PHASE="preheat"

[gcode_macro _print_start_phase_preheat]
description: Inserted by slicer at start of print. Handles the bed and chamber
heating phases and ends when both are stabilized at their target temperatures.
Usage: See PRINT_START.
gcode:
{% set settings = printer["gcode_macro print_start_set"].settings %}
{% if settings.PRINT_START_PHASE != "preheat" %}
{ action_raise_error("Error in PRINT_START_PHASE: %s" %
(settings.PRINT_START_PHASE,)) }
{% endif %}
{% set BED = settings.BED|int %}
{% set EXTRUDER = settings.EXTRUDER|int %}
{% set CHAMBER = settings.CHAMBER|default(0)|int %}
# Clear the print settings after they are no longer needed.
{% set LAYERS = settings.LAYERS|default(0)|int %}
{% set km = printer["gcode_macro _km_globals"] %}
{% set start_level_bed_at_temp = km.start_level_bed_at_temp and
not printer.bed_mesh.profile_name %}
{% set actions_at_temp = start_level_bed_at_temp or
km.start_quad_gantry_level_at_temp or
km.start_z_tilt_adjust_at_temp %}
# The bed started at no more than 0.2C below and 1.0C above the target temp.
Expand Down Expand Up @@ -74,7 +122,28 @@ gcode:
{% if BED > 0.0 and bed_at_target%}
M190 R{BED} # Extra bed stabilization if we skipped it earlier.
{% endif %}
PRINT_START_SET PRINT_START_PHASE="probing"

[gcode_macro _print_start_phase_probing]
description: Inserted by slicer at start of print. Performs probing (including
mesh bed level) and other operations that should be run when the bed and
chamber are stabilized at their target temps.
Usage: See PRINT_START.
gcode:
{% set settings = printer["gcode_macro print_start_set"].settings %}
{% if settings.PRINT_START_PHASE != "probing" %}
{ action_raise_error("Error in PRINT_START_PHASE: %s" %
(settings.PRINT_START_PHASE,)) }
{% endif %}
{% set MESH_MIN = settings.MESH_MIN|default(None) %}
{% set MESH_MAX = settings.MESH_MAX|default(None) %}
{% set EXTRUDER = settings.EXTRUDER|int %}
{% set km = printer["gcode_macro _km_globals"] %}
{% set start_level_bed_at_temp = km.start_level_bed_at_temp and
not printer.bed_mesh.profile_name %}
{% set actions_at_temp = start_level_bed_at_temp or
km.start_quad_gantry_level_at_temp or
km.start_z_tilt_adjust_at_temp %}
{% if actions_at_temp %}
{% if km.start_extruder_probing_temp > 0 %}
_KM_PARK_IF_NEEDED HEATER={printer.toolhead.extruder} RANGE=2
Expand All @@ -91,7 +160,7 @@ gcode:
{% if km.start_quad_gantry_level_at_temp %}
QUAD_GANTRY_LEVEL
{% endif %}
{% if km.start_level_bed_at_temp %}
{% if start_level_bed_at_temp %}
_km_mesh_if_needed {% if MESH_MIN %} MESH_MIN={MESH_MIN}{% endif
%}{% if MESH_MAX %} MESH_MAX={MESH_MAX}{% endif %}
{% endif %}
Expand All @@ -100,17 +169,46 @@ gcode:
{% endif %}
G4
{% endif %}
PRINT_START_SET PRINT_START_PHASE="extruder"

[gcode_macro _print_start_phase_extruder]
description: Inserted by slicer at start of print. Preheats the extruder.
Usage: See PRINT_START.
gcode:
{% set settings = printer["gcode_macro print_start_set"].settings %}
{% if settings.PRINT_START_PHASE != "extruder" %}
{ action_raise_error("Error in PRINT_START_PHASE: %s" %
(settings.PRINT_START_PHASE,)) }
{% endif %}
{% set EXTRUDER = settings.EXTRUDER|default(settings.EXTRUDER_TEMP)|float %}
{% set km = printer["gcode_macro _km_globals"] %}
# Wait for extruder to reach temperature
_KM_PARK_IF_NEEDED HEATER={printer.toolhead.extruder} RANGE=ABOVE
M109 S{EXTRUDER}
# apply Z offset for bed surface (just in case it was reset).
_APPLY_BED_SURFACE_OFFSET
{% if km.start_gcode_before_print %}{ km.start_gcode_before_print }{% endif %}
{% if km.start_purge_length > 0.0 %}
PRINT_START_SET PRINT_START_PHASE="purge"

[gcode_macro _print_start_phase_purge]
description: Inserted by slicer at start of print. Generates purge lines.
Usage: See PRINT_START.
gcode:
{% set settings = printer["gcode_macro print_start_set"].settings %}
{% if settings.PRINT_START_PHASE != "purge" %}
{ action_raise_error("Error in PRINT_START_PHASE: %s" %
(settings.PRINT_START_PHASE,)) }
{% endif %}
{% set MESH_MIN = settings.MESH_MIN|default(None) %}
{% set MESH_MAX = settings.MESH_MAX|default(None) %}
{% set NOZZLE_SIZE = settings.NOZZLE_SIZE|default(settings.NOZZLE_SIZE)|
default(printer.configfile.settings.extruder.nozzle_diameter)|float %}
{% set km = printer["gcode_macro _km_globals"] %}
{% if km.start_purge_length > 0.0 and printer.extruder.can_extrude %}
DRAW_PURGE_LINE WIDTH="{NOZZLE_SIZE * 1.25}" HEIGHT="{NOZZLE_SIZE * 0.625
}"{% if MESH_MIN %} PRINT_MIN={MESH_MIN}{% endif
%}{% if MESH_MAX %} PRINT_MAX={MESH_MAX}{% endif %}
{% endif %}
PRINT_START_SET PRINT_START_PHASE="printing"

[gcode_macro _km_park_if_needed]
description: Parks the extruder if the current temperature of the supplied
Expand Down Expand Up @@ -140,11 +238,10 @@ gcode:

[gcode_macro _km_mesh_if_needed]
gcode:
# TODO: Instead of blindly using the saved mesh we could probe a few key
# TODO: Instead of blindly using the loaded mesh we could probe a few key
# points on the saved grid. If those probes show that the saved grid is no
# longer in tolerance we could just run BED_MESH_CALIBRATE_FAST anyway.
{% if not printer["gcode_macro _km_globals"].start_try_saved_surface_mesh or
not printer.bed_mesh.profile_name %}
{% if not printer.bed_mesh.profile_name %}
BED_MESH_CALIBRATE_FAST {rawparams}
{% endif %}

Expand Down

0 comments on commit 85c3888

Please sign in to comment.