Skip to content

Commit 16cdfb9

Browse files
committed
Added missing resolution compensation and conversion to rad/s
1 parent 6be0c89 commit 16cdfb9

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

Adafruit_L3GD20_Unified/Adafruit_L3GD20_Unified.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
class Adafruit_L3GD20_Unified(Adafruit_I2C):
3030

31+
# Taken from Adafruit_Sensor.h
32+
SENSORS_DPS_TO_RADS = 0.017453293 # Degrees/s to rad/s multiplier
33+
3134
#=========================================================================
3235
# I2C ADDRESS/BITS AND SETTINGS
3336
#-------------------------------------------------------------------------
@@ -79,12 +82,18 @@ class Adafruit_L3GD20_Unified(Adafruit_I2C):
7982
GYRO_RANGE_500DPS = 500
8083
GYRO_RANGE_2000DPS = 2000
8184

85+
86+
GYRO_COMPENSATIONS = {
87+
GYRO_RANGE_250DPS:GYRO_SENSITIVITY_250DPS,
88+
GYRO_RANGE_500DPS:GYRO_SENSITIVITY_500DPS,
89+
GYRO_RANGE_2000DPS:GYRO_SENSITIVITY_2000DPS}
90+
8291
def __init__(self, autorange = False, range = GYRO_RANGE_250DPS, busnum=-1, debug=False):
8392
self._auto_range, self._range = autorange, range
8493

8594

8695
# Create _gyro
87-
self._gyro = Adafruit_I2C.Adafruit_I2C(self.L3GD20_ADDRESS, busnum, debug)
96+
self._gyro = Adafruit_I2C(self.L3GD20_ADDRESS, busnum, debug)
8897
assert self._gyro.readU8(self.GYRO_REGISTER_WHO_AM_I) in (self.L3GD20_ID, self.L3GD20H_ID)
8998

9099

@@ -216,16 +225,19 @@ def read(self):
216225
self._uint16(list, 2),
217226
self._uint16(list, 4))
218227

219-
if not self._auto_range:
220-
return reading
221-
elif self._saturated(reading):
222-
if self._updateRange(): # If it is possible to increase the range, invalidate the reading
223-
return None
224-
else:
225-
return reading
226-
else:
227-
return reading
228+
229+
# Check if ranges should and can be adjusted
230+
if self._auto_range and self._saturated(reading) and self._updateRange():
231+
return None
232+
233+
return tuple(x * self.GYRO_COMPENSATIONS[self._range] * self.SENSORS_DPS_TO_RADS for x in reading)
228234

229235

230236
if __name__ == '__main__':
231-
pass
237+
from time import sleep
238+
239+
l3g = Adafruit_L3GD20_Unified(True)
240+
241+
while True:
242+
print(l3g.read())
243+
sleep(1)

0 commit comments

Comments
 (0)