switched to multiprocessing instead of threading

This commit is contained in:
Félix Boisselier
2024-06-03 17:49:28 +02:00
parent 9ce82e3dc0
commit d22b3fcbef
6 changed files with 59 additions and 52 deletions

View File

@@ -3,12 +3,12 @@
from ..helpers.common_func import AXIS_CONFIG from ..helpers.common_func import AXIS_CONFIG
from ..helpers.console_output import ConsoleOutput from ..helpers.console_output import ConsoleOutput
from ..shaketune_thread import ShakeTuneThread from ..shaketune_process import ShakeTuneProcess
from .accelerometer import Accelerometer from .accelerometer import Accelerometer
from .resonance_test import vibrate_axis from .resonance_test import vibrate_axis
def axes_shaper_calibration(gcmd, config, st_thread: ShakeTuneThread) -> None: def axes_shaper_calibration(gcmd, config, st_process: ShakeTuneProcess) -> None:
min_freq = gcmd.get_float('FREQ_START', default=5, minval=1) min_freq = gcmd.get_float('FREQ_START', default=5, minval=1)
max_freq = gcmd.get_float('FREQ_END', default=133.33, minval=1) max_freq = gcmd.get_float('FREQ_END', default=133.33, minval=1)
hz_per_sec = gcmd.get_float('HZ_PER_SEC', default=1, minval=1) hz_per_sec = gcmd.get_float('HZ_PER_SEC', default=1, minval=1)
@@ -60,7 +60,7 @@ def axes_shaper_calibration(gcmd, config, st_thread: ShakeTuneThread) -> None:
toolhead.dwell(0.5) toolhead.dwell(0.5)
# Configure the graph creator # Configure the graph creator
creator = st_thread.get_graph_creator() creator = st_process.get_graph_creator()
creator.configure(scv, max_sm, accel_per_hz) creator.configure(scv, max_sm, accel_per_hz)
# set the needed acceleration values for the test # set the needed acceleration values for the test
@@ -95,8 +95,8 @@ def axes_shaper_calibration(gcmd, config, st_thread: ShakeTuneThread) -> None:
# And finally generate the graph for each measured axis # And finally generate the graph for each measured axis
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_process.run()
st_thread.wait_for_completion() st_process.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:

View File

@@ -2,11 +2,11 @@
from ..helpers.console_output import ConsoleOutput from ..helpers.console_output import ConsoleOutput
from ..shaketune_thread import ShakeTuneThread from ..shaketune_process import ShakeTuneProcess
from .accelerometer import Accelerometer from .accelerometer import Accelerometer
def axes_map_calibration(gcmd, config, st_thread: ShakeTuneThread) -> None: def axes_map_calibration(gcmd, config, st_process: ShakeTuneProcess) -> None:
z_height = gcmd.get_float('Z_HEIGHT', default=20.0) z_height = gcmd.get_float('Z_HEIGHT', default=20.0)
speed = gcmd.get_float('SPEED', default=80.0, minval=20.0) speed = gcmd.get_float('SPEED', default=80.0, minval=20.0)
accel = gcmd.get_int('ACCEL', default=1500, minval=100) accel = gcmd.get_int('ACCEL', default=1500, minval=100)
@@ -70,6 +70,6 @@ def axes_map_calibration(gcmd, config, st_thread: ShakeTuneThread) -> None:
# Run post-processing # Run post-processing
ConsoleOutput.print('Analysis of the movements...') ConsoleOutput.print('Analysis of the movements...')
creator = st_thread.get_graph_creator() creator = st_process.get_graph_creator()
creator.configure(accel) creator.configure(accel)
st_thread.run() st_process.run()

View File

