Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 13209c0

Browse files
author
Alex Bucknall
committed
Updated LIS2HH12 for scale
1 parent e575bef commit 13209c0

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

pysense/lib/LIS2HH12.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class LIS2HH12:
1515
ACC_Z_L_REG = const(0x2C)
1616
ACC_Z_H_REG = const(0x2D)
1717

18-
def __init__(self, pysense=None, sda='P22', scl='P21'):
18+
SCALE = const(8192)
19+
20+
def __init__(self, pysense = None, sda = 'P22', scl = 'P21'):
1921
if pysense is not None:
2022
self.i2c = pysense.i2c
2123
else:
@@ -30,27 +32,31 @@ def __init__(self, pysense=None, sda='P22', scl='P21'):
3032

3133
whoami = self.i2c.readfrom_mem(ACC_I2CADDR , PRODUCTID_REG, 1)
3234
if (whoami[0] != 0x41):
33-
raise ValueError("Incorrect product ID")
35+
raise ValueError("Incorrect Product ID")
36+
3437
# enable acceleration readings
3538
self.i2c.readfrom_mem_into(ACC_I2CADDR , CTRL1_REG, self.reg)
36-
self.reg[0] &= ~0x70
37-
self.reg[0] |= 0x30
39+
self.reg[0] &= ~0b01110000
40+
self.reg[0] |= 0b00110000
3841
self.i2c.writeto_mem(ACC_I2CADDR , CTRL1_REG, self.reg)
42+
3943
# change the full-scale to 4g
4044
self.i2c.readfrom_mem_into(ACC_I2CADDR , CTRL4_REG, self.reg)
4145
self.reg[0] &= ~0b00110000
4246
self.reg[0] |= 0b00100000
4347
self.i2c.writeto_mem(ACC_I2CADDR , CTRL4_REG, self.reg)
44-
self.read()
4548

46-
def read(self):
49+
# make a first read
50+
self.acceleration()
51+
52+
def acceleration(self):
4753
x = self.i2c.readfrom_mem(ACC_I2CADDR , ACC_X_L_REG, 2)
4854
self.x = struct.unpack('<h', x)
4955
y = self.i2c.readfrom_mem(ACC_I2CADDR , ACC_Y_L_REG, 2)
5056
self.y = struct.unpack('<h', y)
5157
z = self.i2c.readfrom_mem(ACC_I2CADDR , ACC_Z_L_REG, 2)
5258
self.z = struct.unpack('<h', z)
53-
return (self.x[0], self.y[0], self.z[0])
59+
return (self.x[0] / SCALE, self.y[0] / SCALE, self.z[0] / SCALE)
5460

5561
def roll(self):
5662
div = math.sqrt(math.pow(self.y[0], 2) + math.pow(self.z[0], 2))

0 commit comments

Comments
 (0)