import board import busio import adafruit_bno055 import serial # Use these lines for I2C # i2c = busio.I2C(board.SCL, board.SDA) # sensor = adafruit_bno055.BNO055_I2C(i2c) # Use these lines for UART except Linux devices # uart = busio.UART(board.TX, board.RX) # sensor = adafruit_bno055.BNO055_UART(uart) # Use these lines for UART on Linux (e.g., for Raspberry Pi) uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1000) sensor = adafruit_bno055.BNO055_UART(uart) last_val = 0xFFFF sensor.mode = 0x08 AXIS_REMAP_X = 0x00 AXIS_REMAP_Y = 0x01 AXIS_REMAP_Z = 0x02 AXIS_REMAP_POSITIVE = 0x00 AXIS_REMAP_NEGATIVE = 0x01 remap = (0x01, 0x00, 0x02, 0x00, 0x01, 0x00) sensor.axis_remap = remap def temperature(): global last_val # pylint: disable=global-statement
def __init__(self): self.uart = serial.Serial("/dev/serial0") self.sensor = adafruit_bno055.BNO055_UART(self.uart) self.theta = 0
def activate(self, quiet=False): self.uart = serial.Serial(self.port) self.dev = None sys.stdout.write('\tEstablishing connection to BNO055') t0 = time.time() while self.dev is None: try: self.dev = adafruit_bno055.BNO055_UART(self.uart) except: time.sleep(0.1) sys.stdout.write(".") sys.stdout.flush() if time.time() - t0 > 10: raise pyLabDataLoggerIOError( "Timeout attempting to talk to BNO055 - Try power cycling") sys.stdout.write('\n') sys.stdout.flush() self.name = "BNO055 9-axis sensor" # These are constants that we don't expect to change during a run. Record at startup only. nonvars = ['axis_remap','accel_bandwidth','accel_mode','accel_range','gyro_bandwidth','gyro_mode','gyro_range',\ 'magnet_mode','magnet_operation_mode','magnet_rate','mode','offsets_accelerometer','offsets_gyroscope',\ 'offsets_magnetometer','radius_accelerometer','radius_magnetometer','calibrated', 'use_external_crystal',\ 'external_crystal', 'calibration_status'] self.config['channel_names'] = [ x for x in dir(self.dev) if ((x[0] != '_') and (not x in nonvars)) ] self.params['raw_units'] = [''] * len(self.config['channel_names']) self.config['eng_units'] = [''] * len(self.config['channel_names']) self.config['scale'] = [1.] * len(self.config['channel_names']) self.config['offset'] = [0.] * len(self.config['channel_names']) self.params['n_channels'] = len(self.config['channel_names']) for i in range(self.params['n_channels']): c = self.config['channel_names'][i] if 'temperature' in c: self.params['raw_units'][i] = 'C' if 'acceleration' in c: self.params['raw_units'][i] = 'm/s^2' if 'magne' in c: self.params['raw_units'][i] = 'microtesla' if 'gyro' in c: self.params['raw_units'][i] = 'deg/s' if 'gravity' in c: self.params['raw_units'][i] = 'm/s^2' self.config['eng_units'][i] = self.params['raw_units'][i] for v in nonvars: vv = np.nan t0 = time.time() while (np.any(np.isnan(vv)) and ((time.time() - t0) <= 3)): try: vv = getattr(self.dev, v) except: time.sleep(.025) if not self.quiet: print("\t%s = %s" % (v, vv)) self.params[v] = vv self.driverConnected = True # Make first query to get units, description, etc. self.query(reset=True) if not quiet: self.pprint() return