Datalogger eingeführt.
This commit is contained in:
12
conf.txt
12
conf.txt
@@ -1,5 +1,15 @@
|
|||||||
150 Sekunden / 90 Grad{
|
227C-024-15 PID_Regler 1{
|
||||||
Kp:0.068
|
Kp:0.068
|
||||||
Ki:0.03
|
Ki:0.03
|
||||||
Kd:0.00
|
Kd:0.00
|
||||||
}
|
}
|
||||||
|
227C-024-15 PID_Regler 2{
|
||||||
|
Kp:0.11
|
||||||
|
Ki:0.08
|
||||||
|
Kd:0.00
|
||||||
|
}
|
||||||
|
227C-024-15 I-Regler{
|
||||||
|
Kp:0.00
|
||||||
|
Ki:0.02
|
||||||
|
Kd:0.00
|
||||||
|
}
|
||||||
75
main.py
75
main.py
@@ -10,6 +10,7 @@ from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import csv
|
||||||
|
|
||||||
class ArduinoGUI:
|
class ArduinoGUI:
|
||||||
def __init__(self, root):
|
def __init__(self, root):
|
||||||
@@ -43,6 +44,38 @@ class ArduinoGUI:
|
|||||||
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()
|
||||||
|
|
||||||
|
self.logging = False
|
||||||
|
self.start_time = None
|
||||||
|
self.log_file = None
|
||||||
|
self.csv_writer = None
|
||||||
|
|
||||||
|
def toggle_datalogger(self):
|
||||||
|
if not self.logging:
|
||||||
|
self.start_logging()
|
||||||
|
else:
|
||||||
|
self.stop_logging()
|
||||||
|
|
||||||
|
def start_logging(self):
|
||||||
|
self.logging = True
|
||||||
|
self.start_time = time.time()
|
||||||
|
self.log_file = open("Data.csv", mode='w', newline='', encoding='utf-8')
|
||||||
|
self.csv_writer = csv.writer(self.log_file, delimiter='\t')
|
||||||
|
|
||||||
|
# PID parameters under each other
|
||||||
|
self.csv_writer.writerow(["Kp", f"{self.pid_params[self.selected_pid]['Kp']:.3f}".replace('.', ',')])
|
||||||
|
self.csv_writer.writerow(["Ki", f"{self.pid_params[self.selected_pid]['Ki']:.3f}".replace('.', ',')])
|
||||||
|
self.csv_writer.writerow(["Kd", f"{self.pid_params[self.selected_pid]['Kd']:.3f}".replace('.', ',')])
|
||||||
|
|
||||||
|
self.csv_writer.writerow(["Zeit in s", "Winkel in Grad", "Sollwert in Nm", "Istwert in Nm", "Erregerspannung in V"])
|
||||||
|
messagebox.showinfo("Info", "Datalogging gestartet")
|
||||||
|
|
||||||
|
|
||||||
|
def stop_logging(self):
|
||||||
|
self.logging = False
|
||||||
|
if self.log_file:
|
||||||
|
self.log_file.close()
|
||||||
|
messagebox.showinfo("Info", "Datalogging gestoppt")
|
||||||
|
|
||||||
def create_widgets(self):
|
def create_widgets(self):
|
||||||
|
|
||||||
# Labels and ComboBox for Stellantriebstyp
|
# Labels and ComboBox for Stellantriebstyp
|
||||||
@@ -106,6 +139,10 @@ class ArduinoGUI:
|
|||||||
self.exit_button = customtkinter.CTkButton(self.root, text="Beenden", command=self.on_closing, font=self.my_font)
|
self.exit_button = customtkinter.CTkButton(self.root, text="Beenden", command=self.on_closing, font=self.my_font)
|
||||||
self.exit_button.grid(row=5, column=2, padx=(0, 10), pady=(10, 0), sticky="sew")
|
self.exit_button.grid(row=5, column=2, padx=(0, 10), pady=(10, 0), sticky="sew")
|
||||||
|
|
||||||
|
#Datalogger
|
||||||
|
self.datalogger_button = customtkinter.CTkButton(self.root, text="Datenlogger", command=self.toggle_datalogger, font=self.my_font)
|
||||||
|
self.datalogger_button.grid(row=4, column=0, padx=(0, 10), pady=(10, 0), sticky="sew")
|
||||||
|
|
||||||
# Plot
|
# Plot
|
||||||
self.figure, self.ax = plt.subplots()
|
self.figure, self.ax = plt.subplots()
|
||||||
self.canvas = FigureCanvasTkAgg(self.figure, self.root)
|
self.canvas = FigureCanvasTkAgg(self.figure, self.root)
|
||||||
@@ -336,20 +373,34 @@ class ArduinoGUI:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
data = self.arduino.readline().decode(errors='ignore').strip().split(";")
|
data = self.arduino.readline().decode(errors='ignore').strip().split(";")
|
||||||
self.current_angle = float(data[0]) / 1000
|
if len(data) >= 4 and all(data): # Ensure all fields are not empty
|
||||||
self.current_torque = float(data[1]) / 1000
|
self.current_angle = float(data[0]) / 1000 if data[0] else 0
|
||||||
self.analogInput = float(data[2]) / 1000
|
self.current_torque = float(data[1]) / 1000 if data[1] else 0
|
||||||
self.currentSetpoint = float(data[3]) / 1000
|
self.analogInput = float(data[2]) / 1000 if data[2] else 0
|
||||||
|
self.currentSetpoint = float(data[3]) / 1000 if data[3] else 0
|
||||||
|
self.currentOutput = float(data[4]) * 0.006349206 if data[4] else 0
|
||||||
|
|
||||||
|
self.angle_label_var.set(f"Drehwinkel: {self.current_angle:.1f} °")
|
||||||
|
self.torque_label_var.set(f"Istwert: {self.current_torque:.1f} Nm")
|
||||||
|
self.analogread_label_var.set(f"Analogeingang: {self.analogInput:.1f} V")
|
||||||
|
self.currentSetpoint_label_var.set(f"Aktueller Sollwert: {self.currentSetpoint:.1f} Nm")
|
||||||
|
|
||||||
|
if self.current_point is not None:
|
||||||
|
self.current_point.remove()
|
||||||
|
self.current_point, = self.ax.plot([self.current_angle], [self.current_torque], 'ro')
|
||||||
|
self.canvas.draw()
|
||||||
|
|
||||||
|
if self.logging:
|
||||||
|
elapsed_time = time.time() - self.start_time
|
||||||
|
formatted_time = f"{elapsed_time:.3f}".replace('.', ',')
|
||||||
|
formatted_angle = f"{self.current_angle:.3f}".replace('.', ',')
|
||||||
|
formatted_setpoint = f"{self.currentSetpoint:.3f}".replace('.', ',')
|
||||||
|
formatted_torque = f"{self.current_torque:.3f}".replace('.', ',')
|
||||||
|
formatted_output = f"{self.currentOutput:.3f}".replace('.', ',')
|
||||||
|
|
||||||
|
self.csv_writer.writerow([formatted_time, formatted_angle, formatted_setpoint, formatted_torque, formatted_output])
|
||||||
|
|
||||||
self.angle_label_var.set(f"Drehwinkel: {self.current_angle:.1f} °")
|
|
||||||
self.torque_label_var.set(f"Istwert: {self.current_torque:.1f} Nm")
|
|
||||||
self.analogread_label_var.set(f"Analogeingang: {self.analogInput:.1f} V")
|
|
||||||
self.currentSetpoint_label_var.set(f"Aktueller Sollwert: {self.currentSetpoint:.1f} Nm")
|
|
||||||
|
|
||||||
if self.current_point is not None:
|
|
||||||
self.current_point.remove()
|
|
||||||
self.current_point, = self.ax.plot([self.current_angle], [self.current_torque], 'ro')
|
|
||||||
self.canvas.draw()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user