-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
211 lines (138 loc) · 5.65 KB
/
Copy pathmonitor.py
File metadata and controls
211 lines (138 loc) · 5.65 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
### !!! NOT USED !!!
import matplotlib.pyplot as plt
from collections import deque
import numpy as np
plt.switch_backend('Agg')
def compute_rms(data, window=1000):
data_list = list(data)
samples = np.array(data_list[-window:])
if len(samples) == 0:
return 0
return np.sqrt(np.mean(samples**2))
class Monitor:
def __init__(self, scale=1.0):
plt.ion()
self.WINDOW = 4
self.scale = scale
self.max_len = 10000 # recommended range: 1000–3000
self.t = deque(maxlen=self.max_len)
self.omega = deque(maxlen=self.max_len)
self.torque = deque(maxlen=self.max_len)
self.iA = deque(maxlen=self.max_len)
self.iB = deque(maxlen=self.max_len)
self.iC = deque(maxlen=self.max_len)
self.vA = deque(maxlen=self.max_len)
self.vB = deque(maxlen=self.max_len)
self.vC = deque(maxlen=self.max_len)
self.Vdc = deque(maxlen=self.max_len)
# Create figure
self.fig, self.axs = plt.subplots(5, 1, figsize=(max(8, 15 * scale), max(6, 10 * scale)))
self.fig.subplots_adjust(hspace=max(0.3, 0.5 * scale))
# Lines
lw=0.3
self.line_speed, = self.axs[0].plot([], [])
self.line_torque, = self.axs[1].plot([], [])
self.line_iA, = self.axs[2].plot([], [], label="iA")
self.line_iB, = self.axs[2].plot([], [], label="iB")
self.line_iC, = self.axs[2].plot([], [], label="iC")
self.line_vA, = self.axs[3].plot([], [], label="vA")
self.line_vB, = self.axs[3].plot([], [], label="vB")
self.line_vC, = self.axs[3].plot([], [], label="vC")
self.axs[3].set_title("Phase Voltages")
self.axs[3].legend()
self.axs[0].set_title("Speed")
self.axs[1].set_title("Torque")
self.axs[2].set_title("Phase Currents")
self.axs[2].legend()
# -------------------------
# LOG DATA
# -------------------------
def log(self, t, omega, torque, iA, iB, iC, vA, vB, vC, Vdc):
self.t.append(t)
self.omega.append(omega)
self.torque.append(torque)
self.iA.append(iA)
self.iB.append(iB)
self.iC.append(iC)
self.vA.append(vA)
self.vB.append(vB)
self.vC.append(vC)
self.Vdc.append(Vdc)
# -------------------------
# UPDATE PLOT (SCROLLING)
# -------------------------
def update_plot(self):
if len(self.t) < 2:
return
# update lines
lw=0.3
self.line_speed.set_data(list(self.t), self.omega)
self.line_torque.set_data(list(self.t), self.torque)
self.line_iA.set_data(list(self.t), self.iA)
self.line_iB.set_data(list(self.t), self.iB)
self.line_iC.set_data(list(self.t), self.iC)
self.line_vA.set_data(list(self.t), self.vA)
self.line_vB.set_data(list(self.t), self.vB)
self.line_vC.set_data(list(self.t), self.vC)
t_min = self.t[-1] - self.WINDOW
# filter indices for visible data
indices = [i for i, t_val in enumerate(self.t) if t_val >= t_min]
# extract only visible values
torque_visible = [self.torque[i] for i in indices]
# ✅ SCROLLING WINDOW
t_min = self.t[-1] - self.WINDOW
t_max = self.t[-1]
for ax in self.axs:
ax.set_xlim(t_min, t_max)
# -------- SPEED --------
if len(self.omega) > 0:
o_min = min(self.omega)
o_max = max(self.omega)
if abs(o_max - o_min) < 1e-6:
o_max += 0.1
o_min -= 0.1
margin = 0.1 * (o_max - o_min)
self.axs[0].set_ylim(o_min - margin, o_max + margin)
# -------- TORQUE --------
t_min = self.t[-1] - self.WINDOW
# filter indices for visible data
indices = [i for i, t_val in enumerate(self.t) if t_val >= t_min]
# extract only visible values
torque_visible = [self.torque[i] for i in indices]
if len(torque_visible) > 0:
t_min_val = min(torque_visible)*3
t_max_val = max(torque_visible)*3
margin = 0.1 * (t_max_val - t_min_val + 1e-6)
self.axs[1].set_ylim(t_min_val - margin, t_max_val + margin)
# -------- CURRENTS --------
if len(self.iA) > 0:
i_all = self.iA + self.iB + self.iC
i_min = min(i_all)
i_max = max(i_all)
if abs(i_max - i_min) < 1e-6:
i_max += 0.1
i_min -= 0.1
margin = 0.1 * (i_max - i_min)
self.axs[2].set_ylim(i_min - margin, i_max + margin)
# -------- VOLTAGES --------
if len(self.vA) > 0:
v_all = self.vA + self.vB + self.vC
v_min = min(v_all)
v_max = max(v_all)
if abs(v_max - v_min) < 1e-6:
v_max += 0.1
v_min -= 0.1
margin = 0.1 * (v_max - v_min)
self.axs[3].set_ylim(v_min - margin, v_max + margin)
# draw
self.fig.canvas.draw()
self.fig.canvas.flush_events()
def get_image(self):
# draw the matplotlib figure
self.fig.canvas.draw()
# get raw pixel data
raw_data = self.fig.canvas.buffer_rgba()
size = self.fig.canvas.get_width_height()
# convert to pygame surface
import pygame
return pygame.image.frombuffer(raw_data, size, "RGBA")