-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolar_dashboard.py
More file actions
80 lines (58 loc) · 1.81 KB
/
solar_dashboard.py
File metadata and controls
80 lines (58 loc) · 1.81 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import serial
import time
import matplotlib.pyplot as plt
from collections import deque
PORT = "/dev/ttyACM0"
BAUD = 9600
ser = serial.Serial(PORT, BAUD, timeout=1)
time.sleep(2)
max_points = 100
angles = deque(maxlen=max_points)
times = deque(maxlen=max_points)
last_angle = None
stable_count = 0
total_count = 0
plt.style.use("dark_background")
fig, ax = plt.subplots()
fig.canvas.manager.set_window_title("Solar Tracker Dashboard")
ax.set_ylim(20, 160)
ax.set_xlim(0, max_points)
ax.set_ylabel("Servo Angle (degrees)")
ax.set_xlabel("Time (samples)")
ax.axhline(90, linestyle="--", linewidth=1)
(line,) = ax.plot([], [], linewidth=2)
while True:
try:
raw = ser.readline().decode(errors="ignore").strip()
if not raw or raw.count(",") != 2:
continue
left, right, angle = map(int, raw.split(","))
times.append(len(times))
angles.append(angle)
if last_angle == angle:
stable_count += 1
total_count += 1
last_angle = angle
stability = (stable_count / total_count) * 100
if len(angles) > 1:
if angles[-1] > angles[-2]:
direction = "RIGHT"
elif angles[-1] < angles[-2]:
direction = "LEFT"
else:
direction = "STABLE"
else:
direction = "STABLE"
left_state = "BRIGHT" if left else "DARK"
right_state = "BRIGHT" if right else "DARK"
line.set_data(range(len(angles)), angles)
ax.set_xlim(0, max_points)
ax.set_title(
f"Left: {left_state} | Right: {right_state} | "
f"Angle: {angle}° | Dir: {direction} | "
f"Stability: {stability:.1f}%"
)
plt.pause(0.05)
except KeyboardInterrupt:
print("Dashboard closed")
break