diff --git a/.idea/A.K.03.iml b/.idea/A.K.03.iml index c56ca50..7133e2f 100644 --- a/.idea/A.K.03.iml +++ b/.idea/A.K.03.iml @@ -4,7 +4,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 822c310..f42a977 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/student_code/dist_func.py b/student_code/dist_func.py new file mode 100644 index 0000000..44b7222 --- /dev/null +++ b/student_code/dist_func.py @@ -0,0 +1,35 @@ +import time +from kitt import KITT + +def process(recording_duration): + + # Record data for a specified duration (e.g., 10 seconds) + start_time = time.time() + + while time.time() - start_time < recording_duration: + + status = KITT.get_distance_report() + lines = status.splitlines() + + # Initialize variables to hold distance values + dist_L = None + dist_R = None + + # Initialize a list to store recorded data + data = [] + + # Iterate over each line to find distance data + for line in lines: + if "Dist." in line: + words = line.split() + # Extract distance values based on their positions + + # Assign dist_L and dist_R accordingly + dist_L = words[3] + dist_R = words[5] + break # Exit the loop after finding the distances + + current_time = time.time() - start_time + data.append([current_time, dist_L, dist_R]) + + return data, dist_L, dist_R \ No newline at end of file diff --git a/student_code/dist_obj.py b/student_code/dist_obj.py new file mode 100644 index 0000000..0075816 --- /dev/null +++ b/student_code/dist_obj.py @@ -0,0 +1,41 @@ +import time +from kitt import KITT + +class DistanceSensor: + data = [] + + def __init__(self, data): + self.data = data0 + + def process(self, recording_duration): + + # Record data for a specified duration (e.g., 10 seconds) + start_time = time.time() + + while time.time() - start_time < recording_duration: + + status = KITT.get_distance_report() + lines = status.splitlines() + + # Initialize variables to hold distance values + dist_L = None + dist_R = None + + # Initialize a list to store recorded data + data = [] + + # Iterate over each line to find distance data + for line in lines: + if "Dist." in line: + words = line.split() + # Extract distance values based on their positions + + # Assign dist_L and dist_R accordingly + dist_L = words[3] + dist_R = words[5] + break # Exit the loop after finding the distances + + current_time = time.time() - start_time + data.append([current_time, dist_L, dist_R]) + + return data, dist_L, dist_R \ No newline at end of file diff --git a/student_code/distance_sensor.py b/student_code/distance_sensor.py deleted file mode 100644 index e69de29..0000000 diff --git a/student_code/car.py b/student_code/kitt.py similarity index 93% rename from student_code/car.py rename to student_code/kitt.py index 282f78c..fcaba05 100644 --- a/student_code/car.py +++ b/student_code/kitt.py @@ -74,6 +74,11 @@ class KITT: # Use the command 'A0\n' self.send_command(b"A0\n") + def get_distance_report(self): + self.send_command(b"Sd\n") + # self.serial.read_until(b'\x04').decode('utf-8') = status + return self.serial.read_until(b'\x04').decode('utf-8') + def toggle_beacon(self): if self.beacon_state: self.beacon_state = False @@ -87,5 +92,4 @@ class KITT: def close(self): # Close the serial connection - # self.serial.close() self.serial.close() diff --git a/student_code/kitt_main.py b/student_code/kitt_main.py deleted file mode 100644 index 282f78c..0000000 --- a/student_code/kitt_main.py +++ /dev/null @@ -1,91 +0,0 @@ -from serial import Serial -#from Manual.KITT_Simulator.serial_simulator import Serial - - -class KITT: - current_speed = 150 - current_angle = 150 - beacon_state = False - - def __init__(self, port, baudrate=115200): - # Initialize the serial connection - # self.serial = Serial(port, baudrate, rtscts=True) - self.serial = Serial(port, baudrate,rtscts=True) - # Initialize beacon parameters here using send_command - # Set carrier frequency, bit frequency, repetition count, and code pattern - carrier_frequency_value = 10000 - carrier_frequency = carrier_frequency_value.to_bytes(2, byteorder='big') - self.send_command(b"F"+carrier_frequency+b"\n") - - - bit_frequency_value = 5000 - bit_frequency = bit_frequency_value.to_bytes(2, byteorder='big') - self.send_command(b"B"+bit_frequency+b"\n") - - - desired_repetition_frequency = 2 # Replace with your value - repetition_count_value = bit_frequency_value // desired_repetition_frequency - repetition_count = repetition_count_value.to_bytes(2, byteorder='big') - # Ensure repetition_count_value is at least 32 - repetition_count_value = max(repetition_count_value, 32) - repetition_count = repetition_count_value.to_bytes(2, byteorder='big') - self.send_command(b"R"+repetition_count+b"\n") - - code_value = 0x67676767 # Replace with your code - code = code_value.to_bytes(4, byteorder='big') - self.send_command(b"C"+code+b"\n") - - def send_command(self, command): - # Send the command string over the serial connection - self.serial.write(command) - #print(command) - - def set_speed(self, speed): - # Send the motor speed command using send_command - # Use the format 'M\n' - self.send_command(f"M{speed}\n".encode()) - self.current_speed = speed - - def get_speed(self): - return self.current_speed - - def set_angle(self, angle): - # Send the steering angle command using send_command - # Use the format 'D\n' - self.send_command(f"D{angle}\n".encode()) - self.current_angle = angle - - def get_angle(self): - return self.current_angle - - def stop(self): - # Stop the car by setting speed and angle to neutral (150) - # Call set_speed and set_angle with 150 - self.set_speed(150) - self.set_angle(150) - - def start_beacon(self): - # Send commands to start the beacon - # Use the command 'A1\n' - self.send_command(b"A1\n") - - def stop_beacon(self): - # Send commands to stop the beacon - # Use the command 'A0\n' - self.send_command(b"A0\n") - - def toggle_beacon(self): - if self.beacon_state: - self.beacon_state = False - self.stop_beacon() - else: - self.beacon_state = True - self.start_beacon() - - def get_beacon_state(self): - return self.beacon_state - - def close(self): - # Close the serial connection - # self.serial.close() - self.serial.close() diff --git a/student_code/ui.py b/student_code/ui.py index 0add4d9..effe64b 100644 --- a/student_code/ui.py +++ b/student_code/ui.py @@ -2,10 +2,13 @@ import queue import threading import time import tkinter as tk -from car import KITT +from kitt import KITT import serial.tools.list_ports from pynput import keyboard +from dist_func import process +from student_code import dist_func + if __name__ == "__main__": serial_selector = tk.Tk() serial_selector.title("Select serial port") @@ -33,6 +36,8 @@ if __name__ == "__main__": root.title("Patatje Oorloog") root.geometry("800x600") + + # Speed speed_label = tk.Label(root, text=f"Speed {car.get_speed()}") speed_label.grid(row = 0, column = 0) @@ -48,6 +53,8 @@ if __name__ == "__main__": speed_set_btn = tk.Button(root, text="Set Speed", command=speed_btn) speed_set_btn.grid(row = 0, column = 2) + + # Steering angle_label = tk.Label(root, text=f"Angle {car.get_angle()}") angle_label.grid(row = 1, column = 0) @@ -55,16 +62,16 @@ if __name__ == "__main__": angle_entry = tk.Entry(root, textvariable=angle_var) angle_entry.grid(row=1, column=1) - def angle_btn(): global car car.set_angle(angle_var.get()) angle_label.config(text=f"Speed {car.get_angle()}") - angle_set_btn = tk.Button(root, text="Set Angle", command=angle_btn) angle_set_btn.grid(row=1, column=2) + + # Beacon beacon_label = tk.Label(root, text=f"Beacon {car.get_beacon_state()}") beacon_label.grid(row = 2, column = 0) @@ -76,6 +83,33 @@ if __name__ == "__main__": beacon_toggle = tk.Button(root, text="Toggle Beacon", command=toggle_beacon_btn) beacon_toggle.grid(row = 2, column = 1) + + # Recording time + rec_time_var = tk.StringVar() + rec_time_label = tk.Label(root, text=f"Rec Time {rec_time_var}") + rec_time_label.grid(row = 3, column = 0) + + rec_time_entry = tk.Entry(root, textvariable=rec_time_var) + rec_time_entry.grid(row=3, column=1) + + + # Distance sensor + sensor_label = tk.Label(root, text=f"Speed {dist_func.process(rec_time_var)}") + sensor_label.grid(row=0, column=0) + + sensor_var = tk.StringVar() + sensor_entry = tk.Entry(root, textvariable=sensor_var) + sensor_entry.grid(row=0, column=1) + + def sensor_btn(): + dist_func.process(rec_time_var) + sensor_label.config(text=f"Speed {dist_func.process(rec_time_var)}") + + sensor_set_btn = tk.Button(root, text="Request Sensor Reading", command=sensor_btn) + sensor_set_btn.grid(row=0, column=2) + + + # Close UI and disconnect serial connection def close_btn_func(): car.stop_beacon() car.stop()