2020-07-30 07:32:17 +02:00
|
|
|
# Support for reading acceleration data from an adxl345 chip
|
|
|
|
#
|
|
|
|
# Copyright (C) 2020 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2020-10-15 02:08:10 +02:00
|
|
|
import logging, time, collections, multiprocessing, os
|
2020-07-30 07:32:17 +02:00
|
|
|
from . import bus
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
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'))
|
|
|
|
|
|
|
|
# Sample results
|
|
|
|
class ADXL345Results:
|
|
|
|
def __init__(self):
|
2020-10-15 02:08:10 +02:00
|
|
|
self.raw_samples = None
|
2020-07-30 07:32:17 +02:00
|
|
|
self.samples = []
|
|
|
|
self.drops = self.overflows = 0
|
|
|
|
self.time_per_sample = self.start_range = self.end_range = 0.
|
|
|
|
def get_stats(self):
|
|
|
|
return ("drops=%d,overflows=%d"
|
|
|
|
",time_per_sample=%.9f,start_range=%.6f,end_range=%.6f"
|
|
|
|
% (self.drops, self.overflows,
|
|
|
|
self.time_per_sample, self.start_range, self.end_range))
|
|
|
|
def setup_data(self, axes_map, raw_samples, end_sequence, overflows,
|
|
|
|
start1_time, start2_time, end1_time, end2_time):
|
|
|
|
if not raw_samples or not end_sequence:
|
|
|
|
return
|
2020-10-15 02:08:10 +02:00
|
|
|
self.axes_map = axes_map
|
|
|
|
self.raw_samples = raw_samples
|
2020-07-30 07:32:17 +02:00
|
|
|
self.overflows = overflows
|
2020-10-15 02:08:10 +02:00
|
|
|
self.start2_time = start2_time
|
2020-07-30 07:32:17 +02:00
|
|
|
self.start_range = start2_time - start1_time
|
|
|
|
self.end_range = end2_time - end1_time
|
2020-10-15 02:08:10 +02:00
|
|
|
self.total_count = (end_sequence - 1) * 8 + len(raw_samples[-1][1]) // 6
|
2020-07-30 07:32:17 +02:00
|
|
|
total_time = end2_time - start2_time
|
2020-10-15 02:08:10 +02:00
|
|
|
self.time_per_sample = time_per_sample = total_time / self.total_count
|
|
|
|
self.seq_to_time = time_per_sample * 8.
|
|
|
|
actual_count = sum([len(data)//6 for _, data in raw_samples])
|
|
|
|
self.drops = self.total_count - actual_count
|
|
|
|
def decode_samples(self):
|
|
|
|
if not self.raw_samples:
|
|
|
|
return self.samples
|
|
|
|
(x_pos, x_scale), (y_pos, y_scale), (z_pos, z_scale) = self.axes_map
|
2020-07-30 07:32:17 +02:00
|
|
|
actual_count = 0
|
2020-10-15 02:08:10 +02:00
|
|
|
self.samples = samples = [None] * self.total_count
|
|
|
|
for seq, data in self.raw_samples:
|
2020-07-30 07:32:17 +02:00
|
|
|
d = bytearray(data)
|
|
|
|
count = len(data)
|
|
|
|
sdata = [(d[i] | (d[i+1] << 8)) - ((d[i+1] & 0x80) << 9)
|
|
|
|
for i in range(0, count-1, 2)]
|
2020-10-15 02:08:10 +02:00
|
|
|
seq_time = self.start2_time + seq * self.seq_to_time
|
2020-07-30 07:32:17 +02:00
|
|
|
for i in range(count//6):
|
2020-10-15 02:08:10 +02:00
|
|
|
samp_time = seq_time + i * self.time_per_sample
|
2020-07-30 07:32:17 +02:00
|
|
|
x = sdata[i*3 + x_pos] * x_scale
|
|
|
|
y = sdata[i*3 + y_pos] * y_scale
|
|
|
|
z = sdata[i*3 + z_pos] * z_scale
|
|
|
|
samples[actual_count] = Accel_Measurement(samp_time, x, y, z)
|
|
|
|
actual_count += 1
|
|
|
|
del samples[actual_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")
|
|
|
|
f.write("##%s\n#time,accel_x,accel_y,accel_z\n" % (
|
|
|
|
self.get_stats(),))
|
|
|
|
samples = self.samples or self.decode_samples()
|
|
|
|
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
|
|
|
|
self.name = "default"
|
|
|
|
if len(config.get_name().split()) > 1:
|
|
|
|
self.name = config.get_name().split()[1]
|
|
|
|
self.register_commands(self.name)
|
|
|
|
if self.name == "default":
|
|
|
|
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)
|
|
|
|
gcode.register_mux_command("ADXL345_DEBUG_READ", "CHIP", name,
|
|
|
|
self.cmd_ADXL345_DEBUG_READ,
|
|
|
|
desc=self.cmd_ADXL345_DEBUG_READ_help)
|
|
|
|
gcode.register_mux_command("ADXL345_DEBUG_WRITE", "CHIP", name,
|
|
|
|
self.cmd_ADXL345_DEBUG_WRITE,
|
|
|
|
desc=self.cmd_ADXL345_DEBUG_WRITE_help)
|
|
|
|
cmd_ACCELEROMETER_MEASURE_help = "Start/stop accelerometer"
|
|
|
|
def cmd_ACCELEROMETER_MEASURE(self, gcmd):
|
|
|
|
if not self.chip.is_measuring():
|
|
|
|
# Start measurements
|
|
|
|
self.chip.start_measurements()
|
|
|
|
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")
|
|
|
|
res = self.chip.finish_measurements()
|
|
|
|
# Write data to file
|
|
|
|
if self.name == "default":
|
|
|
|
filename = "/tmp/adxl345-%s.csv" % (name,)
|
|
|
|
else:
|
|
|
|
filename = "/tmp/adxl345-%s-%s.csv" % (self.name, name,)
|
|
|
|
res.write_to_file(filename)
|
|
|
|
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):
|
|
|
|
if self.chip.is_measuring():
|
|
|
|
raise gcmd.error("adxl345 measurements in progress")
|
|
|
|
self.chip.start_measurements()
|
|
|
|
self.printer.lookup_object('toolhead').dwell(1.)
|
|
|
|
result = self.chip.finish_measurements()
|
|
|
|
values = result.decode_samples()
|
|
|
|
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))
|
|
|
|
cmd_ADXL345_DEBUG_READ_help = "Query accelerometer register (for debugging)"
|
|
|
|
def cmd_ADXL345_DEBUG_READ(self, gcmd):
|
|
|
|
if self.chip.is_measuring():
|
|
|
|
raise gcmd.error("adxl345 measurements in progress")
|
|
|
|
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))
|
|
|
|
cmd_ADXL345_DEBUG_WRITE_help = "Set accelerometer register (for debugging)"
|
|
|
|
def cmd_ADXL345_DEBUG_WRITE(self, gcmd):
|
|
|
|
if self.chip.is_measuring():
|
|
|
|
raise gcmd.error("adxl345 measurements in progress")
|
|
|
|
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-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
|
|
|
|
self.last_tx_time = 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)
|
|
|
|
self.raw_samples = []
|
|
|
|
self.last_sequence = 0
|
|
|
|
self.samples_start1 = self.samples_start2 = 0.
|
|
|
|
# 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()
|
|
|
|
self.query_adxl345_cmd = self.query_adxl345_end_cmd =None
|
|
|
|
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_start, "adxl345_start", oid)
|
|
|
|
mcu.register_response(self._handle_adxl345_data, "adxl345_data", oid)
|
2021-03-26 19:32:32 +01:00
|
|
|
def is_initialized(self):
|
|
|
|
# In case of miswiring, testing ADXL345 device ID prevents treating
|
|
|
|
# noise or wrong signal as a correctly initialized device
|
|
|
|
return (self.read_reg(REG_DEVID) == ADXL345_DEV_ID and
|
|
|
|
(self.read_reg(REG_DATA_FORMAT) & 0xB) != 0)
|
|
|
|
def initialize(self):
|
|
|
|
# Setup ADXL345 parameters and verify chip connectivity
|
|
|
|
self.set_reg(REG_POWER_CTL, 0x00)
|
|
|
|
dev_id = self.read_reg(REG_DEVID)
|
|
|
|
if dev_id != ADXL345_DEV_ID:
|
|
|
|
raise self.printer.command_error("Invalid adxl345 id (got %x vs %x)"
|
|
|
|
% (dev_id, ADXL345_DEV_ID))
|
|
|
|
self.set_reg(REG_DATA_FORMAT, 0x0B)
|
2020-07-30 07:32:17 +02:00
|
|
|
def _build_config(self):
|
|
|
|
self.query_adxl345_cmd = self.mcu.lookup_command(
|
|
|
|
"query_adxl345 oid=%c clock=%u rest_ticks=%u",
|
|
|
|
cq=self.spi.get_command_queue())
|
|
|
|
self.query_adxl345_end_cmd = self.mcu.lookup_query_command(
|
|
|
|
"query_adxl345 oid=%c clock=%u rest_ticks=%u",
|
2021-04-30 02:49:43 +02:00
|
|
|
"adxl345_end oid=%c end1_clock=%u end2_clock=%u"
|
2020-07-30 07:32:17 +02:00
|
|
|
" limit_count=%hu sequence=%hu",
|
|
|
|
oid=self.oid, cq=self.spi.get_command_queue())
|
|
|
|
def _clock_to_print_time(self, clock):
|
|
|
|
return self.mcu.clock_to_print_time(self.mcu.clock32_to_clock64(clock))
|
|
|
|
def _handle_adxl345_start(self, params):
|
2021-04-30 02:49:43 +02:00
|
|
|
self.samples_start1 = self._clock_to_print_time(params['start1_clock'])
|
|
|
|
self.samples_start2 = self._clock_to_print_time(params['start2_clock'])
|
2020-07-30 07:32:17 +02:00
|
|
|
def _handle_adxl345_data(self, params):
|
|
|
|
last_sequence = self.last_sequence
|
|
|
|
sequence = (last_sequence & ~0xffff) | params['sequence']
|
|
|
|
if sequence < last_sequence:
|
|
|
|
sequence += 0x10000
|
|
|
|
self.last_sequence = sequence
|
|
|
|
raw_samples = self.raw_samples
|
2020-10-15 02:08:10 +02:00
|
|
|
if len(raw_samples) >= 300000:
|
2020-07-30 07:32:17 +02:00
|
|
|
# Avoid filling up memory with too many samples
|
|
|
|
return
|
|
|
|
raw_samples.append((sequence, params['data']))
|
|
|
|
def _convert_sequence(self, sequence):
|
|
|
|
sequence = (self.last_sequence & ~0xffff) | sequence
|
|
|
|
if sequence < self.last_sequence:
|
|
|
|
sequence += 0x10000
|
|
|
|
return sequence
|
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))
|
|
|
|
def is_measuring(self):
|
|
|
|
return self.query_rate > 0
|
2021-08-12 03:54:19 +02:00
|
|
|
def start_measurements(self):
|
2021-03-26 19:32:32 +01:00
|
|
|
if self.is_measuring():
|
|
|
|
return
|
|
|
|
if not self.is_initialized():
|
|
|
|
self.initialize()
|
2020-07-30 07:32:17 +02:00
|
|
|
# Setup chip in requested query rate
|
|
|
|
clock = 0
|
|
|
|
if self.last_tx_time:
|
|
|
|
clock = self.mcu.print_time_to_clock(self.last_tx_time)
|
2021-03-26 19:32:32 +01:00
|
|
|
self.set_reg(REG_POWER_CTL, 0x00, minclock=clock)
|
|
|
|
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-03-26 19:32:32 +01:00
|
|
|
self.set_reg(REG_FIFO_CTL, 0x80)
|
2020-07-30 07:32:17 +02:00
|
|
|
# Setup samples
|
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
self.raw_samples = []
|
|
|
|
self.last_sequence = 0
|
|
|
|
self.samples_start1 = self.samples_start2 = print_time
|
|
|
|
# Start bulk reading
|
|
|
|
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)
|
2020-07-30 07:32:17 +02:00
|
|
|
self.last_tx_time = print_time
|
2021-08-12 03:54:19 +02:00
|
|
|
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)
|
|
|
|
def finish_measurements(self):
|
2021-03-26 19:32:32 +01:00
|
|
|
if not self.is_measuring():
|
2020-07-30 07:32:17 +02:00
|
|
|
return ADXL345Results()
|
|
|
|
# Halt bulk reading
|
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
clock = self.mcu.print_time_to_clock(print_time)
|
|
|
|
params = self.query_adxl345_end_cmd.send([self.oid, 0, 0],
|
|
|
|
minclock=clock)
|
|
|
|
self.last_tx_time = print_time
|
|
|
|
self.query_rate = 0
|
|
|
|
raw_samples = self.raw_samples
|
|
|
|
self.raw_samples = []
|
|
|
|
# Generate results
|
2021-04-30 02:49:43 +02:00
|
|
|
end1_time = self._clock_to_print_time(params['end1_clock'])
|
|
|
|
end2_time = self._clock_to_print_time(params['end2_clock'])
|
2020-07-30 07:32:17 +02:00
|
|
|
end_sequence = self._convert_sequence(params['sequence'])
|
|
|
|
overflows = params['limit_count']
|
|
|
|
res = ADXL345Results()
|
|
|
|
res.setup_data(self.axes_map, raw_samples, end_sequence, overflows,
|
|
|
|
self.samples_start1, self.samples_start2,
|
|
|
|
end1_time, end2_time)
|
|
|
|
logging.info("ADXL345 finished %d measurements: %s",
|
2020-10-15 02:08:10 +02:00
|
|
|
res.total_count, res.get_stats())
|
2020-07-30 07:32:17 +02:00
|
|
|
return res
|
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return ADXL345(config)
|
|
|
|
|
|
|
|
def load_config_prefix(config):
|
|
|
|
return ADXL345(config)
|