Skip to content

Commit 4dc7af5

Browse files
Sigafoostannewt
authored andcommitted
adding a gravitational constant and made the conversions a bit clearer
1 parent 70b82e4 commit 4dc7af5

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

adafruit_lis3dh.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
DATARATE_POWERDOWN = const(0)
6262
DATARATE_LOWPOWER_1K6HZ = const(0b1000)
6363
DATARATE_LOWPOWER_5KHZ = const(0b1001)
64+
65+
# Other constants
66+
STANDARD_GRAVITY = 9.806
6467
# pylint: enable=bad-whitespace
6568

6669

@@ -138,7 +141,12 @@ def acceleration(self):
138141

139142
x, y, z = struct.unpack('<hhh', self._read_register(REG_OUT_X_L | 0x80, 6))
140143

141-
return x / divider * 9.806, y / divider * 9.806, z / divider * 9.806
144+
# convert from Gs to m / s ^ 2 and adjust for the range
145+
x = (x / divider) * STANDARD_GRAVITY
146+
y = (y / divider) * STANDARD_GRAVITY
147+
z = (z / divider) * STANDARD_GRAVITY
148+
149+
return x, y, z
142150

143151
def shake(self, shake_threshold=30, avg_count=10, total_delay=0.1):
144152
"""Detect when the accelerometer is shaken. Optional parameters:

examples/accel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
# Loop forever printing accelerometer values
3838
while True:
3939
# Read accelerometer values (in m / s ^ 2). Returns a 3-tuple of x, y,
40-
# z axis values.
41-
x, y, z = lis3dh.acceleration
42-
print('x = {}G, y = {}G, z = {}G'.format(x / 9.806, y / 9.806, z / 9.806))
40+
# z axis values. Divide them by 9.806 to convert to Gs.
41+
x, y, z = [value / adafruit_lis3dh.STANDARD_GRAVITY for value in lis3dh.acceleration]
42+
print('x = {}G, y = {}G, z = {}G'.format(x, y, z))
4343
# Small delay to keep things responsive but give time for interrupt processing.
4444
time.sleep(0.1)

0 commit comments

Comments
 (0)