aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/extras/adxl345.py
blob: 7870bd4af50da7242e2f2b7a41b12e134b2864f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Support for reading acceleration data from an adxl345 chip
#
# 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
from . import bus, bulk_sensor

# 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,
}

ADXL345_DEV_ID = 0xE5
SET_FIFO_CTL = 0x90

FREEFALL_ACCEL = 9.80665 * 1000.0
SCALE_XY = 0.003774 * FREEFALL_ACCEL  # 1 / 265 (at 3.3V) mg/LSB
SCALE_Z = 0.003906 * FREEFALL_ACCEL  # 1 / 256 (at 3.3V) mg/LSB

Accel_Measurement = collections.namedtuple(
    "Accel_Measurement", ("time", "accel_x", "accel_y", "accel_z")
)


# Helper class to obtain measurements
class AccelQueryHelper:
    def __init__(self, printer):
        self.printer = printer
        self.is_finished = False
        print_time = printer.lookup_object("toolhead").get_last_move_time()
        self.request_start_time = self.request_end_time = print_time
        self.msgs = []
        self.samples = []

    def finish_measurements(self):
        toolhead = self.printer.lookup_object("toolhead")
        self.request_end_time = toolhead.get_last_move_time()
        toolhead.wait_moves()
        self.is_finished = True

    def handle_batch(self, msg):
        if self.is_finished:
            return False
        if len(self.msgs) >= 10000:
            # Avoid filling up memory with too many samples
            return False
        self.msgs.append(msg)
        return True

    def has_valid_samples(self):
        for msg in self.msgs:
            data = msg["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 msgs 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):
        if not self.msgs:
            return self.samples
        total = sum([len(m["data"]) for m in self.msgs])
        count = 0
        self.samples = samples = [None] * total
        for msg in self.msgs:
            for samp_time, x, y, z in msg["data"]:
                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:]
        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("#time,accel_x,accel_y,accel_z\n")
            samples = self.samples or self.get_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()


# Helper class for G-Code commands
class AccelCommandHelper:
    def __init__(self, config, chip):
        self.printer = config.get_printer()
        self.chip = chip
        self.bg_client = None
        name_parts = config.get_name().split()
        self.base_name = name_parts[0]
        self.name = name_parts[-1]
        self.register_commands(self.name)
        if len(name_parts) == 1:
            if self.name == "adxl345" or not config.has_section("adxl345"):
                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(
            "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,
        )

    cmd_ACCELEROMETER_MEASURE_help = "Start/stop accelerometer"

    def cmd_ACCELEROMETER_MEASURE(self, gcmd):
        if self.bg_client is None:
            # Start measurements
            self.bg_client = self.chip.start_internal_client()
            gcmd.respond_info("accelerometer 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 NAME parameter")
        bg_client = self.bg_client
        self.bg_client = None
        bg_client.finish_measurements()
        # Write data to file
        if self.base_name == self.name:
            filename = "/tmp/%s-%s.csv" % (self.base_name, name)
        else:
            filename = "/tmp/%s-%s-%s.csv" % (self.base_name, self.name, name)
        bg_client.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):
        aclient = self.chip.start_internal_client()
        self.printer.lookup_object("toolhead").dwell(1.0)
        aclient.finish_measurements()
        values = aclient.get_samples()
        if not values:
            raise gcmd.error("No accelerometer measurements found")
        _, accel_x, accel_y, accel_z = values[-1]
        gcmd.respond_info(
            "accelerometer values (x, y, z): %.6f, %.6f, %.6f"
            % (accel_x, accel_y, accel_z)
        )

    cmd_ACCELEROMETER_DEBUG_READ_help = "Query register (for debugging)"

    def cmd_ACCELEROMETER_DEBUG_READ(self, gcmd):
        reg = gcmd.get("REG", minval=0, maxval=127, parser=lambda x: int(x, 0))
        val = self.chip.read_reg(reg)
        gcmd.respond_info("Accelerometer REG[0x%x] = 0x%x" % (reg, val))

    cmd_ACCELEROMETER_DEBUG_WRITE_help = "Set register (for debugging)"

    def cmd_ACCELEROMETER_DEBUG_WRITE(self, gcmd):
        reg = gcmd.get("REG", minval=0, maxval=127, 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)


