Wall recordings to be tested

This commit is contained in:
Thijs Driessen 2025-11-21 12:00:30 +01:00
parent 3a2025e0db
commit 0c4774d23f

View File

@ -360,6 +360,11 @@
], ],
"execution_count": 63 "execution_count": 63
}, },
{
"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."
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -409,15 +414,33 @@
] ]
}, },
{ {
"metadata": {
"ExecuteTime": {
"end_time": "2025-11-21T10:39:53.853925Z",
"start_time": "2025-11-21T10:39:53.840058Z"
}
},
"cell_type": "code", "cell_type": "code",
"execution_count": null, "source": "serial.close()",
"metadata": {},
"outputs": [], "outputs": [],
"execution_count": 65
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2025-11-21T10:51:42.241019Z",
"start_time": "2025-11-21T10:51:37.333443Z"
}
},
"source": [ "source": [
"### Student Version ###\n", "### Student Version ###\n",
"from pathlib import Path\n",
"\n", "\n",
"# TODO: Open the serial connection to KITT, set the motor speed\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", "\n",
"# Initialize a list to store recorded data\n", "# Initialize a list to store recorded data\n",
"data = []\n", "data = []\n",
@ -428,22 +451,38 @@
"\n", "\n",
"while time.time() - start_time < recording_duration:\n", "while time.time() - start_time < recording_duration:\n",
" # Send the status command to get the distance readings\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", " \n",
" # Read the status response\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", " \n",
" # TODO: Extract the distance values from the status response\n", " # TODO: Extract the distance values from the status response\n",
"\n", "\n",
" dist_L = None\n", " dist_L = None\n",
" dist_R = None\n", " dist_R = None\n",
" \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", " # TODO: Record current time and distances\n",
"\n",
" current_time = time.time() - start_time\n",
" data.append([current_time, dist_L, dist_R])\n", " data.append([current_time, dist_L, dist_R])\n",
" \n", " \n",
" # Check if KITT is too close to the wall and stop if necessary\n", " # Check if KITT is too close to the wall and stop if necessary\n",
" if dist_L < 40 or dist_R < 40:\n", " if dist_L < 100 or dist_R < 100:\n",
" serial_port.write(b'M150\\n') # Stop the car\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", " print(\"Stopping KITT to avoid collision.\")\n",
" break # Exit the loop\n", " break # Exit the loop\n",
" # Note: you can also add a small loop here and still read the stopping data\n", " # Note: you can also add a small loop here and still read the stopping data\n",
@ -451,11 +490,24 @@
" time.sleep(0.1) # Wait before the next reading\n", " time.sleep(0.1) # Wait before the next reading\n",
"\n", "\n",
"# Close the serial connection\n", "# Close the serial connection\n",
"serial_port.close()\n", "serial.close()\n",
"\n", "\n",
"# TODO: Write the recorded data to a CSV file\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" "# Recommeded file output: Files/Recordings/kitt_distance_data_{speed}.csv"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stopping KITT to avoid collision.\n"
] ]
}
],
"execution_count": 74
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",