mirror of
https://gitlab.ewi.tudelft.nl/ee2l1/2025-2026/A.K.03.git
synced 2025-12-12 15:00:55 +01:00
Started implementing the distance sensor code
This commit is contained in:
parent
6f99f06f5a
commit
f25fac0a3e
2
.idea/A.K.03.iml
generated
2
.idea/A.K.03.iml
generated
@ -4,7 +4,7 @@
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Python 3.13 (A.K.03)" jdkType="Python SDK" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.11 (A.K.03)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
<component name="PyDocumentationSettings">
|
||||
|
||||
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -3,5 +3,5 @@
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.13 (A.K.03)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13 (A.K.03)" project-jdk-type="Python SDK" />
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (A.K.03)" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
35
student_code/dist_func.py
Normal file
35
student_code/dist_func.py
Normal file
@ -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
|
||||
41
student_code/dist_obj.py
Normal file
41
student_code/dist_obj.py
Normal file
@ -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
|
||||
@ -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()
|
||||
@ -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<speed>\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<angle>\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()
|
||||
@ -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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user