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:
26
visualise.py
26
visualise.py
@ -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
|
||||
Reference in New Issue
Block a user