From 9fda5604ea200a73dda435961c1f6e18017dcde5 Mon Sep 17 00:00:00 2001 From: Thijs Driessen Date: Fri, 21 Nov 2025 10:39:45 +0100 Subject: [PATCH] Made 100 measurements and moved the pics --- Manual/4_Module_2.ipynb | 52 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/Manual/4_Module_2.ipynb b/Manual/4_Module_2.ipynb index db42bb9..f98c81a 100644 --- a/Manual/4_Module_2.ipynb +++ b/Manual/4_Module_2.ipynb @@ -104,14 +104,12 @@ "source": [ "### Student Version ###\n", "\n", - "# TODO: Establish a serial connection, ask for a status report, read it out, and print it\n", "serial = Serial('COM4', 115200)\n", "serial.write(b'Sd\\n')\n", "status = serial.read_until(b\"\\x04\")\n", "status = status.decode('utf-8')\n", "print(f\"Car status is:\\n\\n{status}\")\n", "\n", - "# TODO: Close the serial connection\n", "serial.close()" ] }, @@ -164,13 +162,11 @@ "### Student Version ###\n", "\n", "def extract_dis ():\n", - " # TODO: Decode the status response to a string\n", " serial = Serial('COM4', 115200)\n", " serial.write(b'S\\n')\n", " _status = serial.read_until(b'\\x04')\n", " _status = _status.decode('utf-8')\n", "\n", - " # TODO: Split the status string into lines\n", " lines = _status.splitlines()\n", "\n", " # Initialize variables to hold distance values\n", @@ -180,7 +176,6 @@ " # Iterate over each line to find distance data\n", " for line in lines:\n", " if \"Dist.\" in line:\n", - " # TODO: Split the line into words\n", " words = line.split()\n", " # Extract distance values based on their positions\n", "\n", @@ -193,6 +188,8 @@ " print(f\"Left Distance: {dist_L}\")\n", " print(f\"Right Distance: {dist_R}\")\n", "\n", + " serial.close()\n", + "\n", " return dist_L, dist_R" ] }, @@ -229,7 +226,50 @@ "cell_type": "code", "outputs": [], "execution_count": null, - "source": "### Student Version ###\n" + "source": "serial.close()" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": [ + "### Student Version ###\n", + "times = []\n", + "new_times = []\n", + "total_time = 0\n", + "summed_times = 0\n", + "serial = Serial('COM4', 115200)\n", + "\n", + "for i in range(100):\n", + " start_time = time.time()\n", + " serial.write(b'S\\n')\n", + " _status = serial.read_until(b'\\x04')\n", + " _status = _status.decode('utf-8')\n", + " current_time = time.time() - start_time\n", + " times.append(current_time)\n", + "\n", + "serial.close()\n", + "#print(times)\n", + "\n", + "for j in range(100):\n", + " total_time += times[j]\n", + "\n", + "average = total_time / 100\n", + "\n", + "for k in range(100):\n", + " new_times.append((times[k] - average) ** 2)\n", + " summed_times += new_times[k]\n", + "\n", + "variance = summed_times / 100\n", + "standard_deviation = (variance) ** 0.5\n", + "\n", + "print(f'The average time of a distance measurement is: {average:.3f} [s]')\n", + "print(f'The standard deviantion of a distance measurement is: {standard_deviation:.3f} [s]')\n", + "\n", + "plt.hist(times)\n", + "plt.show()" + ] }, { "metadata": {},