Skip to content

Commit

Permalink
Add support for adxl380 and adxl382
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Budai <[email protected]>
  • Loading branch information
rbudai98 committed Nov 4, 2024
1 parent 58c3827 commit 30a47b9
Show file tree
Hide file tree
Showing 9 changed files with 439 additions and 0 deletions.
1 change: 1 addition & 0 deletions adi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
from adi.adxl313 import adxl313
from adi.adxl345 import adxl345
from adi.adxl355 import adxl355
from adi.adxl380 import adxl380
from adi.adxrs290 import adxrs290
from adi.cn0511 import cn0511
from adi.cn0532 import cn0532
Expand Down
187 changes: 187 additions & 0 deletions adi/adxl380.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# Copyright (C) 2021-2024 Analog Devices, Inc.
#
# SPDX short identifier: ADIBSD

from decimal import Decimal

from adi.attribute import attribute
from adi.context_manager import context_manager
from adi.rx_tx import rx


class adxl380(rx, context_manager, attribute):
""" adxl380 3-axis accelerometer """

_device_name = "adxl380"
_rx_unbuffered_data = True
_rx_data_si_type = float

def __init__(self, uri="", device_name=None):
context_manager.__init__(self, uri, self._device_name)

compatible_parts = ["adxl380", "adxl382"]

if not device_name:
device_name = compatible_parts[0]

if device_name not in compatible_parts:
raise Exception(
"Not a compatible device:"
+ str(device_name)
+ ".Please select from:"
+ str(compatible_parts)
)
else:
print("Device found: ", device_name)
self._ctrl = self._ctx.find_device(device_name)
self._rxadc = self._ctx.find_device(device_name)

if self._ctrl is None:
print(
"No device found with device_name = "
+ device_name
+ ". Searching for a device found in the compatible list."
)
for i in compatible_parts:
self._ctrl = self._ctx.find_device(i)
self._rxadc = self._ctx.find_device(i)
if self._ctrl is not None:
print("Found device = " + i + ". Will use this device instead.")
break
if self._ctrl is None:
raise Exception("No compatible device found")

self.accel_x = self._channel(self._ctrl, "accel_x")
self.accel_y = self._channel(self._ctrl, "accel_y")
self.accel_z = self._channel(self._ctrl, "accel_z")
self.temp = self._tempchannel(self._ctrl, "temp")
self._rxadc = self._ctx.find_device("adxl380")
self._rx_channel_names = ["accel_x", "accel_y", "accel_z"]
rx.__init__(self)
self.rx_buffer_size = 16 # Make default buffer smaller

@property
def sampling_frequency(self):
"""Device sampling frequency"""
return self._get_iio_dev_attr_str("sampling_frequency")

@sampling_frequency.setter
def sampling_frequency(self, value):
self._set_iio_dev_attr_str("sampling_frequency", str(Decimal(value).real))

@property
def sampling_frequency_available(self):
"""Device available sampling frequency"""
return self._get_iio_dev_attr_str("sampling_frequency_available")

@property
def waiting_for_supplier(self):
"""Device waiting for supplier"""
return self._get_iio_dev_attr_str("waiting_for_supplier")

def to_degrees(self, raw):
"""Convert raw to degrees Celsius"""
return (raw + self.temp.offset) * self.temp.scale / 1000.0

class _tempchannel(attribute):
"""adxl380 temperature channel"""

def __init__(self, ctrl, channel_name):
self.name = channel_name
self._ctrl = ctrl

@property
def offset(self):
"""adxl380 temperature offset value"""
return self._get_iio_attr(self.name, "offset", False)

@property
def raw(self):
"""adxl380 temperature raw value"""
return self._get_iio_attr(self.name, "raw", False)

@property
def scale(self):
"""adxl380 channel scale value"""
return self._get_iio_attr(self.name, "scale", False)

class _channel(attribute):
"""adxl380 acceleration channel"""

def __init__(self, ctrl, channel_name):
self.name = channel_name
self._ctrl = ctrl

@property
def calibbias(self):
"""adxl380 channel offset"""
return self._get_iio_attr(self.name, "calibbias", False)

