@@ -79,5 +79,153 @@ class Adafruit_L3GD20_Unified(Adafruit_I2C):
7979 GYRO_RANGE_500DPS = 500
8080 GYRO_RANGE_2000DPS = 2000
8181
82+ def __init__ (self , autorange = False , range = GYRO_RANGE_250DPS , busnum = - 1 , debug = False ):
83+ self ._auto_range , self ._range = autorange , range
84+
85+
86+ # Create _gyro
87+ self ._gyro = Adafruit_I2C .Adafruit_I2C (self .L3GD20_ADDRESS , busnum , debug )
88+ assert self ._gyro .readU8 (self .GYRO_REGISTER_WHO_AM_I ) in (self .L3GD20_ID , self .L3GD20H_ID )
89+
90+
91+ # Set CTRL_REG1 (0x20)
92+ # ====================================================================
93+ # BIT Symbol Description Default
94+ # --- ------ --------------------------------------------- -------
95+ # 7-6 DR1/0 Output data rate 00
96+ # 5-4 BW1/0 Bandwidth selection 00
97+ # 3 PD 0 = Power-down mode, 1 = normal/sleep mode 0
98+ # 2 ZEN Z-axis enable (0 = disabled, 1 = enabled) 1
99+ # 1 YEN Y-axis enable (0 = disabled, 1 = enabled) 1
100+ # 0 XEN X-axis enable (0 = disabled, 1 = enabled) 1
101+
102+ # Reset then switch to normal mode and enable all three channels
103+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG1 , 0x00 );
104+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG1 , 0x0F );
105+
106+ # Set CTRL_REG2 (0x21)
107+ # ====================================================================
108+ # BIT Symbol Description Default
109+ # --- ------ --------------------------------------------- -------
110+ # 5-4 HPM1/0 High-pass filter mode selection 00
111+ # 3-0 HPCF3..0 High-pass filter cutoff frequency selection 0000
112+
113+ # Nothing to do ... keep default values
114+ # --------------------------------------------------------------------
115+
116+ # Set CTRL_REG3 (0x22)
117+ # ====================================================================
118+ # BIT Symbol Description Default
119+ # --- ------ --------------------------------------------- -------
120+ # 7 I1_Int1 Interrupt enable on INT1 (0=disable,1=enable) 0
121+ # 6 I1_Boot Boot status on INT1 (0=disable,1=enable) 0
122+ # 5 H-Lactive Interrupt active config on INT1 (0=high,1=low) 0
123+ # 4 PP_OD Push-Pull/Open-Drain (0=PP, 1=OD) 0
124+ # 3 I2_DRDY Data ready on DRDY/INT2 (0=disable,1=enable) 0
125+ # 2 I2_WTM FIFO wtrmrk int on DRDY/INT2 (0=dsbl,1=enbl) 0
126+ # 1 I2_ORun FIFO overrun int on DRDY/INT2 (0=dsbl,1=enbl) 0
127+ # 0 I2_Empty FIFI empty int on DRDY/INT2 (0=dsbl,1=enbl) 0
128+
129+ # Nothing to do ... keep default values
130+ # ------------------------------------------------------------------
131+
132+ # Set CTRL_REG4 (0x23)
133+ # ====================================================================
134+ # BIT Symbol Description Default
135+ # --- ------ --------------------------------------------- -------
136+ # 7 BDU Block Data Update (0=continuous, 1=LSB/MSB) 0
137+ # 6 BLE Big/Little-Endian (0=Data LSB, 1=Data MSB) 0
138+ # 5-4 FS1/0 Full scale selection 00
139+ # 00 = 250 dps
140+ # 01 = 500 dps
141+ # 10 = 2000 dps
142+ # 11 = 2000 dps
143+ # 0 SIM SPI Mode (0=4-wire, 1=3-wire) 0
144+
145+ # Adjust resolution if requested
146+ if self ._range == self .GYRO_RANGE_250DPS :
147+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG4 , 0x00 )
148+ elif self ._range == self .GYRO_RANGE_500DPS :
149+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG4 , 0x10 )
150+ elif self ._range == self .GYRO_RANGE_2000DPS :
151+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG4 , 0x20 )
152+ # ------------------------------------------------------------------
153+
154+ # Set CTRL_REG5 (0x24)
155+ # ====================================================================
156+ # BIT Symbol Description Default
157+ # --- ------ --------------------------------------------- -------
158+ # 7 BOOT Reboot memory content (0=normal, 1=reboot) 0
159+ # 6 FIFO_EN FIFO enable (0=FIFO disable, 1=enable) 0
160+ # 4 HPen High-pass filter enable (0=disable,1=enable) 0
161+ # 3-2 INT1_SEL INT1 Selection config 00
162+ # 1-0 OUT_SEL Out selection config 00
163+
164+ # Nothing to do ... keep default values
165+ # ------------------------------------------------------------------
166+
167+
168+ def _uint16 (self , list , idx ):
169+ n = list [idx ] | (list [idx + 1 ] << 8 ) # Low, high bytes
170+ return n if n < 32768 else n - 65536 # 2's complement signed
171+
172+ def _saturated (self , list ):
173+ """
174+ Checks if any of the entries in the list has saturated the sensor readings
175+ :param list:
176+ :return:
177+ """
178+
179+ for x in list :
180+ if x < - 32760 or x > 32760 :
181+ return True
182+ return False
183+
184+ def _updateRange (self ):
185+ """
186+ Try to increase the sensor range
187+
188+ :return: Boolean. True if the range can be adjusted, False if not
189+ """
190+ if self ._range == self .GYRO_RANGE_250DPS :
191+ self ._range = self .GYRO_RANGE_500DPS
192+
193+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG1 , 0x00 );
194+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG1 , 0x0F );
195+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG4 , 0x10 );
196+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG5 , 0x80 );
197+ return True
198+ elif self ._range == self .GYRO_RANGE_500DPS :
199+ self ._range = self .GYRO_RANGE_2000DPS
200+
201+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG1 , 0x00 );
202+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG1 , 0x0F );
203+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG4 , 0x20 );
204+ self ._gyro .write8 (self .GYRO_REGISTER_CTRL_REG5 , 0x80 );
205+ return True
206+ else :
207+ return False
208+
209+ def read (self ):
210+ """
211+ :return:
212+ """
213+ list = self ._gyro .readList (self .GYRO_REGISTER_OUT_X_L | 0x80 , 6 )
214+ reading = (
215+ self ._uint16 (list , 0 ),
216+ self ._uint16 (list , 2 ),
217+ self ._uint16 (list , 4 ))
218+
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+
82230if __name__ == '__main__' :
83231 pass
0 commit comments