mirror of
https://gitlab.ewi.tudelft.nl/ee2l1/2025-2026/A.K.03.git
synced 2025-12-12 16:00:56 +01:00
Wall recordings to be tested
This commit is contained in:
parent
9fda5604ea
commit
f0e1c993fb
@ -28,6 +28,7 @@
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import csv\n",
|
||||
"\n",
|
||||
"# Uncomment one of the following lines depending on your setup\n",
|
||||
"\n",
|
||||
@ -271,6 +272,11 @@
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "markdown",
|
||||
"source": "<font color=#6698FF> As the ultrasonic sensors refresh every 70 ms taking turns, every 140 ms will give new data. So we need to measure every 140 ms to get new data and not data stored in the buffer."
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "markdown",
|
||||
@ -319,6 +325,13 @@
|
||||
"- Also document the motor speed setting (e.g. use this as part of your file name)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"execution_count": null,
|
||||
"source": "serial.close()"
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
@ -326,9 +339,12 @@
|
||||
"execution_count": null,
|
||||
"source": [
|
||||
"### Student Version ###\n",
|
||||
"from pathlib import Path\n",
|
||||
"\n",
|
||||
"# TODO: Open the serial connection to KITT, set the motor speed\n",
|
||||
"\n",
|
||||
"serial = Serial('COM4', 115200)\n",
|
||||
"serial.write(b'D150\\n')\n",
|
||||
"serial.write(b'M160\\n')\n",
|
||||
"\n",
|
||||
"# Initialize a list to store recorded data\n",
|
||||
"data = []\n",
|
||||
@ -339,22 +355,38 @@
|
||||
"\n",
|
||||
"while time.time() - start_time < recording_duration:\n",
|
||||
" # Send the status command to get the distance readings\n",
|
||||
" serial_port.write(b'S\\n')\n",
|
||||
" serial.write(b'S\\n')\n",
|
||||
" \n",
|
||||
" # Read the status response\n",
|
||||
" status = serial_port.read_until(b'\\x04').decode('utf-8')\n",
|
||||
" status = serial.read_until(b'\\x04').decode('utf-8')\n",
|
||||
" \n",
|
||||
" # TODO: Extract the distance values from the status response\n",
|
||||
"\n",
|
||||
" dist_L = None\n",
|
||||
" dist_R = None\n",
|
||||
"\n",
|
||||
" lines = status.splitlines()\n",
|
||||
"\n",
|
||||
" for line in lines:\n",
|
||||
" if \"Dist.\" in line:\n",
|
||||
" words = line.split()\n",
|
||||
" # Extract distance values based on their positions\n",
|
||||
"\n",
|
||||
" # Assign dist_L and dist_R accordingly\n",
|
||||
" dist_L = int(words[3])\n",
|
||||
" dist_R = int(words[5])\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
" # TODO: Record current time and distances\n",
|
||||
"\n",
|
||||
" current_time = time.time() - start_time\n",
|
||||
" data.append([current_time, dist_L, dist_R])\n",
|
||||
" \n",
|
||||
" # Check if KITT is too close to the wall and stop if necessary\n",
|
||||
" if dist_L < 40 or dist_R < 40:\n",
|
||||
" serial_port.write(b'M150\\n') # Stop the car\n",
|
||||
" if dist_L < 100 or dist_R < 100:\n",
|
||||
" serial.write(b'M145\\n')\n",
|
||||
" time.sleep(0.5)\n",
|
||||
" serial.write(b'M150\\n') # Stop the car\n",
|
||||
" print(\"Stopping KITT to avoid collision.\")\n",
|
||||
" break # Exit the loop\n",
|
||||
" # Note: you can also add a small loop here and still read the stopping data\n",
|
||||
@ -362,9 +394,12 @@
|
||||
" time.sleep(0.1) # Wait before the next reading\n",
|
||||
"\n",
|
||||
"# Close the serial connection\n",
|
||||
"serial_port.close()\n",
|
||||
"serial.close()\n",
|
||||
"\n",
|
||||
"# TODO: Write the recorded data to a CSV file\n",
|
||||
"filepath = Path('kitt_wall_data_160.csv')\n",
|
||||
"df = pd.DataFrame(data,columns = [\"Time\",\"Distance_L\",\"Distance_R\"])\n",
|
||||
"df.to_csv(filepath,index=False)\n",
|
||||
"# Recommeded file output: Files/Recordings/kitt_distance_data_{speed}.csv"
|
||||
]
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user