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

2024-06-01 06:17:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个IMU传感器,获取3个读数(XYZï加速度、XYZï角速度、XYZï磁强计),我想将它们与时间戳一起导出到单个数据文件中。我设法导出到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)

Tags: fromimportgetrawtimeenablenpopen
1条回答
网友
1楼 · 发布于 2024-06-01 06:17:19

我使用的是传感器读数的虚拟值,但下面的方法应该有效:

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

相关问题 更多 >