Skip to content

Commit 0a76595

Browse files
authored
Merge pull request #20 from uutzinger/main
Improvements on datarate, added dataReady function, fixed accelerometer and gyroscope divisor
2 parents d566ca7 + d95ef12 commit 0a76595

5 files changed

+83
-9
lines changed

adafruit_icm20x.py

+34-5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@
6060
_ICM20X_REG_INT_ENABLE_0 = 0x10 # Interrupt enable register 0
6161
_ICM20X_REG_INT_ENABLE_1 = 0x11 # Interrupt enable register 1
6262

63+
_ICM20X_REG_INT_STATUS_0 = (
64+
0x19 # Interrupt status register 0 Wake on motion, DMP int, i2c int
65+
)
66+
_ICM20X_REG_INT_STATUS_1 = (
67+
0x1A # Interrupt status register 1 data register from all sensors
68+
)
69+
_ICM20X_REG_INT_STATUS_2 = 0x1B # Interrupt status register 2 FIFO overflow
70+
_ICM20X_REG_INT_STATUS_3 = 0x1C # Interrupt status register 3 Watermark interrupt
71+
6372
# Bank 2
6473
_ICM20X_GYRO_SMPLRT_DIV = 0x00
6574
_ICM20X_GYRO_CONFIG_1 = 0x01
@@ -159,6 +168,8 @@ class ICM20X: # pylint:disable=too-many-instance-attributes
159168

160169
_lp_config_reg = UnaryStruct(_ICM20X_LP_CONFIG, ">B")
161170

171+
_data_ready = ROBit(_ICM20X_REG_INT_STATUS_1, 0)
172+
162173
_i2c_master_cycle_en = RWBit(_ICM20X_LP_CONFIG, 6)
163174
_accel_cycle_en = RWBit(_ICM20X_LP_CONFIG, 5)
164175
_gyro_cycle_en = RWBit(_ICM20X_LP_CONFIG, 4)
@@ -237,6 +248,8 @@ def initialize(self):
237248
self.accelerometer_data_rate_divisor = 20 # ~53.57Hz
238249
self.gyro_data_rate_divisor = 10 # ~100Hz
239250

251+
self._gravity = G_TO_ACCEL
252+
240253
def reset(self):
241254
"""Resets the internal registers and restores the default settings"""
242255
self._bank = 0
@@ -247,6 +260,11 @@ def reset(self):
247260
while self._reset:
248261
sleep(0.005)
249262

263+
def data_ready(self):
264+
"""Checks if new data is available"""
265+
self._bank = 0
266+
return self._data_ready
267+
250268
@property
251269
def _sleep(self):
252270
self._bank = 0
@@ -286,7 +304,9 @@ def gyro(self):
286304
return (x, y, z)
287305

288306
def _scale_xl_data(self, raw_measurement):
289-
return raw_measurement / AccelRange.lsb[self._cached_accel_range] * G_TO_ACCEL
307+
return (
308+
raw_measurement / AccelRange.lsb[self._cached_accel_range] * self._gravity
309+
)
290310

