# Include servo hardware definition separately to allow for automatic upgrade
[include mmu_erec_cutter_hw.cfg]

###########################################################################
# Optional EREC Filament Cutter Support
#
# https://github.com/kevinakasam/ERCF_Filament_Cutter
# 
# This is the supplementary macro to support filament cutting at the MMU
# on a ERCF design.
#
# To configure:
# 1. Add this to your printer.cfg:
#
#   [include mmu/addons/mmu_erec_cutter.cfg]
#
# 2. In mmu_macro_vars.cfg, change this line:
#
#   variable_user_post_unload_extension : "EREC_CUTTER_ACTION"
#
# 3. Tune the servo configuration and macro "variables" below
#

# EREC CUTTER CONFIGURATION -----------------------------------------------
#   (addons/mmu_erec_cutter.cfg)
#
[gcode_macro _EREC_VARS]
description: Empty macro to store the variables
gcode: # Leave empty

# These variables control the servo movement
variable_servo_closed_angle   : 70	; Servo angle for closed position with bowden aligned MMU
variable_servo_open_angle     : 10	; Servo angle to open up the cutter and move bowden away from MMU
variable_servo_duration       : 1.5	; Time (s) of PWM pulse train to activate servo
variable_servo_idle_time      : 1.8	; Time (s) to let the servo to reach it's position

# Controls for feed and cut lengths
variable_feed_length          : 48	; Distance in mm from gate parking position to blade (ERCFv1.1: 58, v2/other: 48)
variable_cut_length           : 10	; Amount in mm of filament to cut
variable_cut_attempts         : 1	; Number of times the cutter tries to cut the filament


###########################################################################
# Macro to perform the cutting step. Designed to be included to the
# _MMU_POST_UNLOAD step
#
[gcode_macro EREC_CUTTER_ACTION]
description: Cut off the filament tip at the MMU after the unload sequence is complete
gcode:
    {% set vars = printer["gcode_macro _EREC_VARS"] %}
    
    MMU_LOG MSG="Cutting filament tip..."

    _CUTTER_OPEN
    _MMU_STEP_MOVE MOVE={vars.feed_length + vars.cut_length}
    {% for i in range(vars.cut_attempts - 1) %}
        _CUTTER_CLOSE
        _CUTTER_OPEN
    {% endfor %}
    _MMU_STEP_MOVE MOVE=-1
    _CUTTER_CLOSE
    _MMU_EVENT EVENT="filament_cut"	# Count as one cut for consumption counter
    
    _MMU_STEP_SET_FILAMENT STATE=2	# FILAMENT_POS_START_BOWDEN
    _MMU_STEP_UNLOAD_GATE		# Repeat gate parking move
    _MMU_M400				# Wait on both move queues

[gcode_macro _CUTTER_ANGLE]
description: Helper macro to set cutter servo angle
gcode:
    {% set angle = params.ANGLE|default(0)|int %}
    SET_SERVO SERVO=cut_servo ANGLE={angle}
    
[gcode_macro _CUTTER_CLOSE]
description: Helper macro to set cutting servo the closed position
gcode:
    {% set vars = printer["gcode_macro _EREC_VARS"] %}
    SET_SERVO SERVO=cut_servo ANGLE={vars.servo_closed_angle} DURATION={vars.servo_duration}
    G4 P{vars.servo_idle_time * 1000}
    RESPOND MSG="EREC Cutter closed"
    M400

[gcode_macro _CUTTER_OPEN]
description: Helper macro to set cutting servo the open position
gcode:
    {% set vars = printer["gcode_macro _EREC_VARS"] %}
    SET_SERVO SERVO=cut_servo ANGLE={vars.servo_open_angle} DURATION={vars.servo_duration}
    G4 P{vars.servo_idle_time * 1000}
    RESPOND MSG="EREC Cutter open"
    M400