我正在使用 SPI 从 IMU LSM9DS1 读取数据。我想将数据存储到文件中。我尝试使用 with open as file
和 .write
保存为 txt 文件。速度为0.002s。
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()
我还尝试使用 pandas 将数据保存为 .csv 文件。速度比第一个慢。
while flag:
t = time.time()
acc = dev.get_acc()
dps = dev.get_gyro()
ax = acc[0]
ay = acc[1]
az = acc[2]
gx = dps[0]
gy = dps[1]
gz = dps[2]
result = pd.DataFrame({'time':t, 'ax':ax,'ay':ay,'az':az,'gx':gx,'gy':gy,'gz':gz},index=[0])
result.to_csv('/home/pi/Desktop/LSM9DS1/result.csv', mode='a', float_format='%.6f',
header=False, index=0)
dev.close()
如何提高阅读速度?
我更新了路径之外的代码。
file_path = '/home/pi/Desktop/LSM9DS1/result.txt'
while flag:
with open(file_path, 'a') as out_file:
acc = dev.get_acc()
dps = dev.get_gyro()
out_file.write(datetime.datetime.now().strftime('%S.%f'))
out_file.write(" {0:0.3f}, {1:0.3f}, {2:0.3f}".format(acc[0], acc[1], acc[2]))
out_file.write(" {0:0.3f}, {1:0.3f}, {2:0.3f}\n".format(dps[0], dps[1], dps[2]))
这是另一种方式
while flag:
t = time.time()
acc = dev.get_acc()
dps = dev.get_gyro()
arr = [t, acc[0], acc[1], acc[2], dps[0], dps[1],dps[2]],
np_data = np.array(arr)
result = pd.DataFrame(np_data,index=[0])
result.to_csv('/home/pi/Desktop/LSM9DS1/result.csv', mode='a', float_format='%.6f', header=False, index=0)
感谢马克的回答。我按照他说的做了,更改了代码如下。
samples=[]
for i in range(100000):
t = time.time()
acc = dev.get_acc()
dps = dev.get_gyro()
# Append a tuple (containing time, acc and dps) onto sample list
samples.append((t, acc, dps))
name = ['t','acc','dps']
f = pd.DataFrame(columns=name,data=samples)
f.to_csv('/home/pi/Desktop/LSM9DS1/result.csv', mode='a', float_format='%.6f', header=False, index=0)
print('done')
我计算了时间空间(前600条数据),平均值是0.000265,比以前快了很多,几乎是以前的10倍。
最佳答案
正如我在评论中所说:“根据您想要做什么,答案大大不同!如果陀螺仪位于无人机上并且您将数据发送到PC来控制方向,你需要以最小的延迟将最新的读数传到PC上——这不需要存储,4秒前的数据是没有用的。如果你正在运行一个4小时的实验并稍后分析结果,您可能希望以最大速率读取陀螺仪,将其全部存储在本地并在最后传输 - 这需要更多的存储空间。”
存储大量样本的最快位置是 RAM 中的列表:
samples=[]
while flag:
t = time.time()
acc = dev.get_acc()
dps = dev.get_gyro()
# Append a tuple (containing time, acc and dps) onto sample list
samples.append((t, acc, dps))
<小时/>
基准
在我的桌面上的 IPython 中运行,每秒可以存储 280 万个元组,每个元组包含时间和 2 个列表,每个列表包含 3 个元素:
In [92]: %%timeit
...:
...: samples=[]
...: for i in range(2800000):
...: t = time.time()
...: acc = [1,2,3]
...: dps = [4,5,6]
...: # Append a tuple (containing time, acc and dps) onto sample list
...: samples.append((t, acc, dps))
1.05 s ± 7.13 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
关于python - 在 Raspberry Pi 上使用 Python 存储传感器数据的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60338206/