diff --git a/integration/Window2.py b/integration/Window2.py index e072a3106ccb7584209d61e475d05ed4b2fb103f..fc88a66605b3911c38ceba07ef4f9ec42791e321 100644 --- a/integration/Window2.py +++ b/integration/Window2.py @@ -12,9 +12,11 @@ import time import socket import os from PIL import Image, ImageTk +from time import sleep, time class Window: def __init__(self, root): + self.input_port='COM5' self.root = root self.root.title("Integration") self.ports = [port.device for port in serial.tools.list_ports.comports()] @@ -78,9 +80,9 @@ class Window: def initial_IMU(self): # Serial Port Setup - if'COM9' in self.ports:#port maybe different on different laptop + if self.input_port in self.ports:#port maybe different on different laptop - self.label2 = tk.Label(self.frame2, text="Port: COM9 ") + self.label2 = tk.Label(self.frame2, text=f"Port: {self.input_port} ") self.label2.place(relx=0.35, rely=0.8, anchor='center') self.label1 = tk.Label(self.frame2, text="click the Connect button to see the animation", @@ -192,9 +194,9 @@ class Window: def _initialise_EMG_graph(self): - if 'COM9' in self.ports:#port maybe different on different laptop + if self.input_port in self.ports:#port maybe different on different laptop - self.label2 = tk.Label(self.frame3, text="Port: COM9 ") + self.label2 = tk.Label(self.frame3, text=f"Port: {self.input_port} ") self.label2.place(relx=0.23, rely=0.8, anchor='center') self.label1 = tk.Label(self.frame3, text="click the Connect button to see the animation", @@ -281,17 +283,17 @@ class Window: def start_data_transmission(self): # Set the transmitting flag to True and start the update loop - if 'COM9' in self.ports: + if self.input_port in self.ports: if self.arduino==None: - self.arduino = serial.Serial('COM9', 9600) + self.arduino = serial.Serial(self.input_port, 9600) self.transmitting = True self.update_display() def start_EMG_data_transmission(self): # Set the transmitting flag to True and start the update loop - if 'COM9' in self.ports: + if self.input_port in self.ports: if self.arduino==None: - self.arduino = serial.Serial('COM9', 9600) + self.arduino = serial.Serial(self.input_port, 9600) self.EMG_transmitting = True self.EMG_Display() @@ -608,6 +610,7 @@ class WelcomeWindow: class trainingInterface: def __init__(self, root): + self.input_port='COM5' self.root = root self.root.title("preparation Interface") self.width = 1000 @@ -637,7 +640,7 @@ class trainingInterface: self.frame2.grid(row=1, column=0, padx=10, pady=10, sticky="nsew") self.initialEMGTraining() - if 'COM9' in self.ports: + if self.input_port in self.ports: self.emg_data_1 = [-1.0] * 41 self.emg_data_2 = [-1.0] * 41 @@ -649,7 +652,7 @@ class trainingInterface: self.gameStartButton = tk.Button(self.frame2, text="Start", command=self.startButton, width=15, height=2,font=("Helvetica", 12)) self.gameStartButton.place(relx=0.5, rely=0.5, anchor='center') - if 'COM9' not in self.ports: + if self.input_port not in self.ports: self.label=tk.Label(self.frame2, text="No EMG device found, Please check the hardware connection",font=("Helvetica", 15)) self.label.place(relx=0.5, rely=0.3, anchor='center') self.gameStartButton = tk.Button(self.frame2, text="Start", command=self.startButton, width=15, @@ -663,14 +666,14 @@ class trainingInterface: new_root.mainloop() def EMG_connect_HandOpen(self): - self.arduino_EMG = serial.Serial('COM9', 9600, timeout=1) + self.arduino_EMG = serial.Serial(self.input_port, 9600, timeout=1) gesture = "handOpen" self.start_countdown(11) self.displayAndsaveDate() def handCloseButton(self): - self.arduino_EMG = serial.Serial('COM9', 9600, timeout=1) + self.arduino_EMG = serial.Serial(self.input_port, 9600, timeout=1) gesture = "handOpen" self.start_countdown_close(11) self.displayAndsaveDate() @@ -889,6 +892,7 @@ class Algorithm: class gameScreen: def __init__(self, root): + self.input_port='COM5' self.root = root self.root.title("game Interface") self.width = 1000 @@ -915,8 +919,8 @@ class gameScreen: self.frame2 = tk.Frame(self.root, borderwidth=1, relief="solid", width=self.width, height=self.height * 1 / 2) self.frame2.grid(row=1, column=0, padx=10, pady=10, sticky="nsew") - if 'COM9' in self.ports : - self.arduino_EMG = serial.Serial('COM5', 9600, timeout=1) + if self.input_port in self.ports : + #self.arduino_EMG = serial.Serial(self.input_port, 9600, timeout=1) self.outer_EMG_label = tk.Label(self.frame2, text=f"EMG for Extensor Carpi Ulnaris is :") self.outer_EMG_label.config(font=("Arial", 12)) self.outer_EMG_label.place(relx=0.1, rely=0.2, anchor='w') @@ -936,12 +940,12 @@ class gameScreen: self.gesture_predict.config(font=("Arial", 12)) self.gesture_predict.place(relx=0.2, rely=0.7, anchor='w') self.a, self.b = self.load_Function() - self.emg_thread = threading.Thread(target=self.EMG_Display) - self.emg_thread.start() + #self.emg_thread = threading.Thread(target=self.EMG_Display) + # self.emg_thread.start() #self.EMG_Display() - if 'COM9' in self.ports: + if self.input_port in self.ports: self.column_limit = 9 self.last_averageRoll = 0 self.last_averageyaw = 0 @@ -951,7 +955,7 @@ class gameScreen: self.averageyaw = 0 self.averagepitch = 0 self.last_print_time = time() - self.arduino = serial.Serial('COM6', 115200) + self.arduino = serial.Serial(self.input_port, 115200) self.roll_label = tk.Label(self.frame1, text="roll is : ") self.roll_label.config(font=("Arial", 12)) self.roll_label.place(relx=0.2, rely=0.3, anchor='w') @@ -961,8 +965,9 @@ class gameScreen: self.yaw_label = tk.Label(self.frame1, text="yaw is : ") self.yaw_label.config(font=("Arial", 12)) self.yaw_label.place(relx=0.2, rely=0.5, anchor='w') - self.imu_thread = threading.Thread(target=self.IMU_Display) - self.imu_thread.start() + self.IMU_Display() + #self.imu_thread = threading.Thread(target=self.IMU_Display) + #self.imu_thread.start() #self.IMU_Display() @@ -989,7 +994,10 @@ class gameScreen: return self.adc_values - def EMG_Display(self): + + + ''' + def EMG_Display(self): try: while (self.arduino_EMG.inWaiting() > 0): data = self.arduino_EMG.readline() @@ -1088,6 +1096,93 @@ class gameScreen: except Exception as e: print("Error:", str(e)) + + ''' + + def IMU_Display(self): + try: + while (self.arduino.inWaiting() > 0): + dataPacket = self.arduino.readline() + dataPacket = dataPacket.decode() + cleandata = dataPacket.replace("\r\n", "") + row = cleandata.strip().split(',') + + + if len(row) == 10: + splitPacket = cleandata.split(',') + q0 = float(splitPacket[2]) # qw + q1 = float(splitPacket[3]) # qx + q2 = float(splitPacket[4]) # qy + q3 = float(splitPacket[5]) # qz + emg1 = float(splitPacket[0]) # First EMG sensor data + emg2 = float(splitPacket[1]) # Second EMG sensor data + #print(f"emg1: {emg1}, emg2: {emg2}") + data = [emg1, emg2] + predictions = self.predict(data, self.a, self.b) + ges_predictions = None + if predictions is not None: + if predictions == -1: + ges_predictions = "Hand Open" + if predictions == 1: + ges_predictions = "Hand Close" + if predictions == 0: + ges_predictions = "Unknown" + self.gesture_predict.config(text=f"{ges_predictions}") + self.outer_EMG_Number.config(text=f"{emg1}") + self.inner_EMG_Number.config(text=f"{emg2}") + self.send_command_to_unity(f"Hand :{ges_predictions}") + + + # Calculate Angles + roll = math.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2)) + pitch = -math.asin(2 * (q0 * q2 - q3 * q1)) + yaw = -math.atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3)) + + + self.roll_label.config( text="roll is : "+str(roll)) + self.pitch_label.config(text="pitch is : "+str(pitch)) + self.yaw_label.config(text="yaw is : "+str(yaw)) + #print(roll, pitch, yaw) + current_time = time() + if current_time - self.last_print_time >= 0.01: + + print(f"roll is: {roll}") + print(f"last roll is: {self.last_averageRoll}") + differ_roll = self.last_averageRoll - roll + print(f"differ roll is: {differ_roll}") + CalculatedAngle = differ_roll * 3000 / 2.5 + print(f"CalculatedAngle is: {CalculatedAngle}") + if (differ_roll) > 0: + self.send_command_to_unity(f"Command : down {CalculatedAngle}") + if (differ_roll) < 0: + self.send_command_to_unity(f"Command : up {-CalculatedAngle}") + + if (yaw < 0): + yaw = -yaw + + print(f"yaw is: {yaw}") + print(f"last yaw is: {self.last_averageyaw}") + differ_yaw = self.last_averageyaw - yaw + print(f"differ yaw is: {differ_yaw}") + yawAngle = differ_yaw * 90 / 2 + print(f"yawAngle is: {yawAngle}") + if (differ_yaw) < 0: + self.send_command_to_unity(f"Command : back {-yawAngle}") + if (differ_yaw) > 0: + self.send_command_to_unity(f"Command : roll {yawAngle}") + + self.last_print_time = current_time + self.last_averageRoll = roll + self.last_averageyaw = yaw + self.last_averagePitch = pitch + + + + except Exception as e: + print(f"An error occurred: {e}") + + # Call update_display() again after 50 milliseconds + self.update_display_id =self.root.after(1, self.IMU_Display) def load_Function(self,filename='trained.txt'):