Compare commits

...

6 Commits

Author SHA1 Message Date
awe
47be2a137d Fix for web 2025-11-25 15:46:52 +03:00
awe
620bef8c88 initial commit 2025-11-24 15:57:57 +03:00
awe
c8b6aed434 remove junk from repo 2025-11-24 15:57:35 +03:00
awe
d3e39ec3b1 gitignore upd 2025-11-24 15:56:36 +03:00
809f19bea4 comments 2025-11-13 14:17:35 +00:00
dceae9e958 implemented adjusting window size. Now it should fit on 1024x768 screens, such as installed on new radar contrapion. (via codex) 2025-10-30 19:41:21 +03:00
12 changed files with 128 additions and 41 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.venv
*.pyc
__pycache__/
.env

4
README.md Normal file
View File

@ -0,0 +1,4 @@
files description:
deploy -- creates venv and installs python libs in it
run -- resets Generator_PCB by toggling PRi`s GPIO pin, activates venv and runs main program

Binary file not shown.

View File

@ -1,7 +1,7 @@
from enum import IntEnum
from serial import Serial
from serial.tools import list_ports
import device_conversion as cnv
from . import device_conversion as cnv
from datetime import datetime
#### ---- Constants
@ -154,12 +154,20 @@ def send_STATE(prt):
def get_STATE(prt):
''' Get decoded state of the device in byte format (2 bytes).
'''
import time
print("Received "+str(prt.inWaiting())+" bytes.")
if prt.inWaiting()!=2:
print("Error. Couldn't get STATE data. prt.inWaiting():", prt.inWaiting())
print("Flushing input data:", prt.read(prt.inWaiting()))
# print("Flushing input data:", prt.read(2), prt.read(2))
# Wait a bit more if data hasn't arrived yet
waiting_bytes = prt.inWaiting()
if waiting_bytes != 2:
# Give device a bit more time to respond
time.sleep(0.05)
waiting_bytes = prt.inWaiting()
print("Received "+str(waiting_bytes)+" bytes.")
if waiting_bytes != 2:
print("Error. Couldn't get STATE data. prt.inWaiting():", waiting_bytes)
if waiting_bytes > 0:
print("Flushing input data:", prt.read(waiting_bytes))
return None
out_bytes = prt.read(2)

View File

@ -3,7 +3,7 @@
import time
from datetime import datetime
import device_commands as cmd
from . import device_commands as cmd
#### ---- Constants
@ -68,20 +68,41 @@ def create_port_connection():
def reset_port_settings(prt):
# Reset port settings and check status
# First, flush any pending data in the input buffer
if prt.inWaiting() > 0:
flushed = prt.read(prt.inWaiting())
print(f"Flushed {len(flushed)} bytes before reset: {flushed.hex()}")
cmd.send_DEFAULT_ENABLE(prt)
time.sleep(WAIT_AFTER_SEND)
status = cmd.get_STATE(prt).hex()
if status is not None:
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
print("")
# Try multiple times with increasing delays to get response
max_attempts = 3
for attempt in range(max_attempts):
wait_time = WAIT_AFTER_SEND * (attempt + 1) # Increase wait time with each attempt
time.sleep(wait_time)
status_bytes = cmd.get_STATE(prt)
if status_bytes is not None:
status = status_bytes.hex()
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
print("")
return # Success
if attempt < max_attempts - 1:
print(f"Attempt {attempt + 1} failed, retrying with longer delay...")
# If all attempts failed, print warning but don't raise exception
print("Warning: Could not get STATE response after reset, but device may still be reset.")
print("")
def request_state(prt):
# Request data
cmd.send_STATE(prt)
time.sleep(WAIT_AFTER_SEND)
status = cmd.get_STATE(prt).hex()
if status is not None:
status_bytes = cmd.get_STATE(prt)
if status_bytes is not None:
status = status_bytes.hex()
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
print("")
@ -91,8 +112,9 @@ def send_control_parameters(prt, params):
hexstring = cmd.encode_Input(params)
cmd.send_DECODE_ENABLE(prt,hexstring)
time.sleep(WAIT_AFTER_SEND)
status = cmd.get_STATE(prt).hex()
if status is not None:
status_bytes = cmd.get_STATE(prt)
if status_bytes is not None:
status = status_bytes.hex()
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
print("")
else:
@ -103,8 +125,9 @@ def send_task_command(prt, sending_param):
hexstring = cmd.create_TaskEnableCommand(sending_param)
cmd.send_TASK_ENABLE(prt,hexstring)
time.sleep(WAIT_AFTER_SEND)
status = cmd.get_STATE(prt).hex()
if status is not None:
status_bytes = cmd.get_STATE(prt)
if status_bytes is not None:
status = status_bytes.hex()
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
print("")
else:
@ -114,9 +137,10 @@ def request_data(prt):
# Request data
cmd.send_TRANS_ENABLE(prt)
time.sleep(WAIT_AFTER_SEND)
data = cmd.get_DATA(prt).hex()
data_bytes = cmd.get_DATA(prt)
data_dict = []
if data is not None:
if data_bytes is not None:
data = data_bytes.hex()
data_dict = cmd.decode_DATA(data)
return data_dict

74
gui.py
View File

@ -7,6 +7,7 @@ import FreeSimpleGUI as sg
WINDOW_TITLE = 'Модуль управления лазерной схемой оптического смесителя (Отдел радиофотоники МФТИ)'
WINDOW_SIZE = [0, 0]
COMPACT_LAYOUT = False
SET_BUTTON_TEXT = 'Задать'
@ -53,32 +54,64 @@ READ_TEMPERATURE_TEXT = 'Температура лазера'
READ_CURRENT_TEXT = 'Ток фотодиода'
VOLTAGE_TEXT_WIDTH = 15
H_SEPARATOR_PAD = (1, 20)
OUTPUT_TEXT_PAD = (5, (20, 5))
WINDOW_MARGIN = (60, 90)
MIN_WINDOW_SIZE = (880, 660)
#### ---- Setting GUI
def get_screen_size():
global WINDOW_SIZE, GRAPH_CANVAS_SIZE, COMPACT_LAYOUT, SET_TEXT_WIDTH, SET_TEXT_WIDTH_NEW
global H_SEPARATOR_PAD, OUTPUT_TEXT_PAD
window = sg.Window('Test')
global WINDOW_SIZE
WINDOW_SIZE = window.get_screen_size()
screen_width, screen_height = window.get_screen_size()
window.close()
global GRAPH_CANVAS_SIZE
GRAPH_CANVAS_SIZE = (int(WINDOW_SIZE[0]/3.5), int(WINDOW_SIZE[0]/(3*2.75)))
COMPACT_LAYOUT = True
margin_w, margin_h = WINDOW_MARGIN
min_w, min_h = MIN_WINDOW_SIZE
window_width = min(screen_width, max(min_w, screen_width - margin_w))
window_height = min(screen_height, max(min_h, screen_height - margin_h))//2
WINDOW_SIZE = (window_width, window_height)
if COMPACT_LAYOUT:
SET_TEXT_WIDTH = 30
SET_TEXT_WIDTH_NEW = 34
graph_width = min(int(screen_width / 7.2), int(window_width / 5.6))
graph_height = max(90, int(screen_height / 16))
H_SEPARATOR_PAD = (1, 8)
OUTPUT_TEXT_PAD = (5, (8, 3))
else:
SET_TEXT_WIDTH = 34
SET_TEXT_WIDTH_NEW = 40
graph_width = int(screen_width / 4)
graph_height = int(screen_width / (3 * 3.5))
H_SEPARATOR_PAD = (1, 15)
OUTPUT_TEXT_PAD = (5, (15, 5))
graph_width = max(180, graph_width)
GRAPH_CANVAS_SIZE = (graph_width, graph_height)
return WINDOW_SIZE
def setup_gui(params):
sg.theme("DarkBlue12")
screen_size = get_screen_size()
window_size = get_screen_size()
layout_input_col1 = [[sg.Text(SET_TEMPERATURE_TEXT_1), sg.Push(),
layout_input_col1 = [[sg.Text(SET_TEMPERATURE_TEXT_1, size=(SET_TEXT_WIDTH, 1)), sg.Push(),
sg.Input(params['Temp_1'], disabled_readonly_background_color="Gray", size=(SET_INPUT_WIDTH,1), key='-InputT1-', disabled = True)],
[sg.Text(SET_CURRENT_TEXT_1), sg.Push(),
[sg.Text(SET_CURRENT_TEXT_1, size=(SET_TEXT_WIDTH, 1)), sg.Push(),
sg.Input(params['Iset_1'], disabled_readonly_background_color="Gray", size=(SET_INPUT_WIDTH,1), key='-InputI1-', disabled = True)],
[sg.HSeparator(pad=(1,20))],
[sg.HSeparator(pad=H_SEPARATOR_PAD)],
[sg.Push(), sg.Text(READ_TEMPERATURE_TEXT+' 1: ', key='-TOUT_1-')],
@ -87,19 +120,19 @@ def setup_gui(params):
# [sg.HSeparator(pad=(10,15), color=sg.theme_background_color())],
[sg.Push(), sg.Text(READ_CURRENT_TEXT+' 1: ', pad=(5, (20,5)), key='-IOUT_1-')],
[sg.Push(), sg.Text(READ_CURRENT_TEXT+' 1: ', pad=OUTPUT_TEXT_PAD, key='-IOUT_1-')],
[sg.Graph(canvas_size=GRAPH_CANVAS_SIZE, graph_bottom_left=(0, GRAPH_I_MIN), graph_top_right=(GRAPH_POINTS_NUMBER, GRAPH_I_MAX),
background_color=GRAPH_BG_COLOR, enable_events=False, drag_submits=False, key='-GraphI1-')]]
layout_input_col2 = [[sg.Text(SET_TEMPERATURE_TEXT_2), sg.Push(),
layout_input_col2 = [[sg.Text(SET_TEMPERATURE_TEXT_2, size=(SET_TEXT_WIDTH, 1)), sg.Push(),
sg.Input(params['Temp_2'], disabled_readonly_background_color="Gray", size=(SET_INPUT_WIDTH,1), key='-InputT2-', disabled = True)],
[sg.Text(SET_CURRENT_TEXT_2), sg.Push(),
[sg.Text(SET_CURRENT_TEXT_2, size=(SET_TEXT_WIDTH, 1)), sg.Push(),
sg.Input(params['Iset_2'], disabled_readonly_background_color="Gray", size=(SET_INPUT_WIDTH,1), key='-InputI2-', disabled = True)],
[sg.HSeparator(pad=(1,20))],
[sg.HSeparator(pad=H_SEPARATOR_PAD)],
[sg.Push(), sg.Text(READ_TEMPERATURE_TEXT+' 2: ', key='-TOUT_2-')],
@ -108,13 +141,13 @@ def setup_gui(params):
# [sg.HSeparator(pad=(10,15), color=sg.theme_background_color())],
[sg.Push(), sg.Text(READ_CURRENT_TEXT+' 2: ', pad=(5, (20,5)), key='-IOUT_2-')],
[sg.Push(), sg.Text(READ_CURRENT_TEXT+' 2: ', pad=OUTPUT_TEXT_PAD, key='-IOUT_2-')],
[sg.Graph(canvas_size=GRAPH_CANVAS_SIZE, graph_bottom_left=(0, GRAPH_I_MIN), graph_top_right=(GRAPH_POINTS_NUMBER, GRAPH_I_MAX),
background_color=GRAPH_BG_COLOR, enable_events=False, drag_submits=False, key='-GraphI2-')]]
layout_input_col3 = [
[sg.Text(SET_MANUAL_MODE_TEXT), sg.Checkbox('', default=False, key='-EnableManualSettings-')],
[sg.Text(SET_MANUAL_MODE_TEXT, size=(SET_TEXT_WIDTH_NEW, 1)), sg.Checkbox('', default=False, key='-EnableManualSettings-')],
[sg.Text(SET_MIN_TEMPERATURE_TEXT_1, size=(SET_TEXT_WIDTH_NEW,1)),
sg.Input(params['Min_Temp_1'], size=(SET_INPUT_WIDTH,1), key='-InputMinT1-', disabled=True, disabled_readonly_background_color="Gray"), sg.Checkbox('', default=False, key='-EnableT1-')],
@ -134,7 +167,7 @@ def setup_gui(params):
[sg.Text(SET_DELTA_CURRENT_TEXT_1, size=(SET_TEXT_WIDTH_NEW,1)),
sg.Input(params['Delta_Current_1'], size=(SET_INPUT_WIDTH,1), key='-InputDeltaC1-', disabled=True, disabled_readonly_background_color="Gray")],
[sg.HSeparator(pad=(1,20))],
[sg.HSeparator(pad=H_SEPARATOR_PAD)],
[sg.Text(SET_MIN_TEMPERATURE_TEXT_2, size=(SET_TEXT_WIDTH_NEW,1)),
sg.Input(params['Min_Temp_2'], size=(SET_INPUT_WIDTH,1), key='-InputMinT2-', disabled=True, disabled_readonly_background_color="Gray"), sg.Checkbox('', default=False, key='-EnableT2-')],
@ -154,7 +187,7 @@ def setup_gui(params):
[sg.Text(SET_DELTA_CURRENT_TEXT_2, size=(SET_TEXT_WIDTH_NEW,1)),
sg.Input(params['Delta_Current_2'], size=(SET_INPUT_WIDTH,1), key='-InputDeltaC2-', disabled=True, disabled_readonly_background_color="Gray")],
[sg.HSeparator(pad=(1,20))],
[sg.HSeparator(pad=H_SEPARATOR_PAD)],
[sg.Text(SET_DELTA_T_TEXT, size=(SET_TEXT_WIDTH_NEW,1)),
sg.Input(params['Delta_Time'], size=(SET_INPUT_WIDTH,1), key='-InputDeltaTime-', disabled=True, disabled_readonly_background_color="Gray")],
@ -162,16 +195,16 @@ def setup_gui(params):
[sg.Text(SET_TAU_T_TEXT, size=(SET_TEXT_WIDTH_NEW,1)),
sg.Input(params['Tau'], size=(SET_INPUT_WIDTH,1), key='-InputTau-', disabled=True, disabled_readonly_background_color="Gray")],
[sg.HSeparator(pad=(1,20))],
[sg.HSeparator(pad=H_SEPARATOR_PAD)],
[sg.Button(SET_START_BUTTON_TEXT, key='-StartCycle-', disabled_button_color=("Gray22", "Blue"), disabled=True), sg.Button(SET_STOP_BUTTON_TEXT, disabled_button_color=("Gray22", "Blue"), key='-StopCycle-', disabled=True)]]
layout = [[sg.Column(layout_input_col1), sg.VSeparator(), sg.Column(layout_input_col2), sg.VSeparator(), sg.Column(layout_input_col3)],
layout = [[sg.Column(layout_input_col1, pad=(0,0)), sg.VSeparator(pad=(4,0)), sg.Column(layout_input_col2, pad=(0,0)), sg.VSeparator(pad=(4,0)), sg.Column(layout_input_col3, pad=(0,0))],
[sg.HSeparator(pad=(25,10))],
[sg.Text('', size=(7,1)),
[sg.Text('', size=((3 if COMPACT_LAYOUT else 7),1)),
sg.Text('T терм 1:', size=(VOLTAGE_TEXT_WIDTH,1), key='-TTerm1-'), sg.Text('T терм 2:', size=(VOLTAGE_TEXT_WIDTH,1), key='-TTerm2-'),
sg.Text('3V3:', size=(VOLTAGE_TEXT_WIDTH,1), key='-3V3-'), sg.Text('5V1:', size=(VOLTAGE_TEXT_WIDTH,1), key='-5V1-'),
sg.Text('5V2:', size=(VOLTAGE_TEXT_WIDTH,1), key='-5V2-'), sg.Text('7V0:', size=(VOLTAGE_TEXT_WIDTH,1), key='-7V0-'),
@ -180,7 +213,7 @@ def setup_gui(params):
[sg.Exit('Выход', pad=(1,5), size=(10,1), key='-EXIT-')]]
window = sg.Window(WINDOW_TITLE, layout, finalize=True, element_justification='c', size=screen_size)
window = sg.Window(WINDOW_TITLE, layout, finalize=True, element_justification='c', size=window_size, resizable=True)
window.bind('<Escape>', '-EXIT-')
return window
@ -200,4 +233,3 @@ def sign_axes(window):
(window['-GraphI2-'].draw_text(text=str(GRAPH_I_MIN)+' мА', location=(4, GRAPH_I_MIN+(GRAPH_I_MAX-GRAPH_I_MIN)*0.05), color=GRAPH_SIGN_AXES_COLOR),
window['-GraphI2-'].draw_text(text=str(GRAPH_I_MAX)+' мА', location=(4, GRAPH_I_MAX-(GRAPH_I_MAX-GRAPH_I_MIN)*0.05), color=GRAPH_SIGN_AXES_COLOR))
return signs_dict

11
init_params.json Normal file
View File

@ -0,0 +1,11 @@
{
{INITIAL_TEMPERATURE_1 = 28 # Set initial temperature for Laser 1 in Celsius: from -1 to 45 C ??
INITIAL_TEMPERATURE_2 = 28.9 # Set initial temperature for Laser 2 in Celsius: from -1 to 45 C ??
INITIAL_CURRENT_1 = 33 # 64.0879 max # Set initial current for Laser 1, in mA
INITIAL_CURRENT_2 = 35 # 64.0879 max # Set initial current for Laser 2, in mA
GUI_TIMEOUT_INTERVAL = 5#505 - dev.WAIT_AFTER_SEND*1000 # GUI refresh time in milliseconds
SAVE_POINTS_NUMBER = 1000 # Number of most recent data points kept in memory
}

4
run
View File

@ -1,3 +1,7 @@
#!/usr/bin/bash
#reset generator PCB
# pinctrl set 26 op dl # drive PCB NRST LOW -> reset stm32
# pinctrl set 26 op dh # turn stm32 back ON
source .venv/bin/activate
python3 _device_main.py