########################################
# Improved Shapers Configurations
########################################

[respond]

[calibrate_shaper_config]


[gcode_shell_command resonance_graph]
command: /usr/data/printer_data/config/Helper-Script/improved-shapers/scripts/calibrate_shaper.py
timeout: 600.0
verbose: False


[gcode_shell_command belts_graph]
command: /usr/data/printer_data/config/Helper-Script/improved-shapers/scripts/graph_belts.py
timeout: 600.0
verbose: False


[gcode_shell_command delete_graph]
command: sh /usr/data/helper-script/files/improved-shapers/delete_graph.sh
timeout: 600.0
verbose: False


[gcode_shell_command delete_csv]
command: sh /usr/data/helper-script/files/improved-shapers/delete_csv.sh
timeout: 600.0
verbose: False


[gcode_macro INPUT_SHAPER_CALIBRATION]
description: Measure X and Y Axis Resonances and Save values
gcode:
  SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=soc_fan TARGET=30
  {% if printer.toolhead.homed_axes != "xyz" %}
    RESPOND TYPE=command MSG="Homing..."
    G28
  {% endif %}
  RESPOND TYPE=command MSG="Measuring X and Y Resonances..."
  SHAPER_CALIBRATE
  M400
  SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=soc_fan TARGET=45
  RUN_SHELL_COMMAND CMD=delete_csv
  RESPOND TYPE=command MSG="Input Shaper Calibration complete!"
  SAVE_CONFIG


[gcode_macro TEST_RESONANCES_GRAPHS]
description: Test X and Y Axis Resonances and Generate Graphs
gcode:
  RUN_SHELL_COMMAND CMD=delete_graph
  {% set x_png = params.X_PNG|default("/usr/data/printer_data/config/Helper-Script/improved-shapers/resonances_x.png") %}
  {% set y_png = params.Y_PNG|default("/usr/data/printer_data/config/Helper-Script/improved-shapers/resonances_y.png") %}
  SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=soc_fan TARGET=30
  {% if printer.toolhead.homed_axes != "xyz" %}
    RESPOND TYPE=command MSG="Homing..."
    G28
  {% endif %}
  RESPOND TYPE=command MSG="Testing X Resonances..."
  TEST_RESONANCES AXIS=X NAME=x
  M400
  RESPOND TYPE=command MSG="Generating X Graph... This may take some time."
  RUN_SHELL_COMMAND CMD=resonance_graph PARAMS="/tmp/resonances_x_x.csv -o {x_png}"
  RESPOND TYPE=command MSG="X Graph (resonances_x.png) is now available in /Helper-Script/improved-shapers folder."
  RESPOND TYPE=command MSG="Testing Y Resonances..."
  TEST_RESONANCES AXIS=Y NAME=y
  M400
  RESPOND TYPE=command MSG="Generating Y Graph... This may take some time."
  RUN_SHELL_COMMAND CMD=resonance_graph PARAMS="/tmp/resonances_y_y.csv -o {y_png}"
  RESPOND TYPE=command MSG="Y Graph (resonances_y.png) is now available in /Helper-Script/improved-shapers folder."
  SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=soc_fan TARGET=45
  RUN_SHELL_COMMAND CMD=delete_csv
  RESPOND TYPE=command MSG="Resonances Test complete!"


[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:
  RUN_SHELL_COMMAND CMD=delete_graph
  {% 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 %}
  {% set png_width = params.PNG_WIDTH|default(8)|float %}
  {% set png_height = params.PNG_HEIGHT|default(4.8)|float %}
  {% set png_out_path = params.PNG_OUT_PATH|default("/usr/data/printer_data/config/Helper-Script/improved-shapers/belts_calibration.png") %}
  SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=soc_fan TARGET=30
  {% if printer.toolhead.homed_axes != "xyz" %}
    RESPOND TYPE=command MSG="Homing..."
    G28
  {% endif %}
  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
  RESPOND TYPE=command MSG="Generating Belts Frequency Profiles Graph... This may take some time."
  RUN_SHELL_COMMAND CMD=belts_graph PARAMS="-w {png_width} -l {png_height} -n -o {png_out_path} -k /usr/share/klipper /tmp/raw_data_axis=1.000,-1.000_a.csv /tmp/raw_data_axis=1.000,1.000_b.csv"
  RESPOND TYPE=command MSG="Graph (belts_calibration.png) is now available in /Helper-Script/improved-shapers folder."
  SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=soc_fan TARGET=45
  RUN_SHELL_COMMAND CMD=delete_csv
  RESPOND TYPE=command MSG="Belts Shaper Calibration complete!"


[gcode_macro EXCITATE_AXIS_AT_FREQ]
description: Maintain a specified excitation frequency for a period of time to diagnose and locate a vibration source
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 is 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 %}
  {% if printer.toolhead.homed_axes != "xyz" %}
    RESPOND TYPE=command MSG="Homing..."
    G28
  {% endif %}
  TEST_RESONANCES OUTPUT=raw_data AXIS={axis} FREQ_START={frequency-1} FREQ_END={frequency+1} HZ_PER_SEC={1/(time/3)}
  M400
