Updated ADXL345 sandbox test code, trying different library
This commit is contained in:
parent
e5a34a43ae
commit
517af872b7
2 changed files with 58 additions and 32 deletions
|
|
@ -1,26 +1,51 @@
|
|||
from micropython import const
|
||||
from machine import Pin, SoftI2C
|
||||
|
||||
# ADXL-345 Registers
|
||||
_DATA_FORMAT = const(0x31)
|
||||
_POWER_CTL = const(0x2D)
|
||||
_DATAX0 = const(0x32)
|
||||
from machine import Pin,I2C
|
||||
import math
|
||||
import time
|
||||
|
||||
device = const(0x53)
|
||||
regAddress = const(0x32)
|
||||
TO_READ = 6
|
||||
buff = bytearray(6)
|
||||
|
||||
class ADXL345:
|
||||
def __init__(self, i2c, address=0x53):
|
||||
def __init__(self,i2c,addr=device):
|
||||
self.addr = addr
|
||||
self.i2c = i2c
|
||||
self.address = address
|
||||
self.i2c.start()
|
||||
self.i2c.writeto(self.address, bytearray([_POWER_CTL, 0x08])) # power on
|
||||
self.i2c.writeto(self.address, bytearray([_DATA_FORMAT, 0x08])) # full resolution mode
|
||||
self.i2c.stop()
|
||||
b = bytearray(1)
|
||||
b[0] = 0
|
||||
self.i2c.writeto_mem(self.addr,0x2d,b)
|
||||
b[0] = 16
|
||||
self.i2c.writeto_mem(self.addr,0x2d,b)
|
||||
b[0] = 8
|
||||
self.i2c.writeto_mem(self.addr,0x2d,b)
|
||||
|
||||
@property
|
||||
def xValue(self):
|
||||
buff = self.i2c.readfrom_mem(self.addr,regAddress,TO_READ)
|
||||
x = (int(buff[1]) << 8) | buff[0]
|
||||
if x > 32767:
|
||||
x -= 65536
|
||||
return x
|
||||
|
||||
@property
|
||||
def yValue(self):
|
||||
buff = self.i2c.readfrom_mem(self.addr,regAddress,TO_READ)
|
||||
y = (int(buff[3]) << 8) | buff[2]
|
||||
if y > 32767:
|
||||
y -= 65536
|
||||
return y
|
||||
|
||||
@property
|
||||
def zValue(self):
|
||||
buff = self.i2c.readfrom_mem(self.addr,regAddress,TO_READ)
|
||||
z = (int(buff[5]) << 8) | buff[4]
|
||||
if z > 32767:
|
||||
z -= 65536
|
||||
return z
|
||||
|
||||
def RP_calculate(self,x,y,z):
|
||||
roll = math.atan2(y , z) * 57.3
|
||||
pitch = math.atan2((- x) , math.sqrt(y * y + z * z)) * 57.3
|
||||
return roll,pitch
|
||||
|
||||
def read(self):
|
||||
self.i2c.start()
|
||||
self.i2c.writeto(self.address, bytearray([_DATAX0]))
|
||||
data = self.i2c.readfrom(self.address, 6)
|
||||
self.i2c.stop()
|
||||
x = ((data[1] << 8) | data[0])
|
||||
y = ((data[3] << 8) | data[2])
|
||||
z = ((data[5] << 8) | data[4])
|
||||
return (x, y, z)d
|
||||
|
|
@ -1,15 +1,16 @@
|
|||
from machine import Pin, SoftI2C
|
||||
from adxl345 import ADXL345
|
||||
from machine import Pin,I2C
|
||||
import adxl345
|
||||
import time
|
||||
|
||||
# Define I2C pins
|
||||
i2c = SoftI2C(scl=Pin(15), sda=Pin(16))
|
||||
i2c = I2C(scl=Pin(15),sda=Pin(16), freq=10000)
|
||||
adx = adxl345.ADXL345(i2c)
|
||||
|
||||
# Initialize ADXL345
|
||||
accel = ADXL345(i2c)
|
||||
|
||||
# Read accelerometer data
|
||||
while True:
|
||||
x, y, z = accel.read()
|
||||
print("x = {}, y = {}, z = {}".format(x, y, z))
|
||||
time.sleep(0.2)
|
||||
x=adx.xValue
|
||||
y=adx.yValue
|
||||
z=adx.zValue
|
||||
print('The acceleration info of x, y, z are:%d,%d,%d'%(x,y,z))
|
||||
roll,pitch = adx.RP_calculate(x,y,z)
|
||||
print('roll=',roll)
|
||||
print('pitch=',pitch)
|
||||
time.sleep_ms(50)
|
||||
|
|
|
|||
Loading…
Reference in a new issue