feature_lsp_com #8

Merged
musabe24 merged 17 commits from feature_lsp_com into main 2024-07-18 12:26:59 +02:00
2 changed files with 76 additions and 2899 deletions

2881
Data.csv

File diff suppressed because it is too large Load Diff

86
main.py
View File

@@ -11,6 +11,7 @@ import threading
import time
import numpy as np
import csv
import os
class ArduinoGUI:
def __init__(self, root):
@@ -19,14 +20,7 @@ class ArduinoGUI:
self.root.title("PRG 342 GUI")
self.root.attributes('-fullscreen', True) # Setzt das Fenster in den Vollbildmodus
self.ser = serial.Serial(
port='/dev/serial0',
baudrate=192000,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
self.ser = None
self.arduino = None
self.ports = list(serial.tools.list_ports.comports())
@@ -50,8 +44,6 @@ class ArduinoGUI:
self.load_configurations()
# 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.start()
@@ -169,6 +161,8 @@ class ArduinoGUI:
self.current_angle = 0
self.current_torque = 0
self.analogInput = 0
self.currentSetpoint = 0
self.currentOutput = 0
self.angle_label_var = customtkinter.StringVar()
self.angle_label_var.set("Drehwinkel: 0")
@@ -253,6 +247,38 @@ class ArduinoGUI:
else:
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):
if self.arduino:
try:
@@ -411,20 +437,42 @@ class ArduinoGUI:
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:
print(e)
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):
if self.selected_pid in self.pid_params:
pid_values = self.pid_params[self.selected_pid]
@@ -444,6 +492,8 @@ class ArduinoGUI:
self.running = False
if self.arduino:
self.arduino.close()
if self.ser:
self.ser.close()
self.root.quit()
self.root.destroy()