291311
def _scale_gyro_data(self, raw_measurement):
292312
return (
@@ -422,7 +442,8 @@ def accelerometer_data_rate(self, value):
422442
raise AttributeError(
423443
"Accelerometer data rate must be between 0.27 and 1125.0"
424444
)
425-
self.accelerometer_data_rate_divisor = value
445+
divisor = round(((1125.0 - value) / value))
446+
self.accelerometer_data_rate_divisor = divisor
426447

427448
@property
428449
def gyro_data_rate(self):
@@ -445,8 +466,7 @@ def gyro_data_rate(self):
445466
def gyro_data_rate(self, value):
446467
if value < self._gyro_rate_calc(4095) or value > self._gyro_rate_calc(0):
447468
raise AttributeError("Gyro data rate must be between 4.30 and 1100.0")
448-
449-
divisor = round(((1125.0 - value) / value))
469+
divisor = round(((1100.0 - value) / value))
450470
self.gyro_data_rate_divisor = divisor
451471

452472
@property
@@ -511,6 +531,15 @@ def _low_power(self, enabled):
511531
self._bank = 0
512532
self._low_power_en = enabled
513533

534+
@property
535+
def gravity(self):
536+
"""The gravity magnitude in m/s^2."""
537+
return self._gravity
538+
539+
@gravity.setter
540+
def gravity(self, value):
541+
self._gravity = value
542+
514543

515544
class ICM20649(ICM20X):
516545
"""Library for the ST ICM-20649 Wide-Range 6-DoF Accelerometer and Gyro.
@@ -740,7 +769,7 @@ def _mag_id(self):
740769

741770
@property
742771
def magnetic(self):
743-
"""The current magnetic field strengths onthe X, Y, and Z axes in uT (micro-teslas)"""
772+
"""The current magnetic field strengths on the X, Y, and Z axes in uT (micro-teslas)"""
744773

745774
self._bank = 0
746775
full_data = self._raw_mag_data

docs/examples.rst

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ Test using all the ICM20649 sensor capabilities
1616
:caption: examples/examples/icm20x_icm20649_full_test.py
1717
:linenos:
1818

19+
ICM20649 data rate test
20+
-----------------------
21+
22+
Test using all the ICM20649 sensor capabilities
23+
24+
.. literalinclude:: ../examples/icm20x_icm20649_data_rate_test.py
25+
:caption: examples/examples/icm20x_icm20649_data_rate_test.py
26+
:linenos:
27+
1928
ICM20948 Simple test
2029
--------------------
2130

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
from adafruit_icm20x import ICM20649, AccelRange, GyroRange
7+
8+
i2c = board.I2C() # uses board.SCL and board.SDA
9+
10+
ism = ICM20649(i2c, address=0x69)
11+
12+
ism.accelerometer_range = AccelRange.RANGE_4G
13+
print("Accelerometer range set to: %d g" % AccelRange.string[ism.accelerometer_range])
14+
15+
ism.gyro_range = GyroRange.RANGE_500_DPS
16+
print("Gyro range set to: %d DPS" % GyroRange.string[ism.gyro_range])
17+
18+
ism.gyro_data_rate = 1100 # 1100 max
19+
ism.accelerometer_data_rate = 1125 # 1125 max
20+
21+
print("Gyro rate: {:f}".format(ism.gyro_data_rate))
22+
print("Accel rate: {:f}".format(ism.accelerometer_data_rate))
23+
24+
ism.gravity = 9.8
25+
26+
previousTime = time.perf_counter()
27+
while True:
28+
if ism.data_ready:
29+
currentTime = time.perf_counter()
30+
print("\033[2J")
31+
print(
32+
"Accel X:%5.2f Y:%5.2f Z:%5.2f ms^2 "
33+
"Gyro X:%8.3f Y:%8.3f Z:%8.3f degrees/s Sample Rate: %8.1f Hz"
34+
% (*ism.acceleration, *ism.gyro, (1 / (currentTime - previousTime)))
35+
)
36+
previousTime = currentTime

examples/icm20x_icm20649_full_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def printNewMax(value, current_max, axis):
1717
i2c = board.I2C() # uses board.SCL and board.SDA
1818
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
1919

20-
ism = ICM20649(i2c)
20+
ism = ICM20649(i2c, address=0x69)
2121

2222
ism.accelerometer_range = AccelRange.RANGE_30G
2323
print("Accelerometer range set to: %d g" % AccelRange.string[ism.accelerometer_range])
@@ -28,8 +28,8 @@ def printNewMax(value, current_max, axis):
2828
ax_max = ay_max = az_max = 0
2929
gx_max = gy_max = gz_max = 0
3030

31-
ism.gyro_data_rate = 125
32-
ism.accelerometer_data_rate = 4095
31+
ism.gyro_data_rate = 1100
32+
ism.accelerometer_data_rate = 1125
3333
st = time.monotonic()
3434
while time.monotonic() - st < 0.250:
3535
print(

examples/icm20x_icm20649_simpletest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
i2c = board.I2C() # uses board.SCL and board.SDA
99
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
10-
icm = adafruit_icm20x.ICM20649(i2c)
10+
icm = adafruit_icm20x.ICM20649(i2c, address=0x69)
1111

1212
while True:
1313
print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (icm.acceleration))

0 commit comments

Comments
 (0)