I'm using SPI reading data from IMU LSM9DS1. I want to store the data to a file. I have tried to save as a txt file using with open as file
and .write
. the speed is 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()
I also tried to use pandas to save the data as a .csv file. the speed is slower than the first one.
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()
what can I do to improve the reading speed?
I update the code, outside the path.
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]))
this is the other way
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)
Thanks for Mark's answer. I did what he said, changed the code as below.
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')
I have calculated the space of time (first 600 data), the average is 0.000265, it's much faster than before, almost 10 times as before.