0

bno055 UART reading errors
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

bno055 UART reading errors

by Robin_Koek on Wed Jun 02, 2021 10:05 am

Dear support,

I'm working with the bno055 sensor on a Raspberri Pi 4 reading the sensor over UART based on this tutorial: https://learn.adafruit.com/adafruit-bno ... cuitpython

I have wired as described in the 'Python Computer Wiring - UART' diagram and since I only need the quaternions for the project I intend use it I have simplified the example Python code to the following lines:

Code: Select all | TOGGLE FULL SIZE
import time
import serial
import adafruit_bno055

uart = serial.Serial("/dev/serial0")
sensor = adafruit_bno055.BNO055_UART(uart)

while True:
    print("Quaternion: {}".format(sensor.quaternion))

    time.sleep(0.05)


I do get data in from the sensor but I run into 2 issues:
1.) The data seems rather laggy, as if it buffers all the time and catches up
2.) The script is dropping out at random times always stating 'RuntimeError: UART read error: 7'

Here is a traceback error from the shell:

Quaternion: (0.998779296875, 0.00128173828125, -0.00311279296875, -0.04974365234375)
Traceback (most recent call last):
File "/home/pi/Desktop/bno055UARTdevsleeptests.py", line 9, in <module>
print("Quaternion: {}".format(sensor.quaternion))
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_bno055.py", line 456, in quaternion
return self._quaternion
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_bno055.py", line 852, in _quaternion
resp = struct.unpack("<hhhh", self._read_register(0x20, 8))
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_bno055.py", line 821, in _read_register
raise RuntimeError("UART read error: {}".format(resp[1]))
RuntimeError: UART read error: 7
>>>


I have made a few screen captures with the sensor both being stationary as well as rotating for impression stored on a google drive folder

If you have any troubleshooting advice for getting more stable data and the cause of the read error this would be highly appreciated!

In the past I used the old bno055 library and this tutorial and the performance of the sensor was very stable and much faster. Is there anything I should revise BNO055_UART class itself to improve performance? I have to state my Python knowledge is very limited.

Thanks kindly,
Robin

Robin_Koek
 
Posts: 8
Joined: Fri Apr 30, 2021 1:21 pm

Re: bno055 UART reading errors

by mikeysklar on Wed Jun 02, 2021 5:39 pm

Hi Robin,

Good questions and we can get to those.

Can you confirm there is not RF stuff near the sensor? This is some concern about 2MHz RF near BNO055 causing intererance and data to be dropped. If the following code works without bailing out I would think the sleep(1) might be making the difference and you can experiment with reducing that value.

When you run the full code example are you see the same error? Comment out the i2c lines and uncomment the uart ones.

Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_bno055


i2c = board.I2C()
sensor = adafruit_bno055.BNO055_I2C(i2c)

# If you are going to use UART uncomment these lines
# uart = board.UART()
# sensor = adafruit_bno055.BNO055_UART(uart)

last_val = 0xFFFF


def temperature():
    global last_val  # pylint: disable=global-statement
    result = sensor.temperature
    if abs(result - last_val) == 128:
        result = sensor.temperature
        if abs(result - last_val) == 128:
            return 0b00111111 & result
    last_val = result
    return result


while True:
    print("Temperature: {} degrees C".format(sensor.temperature))
    """
    print(
        "Temperature: {} degrees C".format(temperature())
    )  # Uncomment if using a Raspberry Pi
    """
    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)


mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: bno055 UART reading errors

by Robin_Koek on Thu Jun 03, 2021 4:17 pm

Thanks for your reply @mikeysklar.

Once I try to run the basis test script you have pasted I get the following traceback error from the shell:

Traceback (most recent call last):
File "/home/pi/Desktop/adafruit_bno_test.py", line 13, in <module>
uart = board.UART()
AttributeError: module 'board' has no attribute 'UART'
>>>


I had this error before and for this reason I swapped to

'import serial
import adafruit_bno055
uart = serial.Serial("/dev/serial0")
sensor = adafruit_bno055.BNO055_UART(uart)'

Would you have any idea how to resolve this 'board' has no attribute 'UART' error?

I have follow all steps on: https://learn.adafruit.com/circuitpytho ... ypi-linux/ and also found this in the faq (https://learn.adafruit.com/circuitpytho ... leshooting). I have tried:

sudo python3 -m pip install --upgrade --force-reinstall adafruit-blinka Adafruit-PlatformDetect

and python3 -m pip install --upgrade --force-reinstall adafruit-blinka

but neither resolved the issue of the UART attribute

