Skip to content

Commit

Permalink
added a MAX_SCALE parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Frix-x committed Dec 29, 2024
1 parent f07809f commit db578c6
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/macros/axes_shaper_calibrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Then, call the `AXES_SHAPER_CALIBRATION` macro and look for the graphs in the re
|MAX_SMOOTHING|None|max smoothing allowed when calculating shaper recommendations|
|TRAVEL_SPEED|120|speed in mm/s used for all the travel movements (to go to the start position prior to the test)|
|Z_HEIGHT|None|Z height wanted for the test. This value can be used if needed to override the Z value of the probe_point set in your `[resonance_tester]` config section|
|MAX_SCALE|None|Maximum energy value to scale the input shaper graph. Useful for comparing multiple consecutive tests by forcing the same energy scale|


> **Note**
>
Expand Down
1 change: 1 addition & 0 deletions docs/macros/compare_belts_responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Then, call the `COMPARE_BELTS_RESPONSES` macro and look for the graphs in the re
|ACCEL_PER_HZ|None (default to `[resonance_tester]` value)|accel per Hz value used for the test|
|TRAVEL_SPEED|120|speed in mm/s used for all the travel movements (to go to the start position prior to the test)|
|Z_HEIGHT|None|Z height wanted for the test. This value can be used if needed to override the Z value of the probe_point set in your `[resonance_tester]` config section|
|MAX_SCALE|None|Maximum energy value to scale the belts graph. Useful for comparing multiple consecutive tests by forcing the same energy scale|

> **Note**
>
Expand Down
15 changes: 13 additions & 2 deletions shaketune/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ def configure_graph_creator(graph_type, args, dummy_config):
elif graph_type == 'static frequency':
config_kwargs |= {'accel_per_hz': args.accel_per_hz, 'freq': args.frequency, 'duration': args.duration}
elif graph_type == 'belts comparison':
config_kwargs |= {'kinematics': args.kinematics, 'accel_per_hz': args.accel_per_hz}
config_kwargs |= {'kinematics': args.kinematics, 'accel_per_hz': args.accel_per_hz, 'max_scale': args.max_scale}
elif graph_type == 'input shaper':
config_kwargs |= {'scv': args.scv, 'max_smoothing': args.max_smoothing, 'accel_per_hz': args.accel_per_hz}
config_kwargs |= {
'scv': args.scv,
'max_smoothing': args.max_smoothing,
'accel_per_hz': args.accel_per_hz,
'max_scale': args.max_scale,
}
elif graph_type == 'vibrations profile':
config_kwargs |= {'kinematics': args.kinematics, 'accel': args.accel}

Expand Down Expand Up @@ -70,6 +75,9 @@ def main():
belts_parser.add_argument('-k', '--klipper_dir', default='~/klipper', help='Main klipper directory')
belts_parser.add_argument('--kinematics', help='Machine kinematics configuration')
belts_parser.add_argument('--accel_per_hz', type=float, help='Accel per Hz used during the measurement')
belts_parser.add_argument(
'--max_scale', type=lambda x: int(float(x)), help='Maximum energy value to scale the belts graph'
)

# Input Shaper graph parser
shaper_parser = subparsers.add_parser('input_shaper', help='Create input shaper graph')
Expand All @@ -78,6 +86,9 @@ def main():
shaper_parser.add_argument('--scv', type=float, default=5.0, help='Square corner velocity')
shaper_parser.add_argument('--max_smoothing', type=float, help='Maximum shaper smoothing to allow')
shaper_parser.add_argument('--accel_per_hz', type=float, help='Accel per Hz used during the measurement')
shaper_parser.add_argument(
'--max_scale', type=lambda x: int(float(x)), help='Maximum energy value to scale the input shaper graph'
)

# Vibrations graph parser
vibrations_parser = subparsers.add_parser('vibrations', help='Create vibrations profile graph')
Expand Down
3 changes: 2 additions & 1 deletion shaketune/commands/axes_shaper_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def axes_shaper_calibration(gcmd, config, st_process: ShakeTuneProcess) -> None:
max_sm = gcmd.get_float('MAX_SMOOTHING', default=None, minval=0)
feedrate_travel = gcmd.get_float('TRAVEL_SPEED', default=120.0, minval=20.0)
z_height = gcmd.get_float('Z_HEIGHT', default=None, minval=1)
max_scale = gcmd.get_int('MAX_SCALE', default=None, minval=1)

