是否可以使用Python提高覆盆子Pi上ADXL345的采样率?

2024-06-02 05:32:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有2个ADXL345加速计的Raspberry Pi,我想最大化它们的数据采样率。当我在互联网上搜索时,我发现有人在Raspberry Pi论坛(https://www.raspberrypi.org/forums/viewtopic.php?t=254552)上展示了这个代码,在这个例子中,他使用了两个加速计:

import time
import Adafruit_ADXL345


accel1 = Adafruit_ADXL345.ADXL345()
accel2 = Adafruit_ADXL345.ADXL345(address=0x1d, busnum=1)


print('Printing X, Y, Z axis values, press Ctrl-C to quit...')
cordinates = []

import time
start_time = time.time()
NUM_OF_SEC_TO_RUN = 10 

while time.time()<=start_time+NUM_OF_SEC_TO_RUN:

    x1, y1, z1 = accel1.read()
    x2, y2, z2 = accel2.read()
    cordinates.append([x1,y1,z1,x2,y2,z2,time.time()])

import csv
with open('02.txt', 'a') as csvFile:
    writer = csv.writer(csvFile)
    writer.writerows(cordinates)

在后来的帖子中,他说这个部分

x1, y1, z1 = accel1.read()
x2, y2, z2 = accel2.read()
cordinates.append([x1,y1,z1,x2,y2,z2,time.time()])

是加速度计进行下采样的最可能原因,从而降低读取速率并延迟数据采集

他说,

The average time for execution of the above 3 lines of code is 0.002121 seconds. Does this mean than the reading capability is limited because of i2c and Raspberry pi, not the sensor? Or is it because of my code?

我也想知道同样的问题,但我将以不同的方式提出。是否有一种方法可以缩短上面的段,从而可能减少延迟并提高数据采样率?也就是说,有没有办法把它变成一条线? 据我所知,这家伙的线索没有得到回应,也没有任何结论性的线索对我有所帮助

如果没有办法回答这个问题,那么我提出这个问题:有没有办法加快Raspberry Pi 3 B+上I2C的数据采样速度,或者有没有可能复制这个代码用于SPI通信?我知道SPI通信比I2C快,但我不知道如何在Raspberry Pi中的Python上实现它。如果这篇文章太长,我道歉


Tags: of数据importreadtimepiraspberryx1