Hello, I am trying to improve the speed of Python reading MLX90393 sensor data. My sensor data is transmitted to the computer through nrf52832. When 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 Bluetooth, my highest sampling rate is about 66Hz, and the data reading rate is unstable, fluctuating between approximately 38Hz-66Hz per second.
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 MLX90393 through nrf52832. 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)
Can you tell us more on how things are connected ?
What is sent and how?
How do you read the data?
code helps…
As this is your first post, do yourself a favour and please read How to get the best out of this forum and post accordingly (including code tags and necessary documentation for your ask).
Hello Jackson!Thank you for your suggestion. I use Bluetooth to connect to the sensor with the SPI protocol and read the sensor using Python language. The sensor data is first transmitted to nrf52832, and then the data is transmitted to the computer for processing through BLE. The x, y, and z three-axis data and temperature data of MLX90393 are transmitted to the computer. I want to read these data at a high sample rate(about 100Hz).
Bluetooth (a radio connection) and SPI (a wired connection) have nothing to do with each other. If you want help, please take a few minutes to accurately describe your project.
BLE is not suited for that. It has inherent limitations for streaming data, as throttling occurs through the connection interval (the time between consecutive data exchanges. It can ranges from 7.5 ms to multiple seconds in BLE), and your BLE payloads are restricted to a maximum of 20 bytes per connection event… So it’s more suited for intermittent, low-energy data transmissions rather than continuous streaming applications with higher bandwidth requirements.
Thank you for your time. I am in the process of developing a low-power wearable device and am exploring options for high data rate and real-time transmission across multiple devices. While WIFI transmission seems like a viable choice, its higher power consumption is a concern. I would be immensely grateful for any suggestions or solutions you could offer to help address this issue.
Thank you for your input, jremington. I'm aware of the difficulties involved, but I'm committed to researching and experimenting with potential solutions. If you have any advice or could point me towards resources that might help in this endeavor, I would greatly appreciate it.
I can't think of a better solution, unless I decide to undertake the challenging task of customizing a set of protocols myself.
Without a full Requirement specification document it’s hard to provide guidance. Sometimes you need to think out of the box.
Also When it comes to optimizing your design, remember the old saying : « Price, Performance, Power - pick two »
ie you can’t get high performance, low power and low price at the same time.
Of course all these are relative but come a point where you need to make concessions.
May be you are at the point where you need to Define what matters the most to you and if you have explored all options then you need to find the right trade off
Thank you very much! I am currently planning to read data from four devices in real-time at a rate of 60-100Hz. I will further review some relevant BLE standards to decide how to proceed.
A BLE connection interval is the time between two data transfer events between the central and the peripheral device. The theoretical value ranges from 7.5 ms to 4 secs. so theoretically you get 1000/7.5 = ~133 Hz but whilst the peripheral may suggest the interval value, the master has the final say and may end up choosing values different than what you need (and a Mac or a PC or an iPhone could behave differently for example)
You then have the constraint of how many bytes you can pack in an event. (BLE 5 would offer the DLE feature)