Skip to content

Commit

Permalink
buffered writes and waiting for file write everytime it's needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Frix-x committed Aug 4, 2024
1 parent 6776816 commit 8e02ead
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
12 changes: 8 additions & 4 deletions shaketune/commands/accelerometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
# accelerometer measurements and write the data to a file in a blocking manner.


import io
import os
import time
from multiprocessing import Process, Queue

FILE_WRITE_TIMEOUT = 10 # seconds
FILE_WRITE_TIMEOUT = 20 # seconds max to write a whole CSV file
CHUNK_WRITE_SIZE = 1024 * 1024 # CSV is sliced into chunk of 1MB
CHUNK_WRITE_DELAY = 0.01 # 10ms between each chunk write


class Accelerometer:
Expand Down Expand Up @@ -70,13 +73,14 @@ def _queue_file_write(self, bg_client, filename):

def _write_to_file(self, bg_client, filename):
try:
os.nice(20)
os.nice(19)
except Exception:
pass

with open(filename, 'w') as f:
samples = bg_client.samples or bg_client.get_samples()

with io.BufferedWriter(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(f'{t:.6f},{accel_x:.6f},{accel_y:.6f},{accel_z:.6f}\n')

Expand Down
4 changes: 3 additions & 1 deletion shaketune/commands/axes_map_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,20 @@ def axes_map_calibration(gcmd, config, st_process: ShakeTuneProcess) -> None:
toolhead.dwell(0.5)
accelerometer.stop_measurement('axesmap_X', append_time=True)
toolhead.dwell(0.5)
accelerometer.wait_for_file_writes()
accelerometer.start_measurement()
toolhead.dwell(0.5)
toolhead.move([mid_x + SEGMENT_LENGTH / 2, mid_y + SEGMENT_LENGTH / 2, z_height, E], speed)
toolhead.dwell(0.5)
accelerometer.stop_measurement('axesmap_Y', append_time=True)
toolhead.dwell(0.5)
accelerometer.wait_for_file_writes()
accelerometer.start_measurement()
toolhead.dwell(0.5)
toolhead.move([mid_x + SEGMENT_LENGTH / 2, mid_y + SEGMENT_LENGTH / 2, z_height + SEGMENT_LENGTH, E], speed)
toolhead.dwell(0.5)
accelerometer.stop_measurement('axesmap_Z', append_time=True)

toolhead.dwell(0.5)
accelerometer.wait_for_file_writes()

# Re-enable the input shaper if it was active
Expand Down
4 changes: 2 additions & 2 deletions shaketune/commands/axes_shaper_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ def axes_shaper_calibration(gcmd, config, st_process: ShakeTuneProcess) -> None:
accelerometer.start_measurement()
vibrate_axis(toolhead, gcode, config['direction'], min_freq, max_freq, hz_per_sec, accel_per_hz)
accelerometer.stop_measurement(config['label'], append_time=True)

toolhead.dwell(0.5)
toolhead.wait_moves()
accelerometer.wait_for_file_writes()

# And finally generate the graph for each measured axis
Expand All @@ -114,7 +115,6 @@ def axes_shaper_calibration(gcmd, config, st_process: ShakeTuneProcess) -> None:
st_process.run()
st_process.wait_for_completion()
toolhead.dwell(1)
toolhead.wait_moves()

# Re-enable the input shaper if it was active
if input_shaper is not None:
Expand Down
5 changes: 3 additions & 2 deletions shaketune/commands/compare_belts_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ def compare_belts_responses(gcmd, config, st_process: ShakeTuneProcess) -> None:
accelerometer.start_measurement()
vibrate_axis(toolhead, gcode, config['direction'], min_freq, max_freq, hz_per_sec, accel_per_hz)
accelerometer.stop_measurement(config['label'], append_time=True)

accelerometer.wait_for_file_writes()
toolhead.dwell(0.5)
toolhead.wait_moves()
accelerometer.wait_for_file_writes()

# Re-enable the input shaper if it was active
if input_shaper is not None:
Expand Down
3 changes: 1 addition & 2 deletions shaketune/commands/create_vibrations_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ def create_vibrations_profile(gcmd, config, st_process: ShakeTuneProcess) -> Non

toolhead.dwell(0.3)
toolhead.wait_moves()

accelerometer.wait_for_file_writes()
accelerometer.wait_for_file_writes()

# Restore the previous acceleration values
if old_mcr is not None: # minimum_cruise_ratio found: Klipper >= v0.12.0-239
Expand Down
1 change: 1 addition & 0 deletions shaketune/commands/excitate_axis_at_freq.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def excitate_axis_at_freq(gcmd, config, st_process: ShakeTuneProcess) -> None:
# If the user wanted to create a graph, we stop the recording and generate it
if create_graph:
accelerometer.stop_measurement(f'staticfreq_{axis.upper()}', append_time=True)
toolhead.dwell(0.5)
accelerometer.wait_for_file_writes()

creator = st_process.get_graph_creator()
Expand Down

0 comments on commit 8e02ead

Please sign in to comment.