Skip to content

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

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions adafruit_adxl37x.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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."""
Expand Down Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The 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 -> None. Is the intention to allow for silently ignoring the lack of implementation, as opposed to raising NotImplementedError?

Copy link
Contributor Author

@kattni kattni Feb 15, 2022

Choose a reason for hiding this comment

The 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 NotImplementedError when attempting to use it, because you can't set the range on this chip.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm.... if you want the behavior to be that it throws NotImplementedError on both getting and setting, then I'd say it's fine to leave the typing as is because there's never really going to be a case where someone is expecting to get int and actually gets anything at all, so the potentially future-safe implementation of returning int seems fine. If you want the get functionality of range to not fail but also not give anything useful, I would probably type this as returning None. Either implementation works so up to you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is the decision to raise Not ImplementatedError from this property getter as well?

"""Range is fixed. Updating the range is not implemented."""
return
raise NotImplementedError("Range not implemented. ADXL375 is fixed at 200G.")

@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.")
31 changes: 31 additions & 0 deletions examples/adxl37x_offset_calibration.py
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)
2 changes: 1 addition & 1 deletion examples/adxl37x_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
accelerometer = adafruit_adxl37x.ADXL375(i2c)

while True:
print("%f %f %f" % accelerometer.acceleration)
print("%f %f %f m/s^2" % accelerometer.acceleration)
time.sleep(0.2)