Yu *_*ang 6 python raspberry-pi
我正在使用 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 = 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()
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能提高阅读速度?
我更新了路径之外的代码。
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]))
Run Code Online (Sandbox Code Playgroud)
这是另一种方式
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)
Run Code Online (Sandbox Code Playgroud)
感谢马克的回答。我按照他说的做了,将代码更改如下。
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')
Run Code Online (Sandbox Code Playgroud)
我计算了时间的空间(前600个数据),平均值是0.000265,比以前快了很多,几乎是以前的10倍。
正如我在评论中所说:“答案是巨大的不同!如果陀螺仪位于无人机上,并且您将数据发送到 PC 来控制方向,您需要获取最新读数以最小的延迟传输到 PC - 这不需要存储,并且 4 秒前的数据是无用的。如果您正在运行 4 小时的实验并稍后分析结果,您可能希望以最大速率读取陀螺仪,存储一切都在本地进行,并在最后进行传输——这需要更多的存储空间。”
\n\n存储大量样本的最快位置是 RAM 中的列表:
\n\nsamples=[]\nwhile flag:\n t = time.time()\n acc = dev.get_acc()\n dps = dev.get_gyro()\n # Append a tuple (containing time, acc and dps) onto sample list\n samples.append((t, acc, dps))\nRun Code Online (Sandbox Code Playgroud)\n\n基准
\n\n在我的桌面上运行 IPython,每秒可以存储 280 万个元组,每个元组包含时间和 2 个列表,每个列表包含 3 个元素:
\n\nIn [92]: %%timeit \n...: \n...: samples=[] \n...: for i in range(2800000): \n...: t = time.time() \n...: acc = [1,2,3] \n...: dps = [4,5,6] \n...: # Append a tuple (containing time, acc and dps) onto sample list \n...: samples.append((t, acc, dps))\n\n1.05 s \xc2\xb1 7.13 ms per loop (mean \xc2\xb1 std. dev. of 7 runs, 1 loop each)\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2262 次 |
| 最近记录: |