if accel_per_hz == '':
accel_per_hz = None
Expand Down Expand Up @@ -74,7 +75,7 @@ def axes_shaper_calibration(gcmd, config, st_process: ShakeTuneProcess) -> None:

# Configure the graph creator
creator = st_process.get_graph_creator()
creator.configure(scv, max_sm, accel_per_hz)
creator.configure(scv, max_sm, accel_per_hz, max_scale)

# set the needed acceleration values for the test
toolhead_info = toolhead.get_status(systime)
Expand Down
3 changes: 2 additions & 1 deletion shaketune/commands/compare_belts_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def compare_belts_responses(gcmd, config, st_process: ShakeTuneProcess) -> None:
accel_per_hz = gcmd.get_float('ACCEL_PER_HZ', default=None)
feedrate_travel = gcmd.get_float('TRAVEL_SPEED', default=120.0, minval=20.0)
z_height = gcmd.get_float('Z_HEIGHT', default=None, minval=1)
max_scale = gcmd.get_int('MAX_SCALE', default=None, minval=1)

if accel_per_hz == '':
accel_per_hz = None
Expand All @@ -47,7 +48,7 @@ def compare_belts_responses(gcmd, config, st_process: ShakeTuneProcess) -> None:
# Configure the graph creator
motors_config_parser = MotorsConfigParser(config, motors=None)
creator = st_process.get_graph_creator()
creator.configure(motors_config_parser.kinematics, accel_per_hz)
creator.configure(motors_config_parser.kinematics, accel_per_hz, max_scale)

if motors_config_parser.kinematics in {'corexy', 'limited_corexy'}:
filtered_config = [a for a in AXIS_CONFIG if a['axis'] in ('a', 'b')]
Expand Down
8 changes: 6 additions & 2 deletions shaketune/dummy_macros.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ gcode:
{% set accel_per_hz = params.ACCEL_PER_HZ %}
{% set travel_speed = params.TRAVEL_SPEED|default(120) %}
{% set z_height = params.Z_HEIGHT %}
{% set max_scale = params.MAX_SCALE %}
{% set params_filtered = {
"FREQ_START": freq_start if freq_start is not none else '',
"FREQ_END": freq_end if freq_end is not none else '',
"HZ_PER_SEC": hz_per_sec,
"ACCEL_PER_HZ": accel_per_hz if accel_per_hz is not none else '',
"TRAVEL_SPEED": travel_speed,
"Z_HEIGHT": z_height if z_height is not none else ''
"Z_HEIGHT": z_height if z_height is not none else '',
"MAX_SCALE": max_scale if max_scale is not none else ''
} %}
_COMPARE_BELTS_RESPONSES {% for key, value in params_filtered.items() if value is defined and value is not none and value != '' %}{key}={value} {% endfor %}

Expand All @@ -74,6 +76,7 @@ gcode:
{% set max_smoothing = params.MAX_SMOOTHING %}
{% set travel_speed = params.TRAVEL_SPEED|default(120) %}
{% set z_height = params.Z_HEIGHT %}
{% set max_scale = params.MAX_SCALE %}
{% set params_filtered = {
"FREQ_START": freq_start if freq_start is not none else '',
"FREQ_END": freq_end if freq_end is not none else '',
Expand All @@ -83,7 +86,8 @@ gcode:
"SCV": scv if scv is not none else '',
"MAX_SMOOTHING": max_smoothing if max_smoothing is not none else '',
"TRAVEL_SPEED": travel_speed,
"Z_HEIGHT": z_height if z_height is not none else ''
"Z_HEIGHT": z_height if z_height is not none else '',
"MAX_SCALE": max_scale if max_scale is not none else ''
} %}
_AXES_SHAPER_CALIBRATION {% for key, value in params_filtered.items() if value is defined and value is not none and value != '' %}{key}={value} {% endfor %}

Expand Down
9 changes: 8 additions & 1 deletion shaketune/graph_creators/belts_graph_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ def __init__(self, config: ShakeTuneConfig):
self._kinematics: Optional[str] = None
self._accel_per_hz: Optional[float] = None

def configure(self, kinematics: Optional[str] = None, accel_per_hz: Optional[float] = None) -> None:
def configure(
self, kinematics: Optional[str] = None, accel_per_hz: Optional[float] = None, max_scale: Optional[int] = None
) -> None:
self._kinematics = kinematics
self._accel_per_hz = accel_per_hz
self._max_scale = max_scale

