pulse_generator_GUI.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. from dis import pretty_flags
  2. from PySide6 import QtWidgets # Импорт модуля QtWidgets из библиотеки PySide6
  3. from PySide6.QtGui import QPixmap,QFont
  4. import sys # Импорт модуля sys для работы с системными параметрами и выходом из программы
  5. import numpy as np
  6. import struct
  7. import xml.etree.ElementTree as ET
  8. from xml.dom import minidom
  9. def switch(metric):
  10. if metric == 'nS':
  11. return 10**-9
  12. elif metric == 'uS':
  13. return 10**-6
  14. elif metric == 'mS':
  15. return 10**-3
  16. elif metric == 'S':
  17. return 1
  18. def set_params():
  19. pulse_period_metric = pulse_period_combo_box.currentText()
  20. pulse_period = (np.int32(pulse_period_line_edit.text())) * switch(pulse_period_metric)
  21. pulse_width_metric = pulse_width_combo_box.currentText()
  22. pulse_width = (np.int32(pulse_width_line_edit.text())) * switch(pulse_width_metric)
  23. delay1_metric = delay1_combo_box.currentText()
  24. delay1 = (np.int32(delay1_line_edit.text())) * switch(delay1_metric)
  25. delay2_metric = delay2_combo_box.currentText()
  26. delay2 = (np.int32(delay2_line_edit.text())) * switch(delay2_metric)
  27. sample_freq = np.int32(sample_rate_box.currentText())
  28. pulse_number = np.int32(period_number_line_edit.text())
  29. print(f""" Pulse parameters:
  30. pulse width - {pulse_width} S
  31. pulse period - {pulse_period} S
  32. delay 1 - {delay1} S
  33. delay 2 - {delay2} S
  34. pulse_number - {pulse_number}
  35. sample frequency {sample_freq} MHz""")
  36. #add = (pulse_width * pulse_number * sample_freq * (10**6) * 2)
  37. counter = 0
  38. n_high = round(pulse_width * sample_freq * (10 ** 6))
  39. print(f"high samples {n_high}")
  40. n_low = round((pulse_period - pulse_width) * sample_freq * (10 ** 6))
  41. print(f"low samples {n_low}")
  42. size_1 =(n_low + n_high) * 2
  43. print(f'rf array size_1 = {size_1}')
  44. size_2 = np.int32(size_1) * np.int32(pulse_number)
  45. print(f'rf array size_2 = {size_2}')
  46. rf_array = np.zeros(size_2, dtype=np.int8)
  47. for _ in range(0, pulse_number, 1):
  48. for _ in range(0, n_high, 1):
  49. rf_array[counter] = np.int8(127)
  50. rf_array[counter + 1] = np.int8(0)
  51. counter += 2
  52. for _ in range(0, n_low, 1):
  53. rf_array[counter] = np.int8(0)
  54. rf_array[counter + 1] = np.int8(0)
  55. counter += 2
  56. print(rf_array)
  57. f = open(f'pulse_pw_{pulse_width}_S_period_{pulse_period}_S_reps_{pulse_number}_delay1_{delay1}_delay2_{delay2}.bin', 'wb') # открываем файл на чтение
  58. # f = open(f'test_100_pulses_10Msps_1ms.bin', 'wb') # открываем файл на чтение
  59. for elem in rf_array: # берём каждую строчку из файла f
  60. f.write(struct.pack('<b',elem))
  61. f.close()
  62. # write sync params in XML file
  63. root = ET.Element('root')
  64. rf = ET.SubElement(root, 'RF')
  65. sw = ET.SubElement(root, 'SW')
  66. adc = ET.SubElement(root, 'ADC')
  67. gru1 = ET.SubElement(root, 'GR')
  68. first_pulse_delay = 17 * 10 ** -6
  69. cl = ET.SubElement(root, 'CL')
  70. ET.SubElement(rf,'RF1').text = '0'
  71. ET.SubElement(sw,'SW1').text = '0'
  72. ET.SubElement(adc,'ADC1').text = '0'
  73. ET.SubElement(gru1,'GR1').text = '1'
  74. ET.SubElement(cl, 'CL1').text = str(round(first_pulse_delay/ (20*(10**-9))))
  75. param_count = 1
  76. delay1_cycles = round(delay1 / (20*(10**-9)))
  77. pw_cycles = round(pulse_width / (20*(10**-9)))
  78. low_cycles = round((pw_cycles - 2 * delay1_cycles))
  79. delay2_cycles = round (delay2 / (20*(10**-9)))
  80. recieve_time = low_cycles - 2 * delay2_cycles
  81. for _ in range (0, pulse_number):
  82. param_count += 1
  83. ET.SubElement(rf, 'RF' + str(param_count)).text = "1"
  84. ET.SubElement(sw, 'SW' + str(param_count)).text = "1"
  85. ET.SubElement(adc, 'ADC' + str(param_count)).text = "0"
  86. ET.SubElement(gru1, 'GR' + str(param_count)).text = "0"
  87. ET.SubElement(cl, 'CL' + str(param_count)).text = str(2 * delay1_cycles + pw_cycles)
  88. param_count += 1
  89. ET.SubElement(rf, 'RF' + str(param_count)).text = "0"
  90. ET.SubElement(sw, 'SW' + str(param_count)).text = "0"
  91. ET.SubElement(adc, 'ADC' + str(param_count)).text = "0"
  92. ET.SubElement(gru1, 'GR' + str(param_count)).text = "0"
  93. ET.SubElement(cl, 'CL' + str(param_count)).text = str(delay2_cycles)
  94. param_count += 1
  95. ET.SubElement(rf, 'RF' + str(param_count)).text = "0"
  96. ET.SubElement(sw, 'SW' + str(param_count)).text = "0"
  97. ET.SubElement(adc, 'ADC' + str(param_count)).text = "1"
  98. ET.SubElement(gru1, 'GR' + str(param_count)).text = "0"
  99. ET.SubElement(cl, 'CL' + str(param_count)).text = str(recieve_time)
  100. param_count += 1
  101. ET.SubElement(rf, 'RF'+ str(param_count)).text = "0"
  102. ET.SubElement(sw,'SW'+ str(param_count)).text = "0"
  103. ET.SubElement(adc, 'ADC' + str(param_count)).text = "0"
  104. ET.SubElement(gru1, 'GR' + str(param_count)).text = "0"
  105. ET.SubElement(cl, 'CL' + str(param_count)).text = str(delay2_cycles)
  106. ET.SubElement(root, 'ParamCount').text = str(param_count)
  107. xml_str = ET.tostring(root, encoding='utf-8', method='xml')
  108. parsed_str = minidom.parseString(xml_str) # Парсим строку
  109. pretty_xml_str = parsed_str.toprettyxml(indent=" ") # Добавляем отступы
  110. # Сохраняем форматированный XML в файл
  111. with open('Sync_param.xml', 'w', encoding='utf-8') as f:
  112. f.write(pretty_xml_str)
  113. # Создание экземпляра QApplication, который управляет основным циклом событий и инициализацией приложения
  114. app = QtWidgets.QApplication(sys.argv)
  115. # Создание главного окна приложения
  116. window = QtWidgets.QWidget()
  117. # Установка заголовка главного окна
  118. window.setWindowTitle("Pulse_generator")
  119. # Установка размеров главного окна
  120. window.resize(500, 500)
  121. # Создание метки (надписи) с текстом
  122. lbl = QtWidgets.QLabel("Pulse image!")
  123. label = QtWidgets.QLabel("")
  124. pixmap = QPixmap('pulse_template_3.jpg')
  125. label.setPixmap(pixmap)
  126. pulse_width_label = QtWidgets.QLabel("Pulse width")
  127. pulse_period_label = QtWidgets.QLabel("Pulse period")
  128. delay_label1 = QtWidgets.QLabel("Delay 1")
  129. delay_label2 = QtWidgets.QLabel("Delay 2")
  130. period_number_label = QtWidgets.QLabel("Pulse number")
  131. period_number_label_2 = QtWidgets.QLabel("pulses")
  132. sample_rate_label = QtWidgets.QLabel("Sample rate")
  133. sample_rate_label_2 = QtWidgets.QLabel("MHz")
  134. pulse_width_line_edit = QtWidgets.QLineEdit()
  135. pulse_period_line_edit = QtWidgets.QLineEdit()
  136. delay1_line_edit = QtWidgets.QLineEdit()
  137. delay2_line_edit = QtWidgets.QLineEdit()
  138. period_number_line_edit = QtWidgets.QLineEdit()
  139. sample_rate_line_edit = QtWidgets.QLineEdit()
  140. pulse_width_combo_box = QtWidgets.QComboBox()
  141. pulse_period_combo_box = QtWidgets.QComboBox()
  142. delay1_combo_box = QtWidgets.QComboBox()
  143. delay2_combo_box = QtWidgets.QComboBox()
  144. sample_rate_box = QtWidgets.QComboBox()
  145. #Set sizing
  146. pulse_width_label.setFixedWidth(100)
  147. pulse_period_label.setFixedWidth(100)
  148. delay_label1.setFixedWidth(100)
  149. delay_label2.setFixedWidth(100)
  150. period_number_label.setFixedWidth(100)
  151. period_number_label_2.setFixedWidth(50)
  152. sample_rate_label.setFixedWidth(100)
  153. sample_rate_label_2.setFixedWidth(50)
  154. pulse_width_line_edit.setFixedWidth(50)
  155. pulse_period_line_edit.setFixedWidth(50)
  156. delay1_line_edit.setFixedWidth(50)
  157. delay2_line_edit.setFixedWidth(50)
  158. period_number_line_edit.setFixedWidth(50)
  159. sample_rate_box.setFixedWidth(50)
  160. pulse_width_combo_box.setFixedWidth(50)
  161. pulse_period_combo_box.setFixedWidth(50)
  162. delay1_combo_box.setFixedWidth(50)
  163. delay2_combo_box.setFixedWidth(50)
  164. sample_rate_line_edit.setFixedWidth(50)
  165. pulse_width_combo_box.insertItem(1,"uS")
  166. pulse_width_combo_box.insertItem(2,"mS")
  167. pulse_width_combo_box.insertItem(3,"S")
  168. pulse_period_combo_box.insertItem(1,"uS")
  169. pulse_period_combo_box.insertItem(2,"mS")
  170. pulse_period_combo_box.insertItem(3,"S")
  171. delay1_combo_box.insertItem(1,"nS")
  172. delay1_combo_box.insertItem(2,"uS")
  173. delay1_combo_box.insertItem(3,"mS")
  174. delay1_combo_box.insertItem(4,"S")
  175. delay2_combo_box.insertItem(1,"nS")
  176. delay2_combo_box.insertItem(2,"uS")
  177. delay2_combo_box.insertItem(3,"mS")
  178. delay2_combo_box.insertItem(4,"S")
  179. for i in range(2,21):
  180. sample_rate_box.insertItem(i, str(i))
  181. #Set fonts
  182. pulse_width_label.setFont(QFont("Open Sans",13))
  183. pulse_period_label.setFont(QFont("Open Sans",13))
  184. delay_label1.setFont(QFont("Open Sans",13))
  185. delay_label2.setFont(QFont("Open Sans",13))
  186. sample_rate_label.setFont(QFont("Open Sans",13))
  187. period_number_label.setFont(QFont("Open Sans",13))
  188. period_number_label_2.setFont(QFont("Open Sans",13))
  189. pulse_width_line_edit.setFont(QFont("Open Sans",12))
  190. pulse_period_line_edit.setFont(QFont("Open Sans",12))
  191. delay1_line_edit.setFont(QFont("Open Sans",12))
  192. delay2_line_edit.setFont(QFont("Open Sans",12))
  193. sample_rate_label_2.setFont(QFont("Open Sans",13))
  194. period_number_line_edit.setFont(QFont("Open Sans",13))
  195. pulse_width_combo_box.setFont(QFont("Open Sans",12))
  196. pulse_period_combo_box.setFont(QFont("Open Sans",12))
  197. delay1_combo_box.setFont(QFont("Open Sans",12))
  198. delay2_combo_box.setFont(QFont("Open Sans",12))
  199. sample_rate_box.setFont(QFont("Open Sans",13))
  200. # Создание кнопок
  201. btn1 = QtWidgets.QPushButton("Generate")
  202. btn2 = QtWidgets.QPushButton("Close")
  203. btn1.setFont(QFont("Open Sans",12))
  204. btn2.setFont(QFont("Open Sans",12))
  205. box = QtWidgets.QVBoxLayout()
  206. pulse_parameters_layout0 = QtWidgets.QHBoxLayout()
  207. pulse_parameters_layout1 = QtWidgets.QHBoxLayout()
  208. pulse_parameters_layout2 = QtWidgets.QHBoxLayout()
  209. pulse_parameters_layout3 = QtWidgets.QHBoxLayout()
  210. pulse_parameters_layout4 = QtWidgets.QHBoxLayout()
  211. pulse_parameters_layout5 = QtWidgets.QHBoxLayout()
  212. pulse_parameters_layout6 = QtWidgets.QHBoxLayout()
  213. # Добавление метки и кнопки в вертикальный блок
  214. pulse_parameters_layout0.addWidget(label)
  215. box.addLayout(pulse_parameters_layout0)
  216. pulse_parameters_layout1.addWidget(pulse_width_label)
  217. pulse_parameters_layout1.addWidget(pulse_width_line_edit)
  218. pulse_parameters_layout1.addWidget(pulse_width_combo_box)
  219. box.addLayout(pulse_parameters_layout1)
  220. pulse_parameters_layout2.addWidget(pulse_period_label)
  221. pulse_parameters_layout2.addWidget(pulse_period_line_edit)
  222. pulse_parameters_layout2.addWidget(pulse_period_combo_box)
  223. box.addLayout(pulse_parameters_layout2)
  224. pulse_parameters_layout3.addWidget(delay_label1)
  225. pulse_parameters_layout3.addWidget(delay1_line_edit)
  226. pulse_parameters_layout3.addWidget(delay1_combo_box)
  227. box.addLayout(pulse_parameters_layout3)
  228. pulse_parameters_layout6.addWidget(delay_label2)
  229. pulse_parameters_layout6.addWidget(delay2_line_edit)
  230. pulse_parameters_layout6.addWidget(delay2_combo_box)
  231. box.addLayout(pulse_parameters_layout6)
  232. pulse_parameters_layout5.addWidget(period_number_label)
  233. pulse_parameters_layout5.addWidget(period_number_line_edit)
  234. pulse_parameters_layout5.addWidget(period_number_label_2)
  235. box.addLayout(pulse_parameters_layout5)
  236. pulse_parameters_layout4.addWidget(sample_rate_label)
  237. pulse_parameters_layout4.addWidget(sample_rate_box)
  238. pulse_parameters_layout4.addWidget(sample_rate_label_2)
  239. box.addLayout(pulse_parameters_layout4)
  240. box.addWidget(btn1)
  241. box.addWidget(btn2)
  242. # Установка вертикального блока в качестве компоновщика главного окна
  243. window.setLayout(box)
  244. # Подключение обработчика события "clicked" кнопки к методу app.quit, вызывающему завершение приложения
  245. btn1.clicked.connect(set_params)
  246. btn2.clicked.connect(app.quit)
  247. # Отображение главного окна
  248. window.show()
  249. # Запуск основного цикла обработки событий приложения
  250. sys.exit(app.exec()) # После завершения цикла приложение выходит из программы