0

BNO055 calibrations - which code?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

BNO055 calibrations - which code?

by nataelj on Thu Jan 17, 2019 2:05 pm

I'm trying to persist the calibration on a BNO055 bought from you between sessions. The documentation for installation recommends the circuitpython drivers for this device which appears to use the code at https://github.com/adafruit/Adafruit_Ci ... _bno055.py. This contains functions to read the calibration status but not to read or set the calibration values.

By comparison, https://github.com/adafruit/Adafruit_Py ... /BNO055.py seems to have the full functionality built in of reading and setting calibration values but is not the recommended driver in the documentation.

Does anyone know the difference between these two and why the one that seems to have more constrained abilities is the recommended one? For my calibration purposes should I just switch to the other driver?

nataelj
 
Posts: 11
Joined: Tue Jan 08, 2019 6:45 am

Re: BNO055 calibrations - which code?

by adafruit_support_carter on Thu Jan 17, 2019 6:43 pm

That other driver (2nd link) will eventually be deprecated for the newer one (1st link). I'm not sure why it's not there. I have opened an issue:
https://github.com/adafruit/Adafruit_Ci ... /issues/21

adafruit_support_carter
 
Posts: 10411
Joined: Tue Nov 29, 2016 2:45 pm

Re: BNO055 calibrations - which code?

by nataelj on Fri Jan 18, 2019 11:21 pm

FYI, I needed this functionality so I went ahead and ported pieces of the old code to accomplish this in the short term. Code is here in case it's useful to anyone, and if anyone finds bugs please let me know. Of note, a few places have hardcoded variables or refer to parent class variables; those would need to get updated for any implementation that doesn't have this as a child class.

Code: Select all | TOGGLE FULL SIZE
import adafruit_bno055
"""This is a class extender of the bno055 adafruit class. Most of the functionality remains in the parent class,
but functionality here has been ported from an old driver in order to set and get calibration values from the device."""

class BNO055_extended(adafruit_bno055.BNO055):
    def get_calibration(self):
        """Return the sensor's calibration data and return it as an array of
        22 bytes. Can be saved and then reloaded with the set_calibration function
        to quickly calibrate from a previously calculated set of calibration data.
        """
        # Switch to configuration mode, as mentioned in section 3.10.4 of datasheet.
        self.mode = adafruit_bno055.CONFIG_MODE
        # Read the 22 bytes of calibration data and convert it to a list (from
        # a bytearray) so it's more easily serialized should the caller want to
        # store it.
        cal_data = list(self._read_registers(0X55, 22))
        # Go back to normal operation mode.
        self.mode = adafruit_bno055.NDOF_MODE
        return cal_data

    def set_calibration(self, data):
        """Set the sensor's calibration data using a list of 22 bytes that
        represent the sensor offsets and calibration data.  This data should be
        a value that was previously retrieved with get_calibration (and then
        perhaps persisted to disk or other location until needed again).
        """       
        # Check that 22 bytes were passed in with calibration data.
        if data is None or len(data) != 22:
            raise ValueError('Expected a list of 22 bytes for calibration data.')
        # Switch to configuration mode, as mentioned in section 3.10.4 of datasheet.
        self.mode = adafruit_bno055.CONFIG_MODE
        # Set the 22 bytes of calibration data.
        self._write_register_data(0X55, data)
        # Go back to normal operation mode.
        self.mode = adafruit_bno055.NDOF_MODE

    def _write_register_data(self, register, data):
        write_buffer = bytearray(1)
        write_buffer[0] = register & 0xFF
        write_buffer[1:len(data)+1]=data
        with self.i2c_device as i2c:
            i2c.write(write_buffer, start=0, end=len(write_buffer))

    def _read_registers(self, register, length):
        read_buffer = bytearray(23)
        read_buffer[0] = register & 0xFF
        with self.i2c_device as i2c:
            i2c.write(read_buffer, start=0, end=1)
            i2c.readinto(read_buffer, start=0, end=length)
            return read_buffer[0:length]

if __name__ == "__main__":
    import time
    import board
    import busio
    # if run directly we'll just create an instance of the class and output the current readings
    i2c = busio.I2C(board.SCL, board.SDA)
    sensor = BNO055_extended(i2c)

    print("Calibration Status",sensor.calibrated)
    print("Calibration:",sensor.get_calibration())

    sensor.set_calibration([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252, 255, 251, 255, 1, 0, 232, 3, 0, 0])
    print("Calibration Status",sensor.calibrated)
    print("Calibration:",sensor.get_calibration())

    for x in range(0,20):
        print('Temperature: {} degrees C'.format(sensor.temperature))
        print('Accelerometer (m/s^2): {}'.format(sensor.accelerometer))
        print('Magnetometer (microteslas): {}'.format(sensor.magnetometer))
        print('Gyroscope (deg/sec): {}'.format(sensor.gyroscope))
        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)

    print("Calibration Status",sensor.calibrated)
    print("Calibration:",sensor.get_calibration())

nataelj
 
Posts: 11
Joined: Tue Jan 08, 2019 6:45 am

Please be positive and constructive with your questions and comments.