示例#1
0
    def spi(self):
        spi = SPI(1, 0)
        spi.mode = self.mode
        spi.cshigh = False
        spi.msh = 1000000
        spi.bpw = 8
        spi.lsbfirst = False
        spi.open(1, 0)

        # set cycle counts
        spi.writebytes([0x04, 0x03, 0x20, 0x03, 0x20, 0x03,
                        0x20])  # 800 cycle counts
        time.sleep(.01)
        resolution = 3.3  # nT/LSB; corresponding to 800 cycle counts
        # set sampling rate, in hertz
        rate = 32
        # display device settings
        cycleX = spi.xfer2([0x84, 0x00, 0x00])
        cycleY = spi.xfer2([0x86, 0x00, 0x00])
        cycleZ = spi.xfer2([0x88, 0x00, 0x00])
        sensor_settings1 = 'Cycle count [x, y, z]: [' + str(
            int(hex(cycleX[1]) + hex(cycleX[2])[2:], 16)) + ', ' + str(
                int(hex(cycleY[1]) + hex(cycleY[2])[2:], 16)) + ', ' + str(
                    int(hex(cycleZ[1]) + hex(cycleZ[2])[2:], 16)) + ']'
        sensor_settings2 = 'Measurement rate: ' + str(rate) + ' Hz'
        print(sensor_settings1)
        print(sensor_settings2)

        # list of timestamps for each measurement read
        timestamp = []
        # list to store raw data
        raw = []
        # set the total measuring time, in seconds
        run_time = 30
        print('Total measuring time: ' + str(run_time) + ' seconds')
        t_end = time.time() + run_time

        # main loop
        trigger = time.time()
        while time.time() < t_end:
            timestamp.append(time.time())
            target = (len(raw) + 1) * (1 / rate) + trigger
            # initiate single measurement
            spi.writebytes([0x00, 0x70])
            condition = True
            # loop to keep checking until measurement is completed
            while condition:
                # if DRDY is high (measurement is completed)
                if format(spi.xfer2([0xB4, 0x00])[1], '#010b')[2] == '1':
                    # read measurement results
                    raw.append(
                        spi.xfer2([
                            0xA4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00
                        ]))
                    condition = False
            # if there is time remaining before next measurement
            if time.time() < target:
                # wait until appropriate time
                time.sleep(target - time.time())
            else:
                print('Timing error.')
                return

        spi.close()
        magX = []
        magY = []
        magZ = []
        magn = []
        for i in range(len(raw)):
            results = []
            for j in range(0, 9, 3):
                data = float(
                    self.recast24to32(raw[i][j + 1], raw[i][j + 2],
                                      raw[i][j + 3])) * resolution
                status = float
                results.append(data)
            magX.append(results[0])
            magY.append(results[1])
            magZ.append(results[2])
            magn.append(np.sqrt(results[0]**2 + results[1]**2 + results[2]**2))
        start_time = timestamp[0]
        time_diff = [0]
        for k in range(len(timestamp)):
            timestamp[k] = timestamp[k] - start_time
            if k > 0:
                time_diff.append(timestamp[k] - timestamp[k - 1])
        mag_data = np.transpose(
            np.array([magX, magY, magZ, magn, timestamp, time_diff]))
        np.savetxt('mag_data_short_32Hz.txt',
                   mag_data,
                   header=sensor_settings1 + ' ; ' + sensor_settings2)
        print('Successful measurement.')
        return
    def spi(self):
        spi = SPI(1, 0)
        spi.mode = self.mode
        spi.cshigh = False
        spi.msh = 1000000
        spi.bpw = 8
        spi.lsbfirst = False
        spi.open(1, 0)

        # set cycle counts
        spi.writebytes([0x04, 0x03, 0xE8, 0x03, 0xE8, 0x03,
                        0xE8])  # 1000 cycle counts
        time.sleep(.01)
        # set continuous measurement mode
        spi.writebytes([0x01, 0x79])
        time.sleep(.01)
        # set measurement rate
        spi.writebytes([0x0B, 0x95])
        time.sleep(.01)
        resolution = 2.7  # nT/LSB; corresponding to 800 cycle counts
        # display device settings
        cycleX = spi.xfer2([0x84, 0x00, 0x00])
        cycleY = spi.xfer2([0x86, 0x00, 0x00])
        cycleZ = spi.xfer2([0x88, 0x00, 0x00])
        sensor_settings1 = 'Cycle count [x, y, z]: [' + str(
            int(hex(cycleX[1]) + hex(cycleX[2])[2:], 16)) + ', ' + str(
                int(hex(cycleY[1]) + hex(cycleY[2])[2:], 16)) + ', ' + str(
                    int(hex(cycleZ[1]) + hex(cycleZ[2])[2:], 16)) + ']'
        sensor_settings2 = 'Measurement rate: ' + hex(
            spi.xfer2([0x8B, 0x00])[1])
        print(sensor_settings1)
        print(sensor_settings2)

        # list of timestamps for each measurement read
        timestamp = []
        # list to check for rate errors: use this list to display an error if there are consecutive measurements where DRDY (status) is high.
        error_check = []
        # list to store the time values corresponding to each error
        error_log = []
        # list to store export data
        mag_data = []
        # set the total measuring time, in seconds
        run_time = 10
        print('Total measuring time: ' + str(run_time) + ' seconds')
        t_end = time.time() + run_time

        # Main loop
        while time.time() < t_end:
            # if the DRDY (status) pin is high
            if format(spi.xfer2([0xB4, 0x00])[1], '#010b')[2] == '1':
                # append appropriate values to lists
                timestamp.append(time.time())
                error_check.append(1)
                # read measurement results
                raw = spi.xfer2([
                    0xA4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                ])
                results = []
                for i in range(0, 9, 3):
                    data = float(
                        self.recast24to32(raw[i + 1], raw[i + 2],
                                          raw[i + 3])) * resolution
                    status = float
                    results.append(data)
                magXYZ = results
                magn = np.sqrt(magXYZ[0]**2 + magXYZ[1]**2 + magXYZ[2]**2)
                # if mag_data already contains some data
                if len(mag_data) > 0:
                    # if DRDY was high two times in a row
                    if error_check[-2] == 1:
                        # store and display the error
                        error_log.append(timestamp[-1] - timestamp[0])
                        print('Error at t = ' + str(error_log[-1]) +
                              ': Consecutive iterations with DRDY high.')
                    # append results to mag_data
                    mag_data = np.vstack(
                        (mag_data,
                         np.array([
                             magXYZ[0], magXYZ[1], magXYZ[2], magn,
                             timestamp[-1] - timestamp[0],
                             timestamp[-1] - timestamp[-2]
                         ])))
                # if mag_data contains nothing
                else:
                    # create first line in mag_data
                    mag_data = np.array(
                        [magXYZ[0], magXYZ[1], magXYZ[2], magn, 0, 0])
            # if the DRDY pin was NOT high
            else:
                # store appropriate value in error_check
                error_check.append(0)

        spi.close()

        # if mag_data is empty, display error
        if len(mag_data) == 0:
            print('Error: DRDY never went high.')
            return

        # save data as txt file
        np.savetxt('mag_data_short.txt',
                   mag_data,
                   header=sensor_settings1 + ' ; ' + sensor_settings2,
                   comments='')
        # if there was a rate error -> save an additional txt file containing the corresponding time values
        if len(error_log) > 0:
            error_log = np.array(error_log)
            np.savetxt(
                'error_log_short.txt',
                error_log,
                header=
                'Time values for which DRDY was high for consecutive measurements'
            )
            print('Successful measurement, with DRDY errors.')
            return
        print('Successful measurement.')
        return