aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2024-04-13 21:02:04 -0400
committerKevin O'Connor <kevin@koconnor.net>2024-04-20 12:52:47 -0400
commit9ceaae3847b4bb87f0e894d6133995f7a07b21bf (patch)
tree3e24461ea90fb3495ac719328efe81a16c3290e7
parent56829b07d2428abf199bac55cec499286eab5c6e (diff)
downloadkutter-9ceaae3847b4bb87f0e894d6133995f7a07b21bf.tar.gz
kutter-9ceaae3847b4bb87f0e894d6133995f7a07b21bf.tar.xz
kutter-9ceaae3847b4bb87f0e894d6133995f7a07b21bf.zip
bulk_sensor: Refactor ChipClockUpdater constructor
Build the clock_sync and struct.Struct() in the ChipClockUpdater constructor. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
-rw-r--r--klippy/extras/adxl345.py14
-rw-r--r--klippy/extras/bulk_sensor.py20
-rw-r--r--klippy/extras/ldc1612.py14
-rw-r--r--klippy/extras/lis2dw.py14
-rw-r--r--klippy/extras/mpu9250.py14
5 files changed, 31 insertions, 45 deletions
diff --git a/klippy/extras/adxl345.py b/klippy/extras/adxl345.py
index 623b8e50..07973894 100644
--- a/klippy/extras/adxl345.py
+++ b/klippy/extras/adxl345.py
@@ -184,9 +184,6 @@ def read_axes_map(config):
raise config.error("Invalid axes_map parameter")
return [am[a.strip()] for a in axes_map]
-BYTES_PER_SAMPLE = 5
-SAMPLES_PER_BLOCK = bulk_sensor.MAX_BULK_MSG_SIZE // BYTES_PER_SAMPLE
-
BATCH_UPDATES = 0.100
# Printer class that controls ADXL345 chip
@@ -211,9 +208,8 @@ class ADXL345:
self.bulk_queue = bulk_sensor.BulkDataQueue(mcu, oid=oid)
# Clock tracking
chip_smooth = self.data_rate * BATCH_UPDATES * 2
- self.clock_sync = bulk_sensor.ClockSyncRegression(mcu, chip_smooth)
- self.clock_updater = bulk_sensor.ChipClockUpdater(self.clock_sync,
- BYTES_PER_SAMPLE)
+ self.clock_updater = bulk_sensor.ChipClockUpdater(mcu, chip_smooth,
+ "BBBBB")
self.last_error_count = 0
# Process messages in batches
self.batch_bulk = bulk_sensor.BatchBulkHelper(
@@ -227,8 +223,8 @@ class ADXL345:
cmdqueue = self.spi.get_command_queue()
self.query_adxl345_cmd = self.mcu.lookup_command(
"query_adxl345 oid=%c rest_ticks=%u", cq=cmdqueue)
- self.clock_updater.setup_query_command(
- self.mcu, "query_adxl345_status oid=%c", oid=self.oid, cq=cmdqueue)
+ self.clock_updater.setup_query_command("query_adxl345_status oid=%c",
+ oid=self.oid, cq=cmdqueue)
def read_reg(self, reg):
params = self.spi.spi_transfer([reg | REG_MOD_READ, 0x00])
response = bytearray(params['response'])
@@ -249,7 +245,7 @@ class ADXL345:
# Measurement decoding
def _extract_samples(self, raw_samples):
# Convert messages to samples
- samples = self.clock_updater.extract_samples("BBBBB", raw_samples)
+ samples = self.clock_updater.extract_samples(raw_samples)
# Convert samples
(x_pos, x_scale), (y_pos, y_scale), (z_pos, z_scale) = self.axes_map
count = 0
diff --git a/klippy/extras/bulk_sensor.py b/klippy/extras/bulk_sensor.py
index cb4c17ac..d514bc91 100644
--- a/klippy/extras/bulk_sensor.py
+++ b/klippy/extras/bulk_sensor.py
@@ -202,15 +202,17 @@ MAX_BULK_MSG_SIZE = 52
# Handle common periodic chip status query responses
class ChipClockUpdater:
- def __init__(self, clock_sync, bytes_per_sample):
- self.clock_sync = clock_sync
- self.bytes_per_sample = bytes_per_sample
- self.samples_per_block = MAX_BULK_MSG_SIZE // bytes_per_sample
+ def __init__(self, mcu, chip_clock_smooth, unpack_fmt):
+ self.mcu = mcu
+ self.clock_sync = ClockSyncRegression(mcu, chip_clock_smooth)
+ unpack = struct.Struct(unpack_fmt)
+ self.unpack_from = unpack.unpack_from
+ self.bytes_per_sample = unpack.size
+ self.samples_per_block = MAX_BULK_MSG_SIZE // self.bytes_per_sample
self.last_sequence = self.max_query_duration = 0
self.last_overflows = 0
- self.mcu = self.oid = self.query_status_cmd = None
- def setup_query_command(self, mcu, msgformat, oid, cq):
- self.mcu = mcu
+ self.oid = self.query_status_cmd = None
+ def setup_query_command(self, msgformat, oid, cq):
self.oid = oid
self.query_status_cmd = self.mcu.lookup_query_command(
msgformat, "sensor_bulk_status oid=%c clock=%u query_ticks=%u"
@@ -256,11 +258,11 @@ class ChipClockUpdater:
else:
self.clock_sync.update(avg_mcu_clock, chip_clock)
# Convert a list of sensor_bulk_data responses into list of samples
- def extract_samples(self, unpack_fmt, raw_samples):
- unpack_from = struct.Struct(unpack_fmt).unpack_from
+ def extract_samples(self, raw_samples):
# Load variables to optimize inner loop below
last_sequence = self.get_last_sequence()
time_base, chip_base, inv_freq = self.clock_sync.get_time_translation()
+ unpack_from = self.unpack_from
bytes_per_sample = self.bytes_per_sample
samples_per_block = self.samples_per_block
# Process every message in raw_samples
diff --git a/klippy/extras/ldc1612.py b/klippy/extras/ldc1612.py
index e2f502d1..4b8a3f72 100644
--- a/klippy/extras/ldc1612.py
+++ b/klippy/extras/ldc1612.py
@@ -10,9 +10,6 @@ MIN_MSG_TIME = 0.100
BATCH_UPDATES = 0.100
-BYTES_PER_SAMPLE = 4
-SAMPLES_PER_BLOCK = bulk_sensor.MAX_BULK_MSG_SIZE // BYTES_PER_SAMPLE
-
LDC1612_ADDR = 0x2a
LDC1612_FREQ = 12000000
@@ -98,9 +95,8 @@ class LDC1612:
self.bulk_queue = bulk_sensor.BulkDataQueue(mcu, oid=oid)
# Clock tracking
chip_smooth = self.data_rate * BATCH_UPDATES * 2
- self.clock_sync = bulk_sensor.ClockSyncRegression(mcu, chip_smooth)
- self.clock_updater = bulk_sensor.ChipClockUpdater(self.clock_sync,
- BYTES_PER_SAMPLE)
+ self.clock_updater = bulk_sensor.ChipClockUpdater(mcu, chip_smooth,
+ ">I")
self.last_error_count = 0
# Process messages in batches
self.batch_bulk = bulk_sensor.BatchBulkHelper(
@@ -114,8 +110,8 @@ class LDC1612:
cmdqueue = self.i2c.get_command_queue()
self.query_ldc1612_cmd = self.mcu.lookup_command(
"query_ldc1612 oid=%c rest_ticks=%u", cq=cmdqueue)
- self.clock_updater.setup_query_command(
- self.mcu, "query_ldc1612_status oid=%c", oid=self.oid, cq=cmdqueue)
+ self.clock_updater.setup_query_command("query_ldc1612_status oid=%c",
+ oid=self.oid, cq=cmdqueue)
self.ldc1612_setup_home_cmd = self.mcu.lookup_command(
"ldc1612_setup_home oid=%c clock=%u threshold=%u"
" trsync_oid=%c trigger_reason=%c", cq=cmdqueue)
@@ -150,7 +146,7 @@ class LDC1612:
# Measurement decoding
def _extract_samples(self, raw_samples):
# Convert messages to samples
- samples = self.clock_updater.extract_samples(">I", raw_samples)
+ samples = self.clock_updater.extract_samples(raw_samples)
# Convert samples
freq_conv = float(LDC1612_FREQ) / (1<<28)
count = 0
diff --git a/klippy/extras/lis2dw.py b/klippy/extras/lis2dw.py
index a40e7056..96b7db76 100644
--- a/klippy/extras/lis2dw.py
+++ b/klippy/extras/lis2dw.py
@@ -30,9 +30,6 @@ LIS2DW_DEV_ID = 0x44
FREEFALL_ACCEL = 9.80665
SCALE = FREEFALL_ACCEL * 1.952 / 4
-BYTES_PER_SAMPLE = 6
-SAMPLES_PER_BLOCK = bulk_sensor.MAX_BULK_MSG_SIZE // BYTES_PER_SAMPLE
-
BATCH_UPDATES = 0.100
# Printer class that controls LIS2DW chip
@@ -55,9 +52,8 @@ class LIS2DW:
self.bulk_queue = bulk_sensor.BulkDataQueue(mcu, oid=oid)
# Clock tracking
chip_smooth = self.data_rate * BATCH_UPDATES * 2
- self.clock_sync = bulk_sensor.ClockSyncRegression(mcu, chip_smooth)
- self.clock_updater = bulk_sensor.ChipClockUpdater(self.clock_sync,
- BYTES_PER_SAMPLE)
+ self.clock_updater = bulk_sensor.ChipClockUpdater(mcu, chip_smooth,
+ "<hhh")
self.last_error_count = 0
# Process messages in batches
self.batch_bulk = bulk_sensor.BatchBulkHelper(
@@ -72,8 +68,8 @@ class LIS2DW:
cmdqueue = self.spi.get_command_queue()
self.query_lis2dw_cmd = self.mcu.lookup_command(
"query_lis2dw oid=%c rest_ticks=%u", cq=cmdqueue)
- self.clock_updater.setup_query_command(
- self.mcu, "query_lis2dw_status oid=%c", oid=self.oid, cq=cmdqueue)
+ self.clock_updater.setup_query_command("query_lis2dw_status oid=%c",
+ oid=self.oid, cq=cmdqueue)
def read_reg(self, reg):
params = self.spi.spi_transfer([reg | REG_MOD_READ, 0x00])
response = bytearray(params['response'])
@@ -94,7 +90,7 @@ class LIS2DW:
# Measurement decoding
def _extract_samples(self, raw_samples):
# Convert messages to samples
- samples = self.clock_updater.extract_samples("<hhh", raw_samples)
+ samples = self.clock_updater.extract_samples(raw_samples)
# Convert samples
(x_pos, x_scale), (y_pos, y_scale), (z_pos, z_scale) = self.axes_map
count = 0
diff --git a/klippy/extras/mpu9250.py b/klippy/extras/mpu9250.py
index 492131ae..abc7911a 100644
--- a/klippy/extras/mpu9250.py
+++ b/klippy/extras/mpu9250.py
@@ -52,9 +52,6 @@ SCALE = 0.000244140625 * FREEFALL_ACCEL
FIFO_SIZE = 512
-BYTES_PER_SAMPLE = 6
-SAMPLES_PER_BLOCK = bulk_sensor.MAX_BULK_MSG_SIZE // BYTES_PER_SAMPLE
-
BATCH_UPDATES = 0.100
# Printer class that controls MPU9250 chip
@@ -77,9 +74,8 @@ class MPU9250:
self.bulk_queue = bulk_sensor.BulkDataQueue(mcu, oid=oid)
# Clock tracking
chip_smooth = self.data_rate * BATCH_UPDATES * 2
- self.clock_sync = bulk_sensor.ClockSyncRegression(mcu, chip_smooth)
- self.clock_updater = bulk_sensor.ChipClockUpdater(self.clock_sync,
- BYTES_PER_SAMPLE)
+ self.clock_updater = bulk_sensor.ChipClockUpdater(mcu, chip_smooth,
+ ">hhh")
self.last_error_count = 0
# Process messages in batches
self.batch_bulk = bulk_sensor.BatchBulkHelper(
@@ -97,8 +93,8 @@ class MPU9250:
% (self.oid,), on_restart=True)
self.query_mpu9250_cmd = self.mcu.lookup_command(
"query_mpu9250 oid=%c rest_ticks=%u", cq=cmdqueue)
- self.clock_updater.setup_query_command(
- self.mcu, "query_mpu9250_status oid=%c", oid=self.oid, cq=cmdqueue)
+ self.clock_updater.setup_query_command("query_mpu9250_status oid=%c",
+ oid=self.oid, cq=cmdqueue)
def read_reg(self, reg):
params = self.i2c.i2c_read([reg], 1)
return bytearray(params['response'])[0]
@@ -111,7 +107,7 @@ class MPU9250:
# Measurement decoding
def _extract_samples(self, raw_samples):
# Convert messages to samples
- samples = self.clock_updater.extract_samples(">hhh", raw_samples)
+ samples = self.clock_updater.extract_samples(raw_samples)
# Convert samples
(x_pos, x_scale), (y_pos, y_scale), (z_pos, z_scale) = self.axes_map
count = 0