2020-07-30 07:32:17 +02:00
|
|
|
# Support for reading acceleration data from an adxl345 chip
|
|
|
|
#
|
2021-08-12 07:05:09 +02:00
|
|
|
# Copyright (C) 2020-2021 Kevin O'Connor <kevin@koconnor.net>
|
2020-07-30 07:32:17 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2021-08-12 07:05:09 +02:00
|
|
|
import logging, time, collections, threading, multiprocessing, os
|
|
|
|
from . import bus, motion_report
|
2020-07-30 07:32:17 +02:00
|
|
|
|
|
|
|
# ADXL345 registers
|
|
|
|
REG_DEVID = 0x00
|
|
|
|
REG_BW_RATE = 0x2C
|
|
|
|
REG_POWER_CTL = 0x2D
|
|
|
|
REG_DATA_FORMAT = 0x31
|
|
|
|
REG_FIFO_CTL = 0x38
|
|
|
|
REG_MOD_READ = 0x80
|
|
|
|
REG_MOD_MULTI = 0x40
|
|
|
|
|
|
|
|
QUERY_RATES = {
|
|
|
|
25: 0x8, 50: 0x9, 100: 0xa, 200: 0xb, 400: 0xc,
|
|
|
|
800: 0xd, 1600: 0xe, 3200: 0xf,
|
|
|
|
}
|
|
|
|
|
2021-03-26 19:32:32 +01:00
|
|
|
ADXL345_DEV_ID = 0xe5
|
2021-08-22 22:24:16 +02:00
|
|
|
SET_FIFO_CTL = 0x90
|
2021-03-26 19:32:32 +01:00
|
|
|
|
|
|
|
FREEFALL_ACCEL = 9.80665 * 1000.
|
|
|
|
SCALE = 0.0039 * FREEFALL_ACCEL # 3.9mg/LSB * Earth gravity in mm/s**2
|
2020-07-30 07:32:17 +02:00
|
|
|
|
|
|
|
Accel_Measurement = collections.namedtuple(
|
|
|
|
'Accel_Measurement', ('time', 'accel_x', 'accel_y', 'accel_z'))
|
|
|
|
|
2021-08-12 05:09:49 +02:00
|
|
|
# Helper class to obtain measurements
|
|
|
|
class ADXL345QueryHelper:
|
2021-08-14 19:41:12 +02:00
|
|
|
def __init__(self, printer, cconn):
|
2021-08-12 05:09:49 +02:00
|
|
|
self.printer = printer
|
2021-08-12 07:05:09 +02:00
|
|
|
self.cconn = cconn
|
|
|
|
print_time = printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
self.request_start_time = self.request_end_time = print_time
|
2021-09-26 01:54:50 +02:00
|
|
|
self.samples = self.raw_samples = []
|
2021-08-12 05:09:49 +02:00
|
|
|
def finish_measurements(self):
|
2021-08-12 07:05:09 +02:00
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
self.request_end_time = toolhead.get_last_move_time()
|
|
|
|
toolhead.wait_moves()
|
|
|
|
self.cconn.finalize()
|
2021-09-26 01:54:50 +02:00
|
|
|
def _get_raw_samples(self):
|
2021-08-14 19:41:12 +02:00
|
|
|
raw_samples = self.cconn.get_messages()
|
2021-09-26 01:54:50 +02:00
|
|
|
if raw_samples:
|
|
|
|
self.raw_samples = raw_samples
|
|
|
|
return self.raw_samples
|
|
|
|
def has_valid_samples(self):
|
|
|
|
raw_samples = self._get_raw_samples()
|
|
|
|
for msg in raw_samples:
|
|
|
|
data = msg['params']['data']
|
|
|
|
first_sample_time = data[0][0]
|
|
|
|
last_sample_time = data[-1][0]
|
|
|
|
if (first_sample_time > self.request_end_time
|
|
|
|
or last_sample_time < self.request_start_time):
|
|
|
|
continue
|
|
|
|
# The time intervals [first_sample_time, last_sample_time]
|
|
|
|
# and [request_start_time, request_end_time] have non-zero
|
|
|
|
# intersection. It is still theoretically possible that none
|
|
|
|
# of the samples from raw_samples fall into the time interval
|
|
|
|
# [request_start_time, request_end_time] if it is too narrow
|
|
|
|
# or on very heavy data losses. In practice, that interval
|
|
|
|
# is at least 1 second, so this possibility is negligible.
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
def get_samples(self):
|
|
|
|
raw_samples = self._get_raw_samples()
|
2021-08-14 19:41:12 +02:00
|
|
|
if not raw_samples:
|
2020-10-15 02:08:10 +02:00
|
|
|
return self.samples
|
2021-08-14 19:41:12 +02:00
|
|
|
total = sum([len(m['params']['data']) for m in raw_samples])
|
2021-08-12 07:05:09 +02:00
|
|
|
count = 0
|
2021-08-14 19:41:12 +02:00
|
|
|
self.samples = samples = [None] * total
|
|
|
|
for msg in raw_samples:
|
|
|
|
for samp_time, x, y, z in msg['params']['data']:
|
2021-08-12 07:05:09 +02:00
|
|
|
if samp_time < self.request_start_time:
|
|
|
|
continue
|
|
|
|
if samp_time > self.request_end_time:
|
|
|
|
break
|
|
|
|
samples[count] = Accel_Measurement(samp_time, x, y, z)
|
|
|
|
count += 1
|
|
|
|
del samples[count:]
|
2020-10-15 02:08:10 +02:00
|
|
|
return self.samples
|
|
|
|
def write_to_file(self, filename):
|
|
|
|
def write_impl():
|
|
|
|
try:
|
|
|
|
# Try to re-nice writing process
|
|
|
|
os.nice(20)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
f = open(filename, "w")
|
2021-08-14 19:41:12 +02:00
|
|
|
f.write("#time,accel_x,accel_y,accel_z\n")
|
2021-09-26 01:54:50 +02:00
|
|
|
samples = self.samples or self.get_samples()
|
2020-10-15 02:08:10 +02:00
|
|
|
for t, accel_x, accel_y, accel_z in samples:
|
|
|
|
f.write("%.6f,%.6f,%.6f,%.6f\n" % (
|
|
|
|
t, accel_x, accel_y, accel_z))
|
|
|
|
f.close()
|
|
|
|
write_proc = multiprocessing.Process(target=write_impl)
|
|
|
|
write_proc.daemon = True
|
|
|
|
write_proc.start()
|
2020-07-30 07:32:17 +02:00
|
|
|
|
2021-08-12 03:54:19 +02:00
|
|
|
# Helper class for G-Code commands
|
|
|
|
class ADXLCommandHelper:
|
|
|
|
def __init__(self, config, chip):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
self.chip = chip
|
2021-08-12 05:09:49 +02:00
|
|
|
self.bg_client = None
|
2021-08-23 19:56:01 +02:00
|
|
|
self.name = config.get_name().split()[-1]
|
2021-08-12 03:54:19 +02:00
|
|
|
self.register_commands(self.name)
|
2021-08-23 19:56:01 +02:00
|
|
|
if self.name == "adxl345":
|
2021-08-12 03:54:19 +02:00
|
|
|
self.register_commands(None)
|
|
|
|
def register_commands(self, name):
|
|
|
|
# Register commands
|
|
|
|
gcode = self.printer.lookup_object('gcode')
|
|
|
|
gcode.register_mux_command("ACCELEROMETER_MEASURE", "CHIP", name,
|
|
|
|
self.cmd_ACCELEROMETER_MEASURE,
|
|
|
|
desc=self.cmd_ACCELEROMETER_MEASURE_help)
|
|
|
|
gcode.register_mux_command("ACCELEROMETER_QUERY", "CHIP", name,
|
|
|
|
self.cmd_ACCELEROMETER_QUERY,
|
|
|
|
desc=self.cmd_ACCELEROMETER_QUERY_help)
|
2021-09-26 01:05:30 +02:00
|
|
|
gcode.register_mux_command("ACCELEROMETER_DEBUG_READ", "CHIP", name,
|
|
|
|
self.cmd_ACCELEROMETER_DEBUG_READ,
|
|
|
|
desc=self.cmd_ACCELEROMETER_DEBUG_READ_help)
|
|
|
|
gcode.register_mux_command("ACCELEROMETER_DEBUG_WRITE", "CHIP", name,
|
|
|
|
self.cmd_ACCELEROMETER_DEBUG_WRITE,
|
|
|
|
desc=self.cmd_ACCELEROMETER_DEBUG_WRITE_help)
|
2021-08-12 03:54:19 +02:00
|
|
|
cmd_ACCELEROMETER_MEASURE_help = "Start/stop accelerometer"
|
|
|
|
def cmd_ACCELEROMETER_MEASURE(self, gcmd):
|
2021-08-23 20:05:07 +02:00
|
|
|
if self.bg_client is None:
|
2021-08-12 03:54:19 +02:00
|
|
|
# Start measurements
|
2021-08-12 05:09:49 +02:00
|
|
|
self.bg_client = self.chip.start_internal_client()
|
2021-08-12 03:54:19 +02:00
|
|
|
gcmd.respond_info("adxl345 measurements started")
|
|
|
|
return
|
|
|
|
# End measurements
|
|
|
|
name = gcmd.get("NAME", time.strftime("%Y%m%d_%H%M%S"))
|
|
|
|
if not name.replace('-', '').replace('_', '').isalnum():
|
|
|
|
raise gcmd.error("Invalid adxl345 NAME parameter")
|
2021-08-12 05:09:49 +02:00
|
|
|
bg_client = self.bg_client
|
|
|
|
self.bg_client = None
|
|
|
|
bg_client.finish_measurements()
|
2021-08-12 03:54:19 +02:00
|
|
|
# Write data to file
|
2021-08-23 19:56:01 +02:00
|
|
|
if self.name == "adxl345":
|
2021-08-12 03:54:19 +02:00
|
|
|
filename = "/tmp/adxl345-%s.csv" % (name,)
|
|
|
|
else:
|
|
|
|
filename = "/tmp/adxl345-%s-%s.csv" % (self.name, name,)
|
2021-08-12 05:09:49 +02:00
|
|
|
bg_client.write_to_file(filename)
|
2021-08-12 03:54:19 +02:00
|
|
|
gcmd.respond_info("Writing raw accelerometer data to %s file"
|
|
|
|
% (filename,))
|
|
|
|
cmd_ACCELEROMETER_QUERY_help = "Query accelerometer for the current values"
|
|
|
|
def cmd_ACCELEROMETER_QUERY(self, gcmd):
|
2021-08-12 05:09:49 +02:00
|
|
|
aclient = self.chip.start_internal_client()
|
2021-08-12 03:54:19 +02:00
|
|
|
self.printer.lookup_object('toolhead').dwell(1.)
|
2021-08-12 05:09:49 +02:00
|
|
|
aclient.finish_measurements()
|
2021-09-26 01:54:50 +02:00
|
|
|
values = aclient.get_samples()
|
2021-08-12 03:54:19 +02:00
|
|
|
if not values:
|
|
|
|
raise gcmd.error("No adxl345 measurements found")
|
|
|
|
_, accel_x, accel_y, accel_z = values[-1]
|
|
|
|
gcmd.respond_info("adxl345 values (x, y, z): %.6f, %.6f, %.6f"
|
|
|
|
% (accel_x, accel_y, accel_z))
|
2021-09-26 01:05:30 +02:00
|
|
|
cmd_ACCELEROMETER_DEBUG_READ_help = "Query adxl345 register (for debugging)"
|
|
|
|
def cmd_ACCELEROMETER_DEBUG_READ(self, gcmd):
|
2021-08-12 03:54:19 +02:00
|
|
|
reg = gcmd.get("REG", minval=29, maxval=57, parser=lambda x: int(x, 0))
|
|
|
|
val = self.chip.read_reg(reg)
|
|
|
|
gcmd.respond_info("ADXL345 REG[0x%x] = 0x%x" % (reg, val))
|
2021-09-26 01:05:30 +02:00
|
|
|
cmd_ACCELEROMETER_DEBUG_WRITE_help = "Set adxl345 register (for debugging)"
|
|
|
|
def cmd_ACCELEROMETER_DEBUG_WRITE(self, gcmd):
|
2021-08-12 03:54:19 +02:00
|
|
|
reg = gcmd.get("REG", minval=29, maxval=57, parser=lambda x: int(x, 0))
|
|
|
|
val = gcmd.get("VAL", minval=0, maxval=255, parser=lambda x: int(x, 0))
|
|
|
|
self.chip.set_reg(reg, val)
|
|
|
|
|
2021-08-14 19:41:12 +02:00
|
|
|
# Helper class for chip clock synchronization via linear regression
|
|
|
|
class ClockSyncRegression:
|
|
|
|
def __init__(self, mcu, chip_clock_smooth, decay = 1. / 20.):
|
|
|
|
self.mcu = mcu
|
|
|
|
self.chip_clock_smooth = chip_clock_smooth
|
|
|
|
self.decay = decay
|
|
|
|
self.last_chip_clock = self.last_exp_mcu_clock = 0.
|
|
|
|
self.mcu_clock_avg = self.mcu_clock_variance = 0.
|
|
|
|
self.chip_clock_avg = self.chip_clock_covariance = 0.
|
|
|
|
def reset(self, mcu_clock, chip_clock):
|
|
|
|
self.mcu_clock_avg = self.last_mcu_clock = mcu_clock
|
|
|
|
self.chip_clock_avg = chip_clock
|
|
|
|
self.mcu_clock_variance = self.chip_clock_covariance = 0.
|
|
|
|
self.last_chip_clock = self.last_exp_mcu_clock = 0.
|
|
|
|
def update(self, mcu_clock, chip_clock):
|
|
|
|
# Update linear regression
|
|
|
|
decay = self.decay
|
|
|
|
diff_mcu_clock = mcu_clock - self.mcu_clock_avg
|
|
|
|
self.mcu_clock_avg += decay * diff_mcu_clock
|
|
|
|
self.mcu_clock_variance = (1. - decay) * (
|
|
|
|
self.mcu_clock_variance + diff_mcu_clock**2 * decay)
|
|
|
|
diff_chip_clock = chip_clock - self.chip_clock_avg
|
|
|
|
self.chip_clock_avg += decay * diff_chip_clock
|
|
|
|
self.chip_clock_covariance = (1. - decay) * (
|
|
|
|
self.chip_clock_covariance + diff_mcu_clock*diff_chip_clock*decay)
|
|
|
|
def set_last_chip_clock(self, chip_clock):
|
|
|
|
base_mcu, base_chip, inv_cfreq = self.get_clock_translation()
|
|
|
|
self.last_chip_clock = chip_clock
|
|
|
|
self.last_exp_mcu_clock = base_mcu + (chip_clock-base_chip) * inv_cfreq
|
|
|
|
def get_clock_translation(self):
|
|
|
|
inv_chip_freq = self.mcu_clock_variance / self.chip_clock_covariance
|
|
|
|
if not self.last_chip_clock:
|
|
|
|
return self.mcu_clock_avg, self.chip_clock_avg, inv_chip_freq
|
|
|
|
# Find mcu clock associated with future chip_clock
|
|
|
|
s_chip_clock = self.last_chip_clock + self.chip_clock_smooth
|
|
|
|
scdiff = s_chip_clock - self.chip_clock_avg
|
|
|
|
s_mcu_clock = self.mcu_clock_avg + scdiff * inv_chip_freq
|
|
|
|
# Calculate frequency to converge at future point
|
|
|
|
mdiff = s_mcu_clock - self.last_exp_mcu_clock
|
|
|
|
s_inv_chip_freq = mdiff / self.chip_clock_smooth
|
|
|
|
return self.last_exp_mcu_clock, self.last_chip_clock, s_inv_chip_freq
|
|
|
|
def get_time_translation(self):
|
|
|
|
base_mcu, base_chip, inv_cfreq = self.get_clock_translation()
|
|
|
|
clock_to_print_time = self.mcu.clock_to_print_time
|
|
|
|
base_time = clock_to_print_time(base_mcu)
|
|
|
|
inv_freq = clock_to_print_time(base_mcu + inv_cfreq) - base_time
|
|
|
|
return base_time, base_chip, inv_freq
|
|
|
|
|
2021-08-12 07:05:09 +02:00
|
|
|
MIN_MSG_TIME = 0.100
|
|
|
|
|
2021-08-22 22:24:16 +02:00
|
|
|
BYTES_PER_SAMPLE = 5
|
|
|
|
SAMPLES_PER_BLOCK = 10
|
|
|
|
|
2021-03-26 19:32:32 +01:00
|
|
|
# Printer class that controls ADXL345 chip
|
2020-07-30 07:32:17 +02:00
|
|
|
class ADXL345:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
2021-08-12 03:54:19 +02:00
|
|
|
ADXLCommandHelper(config, self)
|
2020-07-30 07:32:17 +02:00
|
|
|
self.query_rate = 0
|
|
|
|
am = {'x': (0, SCALE), 'y': (1, SCALE), 'z': (2, SCALE),
|
|
|
|
'-x': (0, -SCALE), '-y': (1, -SCALE), '-z': (2, -SCALE)}
|
2021-08-19 19:27:46 +02:00
|
|
|
axes_map = config.getlist('axes_map', ('x','y','z'), count=3)
|
|
|
|
if any([a not in am for a in axes_map]):
|
2020-07-30 07:32:17 +02:00
|
|
|
raise config.error("Invalid adxl345 axes_map parameter")
|
|
|
|
self.axes_map = [am[a.strip()] for a in axes_map]
|
2020-10-15 02:08:10 +02:00
|
|
|
self.data_rate = config.getint('rate', 3200)
|
|
|
|
if self.data_rate not in QUERY_RATES:
|
|
|
|
raise config.error("Invalid rate parameter: %d" % (self.data_rate,))
|
2020-07-30 07:32:17 +02:00
|
|
|
# Measurement storage (accessed from background thread)
|
2021-08-12 07:05:09 +02:00
|
|
|
self.lock = threading.Lock()
|
2020-07-30 07:32:17 +02:00
|
|
|
self.raw_samples = []
|
|
|
|
# Setup mcu sensor_adxl345 bulk query code
|
|
|
|
self.spi = bus.MCU_SPI_from_config(config, 3, default_speed=5000000)
|
|
|
|
self.mcu = mcu = self.spi.get_mcu()
|
|
|
|
self.oid = oid = mcu.create_oid()
|
2021-08-14 19:41:12 +02:00
|
|
|
self.query_adxl345_cmd = self.query_adxl345_end_cmd = None
|
|
|
|
self.query_adxl345_status_cmd = None
|
2020-07-30 07:32:17 +02:00
|
|
|
mcu.add_config_cmd("config_adxl345 oid=%d spi_oid=%d"
|
|
|
|
% (oid, self.spi.get_oid()))
|
|
|
|
mcu.add_config_cmd("query_adxl345 oid=%d clock=0 rest_ticks=0"
|
|
|
|
% (oid,), on_restart=True)
|
|
|
|
mcu.register_config_callback(self._build_config)
|
|
|
|
mcu.register_response(self._handle_adxl345_data, "adxl345_data", oid)
|
2021-08-14 19:41:12 +02:00
|
|
|
# Clock tracking
|
2021-08-22 22:24:16 +02:00
|
|
|
self.last_sequence = self.max_query_duration = 0
|
|
|
|
self.last_limit_count = self.last_error_count = 0
|
2021-08-14 19:41:12 +02:00
|
|
|
self.clock_sync = ClockSyncRegression(self.mcu, 640)
|
2021-08-12 07:05:09 +02:00
|
|
|
# API server endpoints
|
|
|
|
self.api_dump = motion_report.APIDumpHelper(
|
|
|
|
self.printer, self._api_update, self._api_startstop, 0.100)
|
2021-08-23 19:56:01 +02:00
|
|
|
self.name = config.get_name().split()[-1]
|
2021-08-12 07:05:09 +02:00
|
|
|
wh = self.printer.lookup_object('webhooks')
|
|
|
|
wh.register_mux_endpoint("adxl345/dump_adxl345", "sensor", self.name,
|
|
|
|
self._handle_dump_adxl345)
|
2020-07-30 07:32:17 +02:00
|
|
|
def _build_config(self):
|
2021-08-14 19:41:12 +02:00
|
|
|
cmdqueue = self.spi.get_command_queue()
|
2020-07-30 07:32:17 +02:00
|
|
|
self.query_adxl345_cmd = self.mcu.lookup_command(
|
2021-08-14 19:41:12 +02:00
|
|
|
"query_adxl345 oid=%c clock=%u rest_ticks=%u", cq=cmdqueue)
|
2020-07-30 07:32:17 +02:00
|
|
|
self.query_adxl345_end_cmd = self.mcu.lookup_query_command(
|
|
|
|
"query_adxl345 oid=%c clock=%u rest_ticks=%u",
|
2021-08-14 19:41:12 +02:00
|
|
|
"adxl345_status oid=%c clock=%u query_ticks=%u next_sequence=%hu"
|
|
|
|
" buffered=%c fifo=%c limit_count=%hu", oid=self.oid, cq=cmdqueue)
|
|
|
|
self.query_adxl345_status_cmd = self.mcu.lookup_query_command(
|
|
|
|
"query_adxl345_status oid=%c",
|
|
|
|
"adxl345_status oid=%c clock=%u query_ticks=%u next_sequence=%hu"
|
|
|
|
" buffered=%c fifo=%c limit_count=%hu", oid=self.oid, cq=cmdqueue)
|
2021-03-26 19:32:32 +01:00
|
|
|
def read_reg(self, reg):
|
|
|
|
params = self.spi.spi_transfer([reg | REG_MOD_READ, 0x00])
|
|
|
|
response = bytearray(params['response'])
|
|
|
|
return response[1]
|
|
|
|
def set_reg(self, reg, val, minclock=0):
|
|
|
|
self.spi.spi_send([reg, val & 0xFF], minclock=minclock)
|
|
|
|
stored_val = self.read_reg(reg)
|
|
|
|
if stored_val != val:
|
|
|
|
raise self.printer.command_error(
|
|
|
|
"Failed to set ADXL345 register [0x%x] to 0x%x: got 0x%x. "
|
|
|
|
"This is generally indicative of connection problems "
|
|
|
|
"(e.g. faulty wiring) or a faulty adxl345 chip." % (
|
|
|
|
reg, val, stored_val))
|
2021-08-12 07:05:09 +02:00
|
|
|
# Measurement collection
|
2021-03-26 19:32:32 +01:00
|
|
|
def is_measuring(self):
|
|
|
|
return self.query_rate > 0
|
2021-08-12 07:05:09 +02:00
|
|
|
def _handle_adxl345_data(self, params):
|
|
|
|
with self.lock:
|
|
|
|
self.raw_samples.append(params)
|
|
|
|
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
|
2021-08-14 19:41:12 +02:00
|
|
|
time_base, chip_base, inv_freq = self.clock_sync.get_time_translation()
|
2021-08-12 07:05:09 +02:00
|
|
|
# Process every message in raw_samples
|
2021-08-14 19:41:12 +02:00
|
|
|
count = seq = 0
|
2021-08-22 22:24:16 +02:00
|
|
|
samples = [None] * (len(raw_samples) * SAMPLES_PER_BLOCK)
|
2021-08-12 07:05:09 +02:00
|
|
|
for params in raw_samples:
|
2021-08-14 19:41:12 +02:00
|
|
|
seq_diff = (last_sequence - params['sequence']) & 0xffff
|
|
|
|
seq_diff -= (seq_diff & 0x8000) << 1
|
|
|
|
seq = last_sequence - seq_diff
|
2021-08-12 07:05:09 +02:00
|
|
|
d = bytearray(params['data'])
|
2021-08-22 22:24:16 +02:00
|
|
|
msg_cdiff = seq * SAMPLES_PER_BLOCK - chip_base
|
|
|
|
for i in range(len(d) // BYTES_PER_SAMPLE):
|
|
|
|
d_xyz = d[i*BYTES_PER_SAMPLE:(i+1)*BYTES_PER_SAMPLE]
|
|
|
|
xlow, ylow, zlow, xzhigh, yzhigh = d_xyz
|
|
|
|
if yzhigh & 0x80:
|
|
|
|
self.last_error_count += 1
|
|
|
|
continue
|
|
|
|
rx = (xlow | ((xzhigh & 0x1f) << 8)) - ((xzhigh & 0x10) << 9)
|
|
|
|
ry = (ylow | ((yzhigh & 0x1f) << 8)) - ((yzhigh & 0x10) << 9)
|
|
|
|
rz = ((zlow | ((xzhigh & 0xe0) << 3) | ((yzhigh & 0xe0) << 6))
|
|
|
|
- ((yzhigh & 0x40) << 7))
|
|
|
|
raw_xyz = (rx, ry, rz)
|
|
|
|
x = round(raw_xyz[x_pos] * x_scale, 6)
|
|
|
|
y = round(raw_xyz[y_pos] * y_scale, 6)
|
|
|
|
z = round(raw_xyz[z_pos] * z_scale, 6)
|
2021-08-14 19:41:12 +02:00
|
|
|
ptime = round(time_base + (msg_cdiff + i) * inv_freq, 6)
|
|
|
|
samples[count] = (ptime, x, y, z)
|
2021-08-12 07:05:09 +02:00
|
|
|
count += 1
|
2021-08-22 22:24:16 +02:00
|
|
|
self.clock_sync.set_last_chip_clock(seq * SAMPLES_PER_BLOCK + i)
|
2021-08-12 07:05:09 +02:00
|
|
|
del samples[count:]
|
|
|
|
return samples
|
2021-08-14 19:41:12 +02:00
|
|
|
def _update_clock(self, minclock=0):
|
|
|
|
# Query current state
|
|
|
|
for retry in range(5):
|
|
|
|
params = self.query_adxl345_status_cmd.send([self.oid],
|
|
|
|
minclock=minclock)
|
|
|
|
fifo = params['fifo'] & 0x7f
|
|
|
|
if fifo <= 32:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise self.printer.command_error("Unable to query adxl345 fifo")
|
|
|
|
mcu_clock = self.mcu.clock32_to_clock64(params['clock'])
|
|
|
|
sequence = (self.last_sequence & ~0xffff) | params['next_sequence']
|
|
|
|
if sequence < self.last_sequence:
|
|
|
|
sequence += 0x10000
|
|
|
|
self.last_sequence = sequence
|
|
|
|
buffered = params['buffered']
|
|
|
|
limit_count = (self.last_limit_count & ~0xffff) | params['limit_count']
|
|
|
|
if limit_count < self.last_limit_count:
|
|
|
|
limit_count += 0x10000
|
|
|
|
self.last_limit_count = limit_count
|
|
|
|
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
|
2021-08-22 22:24:16 +02:00
|
|
|
msg_count = (sequence * SAMPLES_PER_BLOCK
|
|
|
|
+ buffered // BYTES_PER_SAMPLE + fifo)
|
2021-08-14 19:41:12 +02:00
|
|
|
# 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)
|
2021-08-12 05:09:49 +02:00
|
|
|
def _start_measurements(self):
|
2021-03-26 19:32:32 +01:00
|
|
|
if self.is_measuring():
|
|
|
|
return
|
2021-08-12 04:16:03 +02:00
|
|
|
# In case of miswiring, testing ADXL345 device ID prevents treating
|
|
|
|
# noise or wrong signal as a correctly initialized device
|
|
|
|
dev_id = self.read_reg(REG_DEVID)
|
|
|
|
if dev_id != ADXL345_DEV_ID:
|
2021-09-26 01:57:10 +02:00
|
|
|
raise self.printer.command_error(
|
|
|
|
"Invalid adxl345 id (got %x vs %x).\n"
|
|
|
|
"This is generally indicative of connection problems\n"
|
|
|
|
"(e.g. faulty wiring) or a faulty adxl345 chip."
|
|
|
|
% (dev_id, ADXL345_DEV_ID))
|
2020-07-30 07:32:17 +02:00
|
|
|
# Setup chip in requested query rate
|
2021-08-12 07:05:09 +02:00
|
|
|
self.set_reg(REG_POWER_CTL, 0x00)
|
2021-08-12 04:16:03 +02:00
|
|
|
self.set_reg(REG_DATA_FORMAT, 0x0B)
|
2021-03-26 19:32:32 +01:00
|
|
|
self.set_reg(REG_FIFO_CTL, 0x00)
|
2021-08-12 03:54:19 +02:00
|
|
|
self.set_reg(REG_BW_RATE, QUERY_RATES[self.data_rate])
|
2021-08-22 22:24:16 +02:00
|
|
|
self.set_reg(REG_FIFO_CTL, SET_FIFO_CTL)
|
2020-07-30 07:32:17 +02:00
|
|
|
# Setup samples
|
2021-08-12 07:05:09 +02:00
|
|
|
with self.lock:
|
|
|
|
self.raw_samples = []
|
2020-07-30 07:32:17 +02:00
|
|
|
# Start bulk reading
|
2021-08-14 19:41:12 +02:00
|
|
|
systime = self.printer.get_reactor().monotonic()
|
|
|
|
print_time = self.mcu.estimated_print_time(systime) + MIN_MSG_TIME
|
2020-07-30 07:32:17 +02:00
|
|
|
reqclock = self.mcu.print_time_to_clock(print_time)
|
2021-08-12 03:54:19 +02:00
|
|
|
rest_ticks = self.mcu.seconds_to_clock(4. / self.data_rate)
|
|
|
|
self.query_rate = self.data_rate
|
2020-07-30 07:32:17 +02:00
|
|
|
self.query_adxl345_cmd.send([self.oid, reqclock, rest_ticks],
|
|
|
|
reqclock=reqclock)
|
2021-08-12 07:05:09 +02:00
|
|
|
logging.info("ADXL345 starting '%s' measurements", self.name)
|
2021-08-14 19:41:12 +02:00
|
|
|
# Initialize clock tracking
|
|
|
|
self.last_sequence = 0
|
2021-08-22 22:24:16 +02:00
|
|
|
self.last_limit_count = self.last_error_count = 0
|
2021-08-14 19:41:12 +02:00
|
|
|
self.clock_sync.reset(reqclock, 0)
|
|
|
|
self.max_query_duration = 1 << 31
|
|
|
|
self._update_clock(minclock=reqclock)
|
|
|
|
self.max_query_duration = 1 << 31
|
2021-08-12 05:09:49 +02:00
|
|
|
def _finish_measurements(self):
|
2021-03-26 19:32:32 +01:00
|
|
|
if not self.is_measuring():
|
2021-08-12 05:09:49 +02:00
|
|
|
return
|
2020-07-30 07:32:17 +02:00
|
|
|
# Halt bulk reading
|
2021-08-12 07:05:09 +02:00
|
|
|
params = self.query_adxl345_end_cmd.send([self.oid, 0, 0])
|
2020-07-30 07:32:17 +02:00
|
|
|
self.query_rate = 0
|
2021-08-12 07:05:09 +02:00
|
|
|
with self.lock:
|
|
|
|
self.raw_samples = []
|
|
|
|
logging.info("ADXL345 finished '%s' measurements", self.name)
|
|
|
|
# API interface
|
|
|
|
def _api_update(self, eventtime):
|
2021-08-14 19:41:12 +02:00
|
|
|
self._update_clock()
|
2021-08-12 07:05:09 +02:00
|
|
|
with self.lock:
|
|
|
|
raw_samples = self.raw_samples
|
|
|
|
self.raw_samples = []
|
|
|
|
if not raw_samples:
|
|
|
|
return {}
|
|
|
|
samples = self._extract_samples(raw_samples)
|
|
|
|
if not samples:
|
|
|
|
return {}
|
2021-08-22 22:24:16 +02:00
|
|
|
return {'data': samples, 'errors': self.last_error_count,
|
|
|
|
'overflows': self.last_limit_count}
|
2021-08-12 07:05:09 +02:00
|
|
|
def _api_startstop(self, is_start):
|
|
|
|
if is_start:
|
|
|
|
self._start_measurements()
|
|
|
|
else:
|
|
|
|
self._finish_measurements()
|
|
|
|
def _handle_dump_adxl345(self, web_request):
|
|
|
|
self.api_dump.add_client(web_request)
|
2021-08-14 19:41:12 +02:00
|
|
|
hdr = ('time', 'x_acceleration', 'y_acceleration', 'z_acceleration')
|
2021-08-12 07:05:09 +02:00
|
|
|
web_request.send({'header': hdr})
|
2021-08-12 05:09:49 +02:00
|
|
|
def start_internal_client(self):
|
2021-08-12 07:05:09 +02:00
|
|
|
cconn = self.api_dump.add_internal_client()
|
2021-08-14 19:41:12 +02:00
|
|
|
return ADXL345QueryHelper(self.printer, cconn)
|
2020-07-30 07:32:17 +02:00
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return ADXL345(config)
|
|
|
|
|
|
|
|
def load_config_prefix(config):
|
|
|
|
return ADXL345(config)
|