fixed S&T thread and potential timer too close errors
This commit is contained in:
@@ -40,6 +40,8 @@ Follow these steps to install the Shake&Tune module in your printer:
|
|||||||
# show_macros_in_webui: True
|
# show_macros_in_webui: True
|
||||||
# Mainsail and Fluidd doesn't create buttons for "system" macros that are not in the
|
# 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.
|
# 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
|
## Usage
|
||||||
|
|||||||
@@ -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
|
# First we need to find the accelerometer chip suited for the axis
|
||||||
accel_chip = Accelerometer.find_axis_accelerometer(printer, config['axis'])
|
accel_chip = Accelerometer.find_axis_accelerometer(printer, config['axis'])
|
||||||
if accel_chip is None:
|
if accel_chip is None:
|
||||||
gcmd.error(
|
gcmd.error('No suitable accelerometer found for measurement!')
|
||||||
'No suitable accelerometer found for measurement! Multi-accelerometer configurations are not supported for this macro.'
|
|
||||||
)
|
|
||||||
accelerometer = Accelerometer(printer.lookup_object(accel_chip))
|
accelerometer = Accelerometer(printer.lookup_object(accel_chip))
|
||||||
|
|
||||||
# Then do the actual measurements
|
# 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(f'{config["axis"].upper()} axis frequency profile generation...')
|
||||||
ConsoleOutput.print('This may take some time (1-3min)')
|
ConsoleOutput.print('This may take some time (1-3min)')
|
||||||
st_thread.run()
|
st_thread.run()
|
||||||
|
st_thread.wait_for_completion()
|
||||||
|
|
||||||
# Re-enable the input shaper if it was active
|
# Re-enable the input shaper if it was active
|
||||||
if input_shaper is not None:
|
if input_shaper is not None:
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ class ShakeTune:
|
|||||||
if res_tester is None:
|
if res_tester is None:
|
||||||
config.error('No [resonance_tester] config section found in printer.cfg! Please add one to use Shake&Tune.')
|
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 = config.get('result_folder', default='~/printer_data/config/ShakeTune_results')
|
||||||
result_folder_path = Path(result_folder).expanduser() if result_folder else None
|
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)
|
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:
|
def cmd_AXES_MAP_CALIBRATION(self, gcmd) -> None:
|
||||||
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
|
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
|
||||||
axes_map_finder = AxesMapFinder(self._config)
|
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)
|
axes_map_calibration(gcmd, self._pconfig, st_thread)
|
||||||
|
|
||||||
def cmd_COMPARE_BELTS_RESPONSES(self, gcmd) -> None:
|
def cmd_COMPARE_BELTS_RESPONSES(self, gcmd) -> None:
|
||||||
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
|
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
|
||||||
belt_graph_creator = BeltsGraphCreator(self._config)
|
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)
|
compare_belts_responses(gcmd, self._pconfig, st_thread)
|
||||||
|
|
||||||
def cmd_AXES_SHAPER_CALIBRATION(self, gcmd) -> None:
|
def cmd_AXES_SHAPER_CALIBRATION(self, gcmd) -> None:
|
||||||
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
|
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
|
||||||
shaper_graph_creator = ShaperGraphCreator(self._config)
|
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)
|
axes_shaper_calibration(gcmd, self._pconfig, st_thread)
|
||||||
|
|
||||||
def cmd_CREATE_VIBRATIONS_PROFILE(self, gcmd) -> None:
|
def cmd_CREATE_VIBRATIONS_PROFILE(self, gcmd) -> None:
|
||||||
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
|
ConsoleOutput.print(f'Shake&Tune version: {ShakeTuneConfig.get_git_version()}')
|
||||||
vibration_profile_creator = VibrationsGraphCreator(self._config)
|
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)
|
create_vibrations_profile(gcmd, self._pconfig, st_thread)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
import traceback
|
import traceback
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from .helpers import filemanager as fm
|
from .helpers import filemanager as fm
|
||||||
from .helpers.console_output import ConsoleOutput
|
from .helpers.console_output import ConsoleOutput
|
||||||
@@ -11,33 +12,41 @@ from .shaketune_config import ShakeTuneConfig
|
|||||||
|
|
||||||
|
|
||||||
class ShakeTuneThread(threading.Thread):
|
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__()
|
super(ShakeTuneThread, self).__init__()
|
||||||
self._config = config
|
self._config = config
|
||||||
self.graph_creator = graph_creator
|
self.graph_creator = graph_creator
|
||||||
self._reactor = reactor
|
|
||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
|
|
||||||
|
self._internal_thread = None
|
||||||
|
self._stop_event = threading.Event()
|
||||||
|
|
||||||
def get_graph_creator(self):
|
def get_graph_creator(self):
|
||||||
return self.graph_creator
|
return self.graph_creator
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
# Start the target function in a new thread
|
# Start the target function in a new thread
|
||||||
internal_thread = threading.Thread(target=self._shaketune_thread, args=(self.graph_creator,))
|
self._internal_thread = threading.Thread(target=self._shaketune_thread, args=(self.graph_creator,))
|
||||||
internal_thread.start()
|
self._internal_thread.start()
|
||||||
|
|
||||||
# Monitor the thread execution and stop it if it takes too long
|
# If a timeout is specified, start a timer thread to monitor the timeout
|
||||||
event_time = self._reactor.monotonic()
|
if self._timeout is not None:
|
||||||
end_time = event_time + self._timeout
|
timer_thread = threading.Timer(self._timeout, self._handle_timeout)
|
||||||
while event_time < end_time:
|
timer_thread.start()
|
||||||
event_time = self._reactor.pause(event_time + 0.05)
|
|
||||||
if not internal_thread.is_alive():
|
|
||||||
break
|
|
||||||
|
|
||||||
# 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:
|
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
|
# 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:
|
try:
|
||||||
os.nice(20)
|
os.nice(20)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
Reference in New Issue
Block a user