In terms of RF, there is a small antenna next to the bno055 for position tracking but it only transmits pulses on UWB which is from 3.5 GHz to 6.5 GHz, so that should not give any interference as this is far beyond 2 MHz I think ?

Robin_Koek
 
Posts: 8
Joined: Fri Apr 30, 2021 1:21 pm

Re: bno055 UART reading errors

by Robin_Koek on Thu Jun 03, 2021 4:57 pm

Hi,

just a quick follow-up as I found a post from 2020 from a user with an identical problem: viewtopic.php?f=19&t=172219&start=15

And it seems on the UART side there was no solution back then to get a stable reading on Raspberry Pi 4 and also that reading the bno055 on and RPi4 was barely tested at all (to my surprise this not mentioned in any tutorial!). Is this still the case or is there more in-depth knowledge on it and are there more solutions available? This past topic does not make me very hopeful on investing a lot of energy to read the sensor over UART on a Pi 4. I have to state that using the old library with succesful readings was on a Pi 3 so I have the same experience.

Would you recommend switching to i2c directly and apply clock stretching or is there some new developments which would make the issues of this past topic obsolete? I would prefer UART to avoid i2c conflicts with other HATs if possible, but otherwise I will try to work this out.

Thanks for your time!

Robin_Koek
 
Posts: 8
Joined: Fri Apr 30, 2021 1:21 pm

Re: bno055 UART reading errors

by mikeysklar on Thu Jun 03, 2021 7:06 pm

@Robin_Koek,

We should continue trying to get the UART to work if you like. I'd need to see a photo of your wiring making sure you are in UART mode (PS1 --> VIN wiring). The UART code you have been trying to use and what I provided earlier in this thread was not correct. Take a look at the suggested CircuitPython for Linux suggested uart setup. It is not using the serial module.

Code: Select all | TOGGLE FULL SIZE
import board
import busio
import adafruit_bno055
uart = busio.UART(board.TX, board.RX)
sensor = adafruit_bno055.BNO055_UART(uart)


In term of using i2c:

There is clock stretching support for the modern Pi's by modifying adding the following parameter to /boot/config.txt. Now that is a really slow 10kHz versus the default i2c speed of 100kHz so I think it is best to get the UART as you plan on having multiple devices on your i2c bus.

Code: Select all | TOGGLE FULL SIZE
dtparam=i2c_arm_baudrate=10000


https://learn.adafruit.com/circuitpytho ... stretching

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: bno055 UART reading errors

by Robin_Koek on Tue Jun 08, 2021 6:33 am

Hi mikeysklar,

upon your feedback on trying to keeping going communicate over UART I have invested some time but it seems the situation has now degressed from once I made the initial post.

Once calling for the UART based on busio module as you advice (running the last snippet of code you posted) I get the following error:

Traceback (most recent call last):
File "/home/pi/Desktop/basicbnoread.py", line 14, in <module>
uart = busio.UART(board.TX, board.RX)
File "/home/pi/.local/lib/python3.7/site-packages/busio.py", line 442, in __init__
"busio.UART not supported on this platform. Please use pyserial instead."
RuntimeError: busio.UART not supported on this platform. Please use pyserial instead.
>>>


This implies needing to import serial. I have consulted and tried to resolve myself first- https://learn.adafruit.com/circuitpytho ... 2998808-15 following all possible steps and re-checking if I made any mistakes.

Once trying to run over serial module again based on the 'Please use pyserial instead.' log I now get a new error and I'm not able to run the initial script posted at start of the thread.

Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/serial/serialposix.py", line 322, in open
self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/serial0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/pi/Desktop/basicfirstscript.py", line 5, in <module>
uart = serial.Serial("/dev/serial0")
File "/home/pi/.local/lib/python3.7/site-packages/serial/serialutil.py", line 244, in __init__
self.open()
File "/home/pi/.local/lib/python3.7/site-packages/serial/serialposix.py", line 325, in open
raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/serial0: [Errno 2] No such file or directory: '/dev/serial0'
>>>

I have also consulted https://www.raspberrypi.org/documentati ... on/uart.md after and tried to call upon the uart ports with different addresses based on this document (e.g. '/dev/ttyAMA0') but they all result i the same error. This might have been caused I think by running the blinka python script for setup Circuitpython which seemed to have reset some permissions?

So back at ground zero for now I guess.. if you have any insights on the matter would be greatly appreciated.

I can confirm the wiring (PS1 --> VIN wiring) and uploaded some images of the wiring here: https://drive.google.com/drive/folders/ ... sp=sharing

