Skip to content

Commit ce6322a

Browse files
committed
adding filters
1 parent 7d15be8 commit ce6322a

File tree

1 file changed

+96
-5
lines changed

1 file changed

+96
-5
lines changed

adafruit_icm20x.py

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ class GyroRange(CV):
140140
pass # pylint: disable=unnecessary-pass
141141

142142

143+
class GyroDLPFFreq(CV):
144+
"""Options for ``gyro_dlpf_cutoff``"""
145+
146+
pass # pylint: disable=unnecessary-pass
147+
148+
149+
class AccelDLPFFreq(CV):
150+
"""Options for ``accel_dlpf_cutoff``"""
151+
152+
pass # pylint: disable=unnecessary-pass
153+
154+
143155
class ICM20X: # pylint:disable=too-many-instance-attributes
144156
"""Library for the ST ICM-20X Wide-Range 6-DoF Accelerometer and Gyro Family
145157
@@ -160,16 +172,98 @@ class ICM20X: # pylint:disable=too-many-instance-attributes
160172
_raw_gyro_data = Struct(_ICM20X_GYRO_XOUT_H, ">hhh")
161173

162174
# Bank 2
175+
_gyro_dlpf_enable = RWBits(1, _ICM20X_GYRO_CONFIG_1, 0)
163176
_gyro_range = RWBits(2, _ICM20X_GYRO_CONFIG_1, 1)
164-
_accel_config = Struct(_ICM20X_ACCEL_CONFIG_1, ">B")
165-
_gyro_config1 = Struct(_ICM20X_GYRO_CONFIG_1, ">B")
177+
_gyro_dlpf_config = RWBits(3, _ICM20X_GYRO_CONFIG_1, 3)
178+
166179
_accel_dlpf_enable = RWBits(1, _ICM20X_ACCEL_CONFIG_1, 0)
167180
_accel_range = RWBits(2, _ICM20X_ACCEL_CONFIG_1, 1)
168181
_accel_dlpf_config = RWBits(3, _ICM20X_ACCEL_CONFIG_1, 3)
169182

170183
# this value is a 12-bit register spread across two bytes, big-endian first
171184
_accel_rate_divisor = UnaryStruct(_ICM20X_ACCEL_SMPLRT_DIV_1, ">H")
172185
_gyro_rate_divisor = UnaryStruct(_ICM20X_GYRO_SMPLRT_DIV, ">B")
186+
AccelDLPFFreq.add_values(
187+
(
188+
(
189+
"DISABLED",
190+
-1,
191+
"Disabled",
192+
None,
193+
), # magical value that we will use do disable
194+
("FREQ_246_0HZ_3DB", 1, 246.0, None),
195+
("FREQ_111_4HZ_3DB", 2, 111.4, None),
196+
("FREQ_50_4HZ_3DB", 3, 50.4, None),
197+
("FREQ_23_9HZ_3DB", 4, 23.9, None),
198+
("FREQ_11_5HZ_3DB", 5, 11.5, None),
199+
("FREQ_5_7HZ_3DB", 6, 5.7, None),
200+
("FREQ_473HZ_3DB", 7, 473, None),
201+
)
202+
)
203+
GyroDLPFFreq.add_values(
204+
(
205+
(
206+
"DISABLED",
207+
-1,
208+
"Disabled",
209+
None,
210+
), # magical value that we will use do disable
211+
("FREQ_196_6HZ_3DB", 0, 196.6, None),
212+
("FREQ_151_8HZ_3DB", 1, 151.8, None),
213+
("FREQ_119_5HZ_3DB", 2, 119.5, None),
214+
("FREQ_51_2HZ_3DB", 3, 51.2, None),
215+
("FREQ_23_9HZ_3DB", 4, 23.9, None),
216+
("FREQ_11_6HZ_3DB", 5, 11.6, None),
217+
("FREQ_5_7HZ_3DB", 6, 5.7, None),
218+
("FREQ_361_4HZ_3DB", 7, 361.4, None),
219+
)
220+
)
221+
222+
@property
223+
def accel_dlpf_cutoff(self):
224+
"""The cutoff frequency for the accelerometer's digital low pass filter. Signals
225+
above the given frequency will be filtered out. Must be an ``AccelDLPFCutoff``.
226+
Use AccelDLPFCutoff.DISABLED to disable the filter
227+
228+
**Note** readings immediately following setting a cutoff frequency will be
229+
inaccurate due to the filter "warming up" """
230+
self._bank = 2
231+
return self._accel_dlpf_config
232+
233+
@accel_dlpf_cutoff.setter
234+
def accel_dlpf_cutoff(self, cutoff_frequency):
235+
if not AccelDLPFFreq.is_valid(cutoff_frequency):
236+
raise AttributeError("accel_dlpf_cutoff must be an `AccelDLPFFreq`")
237+
self._bank = 2
238+
# check for shutdown
239+
if cutoff_frequency is AccelDLPFFreq.DISABLED: # pylint: disable=no-member
240+
self._accel_dlpf_enable = False
241+
return
242+
self._accel_dlpf_enable = True
243+
self._accel_dlpf_config = cutoff_frequency
244+
245+
@property
246+
def gyro_dlpf_cutoff(self):
247+
"""The cutoff frequency for the gyro's digital low pass filter. Signals above the
248+
given frequency will be filtered out. Must be a ``GyroDLPFFreq``. Use
249+
GyroDLPFCutoff.DISABLED to disable the filter
250+
251+
**Note** readings immediately following setting a cutoff frequency will be
252+
inaccurate due to the filter "warming up" """
253+
self._bank = 2
254+
return self._gyro_dlpf_config
255+
256+
@gyro_dlpf_cutoff.setter
257+
def gyro_dlpf_cutoff(self, cutoff_frequency):
258+
if not GyroDLPFFreq.is_valid(cutoff_frequency):
259+
raise AttributeError("gyro_dlpf_cutoff must be a `GyroDLPFFreq`")
260+
self._bank = 2
261+
# check for shutdown
262+
if cutoff_frequency is GyroDLPFFreq.DISABLED: # pylint: disable=no-member
263+
self._gyro_dlpf_enable = False
264+
return
265+
self._gyro_dlpf_enable = True
266+
self._gyro_dlpf_config = cutoff_frequency
173267

174268
@property
175269
def _bank(self):
@@ -190,14 +284,11 @@ def __init__(self, i2c_bus, address):
190284

191285
def initialize(self):
192286
"""Configure the sensors with the default settings. For use after calling `reset()`"""
193-
# TODO: method-ify
194287
self._bank = 0
195288
sleep(0.005)
196289
self._sleep = False
197290
sleep(0.005)
198291

199-
sleep(0.005)
200-
201292
self.accelerometer_range = AccelRange.RANGE_8G # pylint: disable=no-member
202293
self.gyro_range = GyroRange.RANGE_500_DPS # pylint: disable=no-member
203294

0 commit comments

Comments
 (0)