[gcode_macro CUT_FILAMENT]
description: Cut filament by pressing the cutter on a fixed pin with a horizontal movement. Based on Thiscams macro but updated to work with HHv2

#Modify the below settings in mmu_parameters.cfg

#toolhead_sensor_to_nozzle: 62 this will need to be reduced to compensate for the extra filament letf in the nozzle so probably to 5 to 15mm shorter based on the variable_retract_length: below.
#force_form_tip_standalone: will need to be set to 1 and all tip forming settings will need to be disabled in your slicer including ramming.
#slicer_tip_park_pos: will need to be set just above the cutter slot.

# Distance to retract prior to making the cut, this reduces wasted filament but might cause clog 
# if set too large and/or if there are gaps in the hotend assembly 
# This must be less than the distance from the nozzle to the cutter.
variable_retract_length: 35

# The location of the pin, this should be the position of the toolhead when the cutter 
# just lightly touches the pin
variable_pin_loc_x: 13
variable_pin_loc_y: 56

# The starting and end positions when making the cut
# In particular, instead of making the cut by traveling to the pin location above, 
# we leave a small safety margin along X-axis to avoid scratching on the pin when traveling
# This should also give a small distance to produce some momentum when pressing on the pin 
variable_pin_park_x_dist: 5.0

# Position of the toolhead when the cutter is fully compressed
# Should leave a small headroom to avoid banging the toolhead or gantry
variable_pin_loc_x_compressed: 0

# Speed related settings
# Note that if the cut speed is too fast, the steppers can lose steps
# Therefore, for a cut: 
# - We first make a fast move to accumulate some momentum and get the cut blade to the initial contact with the filament
# - We then make a slow move for the actual cut to happen 
variable_travel_spd: 8000
variable_cut_fast_move_spd: 2000
variable_cut_slow_move_spd: 400
variable_evacuate_speed: 8000
variable_cut_dwell_time: 200 # time to dwell at the cut point in ms
variable_cut_fast_move_fraction: 0.5 # fraction of the move that uses fast move 

# Safety margin for fast vs slow travel
# When traveling to the pin location, we make a safer but longer move if we closer to the pin than this specified margin
# Usually setting these to the size of the toolhead (plus a small margin) should be good enough 
variable_safe_margin_x: 30
variable_safe_margin_y: 30

#this is the length of filament to move after filament has been cut, just need enough to move it away from the cutting slot.
variable_cut_retract_length: 2

# Whether to eject the filament at the end
variable_final_eject: 0

gcode:
    {% set RETRACT_LENGTH = params.RETRACT_LENGTH | default(printer['gcode_macro CUT_FILAMENT']['retract_length']) | float %}
    {% set FINAL_EJECT = params.FINAL_EJECT | default(printer['gcode_macro CUT_FILAMENT']['final_eject'], True) | int %}
    {% set current_loc_x = printer.gcode_move.gcode_position.x %}
    {% set current_loc_y = printer.gcode_move.gcode_position.y %}

    SET_PRESSURE_ADVANCE ADVANCE=0
    
    {% if ("x" not in printer.toolhead.homed_axes) or ("y" not in printer.toolhead.homed_axes) %}
        G28 X Y
    {% endif %}

    {% set prev_pa = printer.extruder.pressure_advance %}
    SET_PRESSURE_ADVANCE ADVANCE=0 # temporaily disable PA

    {% set pin_park_x_loc = pin_loc_x + pin_park_x_dist %}
    {% set pin_park_y_loc = pin_loc_y %}
    {% set _extruder_moved_dist = 0 %}

    M83 # relative extrusion
    {% if RETRACT_LENGTH > 0 %}
        G1 E-{RETRACT_LENGTH} F3000 # retract to save filament waste
        {% set _extruder_moved_dist = _extruder_moved_dist + RETRACT_LENGTH %}
    {% endif %}

    G90 # absolute positioning
    _MOVE_TO_CUTTER_PIN PIN_PARK_X_LOC={pin_park_x_loc} PIN_PARK_Y_LOC={pin_park_y_loc}

    # Make the main cut 
    _DO_CUT_MOTION PIN_PARK_X_LOC={pin_park_x_loc}

    G1 E-{cut_retract_length} F3000 #moves filament after cut, but not past the toolhead sensor

    M400
    M117 Filament cut!
    RESTORE_GCODE_STATE NAME=MMU_state MOVE=1 MOVE_SPEED={travel_spd} #restores MMU state to bring the toolhead back over the purge block after a cut so no oozing.

[gcode_macro _MOVE_TO_CUTTER_PIN]
description: helper to move the toolhead to the target pin in either safe or faster way, depending on toolhead clearance.
gcode:
    {% set safe_margin_x = printer['gcode_macro CUT_FILAMENT']['safe_margin_x'] | float %}
    {% set safe_margin_y = printer['gcode_macro CUT_FILAMENT']['safe_margin_y'] | float %}
    {% set travel_spd = printer['gcode_macro CUT_FILAMENT']['travel_spd'] | float %}
    {% set pin_park_x_loc = params.PIN_PARK_X_LOC | float %}
    {% set pin_park_y_loc = params.PIN_PARK_Y_LOC | float %}

    G90 # absolute positioning
    {% if ((printer.gcode_move.gcode_position.x - pin_park_x_loc) | abs < safe_margin_x) or ((printer.gcode_move.gcode_position.y - pin_park_y_loc | float) | abs < safe_margin_y) %}
        # Make a safe but slower travel move
        G1 X{pin_park_x_loc} F{travel_spd}
        G1 Y{pin_park_y_loc} F{travel_spd}
    {% else %}
        G1 X{pin_park_x_loc} Y{pin_park_y_loc} F{travel_spd}
    {% endif %}

[gcode_macro _DO_CUT_MOTION]
description: helper to do a single horizontal cut movement
gcode:
    {% set pin_loc_x_compressed = printer['gcode_macro CUT_FILAMENT']['pin_loc_x_compressed'] | float %}
    {% set cut_fast_move_fraction = printer['gcode_macro CUT_FILAMENT']['cut_fast_move_fraction'] | float %}
    {% set cut_fast_move_spd = printer['gcode_macro CUT_FILAMENT']['cut_fast_move_spd'] | float %}
    {% set cut_slow_move_spd = printer['gcode_macro CUT_FILAMENT']['cut_slow_move_spd'] | float %}
    {% set cut_dwell_time = printer['gcode_macro CUT_FILAMENT']['cut_dwell_time'] | float %}
    {% set evacuate_speed = printer['gcode_macro CUT_FILAMENT']['evacuate_speed'] | float %}
    {% set pin_park_x_loc = params.PIN_PARK_X_LOC | float %}
    {% set fast_slow_transition_loc = (pin_loc_x_compressed - pin_park_x_loc) * cut_fast_move_fraction + pin_park_x_loc | float %}
    G1 X{fast_slow_transition_loc} F{cut_fast_move_spd} # make a fast move to initiate contact of the blade with the filament
    G1 X{pin_loc_x_compressed} F{cut_slow_move_spd} # do the cut in slow move
    G4 P{cut_dwell_time}
    G1 X{pin_park_x_loc} F{evacuate_speed} # evacuate

    
[gcode_macro _MMU_FORM_TIP_STANDALONE]
gcode:
    CUT_FILAMENT {rawparams} 