# Helper to read the axes_map parameter from the config
def read_axes_map(config, scale_x, scale_y, scale_z):
    am = {
        "x": (0, scale_x),
        "y": (1, scale_y),
        "z": (2, scale_z),
        "-x": (0, -scale_x),
        "-y": (1, -scale_y),
        "-z": (2, -scale_z),
    }
    axes_map = config.getlist("axes_map", ("x", "y", "z"), count=3)
    if any([a not in am for a in axes_map]):
        raise config.error("Invalid axes_map parameter")
    return [am[a.strip()] for a in axes_map]


BATCH_UPDATES = 0.100


# Printer class that controls ADXL345 chip
class ADXL345:
    def __init__(self, config):
        self.printer = config.get_printer()
        AccelCommandHelper(config, self)
        self.axes_map = read_axes_map(config, SCALE_XY, SCALE_XY, SCALE_Z)
        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,))
        # 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 = 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 rest_ticks=0" % (oid,), on_restart=True
        )
        mcu.register_config_callback(self._build_config)
        # Bulk sample message reading
        chip_smooth = self.data_rate * BATCH_UPDATES * 2
        self.ffreader = bulk_sensor.FixedFreqReader(mcu, chip_smooth, "BBBBB")
        self.last_error_count = 0
        # Process messages in batches
        self.batch_bulk = bulk_sensor.BatchBulkHelper(
            self.printer,
            self._process_batch,
            self._start_measurements,
            self._finish_measurements,
            BATCH_UPDATES,
        )
        self.name = config.get_name().split()[-1]
        hdr = ("time", "x_acceleration", "y_acceleration", "z_acceleration")
        self.batch_bulk.add_mux_endpoint(
            "adxl345/dump_adxl345", "sensor", self.name, {"header": hdr}
        )

    def _build_config(self):
        cmdqueue = self.spi.get_command_queue()
        self.query_adxl345_cmd = self.mcu.lookup_command(
            "query_adxl345 oid=%c rest_ticks=%u", cq=cmdqueue
        )
        self.ffreader.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"])
        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 start_internal_client(self):
        aqh = AccelQueryHelper(self.printer)
        self.batch_bulk.add_client(aqh.handle_batch)
        return aqh

    # Measurement decoding
    def _convert_samples(self, samples):
        (x_pos, x_scale), (y_pos, y_scale), (z_pos, z_scale) = self.axes_map
        count = 0
        for ptime, xlow, ylow, zlow, xzhigh, yzhigh in samples:
            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)
            samples[count] = (round(ptime, 6), x, y, z)
            count += 1
        del samples[count:]

    # Start, stop, and process message batches
    def _start_measurements(self):
        # 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:
            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)
            )
        # Setup chip in requested query rate
        self.set_reg(REG_POWER_CTL, 0x00)
        self.set_reg(REG_DATA_FORMAT, 0x0B)
        self.set_reg(REG_FIFO_CTL, 0x00)
        self.set_reg(REG_BW_RATE, QUERY_RATES[self.data_rate])
        self.set_reg(REG_FIFO_CTL, SET_FIFO_CTL)
        # Start bulk reading
        rest_ticks = self.mcu.seconds_to_clock(4.0 / self.data_rate)
        self.query_adxl345_cmd.send([self.oid, rest_ticks])
        self.set_reg(REG_POWER_CTL, 0x08)
        logging.info("ADXL345 starting '%s' measurements", self.name)
        # Initialize clock tracking
        self.ffreader.note_start()
        self.last_error_count = 0

    def _finish_measurements(self):
        # Halt bulk reading
        self.set_reg(REG_POWER_CTL, 0x00)
        self.query_adxl345_cmd.send_wait_ack([self.oid, 0])
        self.ffreader.note_end()
        logging.info("ADXL345 finished '%s' measurements", self.name)

    def _process_batch(self, eventtime):
        samples = self.ffreader.pull_samples()
        self._convert_samples(samples)
        if not samples:
            return {}
        return {
            "data": samples,
            "errors": self.last_error_count,
            "overflows": self.ffreader.get_last_overflows(),
        }


def load_config(config):
    return ADXL345(config)


def load_config_prefix(config):
    return ADXL345(config)