Skip to content

Commit 8434806

Browse files
committed
_read_register now uses loop in uart mode to increase stability and speed
1 parent 0879db5 commit 8434806

File tree

3 files changed

+19
-35
lines changed

3 files changed

+19
-35
lines changed

adafruit_bno055.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -402,28 +402,20 @@ def _write_register(self, register, data): # pylint: disable=arguments-differ
402402
raise RuntimeError("UART write error: {}".format(resp[1]))
403403

404404
def _read_register(self, register, length=1): # pylint: disable=arguments-differ
405-
self._uart.write(bytes([0xAA, 0x01, register, length]))
406-
now = time.monotonic()
407-
while self._uart.in_waiting < length + 2 and time.monotonic() - now < 0.25:
408-
pass
409-
resp = self._uart.read(self._uart.in_waiting)
410-
411-
if resp[0] != 0xBB: # Recursion
405+
i = 0
406+
while i < 3:
412407
self._uart.write(bytes([0xAA, 0x01, register, length]))
413408
now = time.monotonic()
414-
while self._uart.in_waiting < length + 2 and time.monotonic() - now < 0.25:
409+
while self._uart.in_waiting < length + 2 and time.monotonic() - now < 0.1:
415410
pass
416411
resp = self._uart.read(self._uart.in_waiting)
417-
if len(resp) < 2:
418-
raise OSError("UART access error.")
419-
if resp[0] != 0xBB:
420-
raise RuntimeError("UART read error: {}".format(resp[1]))
421-
if length > 1:
422-
return resp[2:]
423-
return int(resp[2])
424-
412+
if len(resp) >= 2 and resp[0] == 0xBB:
413+
i = 3
414+
i += 1
425415
if len(resp) < 2:
426416
raise OSError("UART access error.")
417+
if resp[0] != 0xBB:
418+
raise RuntimeError("UART read error: {}".format(resp[1]))
427419
if length > 1:
428420
return resp[2:]
429421
return int(resp[2])

docs/conf.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
# Add any Sphinx extension module names here, as strings. They can be
1111
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
1212
# ones.
13-
extensions = [
14-
"sphinx.ext.autodoc",
15-
"sphinx.ext.intersphinx",
16-
"sphinx.ext.viewcode",
17-
]
13+
extensions = ["sphinx.ext.autodoc", "sphinx.ext.intersphinx", "sphinx.ext.viewcode"]
1814

1915
# Uncomment the below if you use native CircuitPython modules such as
2016
# digitalio, micropython and busio. List the modules you use. Without it, the
@@ -144,7 +140,7 @@
144140
"Adafruit BNO055 Library Documentation",
145141
"Radomir Dopieralski",
146142
"manual",
147-
),
143+
)
148144
]
149145

150146
# -- Options for manual page output ---------------------------------------
@@ -175,5 +171,5 @@
175171
"AdafruitBNO055Library",
176172
"One line description of project.",
177173
"Miscellaneous",
178-
),
174+
)
179175
]

examples/bno055_simpletest.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@
1212
# sensor = adafruit_bno055.BNO055_UART(uart)
1313

1414
while True:
15-
# Try/except only necessary when usuing UART mode
16-
try:
17-
print("Accelerometer (m/s^2): {}".format(sensor.acceleration))
18-
print("Magnetometer (microteslas): {}".format(sensor.magnetic))
19-
print("Gyroscope (rad/sec): {}".format(sensor.gyro))
20-
print("Euler angle: {}".format(sensor.euler))
21-
print("Quaternion: {}".format(sensor.quaternion))
22-
print("Linear acceleration (m/s^2): {}".format(sensor.linear_acceleration))
23-
print("Gravity (m/s^2): {}".format(sensor.gravity))
24-
print()
25-
except RuntimeError as e:
26-
print(e)
15+
print("Accelerometer (m/s^2): {}".format(sensor.acceleration))
16+
print("Magnetometer (microteslas): {}".format(sensor.magnetic))
17+
print("Gyroscope (rad/sec): {}".format(sensor.gyro))
18+
print("Euler angle: {}".format(sensor.euler))
19+
print("Quaternion: {}".format(sensor.quaternion))
20+
print("Linear acceleration (m/s^2): {}".format(sensor.linear_acceleration))
21+
print("Gravity (m/s^2): {}".format(sensor.gravity))
22+
print()
2723

2824
time.sleep(1)

0 commit comments

Comments
 (0)