@calibbias.setter
def calibbias(self, value):
self._set_iio_attr(self.name, "calibbias", False, value)

@property
def filter_high_pass_3db_frequency(self):
"""adxl380 highpass filter cutoff frequency"""
return self._get_iio_attr(
self.name, "filter_high_pass_3db_frequency", False
)

@filter_high_pass_3db_frequency.setter
def filter_high_pass_3db_frequency(self, value):
self._set_iio_attr(
self.name,
"filter_high_pass_3db_frequency",
False,
str(Decimal(value).real),
)

@property
def filter_high_pass_3db_frequency_available(self):
"""Provides all available highpass filter cutoff frequency settings for the adxl380 channels"""
return self._get_iio_attr(
self.name, "filter_high_pass_3db_frequency_available", False
)

@property
def filter_low_pass_3db_frequency(self):
"""adxl380 lowpass filter cutoff frequency"""
return self._get_iio_attr(self.name, "filter_low_pass_3db_frequency", False)

@filter_low_pass_3db_frequency.setter
def filter_low_pass_3db_frequency(self, value):
self._set_iio_attr(
self.name,
"filter_low_pass_3db_frequency",
False,
str(Decimal(value).real),
)

@property
def filter_low_pass_3db_frequency_available(self):
"""Provides all available lowpass filter cutoff frequency settings for the adxl380 channels"""
return self._get_iio_attr(
self.name, "filter_low_pass_3db_frequency_available", False
)

@property
def raw(self):
"""adxl380 channel raw value"""
return self._get_iio_attr(self.name, "raw", False)

@property
def scale(self):
"""adxl380 channel scale(gain)"""
return float(self._get_iio_attr_str(self.name, "scale", False))

@scale.setter
def scale(self, value):
self._set_iio_attr(
self.name, "scale", False, str(Decimal(value).real),
)

@property
def scale_available(self):
"""Provides all available scale settings for the adxl380 channels"""
return self._get_iio_attr(self.name, "scale_available", False)
7 changes: 7 additions & 0 deletions doc/source/devices/adi.adxl380.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
adxl380
==================

.. automodule:: adi.adxl380
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions doc/source/devices/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Supported Devices
adi.adxl313
adi.adxl345
adi.adxl355
adi.adxl380
adi.adxrs290
adi.cn0511
adi.cn0532
Expand Down
114 changes: 114 additions & 0 deletions examples/adxl380_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright (C) 2021 Analog Devices, Inc.
#
# SPDX short identifier: ADIBSD

import sys
import time

import adi

my_dev_name = sys.argv[1]
my_uri = sys.argv[2]

print("uri: " + str(my_uri))

# Set up adxl380
my_acc = adi.adxl380(uri=my_uri, device_name=my_dev_name)
# my_acc.rx_buffer_size = 16
my_acc.rx_enabled_channels = [0, 1, 2]

print("\nChecking temperature channel...")
print("Temperature raw: " + str(my_acc.temp.raw))
print("Calculated Temperature: " + str(my_acc.to_degrees(my_acc.temp.raw)))


print("\nInitial sample frequency:")
print("Sample frequency: " + str(my_acc.sampling_frequency))

print("Single calculated acceleration values:")
print("\nX acceleration: " + str(my_acc.accel_x.raw * my_acc.accel_x.scale) + " m/s^2")
print("Y acceleration: " + str(my_acc.accel_y.raw * my_acc.accel_y.scale) + " m/s^2")
print("Z acceleration: " + str(my_acc.accel_z.raw * my_acc.accel_z.scale) + " m/s^2")

print("\nSetting sample frequencies to 1000 sps...")
my_acc.sampling_frequency = "{:.6f}".format(16000)
time.sleep(0.25)

print("Verifying new sample rate: " + str(my_acc.sampling_frequency))
print("Setting back to 4000 sps...")
my_acc.sampling_frequency = "{:.6f}".format(4000.0)
time.sleep(0.25)

my_acc.rx_output_type = "raw"
print("\nData using buffered rx(), raw:")
print(my_acc.rx())

