Ask about Python to micropython porting for adxl355 accelerometer. #10343
Unanswered
jwpark98
asked this question in
Libraries & Drivers
Replies: 3 comments 5 replies
-
I started to port from this python code: https://github.com/gpvidal/adxl355-python |
Beta Was this translation helpful? Give feedback.
1 reply
-
Unfortunately, there is no the library I want to get. I want SPI interface and ADXL355 accelerometer. Thanks anyway. |
Beta Was this translation helpful? Give feedback.
3 replies
-
I think this is the micropython version of ADXL345.py https://github.com/DFRobot/micropython-dflib/blob/master/ADXL345/user_lib/ADXL345.py |
Beta Was this translation helpful? Give feedback.
1 reply
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 conducted code porting from python to micropython without any expertise. I am pretty very new to hardware library. Due to some personal reason as a hobby, I had to have library for ADXL355 accelerometer. By referring some python codes and other syntax information, I produced following micropython code. But it does not work well with some error messeges related to byte operator and so on. Could you please review my initial code then some advices needed?
Following setences are probably reasons of the error:
x_data = ~x_data + 1
Experts please check above sentences line by line whether it is well ported from python or needed to change from 000 to 000. I am very sorry for this questions but I have spent a lot of time for this porting by myself. No one can help me around me since they are not s/w engineers.
Best regards,
JW
"""ADXL355 Micropython library for ESP32.
.. _ADXL355 PMDZ Info:
https://wiki.analog.com/resources/eval/user-guides/eval-adicup360/hardware/adxl355
.. modified from: https://github.com/gpvidal/adxl355-python
"""
import time
from machine import SPI, Pin
#Addresses
XDATA3 = 0x08
XDATA2 = 0x09
XDATA1 = 0x0A
YDATA3 = 0x0B
YDATA2 = 0x0C
YDATA1 = 0x0D
ZDATA3 = 0x0E
ZDATA2 = 0x0F
ZDATA1 = 0x10
TEMP1 = 0x07
TEMP2 = 0x06
RANGE = 0x2C
POWER_CTL = 0x2D
FILTER = 0x28
RANGE DATA
RANGE_2G = 0x01
RANGE_4G = 0x02
RANGE_8G = 0x03
POWER CTL
MEASURE_MODE = 0x06 # Only accelerometer, 110
FILTER
HPF_LPF = 0x0A
Values
READ_BIT = 0x01
WRITE_BIT = 0x00
DUMMY_BYTE = 0xAA
class ADXL355:
"""
Class to interact with ADXL355 device
def init(self, cs=5, scl=18, sda=23, sdo=19, cs, measure_range=RANGE_2G, measure_mode=MEASURE_MODE, filters=HPF_LPF):
# SPI init
self.spi = SPI(1, sck=Pin(scl, Pin.OUT), mosi=Pin(sda, Pin.OUT), miso=Pin(sdo))
time.sleep(0.2)
self.cs = Pin(cs, Pin.OUT, value=1)
time.sleep(0.2)
def _set_measure_range(self, measure_range):
"""Sets measure range on ADXL355 device.
Args:
measure_range (int): Measure range to set in ADXL355.
Returns:
None
"""
self.write_data(RANGE, measure_range)
def _set_measure_mode(self, measure_mode):
"""
Enables measure mode on ADXL355 device.
110 (0x06) Acceleration only / 100 (0x04) Accel+Temp
- DRDY_OFF: 1 to force the DRDY output to 0 in modes where it is normally signal data ready
- TEMP_OFF: 1 to disable temperature processing and also disabled when STANDBY = 1
- STANDBY: 1 Standby mode in a low power state. 0 in Measurement mode.
Returns:
None
"""
self.write_data(POWER_CTL, measure_mode)
def _set_filters(self, filters):
'''Set Low Pass Filter, Output Data Rate (ODR) and High Pass Filter (HPF)
Returns:
None
'''
self.write_data(FILTER, filters)
def write_data(self, address, value):
"""Writes data on ADXL355 device address.
Args:
address (int): Address to write in ADXL355.
value (int): Value to write in address.
Returns:
None
"""
device_address = address << 1 | WRITE_BIT
self.cs.value(0)
self.spi.write([device_address, value])
self.cs.value(1)
return self
def read_data(self, address):
"""Reads data from ADXL355 device.
Args:
address (int): Address to read from ADXL355.
Returns:
int: Value in speficied address in accelerometer
"""
device_address = address << 1 | READ_BIT
self.cs.value(0)
value = self.spi.read([device_address, DUMMY_BYTE])[1:]
self.cs.value(1)
return value
def read_multiple_data(self, address_list):
"""Reads multiple data from ADXL355 device.
Args:
address_list (list): List of addresses to read from.
Returns:
list: Value of each address in accelerometer
"""
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_axes(self):
"""
Gets the current data from the axes.
Returns:
dict: Current value for x, y and z axis
"""
# Reading data
raw_data = self.read_multiple_data(
[XDATA1, XDATA2, XDATA3, YDATA1, YDATA2, YDATA3, ZDATA1, ZDATA2, ZDATA3]
)
x_data = raw_data[0:3]
y_data = raw_data[3:6]
z_data = raw_data[6:9]
if name == 'main':
device = ADXL355()
while True:
axes = device.get_axes()
print(axes)
time.sleep(0.1)
Beta Was this translation helpful? Give feedback.
All reactions