Skip to content

Allow uart to go ~100Hz versus 10Hz #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions adafruit_bno055.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ def _write_register(self, register, data): # pylint: disable=arguments-differ
if not isinstance(data, bytes):
data = bytes([data])
self._uart.write(bytes([0xAA, 0x00, register, len(data)]) + data)
time.sleep(0.1)
now = time.monotonic()
while self._uart.in_waiting < 2 and time.monotonic() - now < 0.25:
pass
resp = self._uart.read(self._uart.in_waiting)
if len(resp) < 2:
raise OSError("UART access error.")
Expand All @@ -401,12 +403,27 @@ def _write_register(self, register, data): # pylint: disable=arguments-differ

def _read_register(self, register, length=1): # pylint: disable=arguments-differ
self._uart.write(bytes([0xAA, 0x01, register, length]))
time.sleep(0.1)
now = time.monotonic()
while self._uart.in_waiting < length + 2 and time.monotonic() - now < 0.25:
pass
resp = self._uart.read(self._uart.in_waiting)

if resp[0] != 0xBB: # Recursion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not recursion because recursion would call _read_register. Why did you add this? It wasn't needed before.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial plan was to actually do that, but I couldn't think of a good way to add in an exit condition so it didn't just keep going until it reached the recursion limit, and I guess I forgot to remove the comment.

It's needed because the, when running in UART mode, even in the version that's currently on master, it raises a UART read error every 3 or 4 reads when you're trying to use the simpletest. I think that before it may have mostly been tested by getting a single value, such as acceleration, at a time. That being said, even though I know the majority of people aren't using this with UART, I am quite surprised that I haven't seen anyone else encountering that issue, so it may be worthwhile for some other people with the sensor to do some testing to ensure that issue doesn't only affect me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the errors go away if you increase the timeout? If you really need it, I think you could just have an outer loop and count how many tries you want to do before raising an error. No need to copy things a second time.

self._uart.write(bytes([0xAA, 0x01, register, length]))
now = time.monotonic()
while self._uart.in_waiting < length + 2 and time.monotonic() - now < 0.25:
pass
resp = self._uart.read(self._uart.in_waiting)
if len(resp) < 2:
raise OSError("UART access error.")
if resp[0] != 0xBB:
raise RuntimeError("UART read error: {}".format(resp[1]))
if length > 1:
return resp[2:]
return int(resp[2])

if len(resp) < 2:
raise OSError("UART access error.")
if resp[0] != 0xBB:
raise RuntimeError("UART read error: {}".format(resp[1]))
if length > 1:
return resp[2:]
return int(resp[2])
Expand Down
21 changes: 12 additions & 9 deletions examples/bno055_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
# sensor = adafruit_bno055.BNO055_UART(uart)

while True:
print("Temperature: {} degrees C".format(sensor.temperature))
print("Accelerometer (m/s^2): {}".format(sensor.acceleration))
print("Magnetometer (microteslas): {}".format(sensor.magnetic))
print("Gyroscope (rad/sec): {}".format(sensor.gyro))
print("Euler angle: {}".format(sensor.euler))
print("Quaternion: {}".format(sensor.quaternion))
print("Linear acceleration (m/s^2): {}".format(sensor.linear_acceleration))
print("Gravity (m/s^2): {}".format(sensor.gravity))
print()
# Try/except only necessary when usuing UART mode
try:
print("Accelerometer (m/s^2): {}".format(sensor.acceleration))
print("Magnetometer (microteslas): {}".format(sensor.magnetic))
print("Gyroscope (rad/sec): {}".format(sensor.gyro))
print("Euler angle: {}".format(sensor.euler))
print("Quaternion: {}".format(sensor.quaternion))
print("Linear acceleration (m/s^2): {}".format(sensor.linear_acceleration))
print("Gravity (m/s^2): {}".format(sensor.gravity))
print()
except RuntimeError as e:
print(e)

time.sleep(1)