@@ -3,13 +3,13 @@
from ..helpers.common_func import AXIS_CONFIG from ..helpers.common_func import AXIS_CONFIG
from ..helpers.console_output import ConsoleOutput from ..helpers.console_output import ConsoleOutput
from ..shaketune_thread import ShakeTuneThread from ..shaketune_process import ShakeTuneProcess
from .accelerometer import Accelerometer from .accelerometer import Accelerometer
from .motorsconfigparser import MotorsConfigParser from .motorsconfigparser import MotorsConfigParser
from .resonance_test import vibrate_axis from .resonance_test import vibrate_axis
def compare_belts_responses(gcmd, config, st_thread: ShakeTuneThread) -> None: def compare_belts_responses(gcmd, config, st_process: ShakeTuneProcess) -> None:
min_freq = gcmd.get_float('FREQ_START', default=5.0, minval=1) min_freq = gcmd.get_float('FREQ_START', default=5.0, minval=1)
max_freq = gcmd.get_float('FREQ_END', default=133.33, minval=1) max_freq = gcmd.get_float('FREQ_END', default=133.33, minval=1)
hz_per_sec = gcmd.get_float('HZ_PER_SEC', default=1.0, minval=1) hz_per_sec = gcmd.get_float('HZ_PER_SEC', default=1.0, minval=1)
@@ -29,7 +29,7 @@ def compare_belts_responses(gcmd, config, st_thread: ShakeTuneThread) -> None:
# Configure the graph creator # Configure the graph creator
motors_config_parser = MotorsConfigParser(config, motors=None) motors_config_parser = MotorsConfigParser(config, motors=None)
creator = st_thread.get_graph_creator() creator = st_process.get_graph_creator()
creator.configure(motors_config_parser.kinematics, accel_per_hz) creator.configure(motors_config_parser.kinematics, accel_per_hz)
if motors_config_parser.kinematics == 'corexy': if motors_config_parser.kinematics == 'corexy':
@@ -102,4 +102,4 @@ def compare_belts_responses(gcmd, config, st_thread: ShakeTuneThread) -> None:
# Run post-processing # Run post-processing
ConsoleOutput.print('Belts comparative frequency profile generation...') ConsoleOutput.print('Belts comparative frequency profile generation...')
ConsoleOutput.print('This may take some time (3-5min)') ConsoleOutput.print('This may take some time (3-5min)')
st_thread.run() st_process.run()

View File

@@ -4,14 +4,14 @@
import math import math
from ..helpers.console_output import ConsoleOutput from ..helpers.console_output import ConsoleOutput
from ..shaketune_thread import ShakeTuneThread from ..shaketune_process import ShakeTuneProcess
from .accelerometer import Accelerometer from .accelerometer import Accelerometer
from .motorsconfigparser import MotorsConfigParser from .motorsconfigparser import MotorsConfigParser
MIN_SPEED = 2 # mm/s MIN_SPEED = 2 # mm/s
def create_vibrations_profile(gcmd, config, st_thread: ShakeTuneThread) -> None: def create_vibrations_profile(gcmd, config, st_process: ShakeTuneProcess) -> None:
size = gcmd.get_float('SIZE', default=100.0, minval=50.0) size = gcmd.get_float('SIZE', default=100.0, minval=50.0)
z_height = gcmd.get_float('Z_HEIGHT', default=20.0) z_height = gcmd.get_float('Z_HEIGHT', default=20.0)
max_speed = gcmd.get_float('MAX_SPEED', default=200.0, minval=10.0) max_speed = gcmd.get_float('MAX_SPEED', default=200.0, minval=10.0)
@@ -127,6 +127,6 @@ def create_vibrations_profile(gcmd, config, st_thread: ShakeTuneThread) -> None:
# Run post-processing # Run post-processing
ConsoleOutput.print('Machine vibrations profile generation...') ConsoleOutput.print('Machine vibrations profile generation...')
ConsoleOutput.print('This may take some time (5-8min)') ConsoleOutput.print('This may take some time (5-8min)')
creator = st_thread.get_graph_creator() creator = st_process.get_graph_creator()
creator.configure(motors_config_parser.kinematics, accel, motors_config_parser) creator.configure(motors_config_parser.kinematics, accel, motors_config_parser)
st_thread.run() st_process.run()

View File

