28
28
29
29
class Adafruit_L3GD20_Unified (Adafruit_I2C ):
30
30
31
+ # Taken from Adafruit_Sensor.h
32
+ SENSORS_DPS_TO_RADS = 0.017453293 # Degrees/s to rad/s multiplier
33
+
31
34
#=========================================================================
32
35
# I2C ADDRESS/BITS AND SETTINGS
33
36
#-------------------------------------------------------------------------
@@ -79,12 +82,18 @@ class Adafruit_L3GD20_Unified(Adafruit_I2C):
79
82
GYRO_RANGE_500DPS = 500
80
83
GYRO_RANGE_2000DPS = 2000
81
84
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
+
82
91
def __init__ (self , autorange = False , range = GYRO_RANGE_250DPS , busnum = - 1 , debug = False ):
83
92
self ._auto_range , self ._range = autorange , range
84
93
85
94
86
95
# Create _gyro
87
- self ._gyro = Adafruit_I2C . Adafruit_I2C (self .L3GD20_ADDRESS , busnum , debug )
96
+ self ._gyro = Adafruit_I2C (self .L3GD20_ADDRESS , busnum , debug )
88
97
assert self ._gyro .readU8 (self .GYRO_REGISTER_WHO_AM_I ) in (self .L3GD20_ID , self .L3GD20H_ID )
89
98
90
99
@@ -216,16 +225,19 @@ def read(self):
216
225
self ._uint16 (list , 2 ),
217
226
self ._uint16 (list , 4 ))
218
227
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 )
228
234
229
235
230
236
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