Functioning distance extraction code

This commit is contained in:
Thijs Driessen 2025-11-21 09:39:46 +01:00
parent 1b5cb5c3aa
commit 81786a1e17

View File

@ -19,9 +19,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "metadata": {
"metadata": {}, "ExecuteTime": {
"outputs": [], "end_time": "2025-11-21T08:01:53.138045Z",
"start_time": "2025-11-21T08:01:44.644007Z"
}
},
"source": [ "source": [
"# Import necessary libraries\n", "# Import necessary libraries\n",
"import time\n", "import time\n",
@ -33,15 +36,17 @@
"# Uncomment one of the following lines depending on your setup\n", "# Uncomment one of the following lines depending on your setup\n",
"\n", "\n",
"# If you are using the real car, uncomment the next lines and comment the simulator lines\n", "# If you are using the real car, uncomment the next lines and comment the simulator lines\n",
"# from serial import Serial\n", "from serial import Serial\n",
"# import sounddevice\n", "import sounddevice\n",
"\n", "\n",
"# If you are using the simulator, uncomment the next lines and comment the real car lines\n", "# If you are using the simulator, uncomment the next lines and comment the real car lines\n",
"from KITT_Simulator.serial_simulator import Serial\n", "# from KITT_Simulator.serial_simulator import Serial\n",
"from KITT_Simulator.sounddevice_simulator import sounddevice\n", "# from KITT_Simulator.sounddevice_simulator import sounddevice\n",
"\n", "\n",
"# Note: After changing the import statement, you need to restart the kernel for changes to take effect." "# Note: After changing the import statement, you need to restart the kernel for changes to take effect."
] ],
"outputs": [],
"execution_count": 2
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -99,22 +104,54 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "metadata": {
"metadata": {}, "ExecuteTime": {
"outputs": [], "end_time": "2025-11-21T08:34:56.275481Z",
"start_time": "2025-11-21T08:34:56.020404Z"
}
},
"source": [ "source": [
"### Student Version ###\n", "### Student Version ###\n",
"\n", "\n",
"# TODO: Establish a serial connection, ask for a status report, read it out, and print it\n", "# TODO: Establish a serial connection, ask for a status report, read it out, and print it\n",
"serial = Serial('/dev/rfcomm2', 115200)\n", "serial = Serial('COM4', 115200)\n",
"serial.write(b'S\\n')\n", "serial.write(b'Sd\\n')\n",
"status = serial.read_until(b'\\n')\n", "status = serial.read_until(b\"\\x04\")\n",
"status = status.decode('utf-8')\n", "status = status.decode('utf-8')\n",
"print(f\"Car status is:\\n\\n{status}\")\n", "print(f\"Car status is:\\n\\n{status}\")\n",
"\n", "\n",
"# TODO: Close the serial connection\n", "# TODO: Close the serial connection\n",
"serial.close()" "serial.close()"
] ],
"outputs": [
{
"ename": "SerialException",
"evalue": "could not open port 'COM4': PermissionError(13, 'Access is denied.', None, 5)",
"output_type": "error",
"traceback": [
"\u001B[31m---------------------------------------------------------------------------\u001B[39m",
"\u001B[31mSerialException\u001B[39m Traceback (most recent call last)",
"\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[31]\u001B[39m\u001B[32m, line 4\u001B[39m\n\u001B[32m 1\u001B[39m \u001B[38;5;66;03m### Student Version ###\u001B[39;00m\n\u001B[32m 2\u001B[39m \n\u001B[32m 3\u001B[39m \u001B[38;5;66;03m# TODO: Establish a serial connection, ask for a status report, read it out, and print it\u001B[39;00m\n\u001B[32m----> \u001B[39m\u001B[32m4\u001B[39m serial = \u001B[43mSerial\u001B[49m\u001B[43m(\u001B[49m\u001B[33;43m'\u001B[39;49m\u001B[33;43mCOM4\u001B[39;49m\u001B[33;43m'\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[32;43m115200\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[32m 5\u001B[39m serial.write(\u001B[33mb\u001B[39m\u001B[33m'\u001B[39m\u001B[33mSd\u001B[39m\u001B[38;5;130;01m\\n\u001B[39;00m\u001B[33m'\u001B[39m)\n\u001B[32m 6\u001B[39m status = serial.read_until(\u001B[33mb\u001B[39m\u001B[33m\"\u001B[39m\u001B[38;5;130;01m\\x04\u001B[39;00m\u001B[33m\"\u001B[39m)\n",
"\u001B[36mFile \u001B[39m\u001B[32m~\\PycharmProjects\\A.K.03\\.venv\\Lib\\site-packages\\serial\\serialwin32.py:33\u001B[39m, in \u001B[36mSerial.__init__\u001B[39m\u001B[34m(self, *args, **kwargs)\u001B[39m\n\u001B[32m 31\u001B[39m \u001B[38;5;28mself\u001B[39m._overlapped_read = \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[32m 32\u001B[39m \u001B[38;5;28mself\u001B[39m._overlapped_write = \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[32m---> \u001B[39m\u001B[32m33\u001B[39m \u001B[38;5;28;43msuper\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mSerial\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[43m)\u001B[49m\u001B[43m.\u001B[49m\u001B[34;43m__init__\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43m*\u001B[49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43m*\u001B[49m\u001B[43m*\u001B[49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n",
"\u001B[36mFile \u001B[39m\u001B[32m~\\PycharmProjects\\A.K.03\\.venv\\Lib\\site-packages\\serial\\serialutil.py:244\u001B[39m, in \u001B[36mSerialBase.__init__\u001B[39m\u001B[34m(self, port, baudrate, bytesize, parity, stopbits, timeout, xonxoff, rtscts, write_timeout, dsrdtr, inter_byte_timeout, exclusive, **kwargs)\u001B[39m\n\u001B[32m 241\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[33m'\u001B[39m\u001B[33munexpected keyword arguments: \u001B[39m\u001B[38;5;132;01m{!r}\u001B[39;00m\u001B[33m'\u001B[39m.format(kwargs))\n\u001B[32m 243\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m port \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[32m--> \u001B[39m\u001B[32m244\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43mopen\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n",
"\u001B[36mFile \u001B[39m\u001B[32m~\\PycharmProjects\\A.K.03\\.venv\\Lib\\site-packages\\serial\\serialwin32.py:64\u001B[39m, in \u001B[36mSerial.open\u001B[39m\u001B[34m(self)\u001B[39m\n\u001B[32m 62\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m._port_handle == win32.INVALID_HANDLE_VALUE:\n\u001B[32m 63\u001B[39m \u001B[38;5;28mself\u001B[39m._port_handle = \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;66;03m# 'cause __del__ is called anyway\u001B[39;00m\n\u001B[32m---> \u001B[39m\u001B[32m64\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m SerialException(\u001B[33m\"\u001B[39m\u001B[33mcould not open port \u001B[39m\u001B[38;5;132;01m{!r}\u001B[39;00m\u001B[33m: \u001B[39m\u001B[38;5;132;01m{!r}\u001B[39;00m\u001B[33m\"\u001B[39m.format(\u001B[38;5;28mself\u001B[39m.portstr, ctypes.WinError()))\n\u001B[32m 66\u001B[39m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[32m 67\u001B[39m \u001B[38;5;28mself\u001B[39m._overlapped_read = win32.OVERLAPPED()\n",
"\u001B[31mSerialException\u001B[39m: could not open port 'COM4': PermissionError(13, 'Access is denied.', None, 5)"
]
}
],
"execution_count": 31
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-11-21T08:34:46.482551Z",
"start_time": "2025-11-21T08:34:46.476951Z"
}
},
"cell_type": "code",
"source": "serial.close()",
"outputs": [],
"execution_count": 30
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -151,16 +188,20 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "metadata": {
"metadata": {}, "ExecuteTime": {
"outputs": [], "end_time": "2025-11-21T08:38:32.466922Z",
"start_time": "2025-11-21T08:38:32.458668Z"
}
},
"source": [ "source": [
"### Student Version ###\n", "### Student Version ###\n",
"\n", "\n",
"def extract_dis ():\n", "def extract_dis ():\n",
" # TODO: Decode the status response to a string\n", " # TODO: Decode the status response to a string\n",
" serial = Serial('COM4', 115200)\n",
" serial.write(b'S\\n')\n", " serial.write(b'S\\n')\n",
" _status = serial.read_until(b'\\n')\n", " _status = serial.read_until(b'\\x04')\n",
" _status = _status.decode('utf-8')\n", " _status = _status.decode('utf-8')\n",
"\n", "\n",
" # TODO: Split the status string into lines\n", " # TODO: Split the status string into lines\n",
@ -174,12 +215,12 @@
" for line in lines:\n", " for line in lines:\n",
" if \"Dist.\" in line:\n", " if \"Dist.\" in line:\n",
" # TODO: Split the line into words\n", " # TODO: Split the line into words\n",
" words = lines.split()\n", " words = line.split()\n",
" # Extract distance values based on their positions\n", " # Extract distance values based on their positions\n",
"\n", "\n",
" # Assign dist_L and dist_R accordingly\n", " # Assign dist_L and dist_R accordingly\n",
" dist_L =\n", " dist_L = words[3]\n",
" dist_R =\n", " dist_R = words[5]\n",
" break # Exit the loop after finding the distances\n", " break # Exit the loop after finding the distances\n",
"\n", "\n",
" # Print the extracted distance values\n", " # Print the extracted distance values\n",
@ -187,7 +228,40 @@
" print(f\"Right Distance: {dist_R}\")\n", " print(f\"Right Distance: {dist_R}\")\n",
"\n", "\n",
" return dist_L, dist_R" " return dist_L, dist_R"
] ],
"outputs": [],
"execution_count": 43
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-11-21T08:38:36.110680Z",
"start_time": "2025-11-21T08:38:34.392005Z"
}
},
"cell_type": "code",
"source": "extract_dis()",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Left Distance: 99\n",
"Right Distance: 51\n"
]
},
{
"data": {
"text/plain": [
"('99', '51')"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 44
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",