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

# CHANGELOG:
#   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

# -------------------------------------------------------------------------------------------------------------------------
# If you want to use it into your own config, please install it as a standalone macro as described in the
# installation section of this file: docs > features > is_workflow.md
# -------------------------------------------------------------------------------------------------------------------------

### 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: Run standard input shaper test for all axes
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 %}

    TEST_RESONANCES AXIS=X FREQ_START={min_freq} FREQ_END={max_freq} HZ_PER_SEC={hz_per_sec}
    M400
    TEST_RESONANCES AXIS=Y FREQ_START={min_freq} FREQ_END={max_freq} HZ_PER_SEC={hz_per_sec}
    M400

    {% if verbose %}
        RESPOND MSG="Graphs generation... Please wait a minute or two and look in the configured folder."
    {% endif %}
    RUN_SHELL_COMMAND CMD=plot_graph PARAMS=SHAPER


[gcode_macro BELTS_SHAPER_CALIBRATION]
description: Run custom demi-axe test to analyze 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="Graphs generation... Please wait a minute or two and look in the configured folder."
    {% endif %}
    RUN_SHELL_COMMAND CMD=plot_graph PARAMS=BELTS


[gcode_macro EXCITATE_AXIS_AT_FREQ]
description: Maintain a specified input shaper excitating frequency for some time to diagnose vibrations
gcode:
    {% set FREQUENCY = params.FREQUENCY|default(25)|int %}
    {% set TIME = params.TIME|default(10)|int %}
    {% set AXIS = params.AXIS|default("x")|string|lower %}

    TEST_RESONANCES OUTPUT=raw_data AXIS={AXIS} FREQ_START={FREQUENCY-1} FREQ_END={FREQUENCY+1} HZ_PER_SEC={1/(TIME/3)}
    M400