################################################
###### STANDARD INPUT_SHAPER CALIBRATIONS ######
################################################
# Written by Frix_x#0161 #
# @version: 1.5

# CHANGELOG:
#   v1.5: modified EXCITATE_AXIS_AT_FREQ to allow A or B belt testing
#   v1.4: added possibility to only run one axis at a time for the axes shaper calibration
#   v1.3: added possibility to override the default parameters
#   v1.2: added EXCITATE_AXIS_AT_FREQ to hold a specific excitating frequency on an axis and diagnose mechanical problems
#   v1.1: added M400 to validate that the files are correctly saved to disk
#   v1.0: first version of the automatic input shaper workflow


### What is it ? ###
# This macro helps you to configure the input shaper algorithm of Klipper by running the tests sequencially and calling an automatic script
# that generate the graphs, manage the files and so on. It's basically a fully automatic input shaper calibration workflow.
# Results can be found in your config folder using FLuidd/Maisail file manager.

# The goal is to make it easy to set, share and use it.

# Usage:
#   1. Call the AXES_SHAPER_CALIBRATION macro, wait for it to end and compute the graphs. Then look for the results in the results folder.
#   2. Call the BELTS_SHAPER_CALIBRATION macro, wait for it to end and compute the graphs. Then look for the results in the results folder.
#   3. If you find out some strange noise, you can use the EXCITATE_AXIS_AT_FREQ macro to diagnose the origin


[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 verbose = params.VERBOSE|default(true) %}
    {% 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 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 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

        {% if verbose %}
            RESPOND MSG="X axis frequency profile generation..."
            RESPOND MSG="This may take some time (1-3min)"
        {% endif %}
        RUN_SHELL_COMMAND CMD=plot_graph PARAMS=SHAPER
    {% 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

        {% if verbose %}
            RESPOND MSG="Y axis frequency profile generation..."
            RESPOND MSG="This may take some time (1-3min)"
        {% endif %}
        RUN_SHELL_COMMAND CMD=plot_graph PARAMS=SHAPER
    {% endif %}


[gcode_macro BELTS_SHAPER_CALIBRATION]
description: Perform a custom half-axis test to analyze and compare the frequency profiles of individual belts on CoreXY printers
gcode:
    {% set verbose = params.VERBOSE|default(true) %}
    {% set min_freq = params.FREQ_START|default(5)|float %}
    {% set max_freq = params.FREQ_END|default(133.33)|float %}
    {% set hz_per_sec = params.HZ_PER_SEC|default(1)|float %}

    TEST_RESONANCES AXIS=1,1 OUTPUT=raw_data NAME=b FREQ_START={min_freq} FREQ_END={max_freq} HZ_PER_SEC={hz_per_sec}
    M400

    TEST_RESONANCES AXIS=1,-1 OUTPUT=raw_data NAME=a FREQ_START={min_freq} FREQ_END={max_freq} HZ_PER_SEC={hz_per_sec}
    M400

    {% if verbose %}
        RESPOND MSG="Belts comparative frequency profile generation..."
        RESPOND MSG="This may take some time (3-5min)"
    {% endif %}
    RUN_SHELL_COMMAND CMD=plot_graph PARAMS=BELTS


[gcode_macro EXCITATE_AXIS_AT_FREQ]
description: Maintain a specified input shaper excitation frequency for a period of time to diagnose and locate a source of vibration
gcode:
    {% set frequency = params.FREQUENCY|default(25)|int %}
    {% set time = params.TIME|default(10)|int %}
    {% set axis = params.AXIS|default("x")|string|lower %}

    {% if axis not in ["x", "y", "a", "b"] %}
        { action_raise_error("AXIS selection invalid. Should be either x, y, a or b!") }
    {% endif %}

    {% if axis == "a" %}
        {% set axis = "1,-1" %}
    {% elif axis == "b" %}
        {% set axis = "1,1" %}
    {% endif %}

    TEST_RESONANCES OUTPUT=raw_data AXIS={axis} FREQ_START={frequency-1} FREQ_END={frequency+1} HZ_PER_SEC={1/(time/3)}
    M400
