Compare commits
18 Commits
678357b838
...
1a1f228b69
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a1f228b69 | |||
| a099481b3e | |||
| ffce232d47 | |||
| 87d012086b | |||
| 7456e7f02a | |||
| 9dbe3e1f5d | |||
| c8d8118d98 | |||
| 4b093e86de | |||
| 083767f626 | |||
| b081c33748 | |||
| eae3d4bae1 | |||
| 5531952667 | |||
| 48d347fb06 | |||
| be2a4a8c24 | |||
| 8efc607e03 | |||
| 6e717027c6 | |||
| 8686a8ef19 | |||
| deb7f54bfb |
86
main.py
86
main.py
@@ -11,6 +11,7 @@ import threading
|
|||||||
import time
|
import time
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import csv
|
import csv
|
||||||
|
import os
|
||||||
|
|
||||||
class ArduinoGUI:
|
class ArduinoGUI:
|
||||||
def __init__(self, root):
|
def __init__(self, root):
|
||||||
@@ -19,14 +20,7 @@ class ArduinoGUI:
|
|||||||
self.root.title("PRG 342 GUI")
|
self.root.title("PRG 342 GUI")
|
||||||
self.root.attributes('-fullscreen', True) # Setzt das Fenster in den Vollbildmodus
|
self.root.attributes('-fullscreen', True) # Setzt das Fenster in den Vollbildmodus
|
||||||
|
|
||||||
self.ser = serial.Serial(
|
self.ser = None
|
||||||
port='/dev/serial0',
|
|
||||||
baudrate=192000,
|
|
||||||
parity=serial.PARITY_NONE,
|
|
||||||
stopbits=serial.STOPBITS_ONE,
|
|
||||||
bytesize=serial.EIGHTBITS,
|
|
||||||
timeout=1
|
|
||||||
)
|
|
||||||
|
|
||||||
self.arduino = None
|
self.arduino = None
|
||||||
self.ports = list(serial.tools.list_ports.comports())
|
self.ports = list(serial.tools.list_ports.comports())
|
||||||
@@ -50,8 +44,6 @@ class ArduinoGUI:
|
|||||||
self.load_configurations()
|
self.load_configurations()
|
||||||
|
|
||||||
# Start the communication thread
|
# Start the communication thread
|
||||||
self.lsp_communication_tread = threading.Thread(target=self.lsp_communication_loop)
|
|
||||||
self.lsp_communication_tread.start()
|
|
||||||
self.communication_thread = threading.Thread(target=self.communication_loop)
|
self.communication_thread = threading.Thread(target=self.communication_loop)
|
||||||
self.communication_thread.start()
|
self.communication_thread.start()
|
||||||
|
|
||||||
@@ -169,6 +161,8 @@ class ArduinoGUI:
|
|||||||
self.current_angle = 0
|
self.current_angle = 0
|
||||||
self.current_torque = 0
|
self.current_torque = 0
|
||||||
self.analogInput = 0
|
self.analogInput = 0
|
||||||
|
self.currentSetpoint = 0
|
||||||
|
self.currentOutput = 0
|
||||||
|
|
||||||
self.angle_label_var = customtkinter.StringVar()
|
self.angle_label_var = customtkinter.StringVar()
|
||||||
self.angle_label_var.set("Drehwinkel: 0")
|
self.angle_label_var.set("Drehwinkel: 0")
|
||||||
@@ -252,6 +246,38 @@ class ArduinoGUI:
|
|||||||
messagebox.showerror("Fehler", str(e))
|
messagebox.showerror("Fehler", str(e))
|
||||||
else:
|
else:
|
||||||
messagebox.showwarning("Warnung", "Bitte einen COM-Port auswählen")
|
messagebox.showwarning("Warnung", "Bitte einen COM-Port auswählen")
|
||||||
|
|
||||||
|
def connect_lsp(self):
|
||||||
|
try:
|
||||||
|
ports = serial.tools.list_ports.comports()
|
||||||
|
used_ports = [port.device for port in ports]
|
||||||
|
|
||||||
|
serPort = None
|
||||||
|
|
||||||
|
if os.name == 'nt':
|
||||||
|
for i in range(1, 257):
|
||||||
|
port = f'(COM{i}'
|
||||||
|
if (port not in used_ports) and (serPort == None):
|
||||||
|
serPort = port
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
for i in range(256):
|
||||||
|
port = f'/dev/ttyS{i}'
|
||||||
|
if (port not in used_ports) and (serPort == None):
|
||||||
|
serPort = port
|
||||||
|
pass
|
||||||
|
for i in range(256):
|
||||||
|
port = f'/dev/ttyUSB{i}'
|
||||||
|
if (port not in used_ports) and (serPort == None):
|
||||||
|
serPort = port
|
||||||
|
pass
|
||||||
|
|
||||||
|
if serPort != None:
|
||||||
|
self.ser = serial.Serial(serPort, 9600, timeout=1)
|
||||||
|
print(F"Slave Port bereit an {serPort}")
|
||||||
|
except Exception as e:
|
||||||
|
print(F"Fehler beim Öffnen des Slave Ports an {serPort}")
|
||||||
|
pass
|
||||||
|
|
||||||
def set_setpoint(self):
|
def set_setpoint(self):
|
||||||
if self.arduino:
|
if self.arduino:
|
||||||
@@ -411,20 +437,42 @@ class ArduinoGUI:
|
|||||||
|
|
||||||
self.csv_writer.writerow([formatted_time, formatted_angle, formatted_setpoint, formatted_torque, formatted_output])
|
self.csv_writer.writerow([formatted_time, formatted_angle, formatted_setpoint, formatted_torque, formatted_output])
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
|
if self.ser:
|
||||||
|
if self.ser.in_waiting:
|
||||||
|
try:
|
||||||
|
command = self.ser.read(1).decode(errors='ignore').strip()
|
||||||
|
print(command)
|
||||||
|
if command == 'a':
|
||||||
|
message = f"{self.current_angle};{self.current_torque};{self.analogInput};{self.currentSetpoint};{self.currentOutput}\n"
|
||||||
|
self.ser.write(message.encode())
|
||||||
|
while self.ser.out_waiting:
|
||||||
|
pass
|
||||||
|
if command == 'w':
|
||||||
|
self.tare_angle()
|
||||||
|
if command == 'u':
|
||||||
|
time.sleep(5)
|
||||||
|
string = self.ser.read_until(b"u")
|
||||||
|
outCommand = F"u{string}u\n"
|
||||||
|
self.arduino.write(outCommand.encode())
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
self.ser.read_all()
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
#self.ser.write(b'Hallo\n')
|
||||||
|
else :
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
self.connect_lsp()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
def lsp_communication_loop(self):
|
|
||||||
while self.running:
|
|
||||||
if self.ser:
|
|
||||||
if self.ser.in_waiting:
|
|
||||||
command = self.ser.readline
|
|
||||||
self.ser.write(command)
|
|
||||||
|
|
||||||
|
|
||||||
def send_pid_parameters(self):
|
def send_pid_parameters(self):
|
||||||
if self.selected_pid in self.pid_params:
|
if self.selected_pid in self.pid_params:
|
||||||
pid_values = self.pid_params[self.selected_pid]
|
pid_values = self.pid_params[self.selected_pid]
|
||||||
@@ -444,6 +492,8 @@ class ArduinoGUI:
|
|||||||
self.running = False
|
self.running = False
|
||||||
if self.arduino:
|
if self.arduino:
|
||||||
self.arduino.close()
|
self.arduino.close()
|
||||||
|
if self.ser:
|
||||||
|
self.ser.close()
|
||||||
self.root.quit()
|
self.root.quit()
|
||||||
self.root.destroy()
|
self.root.destroy()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user