-
Notifications
You must be signed in to change notification settings - Fork 3
Add offsets, example, fix acceleration. #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,14 +28,33 @@ | |
|
||
""" | ||
|
||
from struct import unpack | ||
from micropython import const | ||
import adafruit_adxl34x | ||
|
||
try: | ||
from typing import Tuple, Optional | ||
|
||
# This is only needed for typing | ||
import busio # pylint: disable=unused-import | ||
except ImportError: | ||
pass | ||
|
||
__version__ = "0.0.0-auto.0" | ||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADXL37x.git" | ||
|
||
_ADXL375_DEFAULT_ADDRESS = const(0x53) | ||
|
||
_ADXL347_MULTIPLIER: float = 0.049 # 49mg per lsb | ||
_STANDARD_GRAVITY: float = 9.80665 # earth standard gravity | ||
|
||
_REG_DATAX0: int = const(0x32) # X-axis data 0 | ||
_REG_DATAX1: int = const(0x33) # X-axis data 1 | ||
_REG_DATAY0: int = const(0x34) # Y-axis data 0 | ||
_REG_DATAY1: int = const(0x35) # Y-axis data 1 | ||
_REG_DATAZ0: int = const(0x36) # Z-axis data 0 | ||
_REG_DATAZ1: int = const(0x37) # Z-axis data 1 | ||
|
||
|
||
class DataRate(adafruit_adxl34x.DataRate): # pylint: disable=too-few-public-methods | ||
"""Stub class for data rate.""" | ||
|
@@ -79,17 +98,26 @@ class ADXL375(adafruit_adxl34x.ADXL345): | |
|
||
""" | ||
|
||
def __init__(self, i2c, address=None): | ||
def __init__(self, i2c: busio.I2C, address: Optional[int] = None): | ||
super().__init__( | ||
i2c, address if address is not None else _ADXL375_DEFAULT_ADDRESS | ||
) | ||
|
||
@property | ||
def range(self): | ||
def acceleration(self) -> Tuple[int, int, int]: | ||
"""The x, y, z acceleration values returned in a 3-tuple in :math:`m / s ^ 2`""" | ||
x, y, z = unpack("<hhh", self._read_register(_REG_DATAX0, 6)) | ||
x = x * _ADXL347_MULTIPLIER * _STANDARD_GRAVITY | ||
y = y * _ADXL347_MULTIPLIER * _STANDARD_GRAVITY | ||
z = z * _ADXL347_MULTIPLIER * _STANDARD_GRAVITY | ||
return x, y, z | ||
|
||
@property | ||
def range(self) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm torn for what to do here. As is, it technically works, so the correct typing should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tekktrik You're the expert here! I'll defer to you on this one. It throws the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm.... if you want the behavior to be that it throws There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can leave it as it is for now then, and if it comes up in the future as an issue, we'll look at updating it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So is the decision to raise |
||
"""Range is fixed. Updating the range is not implemented.""" | ||
return | ||
|
||
@range.setter | ||
def range(self, val): | ||
def range(self, val: int) -> None: | ||
"""Range is fixed. Updating the range is not implemented.""" | ||
raise NotImplementedError("Range not implemented. ADXL375 is fixed at 200G.") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries | ||
# SPDX-License-Identifier: MIT | ||
|
||
import time | ||
import board | ||
import adafruit_adxl37x | ||
|
||
i2c = board.STEMMA_I2C() # uses board.SCL and board.SDA | ||
accelerometer = adafruit_adxl37x.ADXL375(i2c) | ||
|
||
accelerometer.offset = 0, 0, 0 | ||
|
||
print("Hold accelerometer flat to set offsets to 0, 0, and -1g...") | ||
time.sleep(1) | ||
x = accelerometer.raw_x | ||
y = accelerometer.raw_y | ||
z = accelerometer.raw_z | ||
print("Raw x: ", x) | ||
print("Raw y: ", y) | ||
print("Raw z: ", z) | ||
|
||
accelerometer.offset = ( | ||
round(-x / 4), | ||
round(-y / 4), | ||
round(-(z - 20) / 4), # Z should be '20' at 1g (49mg per bit) | ||
) | ||
print("Calibrated offsets: ", accelerometer.offset) | ||
|
||
while True: | ||
print("%f %f %f m/s^2" % accelerometer.acceleration) | ||
time.sleep(0.2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
pylint
disable required? Looks like the library is used so it should be okay without it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied it from the other lib, I'll try running it without.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It passes without it! I'll remove it, let me know what to with the type hint so I can do a single commit.