Skip to content

Commit 63af6cf

Browse files
authored
Merge pull request #19 from dherrada/master
Added support for setting data rate
2 parents 409586b + d6bb0a3 commit 63af6cf

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

adafruit_l3gd20.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
L3DS20_RANGE_500DPS = const(1)
6868
L3DS20_RANGE_2000DPS = const(2)
6969

70+
L3DS20_RATE_100HZ = const(0x00)
71+
L3DS20_RATE_200HZ = const(0x40)
72+
L3DS20_RATE_400HZ = const(0x80)
73+
L3DS20_RATE_800HZ = const(0xC0)
74+
7075
_L3GD20_REGISTER_CTRL_REG1 = const(0x20)
7176
_L3GD20_REGISTER_CTRL_REG4 = const(0x23)
7277

@@ -91,9 +96,12 @@ class L3GD20:
9196
9297
:param int rng: a range value one of L3DS20_RANGE_250DPS (default), L3DS20_RANGE_500DPS, or
9398
L3DS20_RANGE_2000DPS
99+
100+
:param int rate: a rate value one of L3DS20_RATE_100HZ (default), L3DS20_RATE_200HZ,
101+
L3DS20_RATE_400HZ, or L3DS20_RATE_800HZ
94102
"""
95103

96-
def __init__(self, rng=L3DS20_RANGE_250DPS):
104+
def __init__(self, rng=L3DS20_RANGE_250DPS, rate=L3DS20_RATE_100HZ):
97105
chip_id = self.read_register(_ID_REGISTER)
98106
if chip_id not in (_L3GD20_CHIP_ID, _L3GD20H_CHIP_ID):
99107
raise RuntimeError(
@@ -119,7 +127,7 @@ def __init__(self, rng=L3DS20_RANGE_250DPS):
119127
# 0 XEN X-axis enable (0 = disabled, 1 = enabled)
120128

121129
# Switch to normal mode and enable all three channels
122-
self.write_register(_L3GD20_REGISTER_CTRL_REG1, 0x0F)
130+
self.write_register(_L3GD20_REGISTER_CTRL_REG1, rate | 0x0F)
123131

124132
# Set CTRL_REG2 (0x21)
125133
# ====================================================================
@@ -212,12 +220,14 @@ class L3GD20_I2C(L3GD20):
212220
gyro_raw = Struct(_L3GD20_REGISTER_OUT_X_L_X80, "<hhh")
213221
"""Gives the raw gyro readings, in units of rad/s."""
214222

215-
def __init__(self, i2c, rng=L3DS20_RANGE_250DPS, address=0x6B):
223+
def __init__(
224+
self, i2c, rng=L3DS20_RANGE_250DPS, address=0x6B, rate=L3DS20_RATE_100HZ
225+
):
216226
import adafruit_bus_device.i2c_device as i2c_device # pylint: disable=import-outside-toplevel
217227

218228
self.i2c_device = i2c_device.I2CDevice(i2c, address)
219229
self.buffer = bytearray(2)
220-
super().__init__(rng)
230+
super().__init__(rng, rate)
221231

222232
def write_register(self, register, value):
223233
"""
@@ -254,13 +264,20 @@ class L3GD20_SPI(L3GD20):
254264
:param baudrate: spi baud rate default is 100000
255265
"""
256266

257-
def __init__(self, spi_busio, cs, rng=L3DS20_RANGE_250DPS, baudrate=100000):
267+
def __init__(
268+
self,
269+
spi_busio,
270+
cs,
271+
rng=L3DS20_RANGE_250DPS,
272+
baudrate=100000,
273+
rate=L3DS20_RATE_100HZ,
274+
): # pylint: disable=too-many-arguments
258275
import adafruit_bus_device.spi_device as spi_device # pylint: disable=import-outside-toplevel
259276

260277
self._spi = spi_device.SPIDevice(spi_busio, cs, baudrate=baudrate)
261278
self._spi_bytearray1 = bytearray(1)
262279
self._spi_bytearray6 = bytearray(6)
263-
super().__init__(rng)
280+
super().__init__(rng, rate)
264281

265282
def write_register(self, register, value):
266283
"""

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"Adafruit L3GD20 Library Documentation",
148148
author,
149149
"manual",
150-
),
150+
)
151151
]
152152

153153
# -- Options for manual page output ---------------------------------------
@@ -178,5 +178,5 @@
178178
"AdafruitL3GD20Library",
179179
"One line description of project.",
180180
"Miscellaneous",
181-
),
181+
)
182182
]

examples/l3gd20_simpletest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,35 @@
55

66
# Hardware I2C setup:
77
I2C = busio.I2C(board.SCL, board.SDA)
8+
# Initializes L3GD20 object using default range, 250dps
89
SENSOR = adafruit_l3gd20.L3GD20_I2C(I2C)
10+
# Initialize L3GD20 object using a custom range and output data rate (ODR).
11+
# SENSOR = adafruit_l3gd20.L3GD20_I2C(
12+
# I2C, rng=adafruit_l3gd20.L3DS20_RANGE_500DPS, rate=adafruit_l3gd20.L3DS20_RATE_200HZ
13+
# )
14+
15+
# Possible values for rng are:
16+
# adafruit_l3gd20.L3DS20_Range_250DPS, 250 degrees per second. Default range
17+
# adafruit_l3gd20.L3DS20_Range_500DPS, 500 degrees per second
18+
# adafruit_l3gd20.L3DS20_Range_2000DPS, 2000 degrees per second
19+
20+
# Possible values for rate are:
21+
# adafruit_l3gd20.L3DS20_RATE_100HZ, 100Hz data rate. Default data rate
22+
# adafruit_l3gd20.L3DS20_RATE_200HZ, 200Hz data rate
23+
# adafruit_l3gd20.L3DS20_RATE_400HZ, 400Hz data rate
24+
# adafruit_l3gd20.L3DS20_RATE_800HZ, 800Hz data rate
925

1026
# Hardware SPI setup:
1127
# import digitalio
1228
# CS = digitalio.DigitalInOut(board.D5)
1329
# SPIB = busio.SPI(board.SCK, board.MOSI, board.MISO)
1430
# SENSOR = adafruit_l3gd20.L3GD20_SPI(SPIB, CS)
31+
# SENSOR = adafruit_l3gd20.L3GD20_I2C(
32+
# SPIB,
33+
# CS,
34+
# rng=adafruit_l3gd20.L3DS20_RANGE_500DPS,
35+
# rate=adafruit_l3gd20.L3DS20_RATE_200HZ,
36+
# )
1537

1638
while True:
1739
print("Angular Momentum (rad/s): {}".format(SENSOR.gyro))

0 commit comments

Comments
 (0)