Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How to increase the speed of Python reading device data over BLE? [closed]

$
0
0

I am trying to improve the speed of Python reading device data over BLE My device contains 10 MLX90393 magnetometers and a nRF52832. The nRF52832 communicates with the magnetometers via SPI protocol. Then, my computer read the data through BLE.

For MLX90393, I set FILTER to 1 and OSR to 3, according to the data table, the safe maximum sampling rate is approximately 133Hz.However, when I use Python to read sensor data through BLE, my highest data reading rate is about 66 Hz, and the data reading rate is unstable.

In addition, if I use wire to read the sensor, the sampling rate in the Serial Monitor will reach around 101-105Hz.

I want to know how to improve the speed of Python reading my device data through BLE. Can someone help me?

Thank you!

def notification_handler(sender, data):    global pSensor    global worklist    global reading    global reading_count    global reading_index    num = int(pSensor.size/3)    all_data = []    sensors = np.zeros((num, 3))    current = [datetime.datetime.now()]    calibration = np.load('test.npz')    offset = calibration['offset'].reshape(-1)    scale = calibration['scale'].reshape(-1)    for i in range(num):        sensors[i, 0] = struct.unpack('f', data[12 * i: 12 * i + 4])[0]        sensors[i, 1] = struct.unpack('f', data[12 * i + 4: 12 * i + 8])[0]        sensors[i, 2] = struct.unpack('f', data[12 * i + 8: 12 * i + 12])[0]        current.append("("+str(sensors[i, 0]) +", " + str(sensors[i, 1]) +", " + str(sensors[i, 2])+")")    sensors = sensors.reshape(-1)    sensors = (sensors - offset) / scale * np.mean(scale)    # if len(all_data) > 3:    #     sensors = (sensors + all_data[-1] + all_data[-2]) / 3    all_data.append(sensors)    worklist.put(sensors)    # print("############")    reading.append(current)    reading_count += 1    if reading_count == outNum:        reading_index += 1        test = pd.DataFrame(columns=name_reading, data=reading)        test.to_csv("readings.csv".format(date_order, order, reading_index))        reading_count = 0        reading = []async def run_ble(address, loop):    global stopFlag    async with BleakClient(address, loop=loop) as client:        # wait for BLE client to be connected        x = await client.is_connected()        print("Connected: {0}".format(x))        print("Press Enter to quit...")        # wait for data to be sent from client        await client.start_notify(UART_TX_UUID, notification_handler)        while stopFlag:            await asyncio.sleep(0.01)            # data = await client.read_gatt_char(UART_TX_UUID)        await client.stop_notify(UART_TX_UUID)

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>