pytec: revert changes to example.py and plot.py, save changes for other PR

This commit is contained in:
topquark12 2021-01-05 12:42:50 +08:00
parent 9f872c0113
commit 14a027c0f0
2 changed files with 118 additions and 122 deletions

View File

@ -1,13 +1,11 @@
if __name__ == "__main__": from pytec.client import Client
from pytec.client import Client tec = Client() #(host="localhost", port=6667)
tec.set_param("s-h", 1, "t0", 20)
tec = Client() #(host="localhost", port=6667) print(tec.get_pwm())
tec.set_param("s-h", 1, "t0", 20) print(tec.get_pid())
print(tec.get_pwm()) print(tec.get_pwm())
print(tec.get_pid()) print(tec.get_postfilter())
print(tec.get_pwm()) print(tec.get_steinhart_hart())
print(tec.get_postfilter()) for data in tec.report_mode():
print(tec.get_steinhart_hart()) print(data)
for data in tec.report_mode():
print(data)

View File

@ -4,127 +4,125 @@ import matplotlib.animation as animation
from threading import Thread, Lock from threading import Thread, Lock
from pytec.client import Client from pytec.client import Client
if __name__ == "__main__": TIME_WINDOW = 300.0
TIME_WINDOW = 300.0 tec = Client()
target_temperature = tec.get_pid()[0]['target']
print("Channel 0 target temperature: {:.3f}".format(target_temperature))
tec = Client() class Series:
target_temperature = tec.get_pid()[0]['target'] def __init__(self, conv=lambda x: x):
print("Channel 0 target temperature: {:.3f}".format(target_temperature)) self.conv = conv
self.x_data = []
self.y_data = []
class Series: def append(self, x, y):
def __init__(self, conv=lambda x: x): self.x_data.append(x)
self.conv = conv self.y_data.append(self.conv(y))
self.x_data = []
self.y_data = []
def append(self, x, y): def clip(self, min_x):
self.x_data.append(x) drop = 0
self.y_data.append(self.conv(y)) while drop < len(self.x_data) and self.x_data[drop] < min_x:
drop += 1
self.x_data = self.x_data[drop:]
self.y_data = self.y_data[drop:]
def clip(self, min_x): series = {
drop = 0 'adc': Series(),
while drop < len(self.x_data) and self.x_data[drop] < min_x: 'sens': Series(lambda x: x * 0.0001),
drop += 1 'temperature': Series(lambda t: t - target_temperature),
self.x_data = self.x_data[drop:] 'i_set': Series(),
self.y_data = self.y_data[drop:] 'pid_output': Series(),
'vref': Series(),
'dac_value': Series(),
'dac_feedback': Series(),
'i_tec': Series(),
'tec_i': Series(),
'tec_u_meas': Series(),
'interval': Series(),
}
series_lock = Lock()
series = { quit = False
'adc': Series(),
'sens': Series(lambda x: x * 0.0001),
'temperature': Series(),
'i_set': Series(),
'pid_output': Series(),
'vref': Series(),
'dac_value': Series(),
'dac_feedback': Series(),
'i_tec': Series(),
'tec_i': Series(),
'tec_u_meas': Series(),
'interval': Series(),
}
series_lock = Lock()
quit = False
def recv_data(tec):
global last_packet_time
for data in tec.report_mode():
ch0 = data[0]
series_lock.acquire()
try:
for k, s in series.items():
if k in ch0:
v = ch0[k]
if type(v) is float:
s.append(ch0['time'], v)
finally:
series_lock.release()
if quit:
break
thread = Thread(target=recv_data, args=(tec,))
thread.start()
fig, ax = plt.subplots()
for k, s in series.items():
s.plot, = ax.plot([], [], label=k)
legend = ax.legend()
def animate(i):
min_x, max_x, min_y, max_y = None, None, None, None
def recv_data(tec):
global last_packet_time
for data in tec.report_mode():
ch0 = data[0]
series_lock.acquire() series_lock.acquire()
try: try:
for k, s in series.items(): for k, s in series.items():
s.plot.set_data(s.x_data, s.y_data) if k in ch0:
if len(s.y_data) > 0: v = ch0[k]
s.plot.set_label("{}: {:.3f}".format(k, s.y_data[-1])) if type(v) is float:
s.append(ch0['time'], v)
if len(s.x_data) > 0:
min_x_ = min(s.x_data)
if min_x is None:
min_x = min_x_
else:
min_x = min(min_x, min_x_)
max_x_ = max(s.x_data)
if max_x is None:
max_x = max_x_
else:
max_x = max(max_x, max_x_)
if len(s.y_data) > 0:
min_y_ = min(s.y_data)
if min_y is None:
min_y = min_y_
else:
min_y = min(min_y, min_y_)
max_y_ = max(s.y_data)
if max_y is None:
max_y = max_y_
else:
max_y = max(max_y, max_y_)
if min_x and max_x - TIME_WINDOW > min_x:
for s in series.values():
s.clip(max_x - TIME_WINDOW)
finally: finally:
series_lock.release() series_lock.release()
if min_x != max_x: if quit:
ax.set_xlim(min_x, max_x) break
if min_y != max_y:
margin_y = 0.01 * (max_y - min_y)
ax.set_ylim(min_y - margin_y, max_y + margin_y)
global legend thread = Thread(target=recv_data, args=(tec,))
legend.remove() thread.start()
legend = ax.legend()
ani = animation.FuncAnimation( fig, ax = plt.subplots()
fig, animate, interval=1, blit=False, save_count=50)
plt.show() for k, s in series.items():
quit = True s.plot, = ax.plot([], [], label=k)
thread.join() legend = ax.legend()
def animate(i):
min_x, max_x, min_y, max_y = None, None, None, None
series_lock.acquire()
try:
for k, s in series.items():
s.plot.set_data(s.x_data, s.y_data)
if len(s.y_data) > 0:
s.plot.set_label("{}: {:.3f}".format(k, s.y_data[-1]))
if len(s.x_data) > 0:
min_x_ = min(s.x_data)
if min_x is None:
min_x = min_x_
else:
min_x = min(min_x, min_x_)
max_x_ = max(s.x_data)
if max_x is None:
max_x = max_x_
else:
max_x = max(max_x, max_x_)
if len(s.y_data) > 0:
min_y_ = min(s.y_data)
if min_y is None:
min_y = min_y_
else:
min_y = min(min_y, min_y_)
max_y_ = max(s.y_data)
if max_y is None:
max_y = max_y_
else:
max_y = max(max_y, max_y_)
if min_x and max_x - TIME_WINDOW > min_x:
for s in series.values():
s.clip(max_x - TIME_WINDOW)
finally:
series_lock.release()
if min_x != max_x:
ax.set_xlim(min_x, max_x)
if min_y != max_y:
margin_y = 0.01 * (max_y - min_y)
ax.set_ylim(min_y - margin_y, max_y + margin_y)
global legend
legend.remove()
legend = ax.legend()
ani = animation.FuncAnimation(
fig, animate, interval=1, blit=False, save_count=50)
plt.show()
quit = True
thread.join()