Functioning distance extraction code

# Conflicts:
#	Manual/4_Module_2.ipynb
This commit is contained in:
Thijs Driessen 2025-11-21 09:39:46 +01:00 committed by Nicholas Stănescu
parent 54892b9c6d
commit 1a9b6bbf33

View File

@ -1,8 +1,8 @@
{ {
"cells": [ "cells": [
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"![book header](pictures/header.png)\n", "![book header](pictures/header.png)\n",
"\n", "\n",
@ -18,10 +18,10 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"source": [ "source": [
"# Import necessary libraries\n", "# Import necessary libraries\n",
"import time\n", "import time\n",
@ -32,18 +32,19 @@
"# 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 Manual.KITT_Simulator.sounddevice_simulator import sounddevice\n", "# from KITT_Simulator.serial_simulator import Serial\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."
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"\n", "\n",
"KITT relies on its sensors to drive autonomously. It is equipped with:\n", "KITT relies on its sensors to drive autonomously. It is equipped with:\n",
@ -58,8 +59,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"## Distance Sensors\n", "## Distance Sensors\n",
"\n", "\n",
@ -96,17 +97,17 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"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",
@ -115,8 +116,15 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "serial.close()"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"If you only need distance measurement information, you can request it separately:\n", "If you only need distance measurement information, you can request it separately:\n",
"\n", "\n",
@ -148,17 +156,18 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"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",
@ -172,12 +181,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",
@ -188,8 +197,15 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "extract_dis()"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"2. **Determine how fast you can read out (and process) your distance data** by writing a script that requests the status 100 times. You can calculate the average delay for this operation (and its standard deviation); you could also present the results in a histogram. To measure delays, you will need to keep track of time:\n", "2. **Determine how fast you can read out (and process) your distance data** by writing a script that requests the status 100 times. You can calculate the average delay for this operation (and its standard deviation); you could also present the results in a histogram. To measure delays, you will need to keep track of time:\n",
"\n", "\n",
@ -209,17 +225,15 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"source": [ "execution_count": null,
"### Student Version ###\n" "source": "### Student Version ###\n"
]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"### Step 3: Using Distance Values to Model the Car\n", "### Step 3: Using Distance Values to Model the Car\n",
"\n", "\n",
@ -266,10 +280,10 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"source": [ "source": [
"### Student Version ###\n", "### Student Version ###\n",
"\n", "\n",
@ -315,8 +329,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"#### Processing the Recorded Data\n", "#### Processing the Recorded Data\n",
"\n", "\n",
@ -339,10 +353,10 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"source": [ "source": [
"### Student Version ###\n", "### Student Version ###\n",
"\n", "\n",
@ -404,8 +418,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"**Tips to Consider:**\n", "**Tips to Consider:**\n",
"\n", "\n",
@ -417,10 +431,10 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"source": [ "source": [
"# Define time for continuous measurement (smooth, no gaps)\n", "# Define time for continuous measurement (smooth, no gaps)\n",
"time_continuous = np.linspace(0, 10, 1000) # Time from 0 to 10 seconds, 1000 data points\n", "time_continuous = np.linspace(0, 10, 1000) # Time from 0 to 10 seconds, 1000 data points\n",
@ -443,8 +457,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"### Step 5: Implementing the Distance Sensor Reading in Your KITT Class\n", "### Step 5: Implementing the Distance Sensor Reading in Your KITT Class\n",
"\n", "\n",
@ -466,8 +480,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"## The Microphones\n", "## The Microphones\n",
"\n", "\n",
@ -499,10 +513,10 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"source": [ "source": [
"sounddevice_handle = sounddevice()\n", "sounddevice_handle = sounddevice()\n",
"for i in range(sounddevice_handle.get_device_count()):\n", "for i in range(sounddevice_handle.get_device_count()):\n",
@ -511,8 +525,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"Once you have identified the index of the microphone array device from the list, you can initialize it by specifying the device index (`device_index`) and the desired sampling frequency (`Fs`).\n", "Once you have identified the index of the microphone array device from the list, you can initialize it by specifying the device index (`device_index`) and the desired sampling frequency (`Fs`).\n",
"\n", "\n",
@ -520,10 +534,10 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"source": [ "source": [
"# Initialize sounddevice again\n", "# Initialize sounddevice again\n",
"sounddevice_handle = sounddevice()\n", "sounddevice_handle = sounddevice()\n",
@ -541,8 +555,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"### Step 2: Recording audio data\n", "### Step 2: Recording audio data\n",
"\n", "\n",
@ -554,10 +568,10 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"source": [ "source": [
"Fs = 44100 # Sampling frequency\n", "Fs = 44100 # Sampling frequency\n",
"N = 2*Fs # 2 seconds of audio data\n", "N = 2*Fs # 2 seconds of audio data\n",
@ -565,8 +579,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"At this point, the microphone data is **interleaved**. This means that the first value (`data[0]`) corresponds to the first sample of microphone 0, the second value (`data[1]`) corresponds to the first sample of microphone 1, and so on. For example, `data[5]` contains the second sample of microphone 0, and the pattern continues. To visualize the interleaving of the data, refer to the table below:\n", "At this point, the microphone data is **interleaved**. This means that the first value (`data[0]`) corresponds to the first sample of microphone 0, the second value (`data[1]`) corresponds to the first sample of microphone 1, and so on. For example, `data[5]` contains the second sample of microphone 0, and the pattern continues. To visualize the interleaving of the data, refer to the table below:\n",
"\n", "\n",
@ -583,10 +597,10 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"source": [ "source": [
"### Student Version ###\n", "### Student Version ###\n",
"\n", "\n",
@ -594,8 +608,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"### Plotting the audio data\n", "### Plotting the audio data\n",
"\n", "\n",
@ -603,10 +617,10 @@
] ]
}, },
{ {
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"cell_type": "code",
"outputs": [], "outputs": [],
"execution_count": null,
"source": [ "source": [
"### Student Version ###\n", "### Student Version ###\n",
"\n", "\n",
@ -614,8 +628,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"### Step 3: Testing the microphone array\n", "### Step 3: Testing the microphone array\n",
"Below are some experiments you can perform to test the microphone array, and develop the code.\n", "Below are some experiments you can perform to test the microphone array, and develop the code.\n",
@ -632,8 +646,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"*Bonus Tasks - Optional*\n", "*Bonus Tasks - Optional*\n",
"\n", "\n",
@ -644,8 +658,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"### Mid-term assessment 2.2 and report\n", "### Mid-term assessment 2.2 and report\n",
"\n", "\n",
@ -672,8 +686,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"## FAQ\n", "## FAQ\n",
"\n", "\n",
@ -698,8 +712,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"**I see random numbers from sensors for large distances. Is my sensor damaged ?**\n", "**I see random numbers from sensors for large distances. Is my sensor damaged ?**\n",
"\n", "\n",
@ -707,8 +721,8 @@
] ]
}, },
{ {
"cell_type": "markdown",
"metadata": {}, "metadata": {},
"cell_type": "markdown",
"source": [ "source": [
"**Are the ultrasonic sensor measurements for the left and the right side done at exactly the same time ?**\n", "**Are the ultrasonic sensor measurements for the left and the right side done at exactly the same time ?**\n",
"\n", "\n",