From 9820d4d78e42e4653171b12d391277f58fc7e0bb Mon Sep 17 00:00:00 2001 From: dherrada Date: Mon, 29 Jun 2020 17:25:13 -0400 Subject: [PATCH 1/6] Allow uart to go ~100Hz versus 10Hz --- adafruit_bno055.py | 4 ++-- examples/bno055_simpletest.py | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/adafruit_bno055.py b/adafruit_bno055.py index 10fd1be..909de14 100644 --- a/adafruit_bno055.py +++ b/adafruit_bno055.py @@ -392,7 +392,7 @@ 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) + time.sleep(0.01) resp = self._uart.read(self._uart.in_waiting) if len(resp) < 2: raise OSError("UART access error.") @@ -401,7 +401,7 @@ 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) + time.sleep(0.01) resp = self._uart.read(self._uart.in_waiting) if len(resp) < 2: raise OSError("UART access error.") diff --git a/examples/bno055_simpletest.py b/examples/bno055_simpletest.py index 1e3fa6c..b1a9801 100644 --- a/examples/bno055_simpletest.py +++ b/examples/bno055_simpletest.py @@ -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: + continue time.sleep(1) From 84834158247e7b6e4efead767029c92db53fbe55 Mon Sep 17 00:00:00 2001 From: dherrada Date: Wed, 1 Jul 2020 15:41:15 -0400 Subject: [PATCH 2/6] Allow UART to go even faster with some janky recursion --- adafruit_bno055.py | 25 +++++++++++++++++++++---- examples/bno055_simpletest.py | 4 ++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/adafruit_bno055.py b/adafruit_bno055.py index 909de14..1d96c36 100644 --- a/adafruit_bno055.py +++ b/adafruit_bno055.py @@ -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.01) + now = time.time() + while self._uart.in_waiting < 2 and time.time() - now < 0.25: + pass resp = self._uart.read(self._uart.in_waiting) if len(resp) < 2: raise OSError("UART access error.") @@ -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.01) + now = time.time() + while self._uart.in_waiting < length + 2 and time.time() - now < 0.25: + pass resp = self._uart.read(self._uart.in_waiting) + + if resp[0] != 0xBB: # Recursion + self._uart.write(bytes([0xAA, 0x01, register, length])) + now = time.time() + while self._uart.in_waiting < length + 2 and time.time() - 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]) diff --git a/examples/bno055_simpletest.py b/examples/bno055_simpletest.py index b1a9801..2b12805 100644 --- a/examples/bno055_simpletest.py +++ b/examples/bno055_simpletest.py @@ -22,7 +22,7 @@ print("Linear acceleration (m/s^2): {}".format(sensor.linear_acceleration)) print("Gravity (m/s^2): {}".format(sensor.gravity)) print() - except RuntimeError: - continue + except RuntimeError as e: + print(e) time.sleep(1) From 7147de8190f09b19ff7cc1eb0fe3a528d549f543 Mon Sep 17 00:00:00 2001 From: dherrada Date: Wed, 1 Jul 2020 15:44:47 -0400 Subject: [PATCH 3/6] Formatted --- adafruit_bno055.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_bno055.py b/adafruit_bno055.py index 1d96c36..cdcdeee 100644 --- a/adafruit_bno055.py +++ b/adafruit_bno055.py @@ -408,7 +408,7 @@ def _read_register(self, register, length=1): # pylint: disable=arguments-diffe pass resp = self._uart.read(self._uart.in_waiting) - if resp[0] != 0xBB: # Recursion + if resp[0] != 0xBB: # Recursion self._uart.write(bytes([0xAA, 0x01, register, length])) now = time.time() while self._uart.in_waiting < length + 2 and time.time() - now < 0.25: From 0879db5326e56d281fd9e57785d2b55e5fe98dc5 Mon Sep 17 00:00:00 2001 From: dherrada Date: Thu, 2 Jul 2020 14:30:36 -0400 Subject: [PATCH 4/6] Changed time.time to time.monotonic --- adafruit_bno055.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/adafruit_bno055.py b/adafruit_bno055.py index cdcdeee..e7bf916 100644 --- a/adafruit_bno055.py +++ b/adafruit_bno055.py @@ -392,8 +392,8 @@ 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) - now = time.time() - while self._uart.in_waiting < 2 and time.time() - now < 0.25: + 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: @@ -403,15 +403,15 @@ 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])) - now = time.time() - while self._uart.in_waiting < length + 2 and time.time() - now < 0.25: + 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 self._uart.write(bytes([0xAA, 0x01, register, length])) - now = time.time() - while self._uart.in_waiting < length + 2 and time.time() - now < 0.25: + 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: From 843480682b46731786934197d7efdef6206bb53b Mon Sep 17 00:00:00 2001 From: dherrada Date: Fri, 3 Jul 2020 17:06:53 -0400 Subject: [PATCH 5/6] _read_register now uses loop in uart mode to increase stability and speed --- adafruit_bno055.py | 24 ++++++++---------------- docs/conf.py | 10 +++------- examples/bno055_simpletest.py | 20 ++++++++------------ 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/adafruit_bno055.py b/adafruit_bno055.py index e7bf916..4213062 100644 --- a/adafruit_bno055.py +++ b/adafruit_bno055.py @@ -402,28 +402,20 @@ def _write_register(self, register, data): # pylint: disable=arguments-differ raise RuntimeError("UART write error: {}".format(resp[1])) def _read_register(self, register, length=1): # pylint: disable=arguments-differ - 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 resp[0] != 0xBB: # Recursion + i = 0 + while i < 3: self._uart.write(bytes([0xAA, 0x01, register, length])) now = time.monotonic() - while self._uart.in_waiting < length + 2 and time.monotonic() - now < 0.25: + while self._uart.in_waiting < length + 2 and time.monotonic() - now < 0.1: 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 and resp[0] == 0xBB: + i = 3 + i += 1 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]) diff --git a/docs/conf.py b/docs/conf.py index c5429f0..543cbaa 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -10,11 +10,7 @@ # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. -extensions = [ - "sphinx.ext.autodoc", - "sphinx.ext.intersphinx", - "sphinx.ext.viewcode", -] +extensions = ["sphinx.ext.autodoc", "sphinx.ext.intersphinx", "sphinx.ext.viewcode"] # Uncomment the below if you use native CircuitPython modules such as # digitalio, micropython and busio. List the modules you use. Without it, the @@ -144,7 +140,7 @@ "Adafruit BNO055 Library Documentation", "Radomir Dopieralski", "manual", - ), + ) ] # -- Options for manual page output --------------------------------------- @@ -175,5 +171,5 @@ "AdafruitBNO055Library", "One line description of project.", "Miscellaneous", - ), + ) ] diff --git a/examples/bno055_simpletest.py b/examples/bno055_simpletest.py index 2b12805..ce394f1 100644 --- a/examples/bno055_simpletest.py +++ b/examples/bno055_simpletest.py @@ -12,17 +12,13 @@ # sensor = adafruit_bno055.BNO055_UART(uart) while True: - # 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) + 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() time.sleep(1) From d73e43262871a8b4deffa8f51b538db3bb5be864 Mon Sep 17 00:00:00 2001 From: dherrada Date: Mon, 6 Jul 2020 18:32:47 -0400 Subject: [PATCH 6/6] Added break, reverted simpletest --- adafruit_bno055.py | 2 +- examples/bno055_simpletest.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_bno055.py b/adafruit_bno055.py index 4213062..2ad5a28 100644 --- a/adafruit_bno055.py +++ b/adafruit_bno055.py @@ -410,7 +410,7 @@ def _read_register(self, register, length=1): # pylint: disable=arguments-diffe pass resp = self._uart.read(self._uart.in_waiting) if len(resp) >= 2 and resp[0] == 0xBB: - i = 3 + break i += 1 if len(resp) < 2: raise OSError("UART access error.") diff --git a/examples/bno055_simpletest.py b/examples/bno055_simpletest.py index ce394f1..1e3fa6c 100644 --- a/examples/bno055_simpletest.py +++ b/examples/bno055_simpletest.py @@ -12,6 +12,7 @@ # 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))