将IMU传感器(加速度计,陀螺仪)读数及其时间戳导出到数据文件中

时间:2019-06-07 18:22:56

标签: python python-3.x numpy

我正在使用IMU传感器并获取3个读数(XYZ_acceleration,XYZ_angular velocity和XYZ_magnetometer),我希望将它们连同时间戳一起导出到单个数据文件中。我设法导出了3个没有时间戳的独立数据文件。

from altimu10v5.lsm6ds33 import LSM6DS33
from altimu10v5.lis3mdl import LIS3MDL
from altimu10v5.lps25h import LPS25H
from time import sleep
import numpy as np
import csv
import calendar
import time

lsm6ds33 = LSM6DS33()
lsm6ds33.enable()

lis3mdl = LIS3MDL()
lis3mdl.enable()

lps25h = LPS25H
lis3mdl.enable()

ts = calendar.timegm(time.gmtime())


while True:


    accel_raw=lsm6ds33.get_accelerometer_raw()
    accel_gforce=lsm6ds33.get_accelerometer_g_forces()
    accel_angle=lsm6ds33.get_accelerometer_angles()
    gyro_raw=lsm6ds33.get_gyroscope_raw()
    gyro_ang_vel=lsm6ds33.get_gyro_angular_velocity()
    magnet=lis3mdl.get_magnetometer_raw()


    aaa1=open('data_accele_f_force.dat','ab')
    np.savetxt(aaa1 ,np.expand_dims(accel_gforce, axis=0),  fmt='%4.2f %4.2f %4.2f') 

    aaa2=open('data_accele_raw.dat','ab')
    np.savetxt(aaa2 ,np.expand_dims(accel_raw, axis=0),  fmt='%4.2f %4.2f %4.2f') 

    aaa3=open('data_accele_angles.dat','ab')
    np.savetxt(aaa3 ,np.expand_dims(accel_angle, axis=0), fmt='%4.2f %4.2f') 

sleep(1)

1 个答案:

答案 0 :(得分:0)

我正在使用虚拟值作为传感器读数,但这在下面应该可行:

import calendar
import numpy as np
from time import sleep
import time

while True:
    # get time
    timestamp = ts = calendar.timegm(time.gmtime())
    # sensor readings
    timestamped_sensor_readings = np.ndarray((0,), np.float32)
    # get sensor placeholder values
    timestamped_sensor_readings = np.append(timestamped_sensor_readings, np.random.rand(3))
    timestamped_sensor_readings = np.append(timestamped_sensor_readings, np.random.rand(3))
    timestamped_sensor_readings = np.append(timestamped_sensor_readings, np.random.rand(2))

    # write format for readings
    write_fmt = " ".join("%4.2f" for _ in timestamped_sensor_readings)
    # append time
    timestamped_sensor_readings = np.append(timestamped_sensor_readings, float(timestamp))
    write_fmt += " %.0f"

    with open("mydatafile.txt", "ab") as f:
        np.savetxt(f, np.expand_dims(timestamped_sensor_readings, axis=0),  fmt=write_fmt)

    sleep(1)

输出填写如下:

$ tail -f mydatafile.txt 
0.72 0.90 0.77 0.37 0.51 0.46 0.41 0.76 1560186762
0.69 0.49 0.62 0.32 0.60 0.61 0.59 0.52 1560186763
0.17 0.42 0.97 0.07 0.83 0.67 0.48 0.43 1560186764
0.10 0.02 0.07 0.16 0.05 0.83 0.51 0.31 1560186765
0.64 0.78 0.29 0.96 0.04 0.19 0.11 0.43 1560186766
0.12 0.87 0.04 0.99 0.57 0.81 0.05 0.57 1560186767