70
70
_MPU6050_ACCEL_OUT = 0x3B # base address for sensor data reads
71
71
_MPU6050_TEMP_OUT = 0x41 # Temperature data high byte register
72
72
_MPU6050_GYRO_OUT = 0x43 # base address for sensor data reads
73
+ _MPU6050_SIG_PATH_RESET = 0x68 # register to reset sensor signal paths
73
74
_MPU6050_USER_CTRL = 0x6A # FIFO and I2C Master control register
74
75
_MPU6050_PWR_MGMT_1 = 0x6B # Primary power/sleep control register
75
76
_MPU6050_PWR_MGMT_2 = 0x6C # Secondary power/sleep control register
79
80
# pylint: enable=bad-whitespace
80
81
81
82
class Range : # pylint: disable=too-few-public-methods
82
- """Allowed values for `accelerometer_range`."""
83
+ """Allowed values for `accelerometer_range`.
84
+
85
+ - ``Range.RANGE_2_G``
86
+ - ``Range.RANGE_4_G``
87
+ - ``Range.RANGE_8_G``
88
+ - ``Range.RANGE_16_G``
89
+
90
+ """
83
91
RANGE_2_G = 0 # +/- 2g (default value)
84
92
RANGE_4_G = 1 # +/- 4g
85
93
RANGE_8_G = 2 # +/- 8g
86
94
RANGE_16_G = 3 # +/- 16g
87
95
88
96
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
97
+ """Allowed values for `gyro_range`.
98
+
99
+ - ``GyroRange.RANGE_250_DPS``
100
+ - ``GyroRange.RANGE_500_DPS``
101
+ - ``GyroRange.RANGE_1000_DPS``
102
+ - ``GyroRange.RANGE_2000_DPS``
103
+
104
+ """
105
+ RANGE_250_DPS = 0 # +/- 250 deg/s (default value)
106
+ RANGE_500_DPS = 1 # +/- 500 deg/s
107
+ RANGE_1000_DPS = 2 # +/- 1000 deg/s
108
+ RANGE_2000_DPS = 3 # +/- 2000 deg/s
94
109
95
110
class Bandwidth : # pylint: disable=too-few-public-methods
96
- """Allowed values for `filter_bandwidth`."""
111
+ """Allowed values for `filter_bandwidth`.
112
+
113
+ - ``Bandwidth.BAND_260_HZ``
114
+ - ``Bandwidth.BAND_184_HZ``
115
+ - ``Bandwidth.BAND_94_HZ``
116
+ - ``Bandwidth.BAND_44_HZ``
117
+ - ``Bandwidth.BAND_21_HZ``
118
+ - ``Bandwidth.BAND_10_HZ``
119
+ - ``Bandwidth.BAND_5_HZ``
120
+
121
+ """
97
122
BAND_260_HZ = 0 # Docs imply this disables the filter
98
123
BAND_184_HZ = 1 # 184 Hz
99
124
BAND_94_HZ = 2 # 94 Hz
@@ -103,16 +128,25 @@ class Bandwidth: # pylint: disable=too-few-public-methods
103
128
BAND_5_HZ = 6 # 5 Hz
104
129
105
130
class Rate : # pylint: disable=too-few-public-methods
106
- """Allowed values for `cycle_rate`"""
131
+ """Allowed values for `cycle_rate`.
132
+
133
+ - ``Rate.CYCLE_1_25_HZ``
134
+ - ``Rate.CYCLE_5_HZ``
135
+ - ``Rate.CYCLE_20_HZ``
136
+ - ``Rate.CYCLE_40_HZ``
137
+
138
+ """
107
139
CYCLE_1_25_HZ = 0 # 1.25 Hz
108
140
CYCLE_5_HZ = 1 # 5 Hz
109
141
CYCLE_20_HZ = 2 # 20 Hz
110
142
CYCLE_40_HZ = 3 # 40 Hz
111
143
112
144
class MPU6050 :
113
145
"""Driver for the MPU6050 6-axis accelerometer and gyroscope.
114
- :param ~busio.I2C i2c_bus: The I2C bus the MSA is connected to.
146
+
147
+ :param ~busio.I2C i2c_bus: The I2C bus the MPU6050 is connected to.
115
148
:param address: The I2C slave address of the sensor
149
+
116
150
"""
117
151
def __init__ (self , i2c_bus , address = _MPU6050_DEFAULT_ADDRESS ):
118
152
self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
@@ -122,38 +156,50 @@ def __init__(self, i2c_bus, address=_MPU6050_DEFAULT_ADDRESS):
122
156
123
157
self .reset ()
124
158
159
+ self ._sample_rate_divisor = 0
160
+ self ._filter_bandwidth = Bandwidth .BAND_260_HZ
161
+ self ._gyro_range = GyroRange .RANGE_500_DPS
162
+ self ._accel_range = Range .RANGE_2_G
163
+ sleep (0.100 )
164
+ self ._clock_source = 1 # set to use gyro x-axis as reference
165
+ sleep (0.100 )
166
+ self .sleep = False
167
+ sleep (0.010 )
168
+
169
+
125
170
def reset (self ):
126
171
"""Reinitialize the sensor"""
127
172
self ._reset = True
128
173
while self ._reset is True :
129
- sleep (0.10 )
174
+ sleep (0.001 )
175
+ sleep (0.100 )
130
176
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
137
- sleep (0.1 )
138
- self .sleep = False
177
+ _signal_path_reset = 0b111 # reset all sensors
178
+ sleep (0.100 )
139
179
180
+ _clock_source = RWBits (3 , _MPU6050_PWR_MGMT_1 , 0 )
140
181
_device_id = ROUnaryStruct (_MPU6050_WHO_AM_I , ">B" )
182
+
141
183
_reset = RWBit (_MPU6050_PWR_MGMT_1 , 7 , 1 )
142
- _sample_rate_divisor = UnaryStruct (_MPU6050_SMPLRT_DIV , ">B" )
184
+ _signal_path_reset = RWBits (3 , _MPU6050_SIG_PATH_RESET , 3 )
185
+
143
186
_gyro_range = RWBits (2 , _MPU6050_GYRO_CONFIG , 3 )
144
187
_accel_range = RWBits (2 , _MPU6050_ACCEL_CONFIG , 3 )
188
+
145
189
_filter_bandwidth = RWBits (2 , _MPU6050_CONFIG , 3 )
146
- _clock_source = RWBits ( 3 , _MPU6050_PWR_MGMT_1 , 0 )
190
+
147
191
_raw_accel_data = StructArray (_MPU6050_ACCEL_OUT , ">h" , 3 )
148
192
_raw_gyro_data = StructArray (_MPU6050_GYRO_OUT , ">h" , 3 )
149
193
_raw_temp_data = ROUnaryStruct (_MPU6050_TEMP_OUT , ">h" )
194
+
150
195
_cycle = RWBit (_MPU6050_PWR_MGMT_1 , 5 )
151
196
_cycle_rate = RWBits (2 , _MPU6050_PWR_MGMT_2 , 6 , 1 )
152
197
153
198
sleep = RWBit (_MPU6050_PWR_MGMT_1 , 6 , 1 )
154
199
"""Shuts down the accelerometers and gyroscopes, saving power. No new data will
155
200
be recorded until the sensor is taken out of sleep by setting to `False`"""
156
-
201
+ sample_rate_divisor = UnaryStruct (_MPU6050_SMPLRT_DIV , ">B" )
202
+ """The sample rate divisor. See the datasheet for additional detail"""
157
203
158
204
@property
159
205
def temperature (self ):
@@ -198,13 +244,13 @@ def gyro(self):
198
244
199
245
gyro_scale = 1
200
246
gyro_range = self ._gyro_range
201
- if gyro_range == GyroRange .RANGE_250_DEG :
247
+ if gyro_range == GyroRange .RANGE_250_DPS :
202
248
gyro_scale = 131
203
- if gyro_range == GyroRange .RANGE_500_DEG :
249
+ if gyro_range == GyroRange .RANGE_500_DPS :
204
250
gyro_scale = 65.5
205
- if gyro_range == GyroRange .RANGE_1000_DEG :
251
+ if gyro_range == GyroRange .RANGE_1000_DPS :
206
252
gyro_scale = 32.8
207
- if gyro_range == GyroRange .RANGE_2000_DEG :
253
+ if gyro_range == GyroRange .RANGE_2000_DPS :
208
254
gyro_scale = 16.4
209
255
210
256
# setup range dependant scaling
0 commit comments