def create_graph(self, measurements_manager: MeasurementsManager) -> None:
computer = BeltsGraphComputation(
measurements=measurements_manager.get_measurements(),
kinematics=self._kinematics,
max_freq=self._config.max_freq,
accel_per_hz=self._accel_per_hz,
max_scale=self._max_scale,
st_version=self._version,
)
computation = computer.compute()
Expand Down Expand Up @@ -70,12 +74,14 @@ def __init__(
kinematics: Optional[str],
max_freq: float,
accel_per_hz: Optional[float],
max_scale: Optional[int],
st_version: str,
):
self.measurements = measurements
self.kinematics = kinematics
self.max_freq = max_freq
self.accel_per_hz = accel_per_hz
self.max_scale = max_scale
self.st_version = st_version

def compute(self):
Expand Down Expand Up @@ -132,6 +138,7 @@ def compute(self):
'st_version': self.st_version,
'measurements': self.measurements,
'max_freq': self.max_freq,
'max_scale': self.max_scale,
}

def _compute_signal_data(self, data: np.ndarray, common_freqs: np.ndarray, max_freq: float):
Expand Down
6 changes: 4 additions & 2 deletions shaketune/graph_creators/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ def plot_belts_graph(self, data):
st_version = data['st_version']
measurements = data['measurements']
max_freq = data['max_freq']
max_scale = data['max_scale']

fig, axes = plt.subplots(
1,
Expand Down Expand Up @@ -401,7 +402,7 @@ def plot_belts_graph(self, data):
ax_1.plot(signal2.freqs, signal2.psd, label='Belt ' + signal2_belt, color=self.KLIPPAIN_COLORS['purple'])
psd_highest_max = max(signal1.psd.max(), signal2.psd.max())
ax_1.set_xlim([0, max_freq])
ax_1.set_ylim([0, psd_highest_max * 1.1])
ax_1.set_ylim([0, max_scale if max_scale is not None else psd_highest_max * 1.1])

# Annotate peaks
paired_peak_count = 0
Expand Down Expand Up @@ -645,6 +646,7 @@ def plot_input_shaper_graph(self, data):
max_smoothing = data['max_smoothing']
scv = data['scv']
st_version = data['st_version']
max_scale = data['max_scale']

fig = plt.figure(figsize=(15, 11.6))
gs = fig.add_gridspec(
Expand Down Expand Up @@ -712,7 +714,7 @@ def plot_input_shaper_graph(self, data):
ax_1.plot(freqs, py, label='Y', color='green')
ax_1.plot(freqs, pz, label='Z', color='blue')
ax_1.set_xlim([0, max_freq])
ax_1.set_ylim([0, psd.max() * 1.05])
ax_1.set_ylim([0, max_scale if max_scale is not None else psd.max() * 1.05])

ax_1_2 = ax_1.twinx()
ax_1_2.yaxis.set_visible(False)
Expand Down
11 changes: 10 additions & 1 deletion shaketune/graph_creators/shaper_graph_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ def __init__(self, config: ShakeTuneConfig):
self._accel_per_hz: Optional[float] = None

def configure(
self, scv: float, max_smoothing: Optional[float] = None, accel_per_hz: Optional[float] = None
self,
scv: float,
max_smoothing: Optional[float] = None,
accel_per_hz: Optional[float] = None,
max_scale: Optional[int] = None,
) -> None:
self._scv = scv
self._max_smoothing = max_smoothing
self._accel_per_hz = accel_per_hz
self._max_scale = max_scale

def create_graph(self, measurements_manager: MeasurementsManager) -> None:
computer = ShaperGraphComputation(
Expand All @@ -58,6 +63,7 @@ def create_graph(self, measurements_manager: MeasurementsManager) -> None:
scv=self._scv,
accel_per_hz=self._accel_per_hz,
max_freq=self._config.max_freq,
max_scale=self._max_scale,
st_version=self._version,
)
computation = computer.compute()
Expand Down Expand Up @@ -86,13 +92,15 @@ def __init__(
scv: float,
max_smoothing: Optional[float],
max_freq: float,
max_scale: Optional[int],
st_version: str,
):
self.measurements = measurements
self.accel_per_hz = accel_per_hz
self.scv = scv
self.max_smoothing = max_smoothing
self.max_freq = max_freq
self.max_scale = max_scale
self.st_version = st_version

def compute(self):
Expand Down Expand Up @@ -223,6 +231,7 @@ def compute(self):
'max_smoothing': self.max_smoothing,
'scv': self.scv,
'st_version': self.st_version,
'max_scale': self.max_scale,
}

# Find the best shaper parameters using Klipper's official algorithm selection with
Expand Down

0 comments on commit db578c6

Please sign in to comment.