I need to clarify one part as this might seem confusing as the reset port is still soldered on the bno, but it's actually cut and taped on the Pi side (Justboom) this is an old connection based on the previous bno055 library which was using the reset port upon initialization of the script. I have not desoldered it on the bno side in case I would need to return to the old library if all fails with the new one, but I can confirm it is not active and not attached to the Pi, as hopefully visible in the pictures (it used to be on GPIO16, this is why a short yellow taped cable is located there)

Thanks for your time again!
Robin

Robin_Koek
 
Posts: 8
Joined: Fri Apr 30, 2021 1:21 pm

Re: bno055 UART reading errors

by Robin_Koek on Tue Jun 15, 2021 7:02 am

Hi,

I'm aware that it might be busy, but it's been week and I'm rather stuck on the UART side of this and spend quite some time on documenting all steps, making pictures of the wiring etc. It would be really highly appreciated to get any input, especially since working with UART on RPI4 clearly has not been tested a lot with the latest BNO055 Library I think is beyond the scope of the tutorial as is.

Thanks kindly,
Robin

Robin_Koek
 
Posts: 8
Joined: Fri Apr 30, 2021 1:21 pm

Re: bno055 UART reading errors

by mikeysklar on Tue Jun 15, 2021 4:27 pm

Hi Robin,

I apologize for not following up sooner.

Let's get back to where you started with the pyserial for UART reading on the BNO055 with a forced 115k baud rate and error handling. If it still feels too laggy to use you will need to switch to clock stretched i2c.

Code: Select all | TOGGLE FULL SIZE
import time
import serial
import board
import busio
import adafruit_bno055


#i2c = busio.I2C(board.SCL, board.SDA)
#bno = adafruit_bno055.BNO055_I2C(i2c)


uart = serial.Serial("/dev/ttyUSB0", 115200, timeout=5,writeTimeout=5) #I just added the additional arguments 2-4 -> no changes
bno = adafruit_bno055.BNO055_UART(uart)
time.sleep(5)
heading,roll,pitch=bno.euler
print(bno)
print(uart)


while 1:
   
    try:
        for i in range(0,10000):
   
            heading,roll,pitch=bno.euler
            print(roll,pitch)
            time.sleep(0.001)
   
             
       
        break
    except Exception as e:
       print("Got error: {}".format(e))
       
       time.sleep(0.01)

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: bno055 UART reading errors

by Robin_Koek on Wed Jun 16, 2021 10:22 am

Hi mikeysklar,

Thanks for your feedback and the modified code.

This starts to look really promising! With the forced baud rate the sensor responsivity increased a lot. Also the timeout=5 creates a small buffer it seems which makes that the script does not fully interrupt upon a reading error, I still see it sometimes in the shell but then the script runs on. Unfortunately after some time (avg 10 minutes) it still stops running, now also with no more error log in the shell, it just quits reading and ends at >>> This is both with the sensor in motion and in stasis. Any ideas of last clues to look at? I feel we are really close and thanks for the effort.

Should timeout and writeTimeout always be equal? I experimented with increasing timeout window to give it more margin before drop-out, but does not seem to resolve and increase writeTimeout over 5 introduces too much lag.

Also here I read that it is possible to run at even higher baud rates than 115200, would it do any harm to increase beyond? I did a test at 1152000 and the responsiveness of the sensor is fantastic on this rate.

If it would be possible just to avoid the drop-outs/interruptions of the script after longer time everything is perfect (it has to run continously for 9 to 10 hours on a day without interruption), hopefully you have some clues where to look, thanks so much for the support!

Robin

Robin_Koek
 
Posts: 8
Joined: Fri Apr 30, 2021 1:21 pm

Re: bno055 UART reading errors

by mikeysklar on Wed Jun 16, 2021 10:38 am

Robin,

I think you are far enough along to experiment with different settings for timeout, writetimeout and baudrate to find a sweet spot. Even the time.sleep settings might need some tweaking to prevent the script from erroring out.

The timeout and writetimeout values do not need to be the same. You can even set writetimeout to '0'.

mikeysklar
 
Posts: 4920
Joined: Mon Aug 01, 2016 8:10 pm

Re: bno055 UART reading errors

by gammaburst on Wed Jun 16, 2021 8:26 pm

I don't know much about RPi, but those sleep() statements seem suspicious. I would try eliminating them. Too much sleep/delay would cause the communication buffer (from the BNO) to gradually fill-up (causing lag) and then overflow (causing error). If the data print statements aren't fast enough to display all the BNO's data, then discard some of the data points, rather than sleeping.

Just my two cents.

gammaburst
 
Posts: 579
Joined: Thu Dec 31, 2015 12:06 pm

Please be positive and constructive with your questions and comments.