diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2023-12-16 13:01:34 -0500 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2023-12-26 11:47:21 -0500 |
commit | d6a4669ce092496bb675a28732aa5f6a11360caf (patch) | |
tree | 1387a185bebd3938f447c27d07c3a32e740e11d2 /klippy/extras/adxl345.py | |
parent | e67cbbe5c12bae1deb4651e2b3aa12c3c77c3439 (diff) | |
download | kutter-d6a4669ce092496bb675a28732aa5f6a11360caf.tar.gz kutter-d6a4669ce092496bb675a28732aa5f6a11360caf.tar.xz kutter-d6a4669ce092496bb675a28732aa5f6a11360caf.zip |
bulk_sensor: Add new ChipClockUpdater helper class
All the accelerometers use a standard response for their query_status
messages. Create a common helper class to process those responses.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/extras/adxl345.py')
-rw-r--r-- | klippy/extras/adxl345.py | 40 |
1 files changed, 10 insertions, 30 deletions
diff --git a/klippy/extras/adxl345.py b/klippy/extras/adxl345.py index 1cac6143..bc2dfeb4 100644 --- a/klippy/extras/adxl345.py +++ b/klippy/extras/adxl345.py @@ -1,6 +1,6 @@ # Support for reading acceleration data from an adxl345 chip # -# Copyright (C) 2020-2021 Kevin O'Connor <kevin@koconnor.net> +# Copyright (C) 2020-2023 Kevin O'Connor <kevin@koconnor.net> # # This file may be distributed under the terms of the GNU GPLv3 license. import logging, time, collections, multiprocessing, os @@ -208,10 +208,11 @@ class ADXL345: mcu.register_config_callback(self._build_config) self.bulk_queue = bulk_sensor.BulkDataQueue(mcu, "adxl345_data", oid) # Clock tracking - self.last_sequence = self.max_query_duration = 0 - self.last_limit_count = self.last_error_count = 0 chip_smooth = self.data_rate * API_UPDATES * 2 self.clock_sync = bulk_sensor.ClockSyncRegression(mcu, chip_smooth) + self.clock_updater = bulk_sensor.ChipClockUpdater(self.clock_sync, + BYTES_PER_SAMPLE) + self.last_error_count = 0 # API server endpoints self.api_dump = motion_report.APIDumpHelper( self.printer, self._api_update, self._api_startstop, API_UPDATES) @@ -250,7 +251,7 @@ class ADXL345: def _extract_samples(self, raw_samples): # Load variables to optimize inner loop below (x_pos, x_scale), (y_pos, y_scale), (z_pos, z_scale) = self.axes_map - last_sequence = self.last_sequence + last_sequence = self.clock_updater.get_last_sequence() time_base, chip_base, inv_freq = self.clock_sync.get_time_translation() # Process every message in raw_samples count = seq = 0 @@ -291,26 +292,7 @@ class ADXL345: break else: raise self.printer.command_error("Unable to query adxl345 fifo") - mcu_clock = self.mcu.clock32_to_clock64(params['clock']) - seq_diff = (params['next_sequence'] - self.last_sequence) & 0xffff - self.last_sequence += seq_diff - buffered = params['buffered'] - lc_diff = (params['limit_count'] - self.last_limit_count) & 0xffff - self.last_limit_count += lc_diff - duration = params['query_ticks'] - if duration > self.max_query_duration: - # Skip measurement as a high query time could skew clock tracking - self.max_query_duration = max(2 * self.max_query_duration, - self.mcu.seconds_to_clock(.000005)) - return - self.max_query_duration = 2 * duration - msg_count = (self.last_sequence * SAMPLES_PER_BLOCK - + buffered // BYTES_PER_SAMPLE + fifo) - # The "chip clock" is the message counter plus .5 for average - # inaccuracy of query responses and plus .5 for assumed offset - # of adxl345 hw processing time. - chip_clock = msg_count + 1 - self.clock_sync.update(mcu_clock + duration // 2, chip_clock) + self.clock_updater.update_clock(params) def _start_measurements(self): if self.is_measuring(): return @@ -340,12 +322,10 @@ class ADXL345: reqclock=reqclock) logging.info("ADXL345 starting '%s' measurements", self.name) # Initialize clock tracking - self.last_sequence = 0 - self.last_limit_count = self.last_error_count = 0 - self.clock_sync.reset(reqclock, 0) - self.max_query_duration = 1 << 31 + self.clock_updater.note_start(reqclock) self._update_clock(minclock=reqclock) - self.max_query_duration = 1 << 31 + self.clock_updater.clear_duration_filter() + self.last_error_count = 0 def _finish_measurements(self): if not self.is_measuring(): return @@ -364,7 +344,7 @@ class ADXL345: if not samples: return {} return {'data': samples, 'errors': self.last_error_count, - 'overflows': self.last_limit_count} + 'overflows': self.clock_updater.get_last_limit_count()} def _api_startstop(self, is_start): if is_start: self._start_measurements() |