added my own accelerometer interface

This commit is contained in:
Félix Boisselier
2024-05-12 18:50:31 +02:00
parent 30a1910513
commit 187ba13c98
2 changed files with 46 additions and 2 deletions

View File

@@ -1,2 +1,40 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import time
from ..helpers.console_output import ConsoleOutput
class Accelerometer:
def __init__(self, klipper_accelerometer):
self._k_accelerometer = klipper_accelerometer
def start_measurement(self):
if self._k_accelerometer.bg_client is None:
self._k_accelerometer.bg_client = self._k_accelerometer.chip.start_internal_client()
ConsoleOutput.print('accelerometer measurements started')
else:
raise ValueError('measurements already started!')
def stop_measurement(self, name: str = None):
if self._k_accelerometer.bg_client is not None:
name = name or time.strftime('%Y%m%d_%H%M%S')
if not name.replace('-', '').replace('_', '').isalnum():
raise ValueError('invalid file name!')
bg_client = self._k_accelerometer.bg_client
self._k_accelerometer.bg_client = None
bg_client.finish_measurements()
filename = f'/tmp/shaketune-{name}.csv'
self._write_to_file(bg_client, filename)
ConsoleOutput.print(f'Measurements stopped. Data written to {filename}')
else:
raise ValueError('measurements need to be started first!')
def _write_to_file(self, bg_client, filename):
with open(filename, 'w') as f:
f.write('#time,accel_x,accel_y,accel_z\n')
samples = bg_client.samples or bg_client.get_samples()
for t, accel_x, accel_y, accel_z in samples:
f.write('%.6f,%.6f,%.6f,%.6f\n' % (t, accel_x, accel_y, accel_z))

View File

@@ -3,6 +3,7 @@
from ..helpers.console_output import ConsoleOutput from ..helpers.console_output import ConsoleOutput
from ..shaketune_thread import ShakeTuneThread from ..shaketune_thread import ShakeTuneThread
from .accelerometer import Accelerometer
def find_axis_accelerometer(printer, axis: str = 'xy'): def find_axis_accelerometer(printer, axis: str = 'xy'):
@@ -56,7 +57,10 @@ def axes_map_calibration(gcmd, gcode, printer, st_thread: ShakeTuneThread) -> No
toolhead.dwell(0.5) toolhead.dwell(0.5)
# Start the measurements and do the movements (+X, +Y and then +Z) # Start the measurements and do the movements (+X, +Y and then +Z)
gcode.run_script_from_command(f'ACCELEROMETER_MEASURE CHIP={accel_chip}') accelerometer = Accelerometer(printer.lookup_object(accel_chip))
accelerometer.start_measurement()
# gcode.run_script_from_command(f'ACCELEROMETER_MEASURE CHIP={accel_chip}')
toolhead.dwell(1) toolhead.dwell(1)
toolhead.move([mid_x + 15, mid_y - 15, z_height, E], speed) toolhead.move([mid_x + 15, mid_y - 15, z_height, E], speed)
toolhead.dwell(1) toolhead.dwell(1)
@@ -64,7 +68,9 @@ def axes_map_calibration(gcmd, gcode, printer, st_thread: ShakeTuneThread) -> No
toolhead.dwell(1) toolhead.dwell(1)
toolhead.move([mid_x + 15, mid_y + 15, z_height + 15, E], speed) toolhead.move([mid_x + 15, mid_y + 15, z_height + 15, E], speed)
toolhead.dwell(1) toolhead.dwell(1)
gcode.run_script_from_command(f'ACCELEROMETER_MEASURE CHIP={accel_chip} NAME=axemap')
accelerometer.stop_measurement('axemap')
# gcode.run_script_from_command(f'ACCELEROMETER_MEASURE CHIP={accel_chip} NAME=axemap')
# 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: