From 32047dbdba029d4e11822ab62888b0f64a4969ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Boisselier?= Date: Fri, 5 Jan 2024 00:04:35 +0100 Subject: [PATCH] allow better script behavior customization from the macros --- K-ShakeTune/K-SnT_axes_map.cfg | 2 +- K-ShakeTune/K-SnT_axis.cfg | 10 ++- K-ShakeTune/K-SnT_belts.cfg | 6 +- K-ShakeTune/K-SnT_vibrations.cfg | 7 +- K-ShakeTune/scripts/is_workflow.py | 105 ++++++++++++++++++----------- docs/macros/axis_tuning.md | 2 + docs/macros/belts_tuning.md | 2 + docs/macros/vibrations_tuning.md | 2 + 8 files changed, 93 insertions(+), 43 deletions(-) diff --git a/K-ShakeTune/K-SnT_axes_map.cfg b/K-ShakeTune/K-SnT_axes_map.cfg index 81bcfeb..5de5a30 100644 --- a/K-ShakeTune/K-SnT_axes_map.cfg +++ b/K-ShakeTune/K-SnT_axes_map.cfg @@ -52,7 +52,7 @@ gcode: ACCELEROMETER_MEASURE CHIP={accel_chip} NAME=axemap RESPOND MSG="Analysis of the movements..." - RUN_SHELL_COMMAND CMD=shaketune PARAMS="AXESMAP {accel}" + RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type axesmap --accel {accel}" # Restore the previous acceleration values SET_VELOCITY_LIMIT ACCEL={old_accel} ACCEL_TO_DECEL={old_accel_to_decel} SQUARE_CORNER_VELOCITY={old_sqv} diff --git a/K-ShakeTune/K-SnT_axis.cfg b/K-ShakeTune/K-SnT_axis.cfg index 5fbb589..8d7d777 100644 --- a/K-ShakeTune/K-SnT_axis.cfg +++ b/K-ShakeTune/K-SnT_axis.cfg @@ -10,6 +10,8 @@ gcode: {% 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 keep_results = params.KEEP_N_RESULTS|default(3)|int %} + {% set keep_csv = params.KEEP_CSV|default(True) %} {% set X, Y = False, False %} @@ -29,7 +31,9 @@ gcode: RESPOND MSG="X axis frequency profile generation..." RESPOND MSG="This may take some time (1-3min)" - RUN_SHELL_COMMAND CMD=shaketune PARAMS=SHAPER + RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type shaper {% if keep_csv %}--keep_csv{% endif %}" + M400 + RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type clean --keep_results {keep_results}" {% endif %} {% if Y %} @@ -38,5 +42,7 @@ gcode: RESPOND MSG="Y axis frequency profile generation..." RESPOND MSG="This may take some time (1-3min)" - RUN_SHELL_COMMAND CMD=shaketune PARAMS=SHAPER + RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type shaper {% if keep_csv %}--keep_csv{% endif %}" + M400 + RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type clean --keep_results {keep_results}" {% endif %} diff --git a/K-ShakeTune/K-SnT_belts.cfg b/K-ShakeTune/K-SnT_belts.cfg index 059efea..47bdedc 100644 --- a/K-ShakeTune/K-SnT_belts.cfg +++ b/K-ShakeTune/K-SnT_belts.cfg @@ -9,6 +9,8 @@ gcode: {% 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 keep_results = params.KEEP_N_RESULTS|default(3)|int %} + {% set keep_csv = params.KEEP_CSV|default(True) %} 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 @@ -18,4 +20,6 @@ gcode: RESPOND MSG="Belts comparative frequency profile generation..." RESPOND MSG="This may take some time (3-5min)" - RUN_SHELL_COMMAND CMD=shaketune PARAMS=BELTS + RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type belts {% if keep_csv %}--keep_csv{% endif %}" + M400 + RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type clean --keep_results {keep_results}" diff --git a/K-ShakeTune/K-SnT_vibrations.cfg b/K-ShakeTune/K-SnT_vibrations.cfg index b625390..7eb8642 100644 --- a/K-ShakeTune/K-SnT_vibrations.cfg +++ b/K-ShakeTune/K-SnT_vibrations.cfg @@ -16,6 +16,9 @@ gcode: {% set accel = params.ACCEL|default(3000)|int %} # accel value used to move on the pattern {% set accel_chip = params.ACCEL_CHIP|default("adxl345") %} # ADXL chip name in the config + {% set keep_results = params.KEEP_N_RESULTS|default(3)|int %} + {% set keep_csv = params.KEEP_CSV|default(True) %} + {% set mid_x = printer.toolhead.axis_maximum.x|float / 2 %} {% set mid_y = printer.toolhead.axis_maximum.y|float / 2 %} {% set nb_samples = ((max_speed - min_speed) / speed_increment + 1) | int %} @@ -153,7 +156,9 @@ gcode: RESPOND MSG="Machine and motors vibration graph generation..." RESPOND MSG="This may take some time (3-5min)" - RUN_SHELL_COMMAND CMD=shaketune PARAMS="VIBRATIONS {direction} {accel}" + RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type vibrations --axis_name {direction} --accel {accel} {% if keep_csv %}--keep_csv{% endif %}" + M400 + RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type clean --keep_results {keep_results}" # Restore the previous acceleration values SET_VELOCITY_LIMIT ACCEL={old_accel} ACCEL_TO_DECEL={old_accel_to_decel} SQUARE_CORNER_VELOCITY={old_sqv} diff --git a/K-ShakeTune/scripts/is_workflow.py b/K-ShakeTune/scripts/is_workflow.py index 14459a1..54c6e2b 100755 --- a/K-ShakeTune/scripts/is_workflow.py +++ b/K-ShakeTune/scripts/is_workflow.py @@ -5,14 +5,11 @@ ############################################ # Written by Frix_x#0161 # -# Usage: -# This script was designed to be used with gcode_shell_commands directly from Klipper -# Parameters availables: -# BELTS - To generate belts diagrams after calling the Klipper TEST_RESONANCES AXIS=1,(-)1 OUTPUT=raw_data -# SHAPER - To generate input shaper diagrams after calling the Klipper TEST_RESONANCES AXIS=X/Y OUTPUT=raw_data -# VIBRATIONS - To generate vibration diagram after calling the custom (Frix_x#0161) VIBRATIONS_CALIBRATION macro +# This script is designed to be used with gcode_shell_commands directly from Klipper +# Use the provided Shake&Tune macros instead! +import optparse import os import time import glob @@ -24,7 +21,6 @@ from datetime import datetime ################################################################################################################# RESULTS_FOLDER = os.path.expanduser('~/printer_data/config/K-ShakeTune_results') KLIPPER_FOLDER = os.path.expanduser('~/klipper') -STORE_RESULTS = 3 ################################################################################################################# from graph_belts import belts_calibration @@ -51,7 +47,7 @@ def is_file_open(filepath): return False -def create_belts_graph(): +def create_belts_graph(keep_csv): current_date = datetime.now().strftime('%Y%m%d_%H%M%S') lognames = [] @@ -86,12 +82,18 @@ def create_belts_graph(): # Generate the belts graph and its name fig = belts_calibration(lognames, KLIPPER_FOLDER) png_filename = os.path.join(RESULTS_FOLDER, RESULTS_SUBFOLDERS[0], f'belts_{current_date}.png') + fig.savefig(png_filename, dpi=150) + + # Remove the CSV files if the user don't want to keep them + if not keep_csv: + for csv in lognames: + if os.path.exists(csv): + os.remove(csv) - fig.savefig(png_filename) return -def create_shaper_graph(): +def create_shaper_graph(keep_csv): current_date = datetime.now().strftime('%Y%m%d_%H%M%S') # Get all the files and sort them based on last modified time to select the most recent one @@ -120,12 +122,17 @@ def create_shaper_graph(): # Generate the shaper graph and its name fig = shaper_calibration([new_file], KLIPPER_FOLDER) png_filename = os.path.join(RESULTS_FOLDER, RESULTS_SUBFOLDERS[1], f'resonances_{current_date}_{axis}.png') + fig.savefig(png_filename, dpi=150) + + # Remove the CSV file if the user don't want to keep it + if not keep_csv: + if os.path.exists(new_file): + os.remove(new_file) - fig.savefig(png_filename) return -def create_vibrations_graph(axis_name, accel): +def create_vibrations_graph(axis_name, accel, keep_csv): current_date = datetime.now().strftime('%Y%m%d_%H%M%S') lognames = [] @@ -157,14 +164,19 @@ def create_vibrations_graph(axis_name, accel): # Generate the vibration graph and its name fig = vibrations_calibration(lognames, KLIPPER_FOLDER, axis_name, accel) png_filename = os.path.join(RESULTS_FOLDER, RESULTS_SUBFOLDERS[2], f'vibrations_{current_date}_{axis_name}.png') + fig.savefig(png_filename, dpi=150) - # Archive all the csv files in a tarball and remove them to clean up the results folder - with tarfile.open(os.path.join(RESULTS_FOLDER, RESULTS_SUBFOLDERS[2], f'vibrations_{current_date}_{axis_name}.tar.gz'), 'w:gz') as tar: - for csv_file in glob.glob(os.path.join(RESULTS_FOLDER, RESULTS_SUBFOLDERS[2], f'vibr_{current_date}*.csv')): - tar.add(csv_file, recursive=False) + # Archive all the csv files in a tarball in case the user want to keep them + if keep_csv: + with tarfile.open(os.path.join(RESULTS_FOLDER, RESULTS_SUBFOLDERS[2], f'vibrations_{current_date}_{axis_name}.tar.gz'), 'w:gz') as tar: + for csv_file in lognames: + tar.add(csv_file, recursive=False) + + # Remove the remaining CSV files not needed anymore (tarball is safe if it was created) + for csv_file in lognames: + if os.path.exists(csv_file): os.remove(csv_file) - fig.savefig(png_filename) return @@ -201,10 +213,10 @@ def get_old_files(folder, extension, limit): files.sort(key=lambda x: os.path.getmtime(x), reverse=True) return files[limit:] -def clean_files(): +def clean_files(keep_results): # Define limits based on STORE_RESULTS - keep1 = STORE_RESULTS + 1 - keep2 = 2 * STORE_RESULTS + 1 + keep1 = keep_results + 1 + keep2 = 2 * keep_results + 1 # Find old files in each directory old_belts_files = get_old_files(os.path.join(RESULTS_FOLDER, RESULTS_SUBFOLDERS[0]), '.png', keep1) @@ -236,30 +248,47 @@ def clean_files(): def main(): - # Check if results folders are there or create them + # Parse command-line arguments + usage = "%prog [options] " + opts = optparse.OptionParser(usage) + opts.add_option("-t", "--type", type="string", dest="type", + default=None, help="type of output graph to produce") + opts.add_option("-a", "--accel", type="int", default=None, dest="accel_used", + help="acceleration used during the vibration macro or axesmap macro") + opts.add_option("-x", "--axis_name", type="string", default=None, dest="axis_name", + help="axis tested during the vibration macro") + opts.add_option("-n", "--keep_results", type="int", default=3, dest="keep_results", + help="number of results to keep in the result folder after each run of the script") + opts.add_option("-c", "--keep_csv", action="store_true", default=False, dest="keep_csv", + help="weither or not to keep the CSV files alongside the PNG graphs image results") + options, args = opts.parse_args() + if len(args) < 1: + opts.error("Incorrect number of arguments") + + if options.type is None: + opts.error("You must specify the type of output graph you want to produce (option -t)") + elif options.type.lower() not in ['belts', 'shaper', 'vibrations', 'axesmap', 'clean']: + opts.error("Type of output graph need to be in the list of 'belts', 'shaper', 'vibrations', 'axesmap' or 'clean'") + else: + graph_mode = options.type + + # Check if results folders are there or create them before doing anything else for result_subfolder in RESULTS_SUBFOLDERS: folder = os.path.join(RESULTS_FOLDER, result_subfolder) if not os.path.exists(folder): os.makedirs(folder) - if len(sys.argv) < 2: - print("Usage: is_workflow.py [BELTS|SHAPER|VIBRATIONS|AXESMAP]") - sys.exit(1) + if graph_mode.lower() == 'belts': + create_belts_graph(keep_csv=options.keep_csv) + elif graph_mode.lower() == 'shaper': + create_shaper_graph(keep_csv=options.keep_csv) + elif graph_mode.lower() == 'vibrations': + create_vibrations_graph(axis_name=options.axis_name, accel=options.accel_used, keep_csv=options.keep_csv) + elif graph_mode.lower() == 'axesmap': + find_axesmap(accel=options.accel_used) + elif graph_mode.lower() == 'clean': + clean_files(keep_results=options.keep_results) - if sys.argv[1].lower() == 'belts': - create_belts_graph() - elif sys.argv[1].lower() == 'shaper': - create_shaper_graph() - elif sys.argv[1].lower() == 'vibrations': - create_vibrations_graph(axis_name=sys.argv[2], accel=sys.argv[3]) - elif sys.argv[1].lower() == 'axesmap': - find_axesmap(accel=sys.argv[2]) - else: - print("Usage: is_workflow.py [BELTS|SHAPER|VIBRATIONS|AXESMAP]") - sys.exit(1) - - - clean_files() print(f"Graphs created. You will find the results in {RESULTS_FOLDER}") diff --git a/docs/macros/axis_tuning.md b/docs/macros/axis_tuning.md index 54ce273..f41e8bf 100644 --- a/docs/macros/axis_tuning.md +++ b/docs/macros/axis_tuning.md @@ -15,6 +15,8 @@ Then, call the `AXES_SHAPER_CALIBRATION` macro and look for the graphs in the re |FREQ_END|133|Maximum excitation frequency| |HZ_PER_SEC|1|Number of Hz per seconds for the test| |AXIS|"all"|Axis you want to test in the list of "all", "X" or "Y"| +|KEEP_N_RESULTS|3|Total number of results to keep in the result folder after running the test. The older results are automatically cleaned up| +|KEEP_CSV|True|Weither or not to keep the CSV data file alonside the PNG graphs| ## Graphs description diff --git a/docs/macros/belts_tuning.md b/docs/macros/belts_tuning.md index fc6551a..162ba0b 100644 --- a/docs/macros/belts_tuning.md +++ b/docs/macros/belts_tuning.md @@ -14,6 +14,8 @@ Then, call the `BELTS_SHAPER_CALIBRATION` macro and look for the graphs in the r |FREQ_START|5|Starting excitation frequency| |FREQ_END|133|Maximum excitation frequency| |HZ_PER_SEC|1|Number of Hz per seconds for the test| +|KEEP_N_RESULTS|3|Total number of results to keep in the result folder after running the test. The older results are automatically cleaned up| +|KEEP_CSV|True|Weither or not to keep the CSV data files alonside the PNG graphs| ## Graphs description diff --git a/docs/macros/vibrations_tuning.md b/docs/macros/vibrations_tuning.md index 530de31..c50a676 100644 --- a/docs/macros/vibrations_tuning.md +++ b/docs/macros/vibrations_tuning.md @@ -22,6 +22,8 @@ Call the `VIBRATIONS_CALIBRATION` macro with the direction and speed range you w |SPEED_INCREMENT|2|speed increments of the toolhead in mm/s between every movements| |TRAVEL_SPEED|200|speed in mm/s used for all the travels moves| |ACCEL_CHIP|"adxl345"|accelerometer chip name in the config| +|KEEP_N_RESULTS|3|Total number of results to keep in the result folder after running the test. The older results are automatically cleaned up| +|KEEP_CSV|True|Weither or not to keep the CSV data files alonside the PNG graphs (archived in a tarball)| ## Graphs description