32
32
--------------------
33
33
34
34
**Hardware:**
35
-
36
- .. todo:: Add links to any specific hardware product page(s), or category page(s). Use unordered list & hyperlink rST
37
- inline format: "* `Link Text <url>`_"
35
+ * Adafruit's MPU6050 Breakout: https://adafruit.com/products/3886
38
36
39
37
**Software and Dependencies:**
40
38
41
39
* Adafruit CircuitPython firmware for the supported boards:
42
40
https://github.com/adafruit/circuitpython/releases
43
41
44
- .. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies based on the library's use of either.
45
-
46
- # * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
47
- # * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
42
+ * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
43
+ * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
48
44
"""
49
45
50
46
# imports
58
54
from adafruit_register .i2c_bit import RWBit
59
55
from adafruit_register .i2c_bits import RWBits
60
56
import adafruit_bus_device .i2c_device as i2c_device
61
-
62
- # TODO: Trust but verify
57
+ # pylint: disable=bad-whitespace
63
58
_MPU6050_DEFAULT_ADDRESS = 0x69 # MPU6050 default i2c address w/ AD0 high
64
59
_MPU6050_DEVICE_ID = 0x68 # The correct MPU6050_WHO_AM_I value
65
60
81
76
_MPU6050_WHO_AM_I = 0x75 # Divice ID register
82
77
83
78
STANDARD_GRAVITY = 9.80665
79
+ # pylint: enable=bad-whitespace
80
+
81
+ class Range : # pylint: disable=too-few-public-methods
82
+ """Allowed values for `accelerometer_range`."""
83
+ RANGE_2_G = 0 # +/- 2g (default value)
84
+ RANGE_4_G = 1 # +/- 4g
85
+ RANGE_8_G = 2 # +/- 8g
86
+ RANGE_16_G = 3 # +/- 16g
87
+
88
+ class GyroRange : # pylint: disable=too-few-public-methods
89
+ """Allowed values for `gyro_range`."""
90
+ RANGE_250_DEG = 0 # +/- 250 deg/s (default value)
91
+ RANGE_500_DEG = 1 # +/- 500 deg/s
92
+ RANGE_1000_DEG = 2 # +/- 1000 deg/s
93
+ RANGE_2000_DEG = 3 # +/- 2000 deg/s
94
+
95
+ class Bandwidth : # pylint: disable=too-few-public-methods
96
+ """Allowed values for `filter_bandwidth`."""
97
+ BAND_260_HZ = 0 # Docs imply this disables the filter
98
+ BAND_184_HZ = 1 # 184 Hz
99
+ BAND_94_HZ = 2 # 94 Hz
100
+ BAND_44_HZ = 3 # 44 Hz
101
+ BAND_21_HZ = 4 # 21 Hz
102
+ BAND_10_HZ = 5 # 10 Hz
103
+ BAND_5_HZ = 6 # 5 Hz
104
+
105
+ class Rate : # pylint: disable=too-few-public-methods
106
+ """Allowed values for `cycle_rate`"""
107
+ CYCLE_1_25_HZ = 0 # 1.25 Hz
108
+ CYCLE_5_HZ = 1 # 5 Hz
109
+ CYCLE_20_HZ = 2 # 20 Hz
110
+ CYCLE_40_HZ = 3 # 40 Hz
84
111
85
-
86
- # /**
87
- # * @brief Clock source options
88
- # *
89
- # * Allowed values for `setClock`.
90
- # */
91
- # typedef enum clock_select {
92
- # MPU6050_INTR_8MHz,
93
- # MPU6050_PLL_GYROX,
94
- # MPU6050_PLL_GYROY,
95
- # MPU6050_PLL_GYROZ,
96
- # MPU6050_PLL_EXT_32K,
97
- # MPU6050_PLL_EXT_19MHz,
98
- # MPU6050_STOP = 7,
99
- # } mpu6050_clock_select_t;
100
-
101
- # /**
102
- # * @brief Accelerometer range options
103
- # *
104
- # * Allowed values for `setAccelerometerRange`.
105
- # */
106
- # typedef enum gyro_range {
107
- MPU6050_RANGE_2_G = 0b00 # +/- 2g (default value)
108
- MPU6050_RANGE_4_G = 0b01 # +/- 4g
109
- MPU6050_RANGE_8_G = 0b10 # +/- 8g
110
- MPU6050_RANGE_16_G = 0b11 # +/- 16g
111
- # } mpu6050_accel_range_t;
112
-
113
- # /**
114
- # * @brief Gyroscope range options
115
- # *
116
- # * Allowed values for `setGyroRange`.
117
- # */
118
- # typedef enum gyro_range {
119
- MPU6050_RANGE_250_DEG = 0 # +/- 250 deg/s (default value)
120
- MPU6050_RANGE_500_DEG = 1 # +/- 500 deg/s
121
- MPU6050_RANGE_1000_DEG = 2 # +/- 1000 deg/s
122
- MPU6050_RANGE_2000_DEG = 3 # +/- 2000 deg/s
123
- # } mpu6050_gyro_range_t;
124
-
125
- # /**
126
- # * @brief Digital low pass filter bandthwidth options
127
- # *
128
- # * Allowed values for `setFilterBandwidth`.
129
- # */
130
- # typedef enum bandwidth {
131
- # MPU6050_BAND_260_HZ, # Docs imply this disables the filter
132
- # MPU6050_BAND_184_HZ, # 184 Hz
133
- # MPU6050_BAND_94_HZ, # 94 Hz
134
- # MPU6050_BAND_44_HZ, # 44 Hz
135
- # MPU6050_BAND_21_HZ, # 21 Hz
136
- # MPU6050_BAND_10_HZ, # 10 Hz
137
- # MPU6050_BAND_5_HZ, # 5 Hz
138
- # } mpu6050_bandwidth_t;
139
-
140
- # /**
141
- # * @brief Periodic measurement options
142
- # *
143
- # * Allowed values for `setCycleRate`.
144
- # */
145
- # typedef enum cycle_rate {
146
- MPU6050_CYCLE_1_25_HZ = 0 # 1.25 Hz
147
- MPU6050_CYCLE_5_HZ = 1 # 5 Hz
148
- MPU6050_CYCLE_20_HZ = 2 # 20 Hz
149
- MPU6050_CYCLE_40_HZ = 3 # 40 Hz
150
- # } mpu6050_cycle_rate_t;
151
112
class MPU6050 :
152
-
113
+ """Driver for the MPU6050 6-axis accelerometer and gyroscope.
114
+ :param ~busio.I2C i2c_bus: The I2C bus the MSA is connected to.
115
+ :param address: The I2C slave address of the sensor
116
+ """
153
117
def __init__ (self , i2c_bus , address = _MPU6050_DEFAULT_ADDRESS ):
154
118
self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
155
119
@@ -159,33 +123,38 @@ def __init__(self, i2c_bus, address=_MPU6050_DEFAULT_ADDRESS):
159
123
self .reset ()
160
124
161
125
def reset (self ):
126
+ """Reinitialize the sensor"""
162
127
self ._reset = True
163
128
while self ._reset is True :
164
129
sleep (0.10 )
165
130
166
- self .sample_rate_divisor = 0
167
- self ._gyro_range = 2
168
- # self._accel_range = MPU6050_RANGE_8_G
169
- self ._filter_bandwidth = 0
170
- self ._clock_source = 1
131
+ self ._sample_rate_divisor = 0
132
+ self ._gyro_range = GyroRange .RANGE_1000_DEG
133
+ self ._accel_range = Range .RANGE_8_G
134
+ sleep (0.1 )
135
+ self ._filter_bandwidth = Bandwidth .BAND_260_HZ
136
+ self ._clock_source = 1 # set to use gyro x-axis as reference
171
137
sleep (0.1 )
172
138
self .sleep = False
173
139
174
-
175
- _device_id = UnaryStruct (_MPU6050_WHO_AM_I , ">B" )
140
+ _device_id = ROUnaryStruct (_MPU6050_WHO_AM_I , ">B" )
176
141
_reset = RWBit (_MPU6050_PWR_MGMT_1 , 7 , 1 )
177
- sample_rate_divisor = UnaryStruct (_MPU6050_SMPLRT_DIV , ">B" )
142
+ _sample_rate_divisor = UnaryStruct (_MPU6050_SMPLRT_DIV , ">B" )
178
143
_gyro_range = RWBits (2 , _MPU6050_GYRO_CONFIG , 3 )
179
- _accel_range = RWBits (2 ,_MPU6050_ACCEL_CONFIG , 3 )
144
+ _accel_range = RWBits (2 , _MPU6050_ACCEL_CONFIG , 3 )
180
145
_filter_bandwidth = RWBits (2 , _MPU6050_CONFIG , 3 )
181
146
_clock_source = RWBits (3 , _MPU6050_PWR_MGMT_1 , 0 )
182
- sleep = RWBit (_MPU6050_PWR_MGMT_1 , 6 , 1 )
183
147
_raw_accel_data = StructArray (_MPU6050_ACCEL_OUT , ">h" , 3 )
184
148
_raw_gyro_data = StructArray (_MPU6050_GYRO_OUT , ">h" , 3 )
185
- _raw_temp_data = UnaryStruct (_MPU6050_TEMP_OUT , ">h" )
149
+ _raw_temp_data = ROUnaryStruct (_MPU6050_TEMP_OUT , ">h" )
186
150
_cycle = RWBit (_MPU6050_PWR_MGMT_1 , 5 )
187
151
_cycle_rate = RWBits (2 , _MPU6050_PWR_MGMT_2 , 6 , 1 )
188
152
153
+ sleep = RWBit (_MPU6050_PWR_MGMT_1 , 6 , 1 )
154
+ """Shuts down the accelerometers and gyroscopes, saving power. No new data will
155
+ be recorded until the sensor is taken out of sleep by setting to `False`"""
156
+
157
+
189
158
@property
190
159
def temperature (self ):
191
160
"""The current temperature in º C"""
@@ -203,13 +172,13 @@ def acceleration(self):
203
172
204
173
accel_range = self ._accel_range
205
174
accel_scale = 1
206
- if accel_range == MPU6050_RANGE_16_G :
175
+ if accel_range == Range . RANGE_16_G :
207
176
accel_scale = 2048
208
- if accel_range == MPU6050_RANGE_8_G :
177
+ if accel_range == Range . RANGE_8_G :
209
178
accel_scale = 4096
210
- if accel_range == MPU6050_RANGE_4_G :
179
+ if accel_range == Range . RANGE_4_G :
211
180
accel_scale = 8192
212
- if accel_range == MPU6050_RANGE_2_G :
181
+ if accel_range == Range . RANGE_2_G :
213
182
accel_scale = 16384
214
183
215
184
# setup range dependant scaling
@@ -229,13 +198,13 @@ def gyro(self):
229
198
230
199
gyro_scale = 1
231
200
gyro_range = self ._gyro_range
232
- if gyro_range == MPU6050_RANGE_250_DEG :
201
+ if gyro_range == GyroRange . RANGE_250_DEG :
233
202
gyro_scale = 131
234
- if gyro_range == MPU6050_RANGE_500_DEG :
203
+ if gyro_range == GyroRange . RANGE_500_DEG :
235
204
gyro_scale = 65.5
236
- if gyro_range == MPU6050_RANGE_1000_DEG :
205
+ if gyro_range == GyroRange . RANGE_1000_DEG :
237
206
gyro_scale = 32.8
238
- if gyro_range == MPU6050_RANGE_2000_DEG :
207
+ if gyro_range == GyroRange . RANGE_2000_DEG :
239
208
gyro_scale = 16.4
240
209
241
210
# setup range dependant scaling
@@ -256,3 +225,50 @@ def cycle(self, value):
256
225
self .sleep = not value
257
226
self ._cycle = value
258
227
228
+ @property
229
+ def gyro_range (self ):
230
+ """The measurement range of all gyroscope axes. Must be a `GyroRange`"""
231
+ return self ._gyro_range
232
+
233
+ @gyro_range .setter
234
+ def gyro_range (self , value ):
235
+ if (value < 0 ) or (value > 3 ):
236
+ raise ValueError ("gyro_range must be a GyroRange" )
237
+ self ._gyro_range = value
238
+ sleep (0.01 )
239
+
240
+ @property
241
+ def accelerometer_range (self ):
242
+ """The measurement range of all accelerometer axes. Must be a `Range`"""
243
+ return self ._accel_range
244
+
245
+ @accelerometer_range .setter
246
+ def accelerometer_range (self , value ):
247
+ if (value < 0 ) or (value > 3 ):
248
+ raise ValueError ("accelerometer_range must be a Range" )
249
+ self ._accel_range = value
250
+ sleep (0.01 )
251
+
252
+ @property
253
+ def filter_bandwidth (self ):
254
+ """The bandwidth of the gyroscope Digital Low Pass Filter. Must be a `GyroRange`"""
255
+ return self ._filter_bandwidth
256
+
257
+ @filter_bandwidth .setter
258
+ def filter_bandwidth (self , value ):
259
+ if (value < 0 ) or (value > 6 ):
260
+ raise ValueError ("filter_bandwidth must be a Bandwidth" )
261
+ self ._filter_bandwidth = value
262
+ sleep (0.01 )
263
+
264
+ @property
265
+ def cycle_rate (self ):
266
+ """The rate that measurements are taken while in `cycle` mode. Must be a `Rate`"""
267
+ return self ._cycle_rate
268
+
269
+ @cycle_rate .setter
270
+ def cycle_rate (self , value ):
271
+ if (value < 0 ) or (value > 3 ):
272
+ raise ValueError ("cycle_rate must be a Rate" )
273
+ self ._cycle_rate = value
274
+ sleep (0.01 )
0 commit comments