part 1 measurements available

This commit is contained in:
Вадим Силаев 2022-12-12 14:44:41 +03:00
parent f660b77335
commit c01b53403b
2 changed files with 79 additions and 77 deletions

View File

@ -1,46 +1,50 @@
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import random import random
import voltmeter
import amperemeter
from numpy import tanh from numpy import tanh
# Значения part = 1, 2 соответствуют режимам считывания с первой и второй пары приборов соответственно # Значения part = 1, 2 соответствуют режимам считывания с первой и второй пары приборов соответственно,
# (currently not available), режимы 3 и 4 являются тестовыми и генерируют значения самостоятельно # режимы 3 и 4 являются тестовыми и генерируют значения самостоятельно
def get_point(part, num):
def start_measure(part):
if part == 1: if part == 1:
v1 = open('voltmeter1.txt') global v1, i1
i1 = open('ampere-meter1.txt') v1 = voltmeter.AKIP('/dev/usbtmc0')
vol[0].append(float(v1.readline())) i1 = amperemeter.B7_78('/dev/ttyUSB0')
cur[0].append(float(i1.readline())) i1.getCurrentDC()
newv1 = v1.read().replace(str(vol[0][-1]) + '\n', '')
newi1 = i1.read().replace(str(cur[0][-1]) + '\n', '')
v1.close()
i1.close()
v1 = open('voltmeter1.txt', 'w')
v1.write(newv1)
i1 = open('ampere-meter1.txt', 'w')
i1.write(newi1)
v1.close()
i1.close()
elif part == 2: elif part == 2:
v2 = open('voltmeter2.txt') global v2, i2
i2 = open('ampere-meter2.txt') v2 = voltmeter.AKIP('/dev/usbtmc0')
vol[1].append(float(v2.readline())) i2 = amperemeter.B7_78('/dev/ttyUSB1')
cur[1].append(float(i2.readline())) i2.getCurrentDC()
newv2 = v2.read().replace(str(vol[1][-1]) + '\n', '')
newi2 = i2.read().replace(str(cur[1][-1]) + '\n', '') elif part == 0:
v2.close() global i
i2.close() ir = amperemeter.B7_78('/dev/ttyUSB0')
v2 = open('voltmeter2.txt', 'w') i = float(ir.getCurrentDC()) * 10 ** 3
v2.write(newv2) i = float(ir.getCurrentDC()) * 10 ** 3
i2 = open('ampere-meter2.txt', 'w')
i2.write(newi2)
v2.close() def get_point(part, num):
i2.close() if part == 1:
vol[0][num].append(v1.getVoltageDC())
cur[0][num].append(i1.getCurrentDC() * 10 ** 3)
elif part == 2:
# global i
vol[1][num].append(v2.getVoltageDC())
cur[1][num].append(i2.getCurrentDC() * 10 ** 6)
# i = amperemeter.B7_78('/dev/ttyUSB0').getCurrentDC() * 10 ** 3
# print(vol[1], cur[1])
elif part == 3: elif part == 3:
vol[2][num].append(random.random() * 100) vol[2][num].append(random.random() * 100)
cur[2][num].append(random.random() * 100) cur[2][num].append(random.random() * 100)
elif part == 4: elif part == 4:
vol[3][num].append(random.random() * 50 - 25) vol[3][num].append(random.random() * 50 - 25)
if experiment % 3 == 0: if experiment % 3 == 0:
@ -54,17 +58,16 @@ def get_point(part, num):
(22 * tanh(4.4 / 22 * vol[3][num][-1]) + 0.5 * vol[3][num][-1]) * (0.98 + random.random() / 25)) (22 * tanh(4.4 / 22 * vol[3][num][-1]) + 0.5 * vol[3][num][-1]) * (0.98 + random.random() / 25))
def update_plot(part, num):
for i in range(10):
get_point(part, num)
def saveplot(part): def saveplot(part):
plt.figure(figsize=(8, 6)) plt.figure(figsize=(8, 6))
for i in range(len(vol[part - 1])): for j in range(len(vol[part - 1])):
plt.scatter(vol[part - 1][i], cur[part - 1][i], marker='o') # print(vol[part - 1][j], cur[part - 1][j])
plt.scatter(vol[part - 1][j], cur[part - 1][j], marker='o')
plt.grid() plt.grid()
plt.xlabel('Напряжение, В') plt.xlabel('Напряжение, В')
if part == 1:
plt.ylabel('Ток, мА')
else:
plt.ylabel('Ток, мкА') plt.ylabel('Ток, мкА')
plt.savefig('tmpplots/tmpplot' + str(part) + '.jpg', dpi=200) plt.savefig('tmpplots/tmpplot' + str(part) + '.jpg', dpi=200)
plt.close() plt.close()
@ -74,7 +77,7 @@ def empty_plot(part):
vol[part - 1] = [[]] vol[part - 1] = [[]]
cur[part - 1] = [[]] cur[part - 1] = [[]]
plt.figure(figsize=(8, 6)) plt.figure(figsize=(8, 6))
plt.scatter(cur[part - 1], vol[part - 1], marker='o') plt.scatter(cur[part - 1][0], vol[part - 1][0], marker='o')
plt.grid() plt.grid()
plt.savefig('tmpplots/tmpplot' + str(part) + '.jpg', dpi=200) plt.savefig('tmpplots/tmpplot' + str(part) + '.jpg', dpi=200)
plt.close() plt.close()
@ -94,7 +97,7 @@ def minus_plot(part):
vol[part - 1].append([]) vol[part - 1].append([])
cur[part - 1].append([]) cur[part - 1].append([])
i = 0
vol = [[], [], [[]], [[]]] vol = [[[]], [[]], [[]], [[]]]
cur = [[], [], [[]], [[]]] cur = [[[]], [[]], [[]], [[]]]
experiment = 0 experiment = 0

