Skip to content

Commit

Permalink
park: Account for gcode offsets in park, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
jschuh committed May 22, 2023
1 parent 8768aef commit 15b4903
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
8 changes: 7 additions & 1 deletion draw.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ description: Extrudes a line of filament at the specified height and width from
[FEEDRATE=<mm/m>]
gcode:
{% set dparams = printer["gcode_macro set_draw_params"] %}
{% set position = printer.gcode_move.position %}
{% set position = printer.gcode_move.gcode_position %}
{% set X = params.X|default(position.x)|float %}
{% set Y = params.Y|default(position.y)|float %}
{% set HEIGHT = params.HEIGHT|default(dparams.height)|float %}
Expand Down Expand Up @@ -61,6 +61,12 @@ description: Purges the specified length of filament as a line (or rows of
gcode:
# TODO: Make this work for delta printers.
{% set km = printer["gcode_macro _km_globals"] %}
{% set origin = printer.gcode_move.homing_origin %}
{% set dummy = km.__setitem__('print_min', (km.print_min[0] - origin.x,
km.print_min[1] - origin.y)) %}
{% set dummy = km.__setitem__('print_max', (km.print_max[0] - origin.x,
km.print_max[1] - origin.y)) %}

{% if "PRINT_MIN" in params %}
{% set PRINT_MIN = (
(params.PRINT_MIN.split(",")[0]|float, km.print_min[0])|max,
Expand Down
17 changes: 10 additions & 7 deletions park.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ gcode:
{% set Y = params.Y|default(km.park_y)|float %}
{% set Z = params.Z|default(km.park_z)|float %}

M400
_CHECK_KINEMATIC_LIMITS X="{X}" Y="{Y}" Z="{Z}"
_PARK_INNER X="{X}" Y="{Y}" Z="{Z}" P="{P}" LAZY="{LAZY}"
# Dummy argument block for Mainsail
Expand All @@ -39,33 +40,35 @@ gcode:
{% set travel_speed_z = km.travel_speed_z %}

{% set position = printer.gcode_move.gcode_position %}
{% set origin = printer.gcode_move.homing_origin%}

# Use the taller of the highest printed layer or the current Z height, which
# should helps crashes e.g. when a sequential print in progress.
# should help avoid crashes (e.g. when a sequential print in progress).
{% set clearance_z = (printer["gcode_macro _km_layer_run"].clearance_z,
position.z) | max %}
{% set P = params.P|int %}
{% set X = params.X|float %}
{% set Y = params.Y|float %}
{% set X = params.X|float - origin.x %}
{% set Y = params.Y|float - origin.y %}
{% set Z = params.Z|float %}
{% set LAZY = params.LAZY|int %}

# Convert everything to absolute coordinates.
# P == 1 is absolute, so needs no adjustment.
{% if P == 0 %} # Move absolute to Z if below current Z
{% if clearance_z > Z %}
{% set Z = clearance_z %}
{% endif %}
{% elif P == 1 %} # Move Z absolute.
{% set Z = Z - origin.z %}
{% elif P == 2 %} # Move Z relative
{% set Z = Z + clearance_z %}
{% elif P != 1 %}
{% else %}
{action_raise_error("Invalid parameter P=%i. Value must be 0, 1, or 2." |
format(P)) }
{% endif %}

# Clamp to the printer limits.
{% set Z = ((Z, printer.toolhead.axis_maximum.z)|min,
printer.toolhead.axis_minimum.z)|max %}
{% set Z = ((Z, printer.toolhead.axis_maximum.z - origin.z)|min,
printer.toolhead.axis_minimum.z - origin.z)|max %}

# Don't move if it's a lazy park and we're already in position.
{% if (not LAZY) or P != 2 or X != position.x or Y != position.y
Expand Down
2 changes: 1 addition & 1 deletion pause_resume_cancel.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ description: Cancels the current print.
rename_existing: _KM_CANCEL_PRINT_BASE
gcode:
_KM_CHECK_IS_PRINTING
SET_GCODE_VARIABLE MACRO=print_end VARIABLE=was_cancelled VALUE="{True}"
SET_GCODE_VARIABLE MACRO=_print_end_inner VARIABLE=cancelled VALUE="{True}"
PRINT_END
_KM_CANCEL_PRINT_BASE
{% if printer.pause_resume.is_paused %}
Expand Down
60 changes: 35 additions & 25 deletions start_end.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ description: Inserted by slicer at start of print. Initializes PRINT_START
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}"
SET_GCODE_VARIABLE MACRO=_print_end_inner VARIABLE=cancelled VALUE="{False}"
{% set settings = printer["gcode_macro print_start_set"].settings %}
{% if 'PRINT_START_PHASE' in settings %}
{ action_raise_error("Error in PRINT_START_PHASE: init") }
Expand Down Expand Up @@ -260,22 +260,48 @@ gcode:
[gcode_macro print_end]
description: Inserted by slicer at end of print.
Usage: PRINT_END
variable_was_cancelled: False
gcode:
_KM_CHECK_IS_PRINTING
SET_GCODE_VARIABLE MACRO=print_end VARIABLE=was_cancelled VALUE="{False}"
M400
_PRINT_END_INNER

SET_GCODE_VARIABLE MACRO=print_start_set VARIABLE=settings VALUE="{{}}"
{% set km = printer["gcode_macro _km_globals"] %}

{% if km.start_clear_adjustments_at_end != 0 %}
RESET_HEATER_SCALING
RESET_FAN_SCALING
M220 S100
M221 S100
{% endif %}
_KM_APPLY_PRINT_OFFSET INVERT=1
_RESET_LAYER_GCODE
_RESET_VELOCITY_LIMITS
TURN_OFF_HEATERS
M107; turn off fan
BED_MESH_CLEAR
# Park the toolhead and present the bed
{% if printer.toolhead.homed_axes|lower == "xyz" %}
PARK Y="{km.start_end_park_y}"
{% endif %}
M84 ; disable steppers
CLEAR_PAUSE

[gcode_macro _print_end_inner]
variable_cancelled: False
gcode:
SET_GCODE_VARIABLE MACRO=_print_end_inner VARIABLE=cancelled VALUE="{False}"
{% set km = printer["gcode_macro _km_globals"] %}
{% set toolhead = printer.toolhead %}
{% set max_x = km.print_max[0] %}
{% set max_y = km.print_max[1] %}
{% set max_z = toolhead.axis_maximum.z %}
{% set is_homed = printer.toolhead.homed_axes | lower == "xyz" %}
{% set origin = printer.gcode_move.homing_origin%}
{% set max_x = km.print_max[0] - origin.x %}
{% set max_y = km.print_max[1] - origin.y %}
{% set max_z = toolhead.axis_maximum.z - origin.z %}

{% if printer.extruder.can_extrude %}
# Wipe if we're not cancelling a paused print.
{% if not printer.pause_resume.is_paused and is_homed and
not was_cancelled %}
{% if not printer.pause_resume.is_paused and not cancelled and
toolhead.homed_axes|lower == "xyz" %}
{% set x_safe = (max_x - toolhead.position.x, 2.0)|min %}
{% set y_safe = (max_y - toolhead.position.y, 2.0)|min %}
{% set z_safe = (max_z - toolhead.position.z, 2.0)|min %}
Expand All @@ -290,22 +316,6 @@ gcode:
M400
{% endif %}

{% if km.start_clear_adjustments_at_end != 0 %}
RESET_HEATER_SCALING
RESET_FAN_SCALING
M220 S100
M221 S100
{% endif %}
_RESET_LAYER_GCODE
_RESET_VELOCITY_LIMITS
TURN_OFF_HEATERS
M107; turn off fan
BED_MESH_CLEAR
# Park the toolhead and present the bed
{% if is_homed %} PARK Y="{km.start_end_park_y}" {% endif %}
M84 ; disable steppers
CLEAR_PAUSE

[gcode_macro _km_check_is_printing]
variable_debug_state: False # Disables print state check for debugging.
description: Throws an error if print is not currently in progress.
Expand Down

0 comments on commit 15b4903

Please sign in to comment.