-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_sensor.py
More file actions
58 lines (47 loc) · 1.88 KB
/
graph_sensor.py
File metadata and controls
58 lines (47 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import csv
import os
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from dotenv import load_dotenv
def plot_real_time(is_acc):
load_dotenv()
if is_acc:
threshold = int(os.getenv('IMPACT_THRESHOLD', 1000))
fname = 'Accelerometer'
else:
threshold = int(os.getenv('GYRO_THRESHOLD', 500))
fname = 'Gyroscope'
log_file = f'../logs/{fname}.csv'
window_size = 200 # show only the latest 200 readings
timestamps, x_vals, y_vals, z_vals = [], [], [], []
fig, ax = plt.subplots(figsize=(12, 6))
def animate(i):
try:
with open(log_file) as csvfile:
reader = csv.DictReader(csvfile)
rows = list(reader)[-window_size:] # grab last N rows
timestamps.clear()
x_vals.clear()
y_vals.clear()
z_vals.clear()
for row in rows:
timestamps.append(float(row['ms']))
x_vals.append(float(row['x']))
y_vals.append(float(row['y']))
z_vals.append(float(row['z']))
ax.clear()
ax.plot(timestamps, x_vals, label='X', color='r')
ax.plot(timestamps, y_vals, label='Y', color='g')
ax.plot(timestamps, z_vals, label='Z', color='b')
ax.axhline(y=threshold, color='purple', linestyle='--', label='Threshold')
ax.axhline(y=-threshold, color='purple', linestyle='--')
ax.set_xlabel('Time (ms)')
ax.set_ylabel(f'{fname} raw value')
ax.set_title(f'{fname} Real-Time Readings')
ax.legend()
ax.grid(True)
plt.tight_layout()
except Exception as e:
print(f"[ERROR] Could not update plot: {e}")
ani = animation.FuncAnimation(fig, animate, interval=500) # update every 500 ms
plt.show()