From b0a21852cb238116de437a176712721d24f0b8d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicholas=20St=C4=83nescu?= Date: Fri, 21 Nov 2025 11:30:32 +0100 Subject: [PATCH] fully working :tada: --- Manual/3_Module_1.ipynb | 221 ++++++++++++++++++++++++++++------------ 1 file changed, 154 insertions(+), 67 deletions(-) diff --git a/Manual/3_Module_1.ipynb b/Manual/3_Module_1.ipynb index 2cc342e..a931145 100644 --- a/Manual/3_Module_1.ipynb +++ b/Manual/3_Module_1.ipynb @@ -21,7 +21,12 @@ ] }, { - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2025-11-21T09:42:15.154065Z", + "start_time": "2025-11-21T09:42:15.133482Z" + } + }, "cell_type": "code", "source": [ "# Import necessary libraries\n", @@ -30,15 +35,15 @@ "# Uncomment one of the following lines depending on your setup\n", "\n", "# If you are using the real car, uncomment the next line and comment the simulator line\n", - "#from serial import Serial\n", + "from serial import Serial\n", "\n", "# If you are using the simulator, uncomment the next line and comment the real car line\n", - "from Manual.KITT_Simulator.serial_simulator import Serial\n", + "#from Manual.KITT_Simulator.serial_simulator import Serial\n", "\n", "# Note: After changing the import statement, you need to restart the kernel for changes to take effect." ], "outputs": [], - "execution_count": null + "execution_count": 8 }, { "cell_type": "markdown", @@ -165,7 +170,12 @@ ] }, { - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2025-11-21T09:38:49.876306Z", + "start_time": "2025-11-21T09:38:33.768247Z" + } + }, "cell_type": "code", "source": [ "### Student Version: play with this! ###\n", @@ -204,8 +214,35 @@ "# Close the serial connection (important!)\n", "serial.close()" ], - "outputs": [], - "execution_count": null + "outputs": [ + { + "data": { + "text/plain": [ + "Canvas(height=520, width=520)" + ], + "application/vnd.jupyter.widget-view+json": { + "version_major": 2, + "version_minor": 0, + "model_id": "2d74e35cf2874410947b571c2686a44f" + } + }, + "metadata": {}, + "output_type": "display_data", + "jetTransient": { + "display_id": null + } + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Motors are ON\n", + "Car is outside the field boundaries!\n", + "Motors are OFF\n" + ] + } + ], + "execution_count": 2 }, { "cell_type": "markdown", @@ -265,7 +302,12 @@ ] }, { - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2025-11-21T09:56:34.612180Z", + "start_time": "2025-11-21T09:56:29.551624Z" + } + }, "cell_type": "code", "source": [ "### Student Version ###\n", @@ -279,6 +321,7 @@ "carrier_frequency_value = 10000 # Replace with your value\n", "# Convert the frequency to bytes (2 bytes, big endian)\n", "carrier_frequency = carrier_frequency_value.to_bytes(2, byteorder='big')\n", + "\n", "# TODO: Send the 'F' command with the carrier frequency\n", "serial.write(b\"\".join([b\"F\",carrier_frequency,b\"\\n\"]))\n", "# Set bit frequency\n", @@ -327,8 +370,17 @@ "# TODO: Very Important! Close the serial connection\n", "serial.close()\n" ], - "outputs": [], - "execution_count": null + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Beacon is ON\n", + "Beacon is OFF\n" + ] + } + ], + "execution_count": 37 }, { "metadata": {}, @@ -378,7 +430,12 @@ ] }, { - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2025-11-21T09:38:55.176015Z", + "start_time": "2025-11-21T09:38:55.092901Z" + } + }, "cell_type": "code", "source": [ "### Student Version ###\n", @@ -397,8 +454,50 @@ "serial.close()\n", "# TODO: Close the serial connection (important!)" ], - "outputs": [], - "execution_count": null + "outputs": [ + { + "data": { + "text/plain": [ + "Canvas(height=520, width=520)" + ], + "application/vnd.jupyter.widget-view+json": { + "version_major": 2, + "version_minor": 0, + "model_id": "3017800da43140a2b518e020dcce5af4" + } + }, + "metadata": {}, + "output_type": "display_data", + "jetTransient": { + "display_id": null + } + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Car status is:\n", + "\n", + "**************************\n", + "* Audio Beacon: off\n", + "* c: 0xabcdef00\n", + "* f_c: 5678\n", + "* f_b: 1234\n", + "* c_r: 1337\n", + "**************************\n", + "* PWM:\n", + "* Dir. 150\n", + "* Mot. 150\n", + "**************************\n", + "* Sensors:\n", + "* Dist. L 0 R 0\n", + "* V_batt 11.5 V\n", + "**************************\n", + "\u0004\n" + ] + } + ], + "execution_count": 4 }, { "cell_type": "markdown", @@ -466,8 +565,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-11-19T20:47:25.030226Z", - "start_time": "2025-11-19T20:47:25.017941Z" + "end_time": "2025-11-21T10:27:10.276766Z", + "start_time": "2025-11-21T10:27:10.272096Z" } }, "cell_type": "code", @@ -483,12 +582,12 @@ " # Set carrier frequency, bit frequency, repetition count, and code pattern\n", " carrier_frequency_value = 10000\n", " carrier_frequency = carrier_frequency_value.to_bytes(2, byteorder='big')\n", - " self.send_command(str(b\"\".join([b\"F\",carrier_frequency,b\"\\n\"])))\n", + " self.send_command(b\"F\"+carrier_frequency+b\"\\n\")\n", "\n", "\n", " bit_frequency_value = 5000\n", " bit_frequency = bit_frequency_value.to_bytes(2, byteorder='big')\n", - " self.send_command(str(b\"\".join([b\"B\",bit_frequency,b\"\\n\"])))\n", + " self.send_command(b\"B\"+bit_frequency+b\"\\n\")\n", "\n", "\n", " desired_repetition_frequency = 2 # Replace with your value\n", @@ -497,27 +596,27 @@ " # Ensure repetition_count_value is at least 32\n", " repetition_count_value = max(repetition_count_value, 32)\n", " repetition_count = repetition_count_value.to_bytes(2, byteorder='big')\n", - " self.send_command(str(b\"\".join([b\"R\",repetition_count,b\"\\n\"])))\n", + " self.send_command(b\"R\"+repetition_count+b\"\\n\")\n", "\n", - " code_value = 0x99999999 # Replace with your code\n", + " code_value = 0x67676767 # Replace with your code\n", " code = code_value.to_bytes(4, byteorder='big')\n", - " self.send_command(str(b\"\".join([b\"C\",code,b\"\\n\"])))\n", + " self.send_command(b\"C\"+code+b\"\\n\")\n", "\n", " def send_command(self, command):\n", " # Send the command string over the serial connection\n", - " self.serial.write(command.encode())\n", - "\n", + " self.serial.write(command)\n", + " print(command)\n", "\n", " def set_speed(self, speed):\n", " # Send the motor speed command using send_command\n", " # Use the format 'M\\n'\n", - " self.send_command(f\"M{speed}\\n\")\n", + " self.send_command(f\"M{speed}\\n\".encode())\n", "\n", "\n", " def set_angle(self, angle):\n", " # Send the steering angle command using send_command\n", " # Use the format 'D\\n'\n", - " self.send_command(f\"D{angle}\\n\")\n", + " self.send_command(f\"D{angle}\\n\".encode())\n", "\n", "\n", " def stop(self):\n", @@ -529,12 +628,12 @@ " def start_beacon(self):\n", " # Send commands to start the beacon\n", " # Use the command 'A1\\n'\n", - " self.send_command(\"A1\\n\")\n", + " self.send_command(b\"A1\\n\")\n", "\n", " def stop_beacon(self):\n", " # Send commands to stop the beacon\n", " # Use the command 'A0\\n'\n", - " self.send_command(\"A0\\n\")\n", + " self.send_command(b\"A0\\n\")\n", "\n", " def close(self):\n", " # Close the serial connection\n", @@ -542,13 +641,13 @@ " self.serial.close()\n" ], "outputs": [], - "execution_count": 20 + "execution_count": 44 }, { "metadata": { "ExecuteTime": { - "end_time": "2025-11-19T20:47:26.839827Z", - "start_time": "2025-11-19T20:47:26.826565Z" + "end_time": "2025-11-21T10:27:11.326406Z", + "start_time": "2025-11-21T10:27:11.323674Z" } }, "cell_type": "code", @@ -578,7 +677,6 @@ " elif command == \"r\":\n", " kitt.stop_beacon()\n", " elif command == \"q\":\n", - " kitt.close()\n", " break\n", "\n", " # TODO: depending on the command, adjust the car speed, steering angle, or start/stop the beacon variable\n", @@ -600,13 +698,13 @@ " kitt.close()" ], "outputs": [], - "execution_count": 21 + "execution_count": 45 }, { "metadata": { "ExecuteTime": { - "end_time": "2025-11-19T20:47:41.559047Z", - "start_time": "2025-11-19T20:47:30.566425Z" + "end_time": "2025-11-21T10:29:28.965385Z", + "start_time": "2025-11-21T10:29:01.281710Z" } }, "cell_type": "code", @@ -622,46 +720,35 @@ " wasd_control(kitt)" ], "outputs": [ - { - "data": { - "text/plain": [ - "Canvas(height=520, width=520)" - ], - "application/vnd.jupyter.widget-view+json": { - "version_major": 2, - "version_minor": 0, - "model_id": "bf6b02cc69244330a431bf392738a083" - } - }, - "metadata": {}, - "output_type": "display_data", - "jetTransient": { - "display_id": null - } - }, { "name": "stdout", "output_type": "stream", "text": [ - "Control the car with W (forward), S (backward), A (left), D (right), E (start beacon), R (stop beacon), and Q (to exit).\n" - ] - }, - { - "ename": "KeyboardInterrupt", - "evalue": "Interrupted by user", - "output_type": "error", - "traceback": [ - "\u001B[31m---------------------------------------------------------------------------\u001B[39m", - "\u001B[31mKeyboardInterrupt\u001B[39m Traceback (most recent call last)", - "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[22]\u001B[39m\u001B[32m, line 9\u001B[39m\n\u001B[32m 6\u001B[39m kitt = KITT(\u001B[33m'\u001B[39m\u001B[33m/dev/rfcomm2\u001B[39m\u001B[33m'\u001B[39m) \u001B[38;5;66;03m# Replace with the actual port\u001B[39;00m\n\u001B[32m 8\u001B[39m \u001B[38;5;66;03m# Start the control function\u001B[39;00m\n\u001B[32m----> \u001B[39m\u001B[32m9\u001B[39m \u001B[43mwasd_control\u001B[49m\u001B[43m(\u001B[49m\u001B[43mkitt\u001B[49m\u001B[43m)\u001B[49m\n", - "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[21]\u001B[39m\u001B[32m, line 12\u001B[39m, in \u001B[36mwasd_control\u001B[39m\u001B[34m(kitt)\u001B[39m\n\u001B[32m 9\u001B[39m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[32m 10\u001B[39m \u001B[38;5;28;01mwhile\u001B[39;00m \u001B[38;5;28;01mTrue\u001B[39;00m:\n\u001B[32m 11\u001B[39m \u001B[38;5;66;03m# Read input from the user\u001B[39;00m\n\u001B[32m---> \u001B[39m\u001B[32m12\u001B[39m command = \u001B[38;5;28;43minput\u001B[39;49m\u001B[43m(\u001B[49m\u001B[33;43m\"\u001B[39;49m\u001B[33;43mEnter command: \u001B[39;49m\u001B[33;43m\"\u001B[39;49m\u001B[43m)\u001B[49m.lower()\n\u001B[32m 13\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m command == \u001B[33m\"\u001B[39m\u001B[33mw\u001B[39m\u001B[33m\"\u001B[39m:\n\u001B[32m 14\u001B[39m car_speed = car_speed + \u001B[32m5\u001B[39m\n", - "\u001B[36mFile \u001B[39m\u001B[32m~/Documents/EE/Y2/IP3/A.K.03/.venv/lib/python3.13/site-packages/ipykernel/kernelbase.py:1396\u001B[39m, in \u001B[36mKernel.raw_input\u001B[39m\u001B[34m(self, prompt)\u001B[39m\n\u001B[32m 1394\u001B[39m msg = \u001B[33m\"\u001B[39m\u001B[33mraw_input was called, but this frontend does not support input requests.\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m 1395\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m StdinNotImplementedError(msg)\n\u001B[32m-> \u001B[39m\u001B[32m1396\u001B[39m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43m_input_request\u001B[49m\u001B[43m(\u001B[49m\n\u001B[32m 1397\u001B[39m \u001B[43m \u001B[49m\u001B[38;5;28;43mstr\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mprompt\u001B[49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m 1398\u001B[39m \u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43m_get_shell_context_var\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43m_shell_parent_ident\u001B[49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m 1399\u001B[39m \u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43mget_parent\u001B[49m\u001B[43m(\u001B[49m\u001B[33;43m\"\u001B[39;49m\u001B[33;43mshell\u001B[39;49m\u001B[33;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[32m 1400\u001B[39m \u001B[43m \u001B[49m\u001B[43mpassword\u001B[49m\u001B[43m=\u001B[49m\u001B[38;5;28;43;01mFalse\u001B[39;49;00m\u001B[43m,\u001B[49m\n\u001B[32m 1401\u001B[39m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n", - "\u001B[36mFile \u001B[39m\u001B[32m~/Documents/EE/Y2/IP3/A.K.03/.venv/lib/python3.13/site-packages/ipykernel/kernelbase.py:1441\u001B[39m, in \u001B[36mKernel._input_request\u001B[39m\u001B[34m(self, prompt, ident, parent, password)\u001B[39m\n\u001B[32m 1438\u001B[39m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mKeyboardInterrupt\u001B[39;00m:\n\u001B[32m 1439\u001B[39m \u001B[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001B[39;00m\n\u001B[32m 1440\u001B[39m msg = \u001B[33m\"\u001B[39m\u001B[33mInterrupted by user\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m-> \u001B[39m\u001B[32m1441\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mKeyboardInterrupt\u001B[39;00m(msg) \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[32m 1442\u001B[39m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mException\u001B[39;00m:\n\u001B[32m 1443\u001B[39m \u001B[38;5;28mself\u001B[39m.log.warning(\u001B[33m\"\u001B[39m\u001B[33mInvalid Message:\u001B[39m\u001B[33m\"\u001B[39m, exc_info=\u001B[38;5;28;01mTrue\u001B[39;00m)\n", - "\u001B[31mKeyboardInterrupt\u001B[39m: Interrupted by user" + "b\"F'\\x10\\n\"\n", + "b'B\\x13\\x88\\n'\n", + "b'R\\t\\xc4\\n'\n", + "b'Cgggg\\n'\n", + "Control the car with W (forward), S (backward), A (left), D (right), E (start beacon), R (stop beacon), and Q (to exit).\n", + "b'M150\\n'\n", + "b'D145\\n'\n", + "b'M150\\n'\n", + "b'D140\\n'\n", + "b'M150\\n'\n", + "b'D135\\n'\n", + "b'M150\\n'\n", + "b'D130\\n'\n", + "b'M150\\n'\n", + "b'D125\\n'\n", + "b'M145\\n'\n", + "b'D125\\n'\n", + "b'M140\\n'\n", + "b'D125\\n'\n", + "b'M150\\n'\n", + "b'D150\\n'\n" ] } ], - "execution_count": 22 + "execution_count": 50 }, { "metadata": {}, @@ -695,7 +782,7 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-11-19T19:27:44.069076Z", + "end_time": "2025-11-21T09:39:10.749553211Z", "start_time": "2025-11-19T19:27:44.033559Z" } },