Error relating to ADXL355 accelerometer library in micropyhon. #10271
Unanswered
jwpark98
asked this question in
Libraries & Drivers
Replies: 2 comments 5 replies
-
xData is a list of bytearray objects, so xData[0] is a bytearray object, not an int. You may have intended e.g. xData[0][0] etc., which is an int, or convert the content of what has been returned by read_data89 to an int first and add that to the list. |
Beta Was this translation helpful? Give feedback.
5 replies
-
@jwpark98 Did you manage to get the ADXL355 running with micropython? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am new to micropython. Due to this forum, I have achieved several success. Thank you all for this help. However, I recently suffered from dealing with the accelerometer, adxl355. As a mechanical engineer, I am very interested in measuring acceleration of an object. To do, I have tested several sensors such as adxl345, mpu6050, and adxl355. I completed adxl345 and mpu6050, but adxl355 is not easy due to its complexity. Below is my adxl355.py (mycropython api version) referred from adxl355.py (python version) and adxl345.py (micropython version). Especially, the code treating byte operator is not easy to me. I do not know how to deal with byte sections. Could you please let me know any good adxl355.py (micropython) or teach me how to modify this code. I could not find any reliable code for adxl355 in micropython.
---------- code ------------
Import necessary libraries
from micropython import const
from machine import SPI, Pin
import time
ADXL355 addresses
DEVID_AD = const(0x00)
DEVID_MST = const(0x01)
PARTID = const(0x02)
REVID = const(0x03)
STATUS = const(0x04)
FIFO_ENTRIES = const(0x05)
TEMP2 = const(0x06)
TEMP1 = const(0x07)
XDATA3 = const(0x08)
XDATA2 = const(0x09)
XDATA1 = const(0x0A)
YDATA3 = const(0x0B)
YDATA2 = const(0x0C)
YDATA1 = const(0x0D)
ZDATA3 = const(0x0E)
ZDATA2 = const(0x0F)
ZDATA1 = const(0x10)
FIFO_DATA = const(0x11)
OFFSET_X_H = const(0x1E)
OFFSET_X_L = const(0x1F)
OFFSET_Y_H = const(0x20)
OFFSET_Y_L = const(0x21)
OFFSET_Z_H = const(0x22)
OFFSET_Z_L = const(0x23)
ACT_EN = const(0x24)
ACT_THRESH_H = const(0x25)
ACT_THRESH_L = const(0x26)
ACT_COUNT = const(0x27)
FILTER = const(0x28)
FIFO_SAMPLES = const(0x29)
INT_MAP = const(0x2A)
SYNC = const(0x2B)
RANGE = const(0x2C)
POWER_CTL = const(0x2D)
SELF_TEST = const(0x2E)
RESET = const(0x2F)
Data ranges
RANGE_2G = const(0x01)
RANGE_4G = const(0x02)
RANGE_8G = const(0x03)
Values
READ_BIT = 0x01
WRITE_BIT = 0x00
DUMMY_BYTE = 0xAA
class ADXL355:
def init(self, spi_id, cs_pin=5, scl_pin=18, sda_pin=23, sdo_pin=19, spi_freq=10000000):
#SPI0, id=0 / SPI1(HSPI), id=1 / SPI2(VSPI), id=2
SPI pins
def del(self):
self.spi.deinit()
== writing ==
def write_data(self, regaddr:int, the_byte:int):
self.cs.value(0)
self.spi.write(bytearray((regaddr, the_byte)))
self.cs.value(1)
return self
== readings ==
def read_data(self, regaddr: int, nbytes: int) -> bytearray or int:
self.cs.value(0)
value = self.spi.read(nbytes + 1, regaddr)[1:]
self.cs.value(1)
return value
#buf = bytearray(4)
def read_into(self, buf: bytearray, regaddr: int) -> bytearray:
self.cs.value(0)
self.spi.readinto(buf, regaddr)
self.cs.value(1)
return buf
def read_multiple_data(self, address_list):
spi_ops = []
for address in address_list:
spi_ops.append(address << 1 | READ_BIT)
spi_ops.append(DUMMY_BYTE)
def get_temperature(self):
# Reading data
temp_data = [self.spi.read(TEMP1, 4), self.spi.read(TEMP2, 4)]
# Join data
temp_data = (temp_data[0]) + ((temp_data[1] - ((temp_data[1] >> 4) << 4)) << 8)
# temp_data = (1852 - temp_data) / 9.05 + 19.21
return temp_data
def get_acceleration(self):
## reading data
xData = [self.read_data(XDATA1, 4), self.read_data(XDATA2, 4), self.read_data(XDATA3, 4)]
yData = [self.read_data(YDATA1, 4), self.read_data(YDATA2, 4), self.read_data(YDATA3, 4)]
zData = [self.read_data(ZDATA1, 4), self.read_data(ZDATA2, 4), self.read_data(ZDATA3, 4)]
def _reset_settings(self):
## reset all settings
self.write_data(RESET, 0x52)
== Get measuring range ==
def _set_measure_range(self, measure_range:int):
self.write_data(RANGE, measure_range)
return self
if name == 'main':
spi0 = ADXL355(spi_id=2, cs_pin=5, scl_pin=18, sda_pin=23, sdo_pin=19, spi_freq=10000000)
Set sensor
spi0._reset_settings()
spi0._set_measure_range(RANGE_2G)
f_s = 3 # Hz
dt = 1/f_s # sec.
axes = spi0.get_acceleration() # pylint: disable=invalid-name
print(axes)
When I run the code, following error is occurred.
typeerror: unsupported types for rshift: 'bytes', 'int'
I think the error is related to the part as below:
x_data = (xData[0] >> 4) + (xData[1] << 4) + (xData[2] << 12)
Maybe I did some mistake to port adxl355.py (python version) into micropython version.
This work is pretty important to me since I am studying some measurement system to obtain acceleration data on the surface of machinery. Since I am not expert in micropython programming, I do not know where I have to start to modify my ugly code.
JW
Beta Was this translation helpful? Give feedback.
All reactions