@@ -14,7 +14,7 @@ from .measurement import (
) )
from .post_processing import AxesMapFinder, BeltsGraphCreator, ShaperGraphCreator, VibrationsGraphCreator from .post_processing import AxesMapFinder, BeltsGraphCreator, ShaperGraphCreator, VibrationsGraphCreator
from .shaketune_config import ShakeTuneConfig from .shaketune_config import ShakeTuneConfig
from .shaketune_thread import ShakeTuneThread from .shaketune_process import ShakeTuneProcess
class ShakeTune: class ShakeTune:
@@ -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.timeout) st_process = ShakeTuneProcess(self._config, axes_map_finder, self.timeout)
axes_map_calibration(gcmd, self._pconfig, st_thread) axes_map_calibration(gcmd, self._pconfig, st_process)
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.timeout) st_process = ShakeTuneProcess(self._config, belt_graph_creator, self.timeout)
compare_belts_responses(gcmd, self._pconfig, st_thread) compare_belts_responses(gcmd, self._pconfig, st_process)
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.timeout) st_process = ShakeTuneProcess(self._config, shaper_graph_creator, self.timeout)
axes_shaper_calibration(gcmd, self._pconfig, st_thread) axes_shaper_calibration(gcmd, self._pconfig, st_process)
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.timeout) st_process = ShakeTuneProcess(self._config, vibration_profile_creator, self.timeout)
create_vibrations_profile(gcmd, self._pconfig, st_thread) create_vibrations_profile(gcmd, self._pconfig, st_process)

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import multiprocessing
import threading import threading
import traceback import traceback
from typing import Optional from typing import Optional
@@ -10,51 +10,58 @@ from .helpers.console_output import ConsoleOutput
from .shaketune_config import ShakeTuneConfig from .shaketune_config import ShakeTuneConfig
class ShakeTuneThread(threading.Thread): class ShakeTuneProcess:
def __init__(self, config: ShakeTuneConfig, graph_creator, timeout: Optional[float] = None) -> None: def __init__(self, config: ShakeTuneConfig, graph_creator, timeout: Optional[float] = None) -> None:
super(ShakeTuneThread, self).__init__()
self._config = config self._config = config
self.graph_creator = graph_creator self.graph_creator = graph_creator
self._timeout = timeout self._timeout = timeout
self._internal_thread = None self._process = 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 start(self) -> None:
# Start the target function in a new thread # Start the target function in a new process (a thread is known to cause issues with Klipper and CANbus due to the GIL)
self._internal_thread = threading.Thread(target=self._shaketune_thread, args=(self.graph_creator,)) self._process = multiprocessing.Process(
self._internal_thread.start() target=self._shaketune_process_wrapper, args=(self.graph_creator, self._timeout)
)
# If a timeout is specified, start a timer thread to monitor the timeout self._process.start()
if self._timeout is not None:
timer_thread = threading.Timer(self._timeout, self._handle_timeout)
timer_thread.start()
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: def wait_for_completion(self) -> None:
if self._internal_thread is not None: if self._process is not None:
self._internal_thread.join() self._process.join()
# This function run in a thread is used to do the CSV analysis and create the graphs # This function is a simple wrapper to start the Shake&Tune process. It's needed in order to get the timeout
def _shaketune_thread(self, graph_creator) -> None: # as a Timer in a thread INSIDE the Shake&Tune child process to not interfere with the main Klipper process
# Trying to reduce the Shake&Tune post-processing thread priority to avoid slowing down the main Klipper process def _shaketune_process_wrapper(self, graph_creator, timeout) -> None:
if timeout is not None:
timer = threading.Timer(timeout, self._handle_timeout)
timer.start()
try:
self._shaketune_process(graph_creator)
finally:
if timeout is not None:
timer.cancel()
def _handle_timeout(self) -> None:
ConsoleOutput.print('Timeout: Shake&Tune computation did not finish within the specified timeout!')
os._exit(1) # Forcefully exit the process
def _shaketune_process(self, graph_creator) -> None:
# Trying to reduce Shake&Tune process priority to avoid slowing down the main Klipper process
# as this could lead to random "Timer too close" 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(15)
except Exception: except Exception:
ConsoleOutput.print('Warning: failed reducing Shake&Tune thread priority, continuing...') ConsoleOutput.print('Warning: failed reducing Shake&Tune process priority, continuing...')
# Ensure the output folders exist # Ensure the output folders exist
for folder in self._config.get_results_subfolders(): for folder in self._config.get_results_subfolders():
folder.mkdir(parents=True, exist_ok=True) folder.mkdir(parents=True, exist_ok=True)
# Generate the graphs
try: try:
graph_creator.create_graph() graph_creator.create_graph()
except FileNotFoundError as e: except FileNotFoundError as e: