Skip to content

Commit 385532b

Browse files
committed
linted and doc'd
1 parent fa0044d commit 385532b

File tree

6 files changed

+149
-114
lines changed

6 files changed

+149
-114
lines changed

.travis.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ python:
1111
cache:
1212
pip: true
1313

14-
# TODO: if deployment to PyPi is desired, change 'DEPLOY_PYPI' to "true",
15-
# or remove the env block entirely and remove the condition in the
16-
# deploy block.
1714
env:
18-
- DEPLOY_PYPI="false"
15+
- DEPLOY_PYPI="true"
1916

2017
deploy:
2118
- provider: releases
@@ -26,12 +23,11 @@ deploy:
2623
overwrite: true
2724
on:
2825
tags: true
29-
# TODO: Use 'travis encrypt --com -r adafruit/<repo slug>' to generate
30-
# the encrypted password for adafruit-travis. Paste result below.
26+
3127
- provider: pypi
3228
user: adafruit-travis
3329
password:
34-
secure: #-- PASTE ENCRYPTED PASSWORD HERE --#
30+
secure: bcUJefESH9Tfvr57A8uXh1//Cv97BfAEE81OM61dZmr33Sce27yxdVBbq4vugezP/G9xP9zlXyJemC/w3BCHO28/jNTCjJl5HiZKLGypuQa8TVcBp6gyMUf+MRcYpMSaSXqc6DHdjQvwoKYheMeRifk2bOzIRo9EaDMuiE44PpcJLAVuFm1Q3n3T9kAQkfC9ObYwn0HE1xAnfKgIRIyDUutMI+fry+ZhDYgV+Gj4xJbMQrmDhtZBQXBpNbu4ZgE0oSPzU2NWdvgeXH0sH4qGD/H9CDMaA0OSeGTk9NHAi2El7H6qY0IA3eEsJJELx+g2jbtZ0WmpY5ti19WC+vYFDD8gkCN5pBldIlaaIpaIqf5vDIFXGmIGFoCTt03gV/dMKXDRYhvEbTEsZmiikdGYFyfdXeXfEn4odHvSXtnAQ8Xgoo0N6uJI6AOpu7GBrZSvQkO3ThLR/f1brTeYGoulu35i6IZ/7Gp3nvXGT9XTKa8+u1pF7y2kr1QhidTC23PQ+W7rTPQdYklf+yYBAMfcLadbrZiDSMTR18s3uraO3vGH71VpCcu35pb/kKJ33k7Z6A74avjc3hvaAoW/eNpIdu6qezVeU2l6d2JwK4b4U0FtcpqzGSUGP9YGKe0TaM52HtkqanpxJpYTWLHl2cWq9fLSd8O5LY4Nz9+QWe3Jd40=
3531
on:
3632
tags: true
3733
condition: $DEPLOY_PYPI = "true"

README.rst

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ This is easily achieved by downloading
3030

3131
Installing from PyPI
3232
=====================
33-
.. note:: This library is not available on PyPI yet. Install documentation is included
34-
as a standard element. Stay tuned for PyPI availability!
35-
36-
.. todo:: Remove the above note if PyPI version is/will be available at time of release.
37-
If the library is not planned for PyPI, remove the entire 'Installing from PyPI' section.
38-
3933
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
4034
PyPI <https://pypi.org/project/adafruit-circuitpython-mpu6050/>`_. To install for current user:
4135

@@ -61,7 +55,22 @@ To install in a virtual environment in your current project:
6155
Usage Example
6256
=============
6357

64-
.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst.
58+
.. code-block:: python
59+
60+
import time
61+
import board
62+
import busio
63+
import adafruit_mpu6050
64+
65+
i2c = busio.I2C(board.SCL, board.SDA)
66+
mpu = adafruit_mpu6050.MPU6050(i2c)
67+
68+
while True:
69+
print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2"%(mpu.acceleration))
70+
print("Gyro X:%.2f, Y: %.2f, Z: %.2f degrees/s"%(mpu.gyro))
71+
print("Temperature: %.2f C"%mpu.temperature)
72+
print("")
73+
time.sleep(1)
6574
6675
Contributing
6776
============

adafruit_mpu6050.py

Lines changed: 111 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,15 @@
3232
--------------------
3333
3434
**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
3836
3937
**Software and Dependencies:**
4038
4139
* Adafruit CircuitPython firmware for the supported boards:
4240
https://github.com/adafruit/circuitpython/releases
4341
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
4844
"""
4945

5046
# imports
@@ -58,8 +54,7 @@
5854
from adafruit_register.i2c_bit import RWBit
5955
from adafruit_register.i2c_bits import RWBits
6056
import adafruit_bus_device.i2c_device as i2c_device
61-
62-
# TODO: Trust but verify
57+
# pylint: disable=bad-whitespace
6358
_MPU6050_DEFAULT_ADDRESS = 0x69 # MPU6050 default i2c address w/ AD0 high
6459
_MPU6050_DEVICE_ID = 0x68 # The correct MPU6050_WHO_AM_I value
6560

@@ -81,75 +76,44 @@
8176
_MPU6050_WHO_AM_I = 0x75 # Divice ID register
8277

8378
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
84111

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;
151112
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+
"""
153117
def __init__(self, i2c_bus, address=_MPU6050_DEFAULT_ADDRESS):
154118
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
155119

@@ -159,33 +123,38 @@ def __init__(self, i2c_bus, address=_MPU6050_DEFAULT_ADDRESS):
159123
self.reset()
160124

161125
def reset(self):
126+
"""Reinitialize the sensor"""
162127
self._reset = True
163128
while self._reset is True:
164129
sleep(0.10)
165130

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
171137
sleep(0.1)
172138
self.sleep = False
173139

174-
175-
_device_id = UnaryStruct(_MPU6050_WHO_AM_I, ">B")
140+
_device_id = ROUnaryStruct(_MPU6050_WHO_AM_I, ">B")
176141
_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")
178143
_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)
180145
_filter_bandwidth = RWBits(2, _MPU6050_CONFIG, 3)
181146
_clock_source = RWBits(3, _MPU6050_PWR_MGMT_1, 0)
182-
sleep = RWBit(_MPU6050_PWR_MGMT_1, 6, 1)
183147
_raw_accel_data = StructArray(_MPU6050_ACCEL_OUT, ">h", 3)
184148
_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")
186150
_cycle = RWBit(_MPU6050_PWR_MGMT_1, 5)
187151
_cycle_rate = RWBits(2, _MPU6050_PWR_MGMT_2, 6, 1)
188152

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+
189158
@property
190159
def temperature(self):
191160
"""The current temperature in º C"""
@@ -203,13 +172,13 @@ def acceleration(self):
203172

204173
accel_range = self._accel_range
205174
accel_scale = 1
206-
if accel_range == MPU6050_RANGE_16_G:
175+
if accel_range == Range.RANGE_16_G:
207176
accel_scale = 2048
208-
if accel_range == MPU6050_RANGE_8_G:
177+
if accel_range == Range.RANGE_8_G:
209178
accel_scale = 4096
210-
if accel_range == MPU6050_RANGE_4_G:
179+
if accel_range == Range.RANGE_4_G:
211180
accel_scale = 8192
212-
if accel_range == MPU6050_RANGE_2_G:
181+
if accel_range == Range.RANGE_2_G:
213182
accel_scale = 16384
214183

215184
# setup range dependant scaling
@@ -229,13 +198,13 @@ def gyro(self):
229198

230199
gyro_scale = 1
231200
gyro_range = self._gyro_range
232-
if gyro_range == MPU6050_RANGE_250_DEG:
201+
if gyro_range == GyroRange.RANGE_250_DEG:
233202
gyro_scale = 131
234-
if gyro_range == MPU6050_RANGE_500_DEG:
203+
if gyro_range == GyroRange.RANGE_500_DEG:
235204
gyro_scale = 65.5
236-
if gyro_range == MPU6050_RANGE_1000_DEG:
205+
if gyro_range == GyroRange.RANGE_1000_DEG:
237206
gyro_scale = 32.8
238-
if gyro_range == MPU6050_RANGE_2000_DEG:
207+
if gyro_range == GyroRange.RANGE_2000_DEG:
239208
gyro_scale = 16.4
240209

241210
# setup range dependant scaling
@@ -256,3 +225,50 @@ def cycle(self, value):
256225
self.sleep = not value
257226
self._cycle = value
258227

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)

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# Uncomment the below if you use native CircuitPython modules such as
2121
# digitalio, micropython and busio. List the modules you use. Without it, the
2222
# autodoc module docs will fail to generate with a warning.
23-
# autodoc_mock_imports = ["digitalio", "busio"]
23+
autodoc_mock_imports = ["adafruit_register", "adafruit_bus_device"]
2424

2525

2626
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'Register': ('https://circuitpython.readthedocs.io/projects/register/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

docs/index.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ Table of Contents
2323
.. toctree::
2424
:caption: Tutorials
2525

26-
.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave
27-
the toctree above for use later.
26+
2827

2928
.. toctree::
3029
:caption: Related Products
3130

32-
.. todo:: Add any product links here. If there are none, then simply delete this todo and leave
33-
the toctree above for use later.
31+
* Adafruit's MPU6050 Breakout: https://adafruit.com/products/3886
32+
33+
3434

3535
.. toctree::
3636
:caption: Other Links

examples/mpu6050_simpletest.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+
9+
while True:
10+
print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2"%(mpu.acceleration))
11+
print("Gyro X:%.2f, Y: %.2f, Z: %.2f degrees/s"%(mpu.gyro))
12+
print("Temperature: %.2f C"%mpu.temperature)
13+
print("")
14+
time.sleep(1)

0 commit comments

Comments
 (0)