Skip to content

Commit

Permalink
shaper_calibrate: store accel_per_hz
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerlz committed Jun 10, 2024
1 parent b5c7b0a commit 782e062
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
8 changes: 7 additions & 1 deletion klippy/extras/resonance_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ def _run_test(self, axis, gcmd):
def get_max_freq(self):
return self.freq_end

def get_accel_per_hz(self):
return self.accel_per_hz


class ResonanceTester:
def __init__(self, config):
Expand Down Expand Up @@ -380,6 +383,7 @@ def cmd_TEST_RESONANCES(self, gcmd):
data,
point=test_point,
max_freq=self._get_max_calibration_freq(),
accel_per_hz=self.test.get_accel_per_hz(),
)
gcmd.respond_info(
"Resonances data written to %s file" % (csv_name,)
Expand Down Expand Up @@ -457,6 +461,7 @@ def cmd_SHAPER_CALIBRATE(self, gcmd):
calibration_data[axis],
all_shapers,
max_freq=max_freq,
accel_per_hz=self.test.get_accel_per_hz(),
)
gcmd.respond_info(
"Shaper calibration data written to %s file" % (csv_name,)
Expand Down Expand Up @@ -520,10 +525,11 @@ def save_calibration_data(
all_shapers=None,
point=None,
max_freq=None,
accel_per_hz=None,
):
output = self.get_filename(base_name, name_suffix, axis, point)
shaper_calibrate.save_calibration_data(
output, calibration_data, all_shapers, max_freq
output, calibration_data, all_shapers, max_freq, accel_per_hz
)
return output

Expand Down
12 changes: 9 additions & 3 deletions klippy/extras/shaper_calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,17 @@ def apply_params(self, input_shaper, axis, shaper_name, shaper_freq):
)

def save_calibration_data(
self, output, calibration_data, shapers=None, max_freq=None
self,
output,
calibration_data,
shapers=None,
max_freq=None,
accel_per_hz=None,
):
try:
max_freq = max_freq or MAX_FREQ
with open(output, "w") as csvfile:
csvfile.write("freq,psd_x,psd_y,psd_z,psd_xyz")
csvfile.write("freq,psd_x,psd_y,psd_z,psd_xyz,accel_per_hz")
if shapers:
for shaper in shapers:
csvfile.write(",%s(%.1f)" % (shaper.name, shaper.freq))
Expand All @@ -471,13 +476,14 @@ def save_calibration_data(
if calibration_data.freq_bins[i] >= max_freq:
break
csvfile.write(
"%.1f,%.3e,%.3e,%.3e,%.3e"
"%.1f,%.3e,%.3e,%.3e,%.3e,%.1f"
% (
calibration_data.freq_bins[i],
calibration_data.psd_x[i],
calibration_data.psd_y[i],
calibration_data.psd_z[i],
calibration_data.psd_sum[i],
accel_per_hz,
)
)
if shapers:
Expand Down
38 changes: 35 additions & 3 deletions scripts/calibrate_shaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def parse_log(logname):
for header in f:
if not header.startswith("#"):
break
if not header.startswith("freq,psd_x,psd_y,psd_z,psd_xyz"):
if not header.startswith("freq,psd_x,psd_y,psd_z,psd_xyz,accel_per_hz"):
# Raw accelerometer data
return np.loadtxt(logname, comments="#", delimiter=",")
# Parse power spectral density data
Expand All @@ -43,6 +43,20 @@ def parse_log(logname):
return calibration_data


def parse_accel_per_hz(logname):
with open(logname) as f:
for header in f:
if not header.startswith("#"):
break
if not header.startswith("freq,psd_x,psd_y,psd_z,psd_xyz,accel_per_hz"):
return None # TODO

data = np.loadtxt(
logname, skiprows=1, comments="#", delimiter=",", max_rows=2
)
return data[0][5].item()


######################################################################
# Shaper calibration
######################################################################
Expand Down Expand Up @@ -102,7 +116,12 @@ def calibrate_shaper(


def plot_freq_response(
lognames, calibration_data, shapers, selected_shaper, max_freq
lognames,
calibration_data,
shapers,
selected_shaper,
max_freq,
accels_per_hz,
):
freqs = calibration_data.freq_bins
psd = calibration_data.psd_sum[freqs <= max_freq]
Expand Down Expand Up @@ -154,6 +173,13 @@ def plot_freq_response(
[], [], " ", label="Recommended shaper: %s" % (selected_shaper.upper())
)

ax2.plot(
[],
[],
" ",
label="accels_per_hz: %s" % (", ".join(str(e) for e in accels_per_hz)),
)

ax.legend(loc="upper left", prop=fontP)
ax2.legend(loc="upper right", prop=fontP)

Expand Down Expand Up @@ -309,6 +335,7 @@ def main():

# Parse data
datas = [parse_log(fn) for fn in args]
accels_per_hz = [parse_accel_per_hz(fn) for fn in args]

# Calibrate shaper and generate outputs
selected_shaper, shapers, calibration_data = calibrate_shaper(
Expand All @@ -330,7 +357,12 @@ def main():
setup_matplotlib(options.output is not None)

fig = plot_freq_response(
args, calibration_data, shapers, selected_shaper, max_freq
args,
calibration_data,
shapers,
selected_shaper,
max_freq,
accels_per_hz,
)

# Show graph
Expand Down

0 comments on commit 782e062

Please sign in to comment.