Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for setting data rate #19

Merged
merged 9 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions adafruit_l3gd20.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
L3DS20_RANGE_500DPS = const(1)
L3DS20_RANGE_2000DPS = const(2)

L3DS20_RATE_100HZ = const(0x00)
L3DS20_RATE_200HZ = const(0x40)
L3DS20_RATE_400HZ = const(0x80)
L3DS20_RATE_800HZ = const(0xC0)

_L3GD20_REGISTER_CTRL_REG1 = const(0x20)
_L3GD20_REGISTER_CTRL_REG4 = const(0x23)

Expand All @@ -91,9 +96,12 @@ class L3GD20:

:param int rng: a range value one of L3DS20_RANGE_250DPS (default), L3DS20_RANGE_500DPS, or
L3DS20_RANGE_2000DPS

:param int rate: a rate value one of L3DS20_RATE_100HZ (default), L3DS20_RATE_200HZ,
L3DS20_RATE_400HZ, or L3DS20_RATE_800HZ
"""

def __init__(self, rng=L3DS20_RANGE_250DPS):
def __init__(self, rng=L3DS20_RANGE_250DPS, rate=L3DS20_RATE_100HZ):
chip_id = self.read_register(_ID_REGISTER)
if chip_id not in (_L3GD20_CHIP_ID, _L3GD20H_CHIP_ID):
raise RuntimeError(
Expand All @@ -119,7 +127,8 @@ def __init__(self, rng=L3DS20_RANGE_250DPS):
# 0 XEN X-axis enable (0 = disabled, 1 = enabled)

# Switch to normal mode and enable all three channels
self.write_register(_L3GD20_REGISTER_CTRL_REG1, 0x0F)
# self.write_register(_L3GD20_REGISTER_CTRL_REG1, 0x0F)
self.write_register(_L3GD20_REGISTER_CTRL_REG1, rate + 0x0F)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# self.write_register(_L3GD20_REGISTER_CTRL_REG1, 0x0F)
self.write_register(_L3GD20_REGISTER_CTRL_REG1, rate + 0x0F)
self.write_register(_L3GD20_REGISTER_CTRL_REG1, rate | 0x0F)


# Set CTRL_REG2 (0x21)
# ====================================================================
Expand Down Expand Up @@ -212,12 +221,14 @@ class L3GD20_I2C(L3GD20):
gyro_raw = Struct(_L3GD20_REGISTER_OUT_X_L_X80, "<hhh")
"""Gives the raw gyro readings, in units of rad/s."""

def __init__(self, i2c, rng=L3DS20_RANGE_250DPS, address=0x6B):
def __init__(
self, i2c, rng=L3DS20_RANGE_250DPS, rate=L3DS20_RATE_100HZ, address=0x6B
tannewt marked this conversation as resolved.
Show resolved Hide resolved
):
import adafruit_bus_device.i2c_device as i2c_device # pylint: disable=import-outside-toplevel

self.i2c_device = i2c_device.I2CDevice(i2c, address)
self.buffer = bytearray(2)
super().__init__(rng)
super().__init__(rng, rate)

def write_register(self, register, value):
"""
Expand Down Expand Up @@ -254,13 +265,20 @@ class L3GD20_SPI(L3GD20):
:param baudrate: spi baud rate default is 100000
"""

def __init__(self, spi_busio, cs, rng=L3DS20_RANGE_250DPS, baudrate=100000):
def __init__(
self,
spi_busio,
cs,
rng=L3DS20_RANGE_250DPS,
rate=L3DS20_RATE_100HZ,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add rate after baudrate to prevent breaking code that relies on position of these args. It wouldn't matter if there was a *, before the kwargs to make them kwarg-only.

baudrate=100000,
): # pylint: disable=too-many-arguments
import adafruit_bus_device.spi_device as spi_device # pylint: disable=import-outside-toplevel

self._spi = spi_device.SPIDevice(spi_busio, cs, baudrate=baudrate)
self._spi_bytearray1 = bytearray(1)
self._spi_bytearray6 = bytearray(6)
super().__init__(rng)
super().__init__(rng, rate)

def write_register(self, register, value):
"""
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"Adafruit L3GD20 Library Documentation",
author,
"manual",
),
)
]

# -- Options for manual page output ---------------------------------------
Expand Down Expand Up @@ -178,5 +178,5 @@
"AdafruitL3GD20Library",
"One line description of project.",
"Miscellaneous",
),
)
]
22 changes: 22 additions & 0 deletions examples/l3gd20_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,35 @@

# Hardware I2C setup:
I2C = busio.I2C(board.SCL, board.SDA)
# Initializes L3GD20 object using default range, 250dps
SENSOR = adafruit_l3gd20.L3GD20_I2C(I2C)
# Initialize L3GD20 object using a custom range and output data rate (ODR).
# SENSOR = adafruit_l3gd20.L3GD20_I2C(
# I2C, rng=adafruit_l3gd20.L3DS20_RANGE_500DPS, rate=adafruit_l3gd20.L3DS20_RATE_200HZ
# )

# Possible values for rng are:
# adafruit_l3gd20.L3DS20_Range_250DPS, 250 degrees per second. Default range
# adafruit_l3gd20.L3DS20_Range_500DPS, 500 degrees per second
# adafruit_l3gd20.L3DS20_Range_2000DPS, 2000 degrees per second

# Possible values for rate are:
# adafruit_l3gd20.L3DS20_RATE_100HZ, 100Hz data rate. Default data rate
# adafruit_l3gd20.L3DS20_RATE_200HZ, 200Hz data rate
# adafruit_l3gd20.L3DS20_RATE_400HZ, 400Hz data rate
# adafruit_l3gd20.L3DS20_RATE_800HZ, 800Hz data rate

# Hardware SPI setup:
# import digitalio
# CS = digitalio.DigitalInOut(board.D5)
# SPIB = busio.SPI(board.SCK, board.MOSI, board.MISO)
# SENSOR = adafruit_l3gd20.L3GD20_SPI(SPIB, CS)
# SENSOR = adafruit_l3gd20.L3GD20_I2C(
# SPIB,
# CS,
# rng=adafruit_l3gd20.L3DS20_RANGE_500DPS,
# rate=adafruit_l3gd20.L3DS20_RATE_200HZ,
# )

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