forked from Klipper3d/klipper
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adxl345: improve ACCELEROMETER_QUERY command (Klipper3d#124)
Signed-off-by: Lasse Dalegaard <[email protected]>
- Loading branch information
Showing
3 changed files
with
46 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
# Copyright (C) 2020-2021 Kevin O'Connor <[email protected]> | ||
# | ||
# This file may be distributed under the terms of the GNU GPLv3 license. | ||
import logging, time, collections, threading, multiprocessing, os | ||
import logging, time, collections, threading, multiprocessing, os, math | ||
from . import bus, motion_report | ||
|
||
# ADXL345 registers | ||
|
@@ -37,6 +37,7 @@ | |
"Accel_Measurement", ("time", "accel_x", "accel_y", "accel_z") | ||
) | ||
|
||
|
||
# Helper class to obtain measurements | ||
class AccelQueryHelper: | ||
def __init__(self, printer, cconn): | ||
|
@@ -192,17 +193,38 @@ def cmd_ACCELEROMETER_MEASURE(self, gcmd): | |
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) | ||
) | ||
num_samples = gcmd.get_int("SAMPLES", 1) | ||
samples = [] | ||
while num_samples > 0: | ||
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") | ||
take = min(len(values), num_samples) | ||
num_samples -= take | ||
samples.extend(values[-take:]) | ||
|
||
accel_x = sum([x for (_, x, y, z) in samples]) / len(samples) | ||
accel_y = sum([y for (_, x, y, z) in samples]) / len(samples) | ||
accel_z = sum([z for (_, x, y, z) in samples]) / len(samples) | ||
|
||
return_type = gcmd.get("RETURN", "vector") | ||
if return_type == "tilt": | ||
tilt_x = math.degrees(math.atan2(accel_x, accel_z)) | ||
tilt_y = math.degrees(math.atan2(accel_y, accel_z)) | ||
gcmd.respond_info( | ||
"accelerometer plane tilt degrees (x, y): %.6f, %.6f" | ||
% (tilt_x, tilt_y) | ||
) | ||
elif return_type == "vector": | ||
gcmd.respond_info( | ||
"accelerometer values (x, y, z): %.6f, %.6f, %.6f" | ||
% (accel_x, accel_y, accel_z) | ||
) | ||
else: | ||
raise gcmd.error("Unknown 'return' type '%s'" % (return_type,)) | ||
|
||
cmd_ACCELEROMETER_DEBUG_READ_help = "Query register (for debugging)" | ||
|
||
|
@@ -283,6 +305,7 @@ def get_time_translation(self): | |
BYTES_PER_SAMPLE = 5 | ||
SAMPLES_PER_BLOCK = 10 | ||
|
||
|
||
# Printer class that controls ADXL345 chip | ||
class ADXL345: | ||
def __init__(self, config): | ||
|