Skip to content

Commit e065b6d

Browse files
committed
good for 1.0.0
1 parent 385532b commit e065b6d

File tree

6 files changed

+155
-48
lines changed

6 files changed

+155
-48
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ deploy:
2727
- provider: pypi
2828
user: adafruit-travis
2929
password:
30-
secure: bcUJefESH9Tfvr57A8uXh1//Cv97BfAEE81OM61dZmr33Sce27yxdVBbq4vugezP/G9xP9zlXyJemC/w3BCHO28/jNTCjJl5HiZKLGypuQa8TVcBp6gyMUf+MRcYpMSaSXqc6DHdjQvwoKYheMeRifk2bOzIRo9EaDMuiE44PpcJLAVuFm1Q3n3T9kAQkfC9ObYwn0HE1xAnfKgIRIyDUutMI+fry+ZhDYgV+Gj4xJbMQrmDhtZBQXBpNbu4ZgE0oSPzU2NWdvgeXH0sH4qGD/H9CDMaA0OSeGTk9NHAi2El7H6qY0IA3eEsJJELx+g2jbtZ0WmpY5ti19WC+vYFDD8gkCN5pBldIlaaIpaIqf5vDIFXGmIGFoCTt03gV/dMKXDRYhvEbTEsZmiikdGYFyfdXeXfEn4odHvSXtnAQ8Xgoo0N6uJI6AOpu7GBrZSvQkO3ThLR/f1brTeYGoulu35i6IZ/7Gp3nvXGT9XTKa8+u1pF7y2kr1QhidTC23PQ+W7rTPQdYklf+yYBAMfcLadbrZiDSMTR18s3uraO3vGH71VpCcu35pb/kKJ33k7Z6A74avjc3hvaAoW/eNpIdu6qezVeU2l6d2JwK4b4U0FtcpqzGSUGP9YGKe0TaM52HtkqanpxJpYTWLHl2cWq9fLSd8O5LY4Nz9+QWe3Jd40=
30+
secure: Gn6iXRZcCRbyC8tw1ok3LhRkRPA4cQY1mF8YEzagXzclwDA752MWoIZrOVlfdeiawvC/IS5y2RSUMcYUF6aoY6FlodQZ3nZlbKfn64iwasl7iqq21hNkByhfqnPrvDZdwqqt7LaXvacGvfHUuZ4cEqItJ+zGwCUXeRra3s8UbGzzhKZp51INv4xzJ2tP1T1nfpwpvVUHfxied52XBoEwmJ9/sV4BCmkIhB2OnR/jANPLUjPkLIHJjl9jc35Z7TciODtxnx9oWVtviztMOTJNT4U+mlbnAR82HiYhKxzpavq6NTqumUQJIGp57wTT7b9cKGzmAgZy7WMJzFhcMmGsTuqoYUyHNAEHFcri18Vot1RFvd8+HZR/aeG5bJcK5WkHveiFUn96O4Gd8GaNDMjAZS+ay0ZOepEDSBidLlmo1067Qkkc50idGdvnAop9yg16iSw3TU421b4jzXDCNE7+UJS65xSdvnLgyCFSpX6O2viCArOW4b99MKLdS2VJhAYRabAeBd+aCwMPnjHEz9fpkoohJzYT9pUa612lOvcBdFTeKBQfBBc4lb3RT6D3NvKpdpo1Hzdi4XI1clPzXA9F1AnQPxVaJCPCgAQqk8sn72PSDxuOJ+XyFr+rWBA/flVffNMJ49QAqFlN/wQgqBnevxJagWtFdoG1tIwAHB9cRvU=
3131
on:
3232
tags: true
3333
condition: $DEPLOY_PYPI = "true"

README.rst

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,10 @@ Contributing
7676
============
7777

7878
Contributions are welcome! Please read our `Code of Conduct
79-
<https://github.com/adafruit/Adafruit_CircuitPython_MPU6050/blob/master/CODE_OF_CONDUCT.md>`_
79+
<https://github.com/adafruit/Adafruit_CircuitPython_74HC595/blob/master/CODE_OF_CONDUCT.md>`_
8080
before contributing to help this project stay welcoming.
8181

