fixed S&T thread and potential timer too close errors

This commit is contained in:
Félix Boisselier
2024-05-24 14:49:27 +02:00
parent 0e96b36703
commit ee9d9f994a
4 changed files with 31 additions and 21 deletions

View File

@@ -40,6 +40,8 @@ Follow these steps to install the Shake&Tune module in your printer:
# show_macros_in_webui: True
# Mainsail and Fluidd doesn't create buttons for "system" macros that are not in the
# printer.cfg file. If you want to see the macros in the webui, set this to True.
# timeout: 300
# The maximum time in seconds to let Shake&Tune process the CSV files and generate the graphs.
```
## Usage

View File

@@ -84,9 +84,7 @@ def axes_shaper_calibration(gcmd, config, st_thread: ShakeTuneThread) -> None:
# First we need to find the accelerometer chip suited for the axis
accel_chip = Accelerometer.find_axis_accelerometer(printer, config['axis'])
if accel_chip is None:
gcmd.error(
'No suitable accelerometer found for measurement! Multi-accelerometer configurations are not supported for this macro.'
)
gcmd.error('No suitable accelerometer found for measurement!')
accelerometer = Accelerometer(printer.lookup_object(accel_chip))
# Then do the actual measurements
@@ -98,6 +96,7 @@ def axes_shaper_calibration(gcmd, config, st_thread: ShakeTuneThread) -> None:
ConsoleOutput.print(f'{config["axis"].upper()} axis frequency profile generation...')
ConsoleOutput.print('This may take some time (1-3min)')
st_thread.run()
st_thread.wait_for_completion()
# Re-enable the input shaper if it was active
if input_shaper is not None:

View File

@@ -27,7 +27,7 @@ class ShakeTune:
if res_tester is None:
config.error('No [resonance_tester] config section found in printer.cfg! Please add one to use Shake&Tune.')
self.timeout = config.getfloat('timeout', 2.0, above=0.0)
self.timeout = config.getfloat('timeout', 300, above=0.0)
result_folder = config.get('result_folder', default='~/printer_data/config/ShakeTune_results')
result_folder_path = Path(result_folder).expanduser() if result_folder else None
keep_n_results = config.getint('number_of_results_to_keep', default=3, minval=0)
@@ -108,23 +108,23 @@ class ShakeTune:
def cmd_AXES_MAP_CALIBRATION(self, gcmd) -> None:
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
axes_map_finder = AxesMapFinder(self._config)
st_thread = ShakeTuneThread(self._config, axes_map_finder, self._printer.get_reactor(), self.timeout)
st_thread = ShakeTuneThread(self._config, axes_map_finder, self.timeout)
axes_map_calibration(gcmd, self._pconfig, st_thread)
def cmd_COMPARE_BELTS_RESPONSES(self, gcmd) -> None:
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
belt_graph_creator = BeltsGraphCreator(self._config)
st_thread = ShakeTuneThread(self._config, belt_graph_creator, self._printer.get_reactor(), self.timeout)
st_thread = ShakeTuneThread(self._config, belt_graph_creator, self.timeout)
compare_belts_responses(gcmd, self._pconfig, st_thread)
def cmd_AXES_SHAPER_CALIBRATION(self, gcmd) -> None:
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
shaper_graph_creator = ShaperGraphCreator(self._config)
st_thread = ShakeTuneThread(self._config, shaper_graph_creator, self._printer.get_reactor(), self.timeout)
st_thread = ShakeTuneThread(self._config, shaper_graph_creator, self.timeout)
axes_shaper_calibration(gcmd, self._pconfig, st_thread)
def cmd_CREATE_VIBRATIONS_PROFILE(self, gcmd) -> None:
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
vibration_profile_creator = VibrationsGraphCreator(self._config)
st_thread = ShakeTuneThread(self._config, vibration_profile_creator, self._printer.get_reactor(), self.timeout)
st_thread = ShakeTuneThread(self._config, vibration_profile_creator, self.timeout)
create_vibrations_profile(gcmd, self._pconfig, st_thread)

View File

@@ -4,6 +4,7 @@
import os
import threading
import traceback
from typing import Optional
from .helpers import filemanager as fm
from .helpers.console_output import ConsoleOutput
@@ -11,33 +12,41 @@ from .shaketune_config import ShakeTuneConfig
class ShakeTuneThread(threading.Thread):
def __init__(self, config: ShakeTuneConfig, graph_creator, reactor, timeout: float):
def __init__(self, config: ShakeTuneConfig, graph_creator, timeout: Optional[float] = None) -> None:
super(ShakeTuneThread, self).__init__()
self._config = config
self.graph_creator = graph_creator
self._reactor = reactor
self._timeout = timeout
self._internal_thread = None
self._stop_event = threading.Event()
def get_graph_creator(self):
return self.graph_creator
def run(self) -> None:
# Start the target function in a new thread
internal_thread = threading.Thread(target=self._shaketune_thread, args=(self.graph_creator,))
internal_thread.start()
self._internal_thread = threading.Thread(target=self._shaketune_thread, args=(self.graph_creator,))
self._internal_thread.start()
# Monitor the thread execution and stop it if it takes too long
event_time = self._reactor.monotonic()
end_time = event_time + self._timeout
while event_time < end_time:
event_time = self._reactor.pause(event_time + 0.05)
if not internal_thread.is_alive():
break
# If a timeout is specified, start a timer thread to monitor the timeout
if self._timeout is not None:
timer_thread = threading.Timer(self._timeout, self._handle_timeout)
timer_thread.start()
# This function run in its own thread is used to do the CSV analysis and create the graphs
def _handle_timeout(self) -> None:
if self._internal_thread.is_alive():
self._stop_event.set()
ConsoleOutput.print('Timeout: Shake&Tune computation did not finish within the specified timeout!')
def wait_for_completion(self) -> None:
if self._internal_thread is not None:
self._internal_thread.join()
# This function run in a thread is used to do the CSV analysis and create the graphs
def _shaketune_thread(self, graph_creator) -> None:
# Trying to reduce the Shake&Tune prost-processing thread priority to avoid slowing down the main Klipper process
# as this could lead to random "Timer" errors when already running CANbus, etc...
# as this could lead to random "Timer too close" errors when already running CANbus, etc...
try:
os.nice(20)
except Exception: