小编Yu *_*ang的帖子

在 Raspberry Pi 上使用 Python 存储传感器数据的最有效方法

我正在使用 SPI 从 IMU LSM9DS1 读取数据。我想将数据存储到文件中。我尝试使用with open as file和保存为 txt 文件.write。速度为0.002秒。

while flag:
    file_path_g = '/home/pi/Desktop/LSM9DS1/gyro.txt'
    with open(file_path_g, 'a') as out_file_g:
        dps = dev.get_gyro()
        out_file_g.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
        out_file_g.write(" {0:0.3f}, {1:0.3f}, {2:0.3f}\n".format(dps[0], dps[1], dps[2]))

    file_path_a = '/home/pi/Desktop/LSM9DS1/accel.txt'
    with open(file_path_a, 'a') as out_file_a:
        acc = dev.get_acc()
        out_file_a.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
        out_file_g.write(" {0:0.3f}, {1:0.3f}, {2:0.3f}\n".format(acc[0], acc[1], acc[2]))
    # time.sleep(0.2)

print("interrupt occured")

dev.close()
Run Code Online (Sandbox Code Playgroud)

我还尝试使用 pandas 将数据保存为 .csv 文件。速度比第一个慢。

while flag:
    t = time.time()
    acc = dev.get_acc()
    dps = dev.get_gyro()
    ax = acc[0]
    ay …
Run Code Online (Sandbox Code Playgroud)

python raspberry-pi

6
推荐指数
1
解决办法
2262
查看次数

标签 统计

python ×1

raspberry-pi ×1