GeekIMU/4.Software/VisualizeTools 1.0/core/data_logger.py

32 lines
1.0 KiB
Python

# core/data_logger.py
import csv
import time
from pathlib import Path
class DataLogger:
def __init__(self):
self.filename = Path(f"logs/sensor_{time.strftime('%Y%m%d_%H%M')}.csv")
self.filename.parent.mkdir(exist_ok=True)
with open(self.filename, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow([
'Timestamp', 'Roll', 'Pitch', 'Yaw',
'AccX', 'AccY', 'AccZ',
'GyroX', 'GyroY', 'GyroZ'
])
def log_data(self, data):
with open(self.filename, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow([
data['timestamp'],
data['attitude']['roll'],
data['attitude']['pitch'],
data['attitude']['yaw'],
data['acc']['x'],
data['acc']['y'],
data['acc']['z'],
data['gyro']['x'],
data['gyro']['y'],
data['gyro']['z']
])