import tkinter as tk from tkinter import messagebox import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure import serial # Globale Variablen Kp = 0.0 Ki = 0.0 Kd = 0.0 arduino = None root = None torque_mode = None def open_serial_connection(): global arduino try: arduino = serial.Serial(port="COM4", baudrate=115200, timeout=1) print("Verbindung zu Arduino hergestellt.") except Exception as e: print(f"Fehler beim Öffnen der seriellen Verbindung: {e}") def close_application(): if messagebox.askokcancel("Schließen", "Wollen Sie die Anwendung wirklich schließen?"): if arduino and arduino.is_open: arduino.close() root.destroy() def create_diagram(plot, coordinates_text, canvas): plot.clear() plot.set_xlim(0, 90) plot.set_ylim(0, 50) # Beispielgrenzen für Drehmoment plot.set_xlabel('Drehwinkel in Grad') plot.set_ylabel('Drehmoment in Nm') coordinates = coordinates_text.get("1.0", tk.END).strip().split("\n") valid_coords = [coord for coord in coordinates if ',' in coord and len(coord.split(',')) == 2] if valid_coords: x_coords, y_coords = zip(*(map(float, coord.split(',')) for coord in valid_coords)) plot.plot(x_coords, y_coords, marker='o', linestyle='-') else: messagebox.showerror("Eingabefehler", "Bitte geben Sie gültige Koordinaten ein. Beispiel: '10,20'") canvas.draw() def send_command_to_arduino(command): if arduino and arduino.is_open: try: arduino.write(command.encode()) response = arduino.readline().decode().strip() print(f"Arduino Antwort: {response}") except Exception as e: print(f"Fehler beim Senden des Befehls {command}: {e}") def on_plot_button_click(torque_input, coordinates_text, plot, canvas): if torque_mode.get(): set_torque = float(torque_input.get()) command = f's{int(set_torque * 1000000):08x}' send_command_to_arduino(command) else: create_diagram(plot, coordinates_text, canvas) def setup_gui(): global root, torque_mode root = tk.Tk() root.title("Drehmoment-Regelungssystem") main_frame = tk.Frame(root) main_frame.pack(fill=tk.BOTH, expand=True) left_frame = tk.Frame(main_frame, borderwidth=2, relief=tk.GROOVE) left_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) right_frame = tk.Frame(main_frame, borderwidth=2, relief=tk.GROOVE) right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True) coordinates_label = tk.Label(left_frame, text="Koordinaten (Drehwinkel, Drehmoment):") coordinates_label.pack() coordinates_text = tk.Text(left_frame, height=5) coordinates_text.pack() torque_label = tk.Label(left_frame, text="Soll-Drehmoment in Nm:") torque_label.pack() torque_input = tk.Entry(left_frame) torque_input.pack() toggle_button = tk.Button(left_frame, text="Wechsel Drehmomentmodus", command=lambda: toggle_torque_mode(torque_input, coordinates_text)) toggle_button.pack() plot_button = tk.Button(left_frame, text="Eingaben übernehmen", command=lambda: on_plot_button_click(torque_input, coordinates_text, plot, canvas)) plot_button.pack() close_button = tk.Button(left_frame, text="Schließen", command=close_application) close_button.pack() figure = Figure(figsize=(5, 4), dpi=100) plot = figure.add_subplot(111) canvas = FigureCanvasTkAgg(figure, right_frame) canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True) torque_mode = tk.BooleanVar() torque_mode.set(False) def toggle_torque_mode(torque_input, coordinates_text): if torque_mode.get(): torque_input.config(state='normal') coordinates_text.config(state='disabled') else: torque_input.config(state='disabled') coordinates_text.config(state='normal') def main(): setup_gui() open_serial_connection() root.mainloop() if __name__ == '__main__': main()