82-
Sphinx documentation
83-
-----------------------
84-
85-
Sphinx is used to build the documentation based on rST files and comments in the code. First,
86-
install dependencies (feel free to reuse the virtual environment from above):
87-
88-
.. code-block:: shell
89-
90-
python3 -m venv .env
91-
source .env/bin/activate
92-
pip install Sphinx sphinx-rtd-theme
93-
94-
Now, once you have the virtual environment activated:
95-
96-
.. code-block:: shell
97-
98-
cd docs
99-
sphinx-build -E -W -b html . _build/html
82+
Documentation
83+
=============
10084

101-
This will output the documentation to ``docs/_build/html``. Open the index.html in your browser to
102-
view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to
103-
locally verify it will pass.
85+
For information on building library documentation, please check out `this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.

adafruit_mpu6050.py

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
_MPU6050_ACCEL_OUT = 0x3B # base address for sensor data reads
7171
_MPU6050_TEMP_OUT = 0x41 # Temperature data high byte register
7272
_MPU6050_GYRO_OUT = 0x43 # base address for sensor data reads
73+
_MPU6050_SIG_PATH_RESET = 0x68 # register to reset sensor signal paths
7374
_MPU6050_USER_CTRL = 0x6A # FIFO and I2C Master control register
7475
_MPU6050_PWR_MGMT_1 = 0x6B # Primary power/sleep control register
7576
_MPU6050_PWR_MGMT_2 = 0x6C # Secondary power/sleep control register
@@ -79,21 +80,45 @@
7980
# pylint: enable=bad-whitespace
8081

8182
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+
"""
8391
RANGE_2_G = 0 # +/- 2g (default value)
8492
RANGE_4_G = 1 # +/- 4g
8593
RANGE_8_G = 2 # +/- 8g
8694
RANGE_16_G = 3 # +/- 16g
8795

8896
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
94109

95110
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+
"""
97122
BAND_260_HZ = 0 # Docs imply this disables the filter
98123
BAND_184_HZ = 1 # 184 Hz
99124
BAND_94_HZ = 2 # 94 Hz
@@ -103,16 +128,25 @@ class Bandwidth: # pylint: disable=too-few-public-methods
103128
BAND_5_HZ = 6 # 5 Hz
104129

105130
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+
"""
107139
CYCLE_1_25_HZ = 0 # 1.25 Hz
108140
CYCLE_5_HZ = 1 # 5 Hz
109141
CYCLE_20_HZ = 2 # 20 Hz
110142
CYCLE_40_HZ = 3 # 40 Hz
111143

112144
class MPU6050:
113145
"""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.
115148
:param address: The I2C slave address of the sensor
149+
116150
"""
117151
def __init__(self, i2c_bus, address=_MPU6050_DEFAULT_ADDRESS):
118152
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
@@ -122,38 +156,50 @@ def __init__(self, i2c_bus, address=_MPU6050_DEFAULT_ADDRESS):
122156

123157
self.reset()
124158

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+
125170
def reset(self):
126171
"""Reinitialize the sensor"""
127172
self._reset = True
128173
while self._reset is True:
129-
sleep(0.10)
174+
sleep(0.001)
175+
sleep(0.100)
130176

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)
139179

180+
_clock_source = RWBits(3, _MPU6050_PWR_MGMT_1, 0)
140181
_device_id = ROUnaryStruct(_MPU6050_WHO_AM_I, ">B")
182+
141183
_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+
143186
_gyro_range = RWBits(2, _MPU6050_GYRO_CONFIG, 3)
144187
_accel_range = RWBits(2, _MPU6050_ACCEL_CONFIG, 3)
188+
145189
_filter_bandwidth = RWBits(2, _MPU6050_CONFIG, 3)
146-
_clock_source = RWBits(3, _MPU6050_PWR_MGMT_1, 0)
190+
147191
_raw_accel_data = StructArray(_MPU6050_ACCEL_OUT, ">h", 3)
148192
_raw_gyro_data = StructArray(_MPU6050_GYRO_OUT, ">h", 3)
149193
_raw_temp_data = ROUnaryStruct(_MPU6050_TEMP_OUT, ">h")
194+
150195
_cycle = RWBit(_MPU6050_PWR_MGMT_1, 5)
151196
_cycle_rate = RWBits(2, _MPU6050_PWR_MGMT_2, 6, 1)
152197

153198
sleep = RWBit(_MPU6050_PWR_MGMT_1, 6, 1)
154199
"""Shuts down the accelerometers and gyroscopes, saving power. No new data will
155200
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"""
157203

158204
@property
159205
def temperature(self):
@@ -198,13 +244,13 @@ def gyro(self):
198244

199245
gyro_scale = 1
200246
gyro_range = self._gyro_range
201-
if gyro_range == GyroRange.RANGE_250_DEG:
247+
if gyro_range == GyroRange.RANGE_250_DPS:
202248
gyro_scale = 131
203-
if gyro_range == GyroRange.RANGE_500_DEG:
249+
if gyro_range == GyroRange.RANGE_500_DPS:
204250
gyro_scale = 65.5
205-
if gyro_range == GyroRange.RANGE_1000_DEG:
251+
if gyro_range == GyroRange.RANGE_1000_DPS:
206252
gyro_scale = 32.8
207-
if gyro_range == GyroRange.RANGE_2000_DEG:
253+
if gyro_range == GyroRange.RANGE_2000_DPS:
208254
gyro_scale = 16.4
209255

210256
# setup range dependant scaling

docs/examples.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,23 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/mpu6050_simpletest.py
77
:caption: examples/mpu6050_simpletest.py
88
:linenos:
9+
10+
Plotter Example
11+
---------------
12+
13+
See the effects of changing the gyroscope and accelerometer range
14+
by viewing the data in a serial plotter
15+
16+
.. literalinclude:: ../examples/mpu6050_plotter_example.py
17+
:caption: examples/mpu6050_plotter_example.py
18+
:linenos:
19+
20+
Sleep Example
21+
---------------
22+
23+
Observe how the cycle and sleep modes effect measurements
24+
by viewing the data in a serial plotter
25+
26+
.. literalinclude:: ../examples/mpu6050_sleep_example.py
27+
:caption: examples/mpu6050_sleep_example.py
28+
:linenos:

examples/mpu6050_plotter_example.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import time
2+
import board
3+
import busio
4+
import adafruit_mpu6050
5+
6+
i2c = busio.I2C(board.SCL, board.SDA)
7+
mpu = adafruit_mpu6050.MPU6050(i2c)
8+
mpu.accelerometer_range = adafruit_mpu6050.Range.RANGE_2_G
9+
mpu.gyro_range = adafruit_mpu6050.GyroRange.RANGE_250_DPS
10+
while True:
11+
# this prints out all the values like a tuple which Mu's plotter prefer
12+
print("(%.2f, %.2f, %.2f "%(mpu.acceleration), end=", ")
13+
print("%.2f, %.2f, %.2f)"%(mpu.gyro))
14+
time.sleep(0.010)

examples/mpu6050_sleep_example.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import time
2+
import board
3+
import busio
4+
import adafruit_mpu6050
5+
6+
i2c = busio.I2C(board.SCL, board.SDA)
7+
mpu = adafruit_mpu6050.MPU6050(i2c)
8+
9+
# This example is meant to be used with the serial plotter which makes
10+
# it easier to see how the readings change with different settings.
11+
# Make sure to poke and prod the sensor while the demo is running to
12+
# generate some intersting data!
13+
14+
while True:
15+
# first show some 'normal' readings
16+
17+
mpu.sleep = False
18+
mpu.cycle = False
19+
20+
for count in range(0, 100):
21+
print(mpu.acceleration)
22+
time.sleep(0.010)
23+
24+
# Next, set a slow cycle rate so the effect can be seen clearly.
25+
mpu.cycle_Rate = adafruit_mpu6050.Rate.CYCLE_5_HZ
26+
# ensure that we're not sleeping or cycle won't work
27+
mpu.sleep = False
28+
# Finally, enable cycle mode
29+
mpu.cycle = True
30+
31+
for count in range(0, 100):
32+
print(mpu.acceleration)
33+
time.sleep(0.010)
34+
35+
# Finally enable sleep mode. Note that while we can still fetch
36+
# data from the measurement registers, the measurements are not
37+
# updated. In sleep mode the accelerometer and gyroscope are
38+
# deactivated to save power, so measurements are halted.
39+
40+
mpu.cycle = False
41+
mpu.sleep = True
42+
43+
for count in range(0, 100):
44+
print(mpu.acceleration)
45+
time.sleep(0.010)

0 commit comments

Comments
 (0)