View File

@ -72,7 +72,7 @@ class Part1(QtWidgets.QWidget):
self.started = False self.started = False
self.q = QtGui.QScreen.availableGeometry(QtWidgets.QApplication.primaryScreen()) self.q = QtGui.QScreen.availableGeometry(QtWidgets.QApplication.primaryScreen())
h = self.q.height() * 0.2 h = self.q.height() * 0.2
plots.empty_plot(3) plots.empty_plot(1)
self.points = QtWidgets.QLabel(self) self.points = QtWidgets.QLabel(self)
self.points.setGeometry(QtCore.QRect(7.5 * h - 60, 3 * h - 80, 140, 20)) self.points.setGeometry(QtCore.QRect(7.5 * h - 60, 3 * h - 80, 140, 20))
@ -117,6 +117,7 @@ class Part1(QtWidgets.QWidget):
self.back_button.setGeometry(QtCore.QRect(20, self.q.height() - 20, 80, 40)) self.back_button.setGeometry(QtCore.QRect(20, self.q.height() - 20, 80, 40))
self.back_button.setText("Назад") self.back_button.setText("Назад")
plots.empty_plot(1)
self.pixmap0 = QtGui.QPixmap('tmpplots/tmpplot1.jpg') self.pixmap0 = QtGui.QPixmap('tmpplots/tmpplot1.jpg')
self.pixmap = self.pixmap0.scaledToHeight(3 * h) self.pixmap = self.pixmap0.scaledToHeight(3 * h)
self.plot1 = QtWidgets.QLabel(self) self.plot1 = QtWidgets.QLabel(self)
@ -158,7 +159,7 @@ class Part1(QtWidgets.QWidget):
timer = QtCore.QTimer(self) timer = QtCore.QTimer(self)
timer.timeout.connect(self.upd_plot) timer.timeout.connect(self.upd_plot)
timer.start(500) timer.start(3000)
@QtCore.Slot() @QtCore.Slot()
def save_plot(self): def save_plot(self):
@ -166,7 +167,7 @@ class Part1(QtWidgets.QWidget):
file, check = QtWidgets.QFileDialog.getSaveFileName(None, "Сохранить график 1", file, check = QtWidgets.QFileDialog.getSaveFileName(None, "Сохранить график 1",
r"C:\Users\Vadim\Desktop\plot1.jpg", "Images (*.png *.jpg)") r"C:\Users\Vadim\Desktop\plot1.jpg", "Images (*.png *.jpg)")
if check: if check:
plot1 = PIL.Image.open('tmpplots/tmpplot3.jpg') plot1 = PIL.Image.open('tmpplots/tmpplot1.jpg')
plot1.save(file) plot1.save(file)
def save_values(self): def save_values(self):
@ -174,7 +175,7 @@ class Part1(QtWidgets.QWidget):
file, check = QtWidgets.QFileDialog.getSaveFileName(None, "Сохранить значения", file, check = QtWidgets.QFileDialog.getSaveFileName(None, "Сохранить значения",
r"C:\Users\Vadim\Desktop\experiment1.xlsx", "Table (.xlsx)") r"C:\Users\Vadim\Desktop\experiment1.xlsx", "Table (.xlsx)")
if check: if check:
data = {'U_р, В': plots.vol[2][0], 'I_р, мА': plots.cur[2][0]} data = {'U_р, В': plots.vol[0][0], 'I_р, мА': plots.cur[0][0]}
df = pd.DataFrame(data) df = pd.DataFrame(data)
df.to_excel(file, index=False) df.to_excel(file, index=False)
@ -196,19 +197,20 @@ class Part1(QtWidgets.QWidget):
def show_plot(self): def show_plot(self):
self.points.setText('Измеренных точек: ' + str(self.n)) self.points.setText('Измеренных точек: ' + str(self.n))
self.pixmap0 = QtGui.QPixmap('tmpplots/tmpplot3.jpg') self.pixmap0 = QtGui.QPixmap('tmpplots/tmpplot1.jpg')
self.pixmap = self.pixmap0.scaledToHeight(0.6 * self.q.height()) self.pixmap = self.pixmap0.scaledToHeight(0.6 * self.q.height())
self.plot1.setPixmap(self.pixmap) self.plot1.setPixmap(self.pixmap)
def upd_plot(self): def upd_plot(self):
if self.started: if self.started:
plots.update_plot(3, 0) plots.get_point(1, 0)
plots.saveplot(3) plots.saveplot(1)
self.n += 10 self.n += 1
self.show_plot() self.show_plot()
def start_stop(self): def start_stop(self):
if not self.started: if not self.started:
plots.start_measure(1)
self.start_btn.setText("Стоп") self.start_btn.setText("Стоп")
self.started = True self.started = True
self.save_plot_btn.hide() self.save_plot_btn.hide()
@ -229,7 +231,7 @@ class Part1(QtWidgets.QWidget):
self.save_values_btn.hide() self.save_values_btn.hide()
self.hint_btn.show() self.hint_btn.show()
self.n = 0 self.n = 0
plots.empty_plot(3) plots.empty_plot(1)
self.show_plot() self.show_plot()
@ -243,7 +245,7 @@ class Part2(QtWidgets.QWidget):
self.started = False self.started = False
self.q = QtGui.QScreen.availableGeometry(QtWidgets.QApplication.primaryScreen()) self.q = QtGui.QScreen.availableGeometry(QtWidgets.QApplication.primaryScreen())
h = self.q.height() * 0.2 h = self.q.height() * 0.2
plots.empty_plot(4) plots.empty_plot(2)
self.button = QtWidgets.QPushButton(self) self.button = QtWidgets.QPushButton(self)
self.button.setGeometry(QtCore.QRect(20, 20, 40, 40)) self.button.setGeometry(QtCore.QRect(20, 20, 40, 40))
@ -298,7 +300,7 @@ class Part2(QtWidgets.QWidget):
self.hint_btn.setText('?') self.hint_btn.setText('?')
self.hint_btn.setGeometry(QtCore.QRect(self.q.width() - 60, 20, 40, 40)) self.hint_btn.setGeometry(QtCore.QRect(self.q.width() - 60, 20, 40, 40))
self.pixmap0 = QtGui.QPixmap('tmpplots/tmpplot4.jpg') self.pixmap0 = QtGui.QPixmap('tmpplots/tmpplot2.jpg')
self.pixmap = self.pixmap0.scaledToHeight(3 * h) self.pixmap = self.pixmap0.scaledToHeight(3 * h)
self.plot1 = QtWidgets.QLabel(self) self.plot1 = QtWidgets.QLabel(self)
self.plot1.setPixmap(self.pixmap) self.plot1.setPixmap(self.pixmap)
@ -336,28 +338,24 @@ class Part2(QtWidgets.QWidget):
timer = QtCore.QTimer(self) timer = QtCore.QTimer(self)
timer.timeout.connect(self.upd_plot) timer.timeout.connect(self.upd_plot)
timer.start(500) timer.start(3000)
timer2 = QtCore.QTimer(self) timer2 = QtCore.QTimer(self)
timer2.timeout.connect(self.cur_check) timer2.timeout.connect(self.cur_check)
timer2.start(1000) timer2.start(10000)
@QtCore.Slot() @QtCore.Slot()
def cur_check(self): def cur_check(self):
if plots.experiment % 3 == 0: plots.start_measure(0)
self.i = 5 # plots.get_point(2, self.num)
elif plots.experiment % 3 == 1: self.current.setText('Текущий ток разряда: ' + str(plots.i) + 'мА')
self.i = 3
else:
self.i = 1.5
self.current.setText('Текущий ток разряда: ' + str(self.i) + 'мА')
def save_plot(self): def save_plot(self):
# при замене устройства директорию надо изменить # при замене устройства директорию надо изменить
file, check = QtWidgets.QFileDialog.getSaveFileName(None, "Сохранить график 2", file, check = QtWidgets.QFileDialog.getSaveFileName(None, "Сохранить график 2",
r"C:\Users\Vadim\Desktop\plot2.jpg", "Images (*.png *.jpg)") r"C:\Users\Vadim\Desktop\plot2.jpg", "Images (*.png *.jpg)")
if check: if check:
plot1 = PIL.Image.open('tmpplots/tmpplot4.jpg') plot1 = PIL.Image.open('tmpplots/tmpplot2.jpg')
plot1.save(file) plot1.save(file)
def save_values(self): def save_values(self):
@ -365,21 +363,21 @@ class Part2(QtWidgets.QWidget):
file, check = QtWidgets.QFileDialog.getSaveFileName(None, "Сохранить значения", file, check = QtWidgets.QFileDialog.getSaveFileName(None, "Сохранить значения",
r"C:\Users\Vadim\Desktop\experiment2.xlsx", "Table (.xlsx)") r"C:\Users\Vadim\Desktop\experiment2.xlsx", "Table (.xlsx)")
if check: if check:
data = {'U_1, В': plots.vol[3][0], 'I_1, мкА': plots.cur[3][0]} data = {'U_1, В': plots.vol[1][0], 'I_1, мкА': plots.cur[1][0]}
I_0, k_0, A = [], [], [] I_0, k_0, A = [], [], []
params = fitting.fit(plots.vol[3][0], plots.cur[3][0]) params = fitting.fit(plots.vol[1][0], plots.cur[1][0])
I_0.append(params[0]) I_0.append(params[0])
k_0.append(params[1]) k_0.append(params[1])
A.append(params[2]) A.append(params[2])
for i in range(len(plots.vol[3]) - 1): for i in range(len(plots.vol[1]) - 1):
mes = {'U_' + str(i+2) + ', В': plots.vol[3][i + 1], 'I_' + str(i + 2) + ', мкА': plots.cur[3][i + 1]} mes = {'U_' + str(i+2) + ', В': plots.vol[1][i + 1], 'I_' + str(i + 2) + ', мкА': plots.cur[1][i + 1]}
data.update(mes) data.update(mes)
params = fitting.fit(plots.vol[3][i + 1], plots.cur[3][i + 1]) params = fitting.fit(plots.vol[1][i + 1], plots.cur[1][i + 1])
I_0.append(params[0]) I_0.append(params[0])
k_0.append(params[1]) k_0.append(params[1])
A.append(params[2]) A.append(params[2])
data.update({'I_0, мкА': I_0, 'k_0, мкА/В': k_0, 'A, мкА/В': A}) data.update({'I_0, мкА': I_0, 'T_e, K': k_0, 'A, мкА/В': A})
df = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in data.items()])) df = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in data.items()]))
df.to_excel(file, index=False) df.to_excel(file, index=False)
@ -402,26 +400,27 @@ class Part2(QtWidgets.QWidget):
def show_plot(self): def show_plot(self):
self.points.setText('Измеренных точек в текущей серии: ' + str(self.n)) self.points.setText('Измеренных точек в текущей серии: ' + str(self.n))
self.pixmap0 = QtGui.QPixmap('tmpplots/tmpplot4.jpg') self.pixmap0 = QtGui.QPixmap('tmpplots/tmpplot2.jpg')
self.pixmap = self.pixmap0.scaledToHeight(0.6 * self.q.height()) self.pixmap = self.pixmap0.scaledToHeight(0.6 * self.q.height())
self.plot1.setPixmap(self.pixmap) self.plot1.setPixmap(self.pixmap)
def upd_plot(self): def upd_plot(self):
if self.started: if self.started:
plots.update_plot(4, self.num - 1) plots.get_point(2, self.num - 1)
plots.saveplot(4) plots.saveplot(2)
self.n += 10 self.n += 1
self.show_plot() self.show_plot()
def start_stop(self): def start_stop(self):
if not self.started: if not self.started:
plots.start_measure(2)
self.start_btn.setText("Стоп") self.start_btn.setText("Стоп")
self.started = True self.started = True
self.save_plot_btn.hide() self.save_plot_btn.hide()
self.save_values_btn.hide() self.save_values_btn.hide()
self.hint_btn.hide() self.hint_btn.hide()
if self.num != 0: if self.num != 0:
plots.plus_plot(4) plots.plus_plot(2)
self.num += 1 self.num += 1
self.n = 0 self.n = 0
@ -440,8 +439,8 @@ class Part2(QtWidgets.QWidget):
self.save_values_btn.hide() self.save_values_btn.hide()
self.hint_btn.show() self.hint_btn.show()
self.n = 0 self.n = 0
plots.minus_plot(4) plots.minus_plot(2)
plots.saveplot(4) plots.saveplot(2)
self.show_plot() self.show_plot()
self.num -= 1 self.num -= 1
if self.num == 0: if self.num == 0: