Enhance IMU data visualization and file handling

- Updated `main.py` to include a flag for real GPIO output control.
- Added a new function in `visualise.py` to generate unique file paths for CSV and plot outputs by appending timestamps if the file already exists.
- Modified CSV and plot saving logic to utilize the new unique path function, ensuring no overwriting of existing files.
- Introduced a new image file `imu_quality_20260701_131902.png` and updated the existing `imu_quality.png`.
This commit is contained in:
Brunsmeier
2026-07-01 13:21:15 +08:00
parent e2c1597b68
commit f33a3f1886
5 changed files with 530 additions and 5 deletions

View File

@ -5,7 +5,7 @@ import os
import re
import sys
import time
from datetime import datetime
os.environ.setdefault("MPLCONFIGDIR", "/tmp/matplotlib")
import matplotlib.pyplot as plt
@ -184,7 +184,21 @@ def write_csv(samples, path):
writer = csv.DictWriter(f, fieldnames=columns)
writer.writeheader()
writer.writerows(samples)
def unique_path(path):
"""
If path already exists, append timestamp before extension.
Example:
imu_quality.png -> imu_quality_20260701_123045.png
"""
if not path:
return path
if not os.path.exists(path):
return path
root, ext = os.path.splitext(path)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
return "{}_{}{}".format(root, timestamp, ext)
def print_stats(samples):
if not samples:
@ -317,11 +331,13 @@ def main():
print_stats(samples)
if args.csv:
write_csv(samples, args.csv)
print("Saved CSV to {}".format(args.csv))
plot_samples(samples, output=args.output, show=not args.no_show)
csv_path = unique_path(args.csv)
write_csv(samples, csv_path)
print("Saved CSV to {}".format(csv_path))
output_path = unique_path(args.output)
plot_samples(samples, output=output_path, show=not args.no_show)
if __name__ == "__main__":
main()
# python3 visualise.py --port /dev/ttyUSB0 --baud 115200 --seconds 10 --csv imu_data.csv