예제 #1
0
def test_readSingleBatchWithVaryingFifoSizes():
    fifoCounter = 0
    fifoReader = 0

    def provider(register, length=None):
        if register is mpu6050.MPU6050_RA_INT_STATUS:
            return 0x01
        elif register is mpu6050.MPU6050_RA_FIFO_COUNTH:
            nonlocal fifoCounter
            fifoCounter += 1
            to_bytes = (randint(1, 50) * 6).to_bytes(2, 'big')
            return to_bytes
        elif register is mpu6050.MPU6050_RA_FIFO_R_W:
            nonlocal fifoReader
            fifoReader += 1
            assert length is not None
            assert length in [6, 12, 18, 24, 30]
            return [0b0000, 0b0001, 0b0010, 0b0011, 0b0100, 0b0101] * (length // 6)

    io = mockIO(data_provider=provider)
    mpu = mpu6050(io, self_test=False)
    output = mpu.provide_data()
    assert output is not None
    assert len(output) == 125
    assert all(len(i) == len(output[0]) for i in output)
    # check all actual values are identical (as a short cut for these are the same values as returned by the fifo)
    assert all(x[1:] == output[0][1:] for x in output)
예제 #2
0
def test_whenSensorsAreToggled_fifoPacketSizeIsCorrect():
    mpu = mpu6050(mockIO(), self_test=False)
    # -gyro-temperature+acceleration
    assert mpu.get_packet_size() == 6
    assert mpu.fifo_sensor_mask == 0b00001000
    mpu.enable_gyro()
    # +gyro-temperature+acceleration
    assert mpu.get_packet_size() == 12
    assert mpu.fifo_sensor_mask == 0b01111000
    mpu.enable_temperature()
    # +gyro+temperature+acceleration
    assert mpu.get_packet_size() == 14
    assert mpu.fifo_sensor_mask == 0b11111000
    mpu.disable_accelerometer()
    # +gyro+temperature-acceleration
    assert mpu.get_packet_size() == 8
    assert mpu.fifo_sensor_mask == 0b11110000
    mpu.disable_gyro()
    # -gyro+temperature-acceleration
    assert mpu.get_packet_size() == 2
    assert mpu.fifo_sensor_mask == 0b10000000
    mpu.disable_temperature()
    # -gyro-temperature-acceleration
    assert mpu.get_packet_size() == 0
    assert mpu.fifo_sensor_mask == 0b00000000
    mpu.enable_gyro()
    # +gyro-temperature-acceleration
    assert mpu.get_packet_size() == 6
    assert mpu.fifo_sensor_mask == 0b01110000
    mpu.disable_gyro()
    mpu.enable_accelerometer()
    mpu.enable_temperature()
    # -gyro+temperature+acceleration
    assert mpu.get_packet_size() == 8
    assert mpu.fifo_sensor_mask == 0b10001000
예제 #3
0
def test_readSingleBatchWhenLastSizeHasSamplesRemaining():
    fifoCounter = 0
    fifoReader = 0

    def provider(register, length=None):
        if register is mpu6050.MPU6050_RA_INT_STATUS:
            return 0x01
        elif register is mpu6050.MPU6050_RA_FIFO_COUNTH:
            nonlocal fifoCounter
            fifoCounter += 1
            return [0b00, 0b00001100]
        elif register is mpu6050.MPU6050_RA_FIFO_R_W:
            nonlocal fifoReader
            fifoReader += 1
            assert length is not None
            assert length == 6 or length == 12
            return [0b0000, 0b0001, 0b0010, 0b0011, 0b0100, 0b0101] * (length // 6)

    io = mockIO(data_provider=provider)
    mpu = mpu6050(io, self_test=False)
    output = mpu.provide_data()
    assert output is not None
    assert len(output) == 125
    assert all(len(i) == len(output[0]) for i in output)
    # check all actual values are identical (as a short cut for these are the same values as returned by the fifo)
    assert all(x[1:] == output[0][1:] for x in output)
    assert fifoCounter == 63
    assert fifoReader == 63
예제 #4
0
def test_readSingleBatchWithFourteenBytesAvailableOnEachReadWhenAllSensorsAreAvailable():
    fifoCounter = 0
    fifoReader = 0

    def provider(register, length=None):
        if register is mpu6050.MPU6050_RA_INT_STATUS:
            return 0x01
        elif register is mpu6050.MPU6050_RA_FIFO_COUNTH:
            nonlocal fifoCounter
            fifoCounter += 1
            return [0b00, 0b00001110]
        elif register is mpu6050.MPU6050_RA_FIFO_R_W:
            nonlocal fifoReader
            fifoReader += 1
            assert length is not None
            assert length == 14
            return [0b0000, 0b0001, 0b0010, 0b0011, 0b0100, 0b0101, 0b0111,
                    0b1000, 0b1001, 0b1010, 0b1011, 0b1100, 0b1101, 0b1110]

    io = mockIO(data_provider=provider)
    mpu = mpu6050(io, self_test=False)
    mpu.enable_gyro()
    mpu.enable_temperature()
    output = mpu.provide_data()
    assert output is not None
    assert len(output) == 125
    assert all(len(i) == len(output[0]) for i in output)
    # check all actual values are identical (as a short cut for these are the same values as returned by the fifo)
    assert all(x[1:] == output[0][1:] for x in output)
    assert fifoCounter == 125
    assert fifoReader == 125
예제 #5
0
def test_readSingleBatchInOneTripToFifo():
    # provide a single 6*125 = 750 byte fifo
    fifoCounter = 0
    fifoReader = 0

    def provider(register, length=None):
        if register is mpu6050.MPU6050_RA_INT_STATUS:
            return 0x01
        elif register is mpu6050.MPU6050_RA_FIFO_COUNTH:
            nonlocal fifoCounter
            fifoCounter += 1
            return [0b0010, 0b11101110]
        elif register is mpu6050.MPU6050_RA_FIFO_R_W:
            nonlocal fifoReader
            fifoReader += 1
            assert length is not None
            assert 30 == length
            return [0b0000, 0b0001, 0b0010, 0b0011, 0b0100, 0b0101] * 5

    io = mockIO(data_provider=provider)
    mpu = mpu6050(io, self_test=False)
    output = mpu.provide_data()
    assert output is not None
    assert len(output) == 125
    assert all(len(i) == len(output[0]) for i in output)
    # check all actual values are identical (as a short cut for these are the same values as returned by the fifo)
    assert all(x[1:] == output[0][1:] for x in output)
    assert fifoCounter == 1
    assert fifoReader == 25
예제 #6
0
def test_readSingleBatchWithSixBytesAvailableOnEachRead():
    # provide a constant stream of 6 byte samples
    fifoCounter = 0
    fifoReader = 0
    fifoValues = [0b0000, 0b0001, 0b0010, 0b0011, 0b0100, 0b0101]

    def provider(register, length=None):
        if register is mpu6050.MPU6050_RA_INT_STATUS:
            return 0x01
        elif register is mpu6050.MPU6050_RA_FIFO_COUNTH:
            nonlocal fifoCounter
            fifoCounter += 1
            return [0b0000, 0b0110]
        elif register is mpu6050.MPU6050_RA_FIFO_R_W:
            nonlocal fifoReader
            fifoReader += 1
            assert length is not None
            assert 6 == length
            return fifoValues

    io = mockIO(data_provider=provider)
    mpu = mpu6050(io, self_test=False)
    output = mpu.provide_data()
    assert output is not None
    assert len(output) == 125
    assert all(len(i) == len(output[0]) for i in output)
    # check all actual values are identical (as a short cut for these are the same values as returned by the fifo)
    assert all(x[1:] == output[0][1:] for x in output)
    assert fifoCounter == 125
    assert fifoReader == 125
예제 #7
0
def test_sampleRateMaxesAt1000():
    io = mockIO()
    mpu = mpu6050(io, self_test=False)
    io.values_written.clear()
    mpu.set_sample_rate(2000)
    assert 1000 == mpu.fs
    # check the correct SMPLRT_DIV is written to the device
    assert len(io.values_written) == 1
    assert io.values_written[0][2] == 7
예제 #8
0
def test_canToggleAccelerometer():
    mpu = mpu6050(mockIO(), self_test=False)
    assert mpu.is_accelerometer_enabled()
    mpu.disable_accelerometer()
    assert not mpu.is_accelerometer_enabled()
    mpu.enable_accelerometer()
    assert mpu.is_accelerometer_enabled()
    mpu.disable_accelerometer()
    assert not mpu.is_accelerometer_enabled()
예제 #9
0
def test_canToggleTemperature():
    mpu = mpu6050(mockIO(), self_test=False)
    mpu.enable_temperature()
    assert mpu.is_temperature_enabled()
    mpu.disable_temperature()
    assert not mpu.is_temperature_enabled()
    mpu.enable_temperature()
    assert mpu.is_temperature_enabled()
    mpu.disable_temperature()
    assert not mpu.is_temperature_enabled()
예제 #10
0
def test_canToggleGyro():
    mpu = mpu6050(mockIO(), self_test=False)
    mpu.enable_gyro()
    assert mpu.is_gyro_enabled()
    mpu.disable_gyro()
    assert not mpu.is_gyro_enabled()
    mpu.enable_gyro()
    assert mpu.is_gyro_enabled()
    mpu.disable_gyro()
    assert not mpu.is_gyro_enabled()
예제 #11
0
def test_default_state():
    def provider(register, length=None):
        if register is mpu6050.MPU6050_RA_INT_STATUS:
            return 0x01

    mpu = mpu6050(mockIO(data_provider=provider), self_test=False)
    assert mpu.is_accelerometer_enabled()
    assert not mpu.is_temperature_enabled()
    assert not mpu.is_gyro_enabled()
    assert mpu.fs == 500
    assert mpu.samples_per_batch == 125
예제 #12
0
def test_getFifoCount():
    values = (
        ((0b00000000, 0b00000000), 0),
        ((0b00000001, 0b00000000), 256),
        ((0b00000000, 0b00000001), 1),
        ((0b00000001, 0b00000001), 257),
        ((0b10000000, 0b10000000), 32896),
        ((0b11111111, 0b10000000), 65408),
    )
    io = mockIO()
    mpu = mpu6050(io, self_test=False)
    for testcase in values:
        io.vals_to_read.put(testcase[0])
        assert mpu.get_fifo_count() == testcase[1]
예제 #13
0
def create_device(device_cfg):
    """
    Creates a measurement device from the input configuration.
    :param: device_cfg: the device cfg.
    :return: the constructed device.
    """
    io_cfg = device_cfg['io']
    device_type = device_cfg['type']
    if device_type == 'mpu6050':
        fs = device_cfg.get('fs')
        name = device_cfg.get('name', 'mpu6050')
        if io_cfg['type'] == 'mock':
            provider = io_cfg.get('provider')
            if provider is not None:
                if provider == 'white noise':
                    data_provider = WhiteNoiseProvider()
                elif provider == 'dbl':
                    data_provider = WavProvider(io_cfg.get('file'))
                else:
                    raise ValueError(
                        provider + " is not a supported mock io data provider")
            else:
                raise ValueError(
                    'No provider supplied for a mock io data provider')
            logger.warning(
                f"Loading {provider} mock data provider for mpu6050")
            io = mockIO(data_provider=data_provider.provide)
        elif io_cfg['type'] == 'smbus':
            bus_id = io_cfg['busId']
            logger.warning("Loading smbus %d", bus_id)
            io = smbusIO(bus_id)
        else:
            raise ValueError(io_cfg['type'] +
                             " is not a supported io provider")
        logger.warning(f"Loading mpu6050 {name}/{fs}")
        samples_per_batch = int(device_cfg['samplesPerBatch']
                                ) if 'samplesPerBatch' in device_cfg else None
        mpu = mpu6050(io,
                      name=name,
                      fs=fs,
                      data_handler=AsyncHandler(),
                      self_test=True,
                      samples_per_batch=samples_per_batch)
        worker = threading.Thread(target=mpu.run, daemon=True)
        worker.start()
        return mpu
    else:
        raise ValueError(device_type + " is not a supported device")