cutoffs = my_acc.accel_x.filter_high_pass_3db_frequency_available
print("\nAvailable highpass cutoff frequencies: " + str(cutoffs))

print(
"\nSetting highpass cutoff frequency to "
+ str(cutoffs[1])
+ " then taking a 2 second nap to settle..."
)
my_acc.accel_x.filter_high_pass_3db_frequency = "{:.6f}".format(cutoffs[1])
my_acc.accel_y.filter_high_pass_3db_frequency = "{:.6f}".format(cutoffs[1])
my_acc.accel_z.filter_high_pass_3db_frequency = "{:.6f}".format(cutoffs[1])

print(
"\nX highpass cutoff frequency: "
+ str(my_acc.accel_x.filter_high_pass_3db_frequency)
)
print(
"Y highpass cutoff frequency: " + str(my_acc.accel_y.filter_high_pass_3db_frequency)
)
print(
"Z highpass cutoff frequency: " + str(my_acc.accel_z.filter_high_pass_3db_frequency)
)

time.sleep(2.0)

print(
"\nAccelerations after highpass, should be close to zero if the adxl380 is sitting still...\n"
)
print("X acceleration: " + str(my_acc.accel_x.raw * my_acc.accel_x.scale) + " m/s^2")
print("Y acceleration: " + str(my_acc.accel_y.raw * my_acc.accel_y.scale) + " m/s^2")
print("Z acceleration: " + str(my_acc.accel_z.raw * my_acc.accel_z.scale) + " m/s^2")

print(
"\nSetting highpass cutoff frequency back to zero, then taking a 4 second nap to settle..."
)
my_acc.accel_x.filter_high_pass_3db_frequency = "{:.6f}".format(0.0)
my_acc.accel_y.filter_high_pass_3db_frequency = "{:.6f}".format(0.0)
my_acc.accel_z.filter_high_pass_3db_frequency = "{:.6f}".format(0.0)

time.sleep(4.0)

print("\nAccelerations after highpass settling...")

print("X acceleration: " + str(my_acc.accel_x.raw * my_acc.accel_x.scale) + " m/s^2")
print("Y acceleration: " + str(my_acc.accel_y.raw * my_acc.accel_y.scale) + " m/s^2")
print("Z acceleration: " + str(my_acc.accel_z.raw * my_acc.accel_z.scale) + " m/s^2")

print("\nSetting offset for each axis...\n")
print("X offset set to: " + str(my_acc.accel_x.raw >> 4))
print("Y offset set to: " + str(my_acc.accel_y.raw >> 4))
print("Z offset set to: " + str(my_acc.accel_z.raw >> 4))

my_acc.accel_x.calibbias = "{:.6f}".format(my_acc.accel_x.raw >> 4)
my_acc.accel_y.calibbias = "{:.6f}".format(my_acc.accel_y.raw >> 4)
my_acc.accel_z.calibbias = "{:.6f}".format(my_acc.accel_z.raw >> 4)

print(
"\nAccelerations after setting offset, should be close to zero if the adxl380 is sitting still...\n"
)
print("X acceleration: " + str(my_acc.accel_x.raw * my_acc.accel_x.scale) + " m/s^2")
print("Y acceleration: " + str(my_acc.accel_y.raw * my_acc.accel_y.scale) + " m/s^2")
print("Z acceleration: " + str(my_acc.accel_z.raw * my_acc.accel_z.scale) + " m/s^2")

print("\nSetting offset for each axis back to 0...")
my_acc.accel_x.calibbias = "{:.6f}".format(0.0)
my_acc.accel_y.calibbias = "{:.6f}".format(0.0)
my_acc.accel_z.calibbias = "{:.6f}".format(0.0)

del my_acc
2 changes: 2 additions & 0 deletions supported_parts.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
- ADXL314
- ADXL345
- ADXL355
- ADXL380
- ADXL382
- ADXL1002
- ADXRS290
- CN0511
Expand Down
Loading

0 comments on commit 30a47b9

Please sign in to comment.