################################################
###### STANDARD INPUT_SHAPER CALIBRATIONS ######
################################################
# Written by Frix_x#0161 #

[gcode_macro AXES_SHAPER_CALIBRATION]
description: Perform standard axis input shaper tests on one or both XY axes to select the best input shaper filter
gcode:
    {% set min_freq = params.FREQ_START|default(5)|float %}
    {% set max_freq = params.FREQ_END|default(133.3)|float %}
    {% set hz_per_sec = params.HZ_PER_SEC|default(1)|float %}
    {% set axis = params.AXIS|default("all")|string|lower %}
    {% set scv = params.SCV|default(None) %}
    {% set max_sm = params.MAX_SMOOTHING|default(None) %}
    {% set keep_results = params.KEEP_N_RESULTS|default(3)|int %}
    {% set keep_csv = params.KEEP_CSV|default(0)|int %}

    {% set X, Y = False, False %}

    {% if axis == "all" %}
        {% set X, Y = True, True %}
    {% elif axis == "x" %}
        {% set X = True %}
    {% elif axis == "y" %}
        {% set Y = True %}
    {% else %}
        { action_raise_error("AXIS selection invalid. Should be either all, x or y!") }
    {% endif %}

    {% if scv is none or scv == "" %}
        {% set scv = printer.toolhead.square_corner_velocity %}
    {% endif %}

    {% if max_sm == "" %}
        {% set max_sm = none %}
    {% endif %}

    {% if X %}
        TEST_RESONANCES AXIS=X OUTPUT=raw_data NAME=x FREQ_START={min_freq} FREQ_END={max_freq} HZ_PER_SEC={hz_per_sec}
        M400

        RESPOND MSG="X axis frequency profile generation..."
        RESPOND MSG="This may take some time (1-3min)"
        RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type shaper --scv {scv} {% if max_sm is not none %}--max_smoothing {max_sm}{% endif %} {% if keep_csv %}--keep_csv{% endif %} --keep_results {keep_results}"
    {% endif %}

    {% if Y %}
        TEST_RESONANCES AXIS=Y OUTPUT=raw_data NAME=y FREQ_START={min_freq} FREQ_END={max_freq} HZ_PER_SEC={hz_per_sec}
        M400

        RESPOND MSG="Y axis frequency profile generation..."
        RESPOND MSG="This may take some time (1-3min)"
        RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type shaper --scv {scv} {% if max_sm is not none %}--max_smoothing {max_sm}{% endif %} {% if keep_csv %}--keep_csv{% endif %} --keep_results {keep_results}"
    {% endif %}
