scripts: Added shaper tuning parameters to calibrate_shaper script

The added parameters include square_corner_velocity, shaper frequencies
to optimize, input shapers to test, input shaper damping ratio and
damping ratios to test. All these options can be useful for fine-tuning
the input shapers when the default suggestions generated by the tuning
script are not optimal.

Also the `SHAPER_CALIBRATE` command was modified to pass some of these
parameters to the shaper tuning routine. Specifically, square corner
velocity and the maximum tested frequency are used to adjust shaper
tuning and maximum acceleration recommendations.

Signed-off-by: Dmitry Butyugin <dmbutyugin@google.com>
This commit is contained in:
Dmitry Butyugin 2024-02-08 03:06:48 +01:00 committed by KevinOConnor
parent 4f00f21991
commit 72b301a285
4 changed files with 145 additions and 25 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# Shaper auto-calibration script
#
# Copyright (C) 2020 Dmitry Butyugin <dmbutyugin@google.com>
# Copyright (C) 2020-2024 Dmitry Butyugin <dmbutyugin@google.com>
# Copyright (C) 2020 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
@ -40,7 +40,9 @@ def parse_log(logname):
######################################################################
# Find the best shaper parameters
def calibrate_shaper(datas, csv_output, max_smoothing):
def calibrate_shaper(datas, csv_output, *, shapers, damping_ratio, scv,
shaper_freqs, max_smoothing, test_damping_ratios,
max_freq):
helper = shaper_calibrate.ShaperCalibrate(printer=None)
if isinstance(datas[0], shaper_calibrate.CalibrationData):
calibration_data = datas[0]
@ -52,8 +54,17 @@ def calibrate_shaper(datas, csv_output, max_smoothing):
for data in datas[1:]:
calibration_data.add_data(helper.process_accelerometer_data(data))
calibration_data.normalize_to_frequencies()
shaper, all_shapers = helper.find_best_shaper(
calibration_data, max_smoothing, print)
calibration_data, shapers=shapers, damping_ratio=damping_ratio,
scv=scv, shaper_freqs=shaper_freqs, max_smoothing=max_smoothing,
test_damping_ratios=test_damping_ratios, max_freq=max_freq,
logger=print)
if not shaper:
print("No recommended shaper, possibly invalid value for --shapers=%s" %
(','.join(shapers)))
return None, None, None
print("Recommended shaper is %s @ %.1f Hz" % (shaper.name, shaper.freq))
if csv_output is not None:
helper.save_calibration_data(
@ -140,28 +151,94 @@ def main():
opts.add_option("-c", "--csv", type="string", dest="csv",
default=None, help="filename of output csv file")
opts.add_option("-f", "--max_freq", type="float", default=200.,
help="maximum frequency to graph")
opts.add_option("-s", "--max_smoothing", type="float", default=None,
help="maximum shaper smoothing to allow")
help="maximum frequency to plot")
opts.add_option("-s", "--max_smoothing", type="float", dest="max_smoothing",
default=None, help="maximum shaper smoothing to allow")
opts.add_option("--scv", "--square_corner_velocity", type="float",
dest="scv", default=5., help="square corner velocity")
opts.add_option("--shaper_freq", type="string", dest="shaper_freq",
default=None, help="shaper frequency(-ies) to test, " +
"either a comma-separated list of floats, or a range in " +
"the format [start]:end[:step]")
opts.add_option("--shapers", type="string", dest="shapers", default=None,
help="a comma-separated list of shapers to test")
opts.add_option("--damping_ratio", type="float", dest="damping_ratio",
default=None, help="shaper damping_ratio parameter")
opts.add_option("--test_damping_ratios", type="string",
dest="test_damping_ratios", default=None,
help="a comma-separated liat of damping ratios to test " +
"input shaper for")
options, args = opts.parse_args()
if len(args) < 1:
opts.error("Incorrect number of arguments")
if options.max_smoothing is not None and options.max_smoothing < 0.05:
opts.error("Too small max_smoothing specified (must be at least 0.05)")
max_freq = options.max_freq
if options.shaper_freq is None:
shaper_freqs = []
elif options.shaper_freq.find(':') >= 0:
freq_start = None
freq_end = None
freq_step = None
try:
freqs_parsed = options.shaper_freq.partition(':')
if freqs_parsed[0]:
freq_start = float(freqs_parsed[0])
freqs_parsed = freqs_parsed[-1].partition(':')
freq_end = float(freqs_parsed[0])
if freq_start and freq_start > freq_end:
opts.error("Invalid --shaper_freq param: start range larger " +
"than its end")
if freqs_parsed[-1].find(':') >= 0:
opts.error("Invalid --shaper_freq param format")
if freqs_parsed[-1]:
freq_step = float(freqs_parsed[-1])
except ValueError:
opts.error("--shaper_freq param does not specify correct range " +
"in the format [start]:end[:step]")
shaper_freqs = (freq_start, freq_end, freq_step)
max_freq = max(max_freq, freq_end * 4./3.)
else:
try:
shaper_freqs = [float(s) for s in options.shaper_freq.split(',')]
except ValueError:
opts.error("invalid floating point value in --shaper_freq param")
max_freq = max(max_freq, max(shaper_freqs) * 4./3.)
if options.test_damping_ratios:
try:
test_damping_ratios = [float(s) for s in
options.test_damping_ratios.split(',')]
except ValueError:
opts.error("invalid floating point value in " +
"--test_damping_ratios param")
else:
test_damping_ratios = None
if options.shapers is None:
shapers = None
else:
shapers = options.shapers.lower().split(',')
# Parse data
datas = [parse_log(fn) for fn in args]
# Calibrate shaper and generate outputs
selected_shaper, shapers, calibration_data = calibrate_shaper(
datas, options.csv, options.max_smoothing)
datas, options.csv, shapers=shapers,
damping_ratio=options.damping_ratio,
scv=options.scv, shaper_freqs=shaper_freqs,
max_smoothing=options.max_smoothing,
test_damping_ratios=test_damping_ratios,
max_freq=max_freq)
if selected_shaper is None:
return
if not options.csv or options.output:
# Draw graph
setup_matplotlib(options.output is not None)
fig = plot_freq_response(args, calibration_data, shapers,
selected_shaper, options.max_freq)
selected_shaper, max_freq)
# Show graph
if options.output is None: