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": [
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"![book header](pictures/header.png)\n",
"\n",
@ -18,10 +18,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# Import necessary libraries\n",
"import time\n",
@ -32,18 +32,19 @@
"# Uncomment one of the following lines depending on your setup\n",
"\n",
"# If you are using the real car, uncomment the next lines and comment the simulator lines\n",
"# from serial import Serial\n",
"# import sounddevice\n",
"from serial import Serial\n",
"import sounddevice\n",
"\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",
"# Note: After changing the import statement, you need to restart the kernel for changes to take effect."
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"\n",
"KITT relies on its sensors to drive autonomously. It is equipped with:\n",
@ -58,8 +59,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"## Distance Sensors\n",
"\n",
@ -96,17 +97,17 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"### Student Version ###\n",
"\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.write(b'S\\n')\n",
"status = serial.read_until(b'\\n')\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",
@ -115,8 +116,15 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "serial.close()"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"If you only need distance measurement information, you can request it separately:\n",
"\n",
@ -148,17 +156,18 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"### 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'\\n')\n",
" _status = serial.read_until(b'\\x04')\n",
" _status = _status.decode('utf-8')\n",
"\n",
" # TODO: Split the status string into lines\n",
@ -172,12 +181,12 @@
" for line in lines:\n",
" if \"Dist.\" in line:\n",
" # TODO: Split the line into words\n",
" words = lines.split()\n",
" words = line.split()\n",
" # Extract distance values based on their positions\n",
"\n",
" # Assign dist_L and dist_R accordingly\n",
" dist_L =\n",
" dist_R =\n",
" dist_L = words[3]\n",
" dist_R = words[5]\n",
" break # Exit the loop after finding the distances\n",
"\n",
" # Print the extracted distance values\n",
@ -188,8 +197,15 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "extract_dis()"
},
{
"metadata": {},
"cell_type": "markdown",
"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",
"\n",
@ -209,17 +225,15 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"source": [
"### Student Version ###\n"
]
"execution_count": null,
"source": "### Student Version ###\n"
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"### Step 3: Using Distance Values to Model the Car\n",
"\n",
@ -266,10 +280,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"### Student Version ###\n",
"\n",
@ -315,8 +329,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"#### Processing the Recorded Data\n",
"\n",
@ -339,10 +353,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"### Student Version ###\n",
"\n",
@ -404,8 +418,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"**Tips to Consider:**\n",
"\n",
@ -417,10 +431,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# 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",
@ -443,8 +457,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"### Step 5: Implementing the Distance Sensor Reading in Your KITT Class\n",
"\n",
@ -466,8 +480,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"## The Microphones\n",
"\n",
@ -499,10 +513,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"sounddevice_handle = sounddevice()\n",
"for i in range(sounddevice_handle.get_device_count()):\n",
@ -511,8 +525,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"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",
"\n",
@ -520,10 +534,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# Initialize sounddevice again\n",
"sounddevice_handle = sounddevice()\n",
@ -541,8 +555,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"### Step 2: Recording audio data\n",
"\n",
@ -554,10 +568,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"Fs = 44100 # Sampling frequency\n",
"N = 2*Fs # 2 seconds of audio data\n",
@ -565,8 +579,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"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",
"\n",
@ -583,10 +597,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"### Student Version ###\n",
"\n",
@ -594,8 +608,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"### Plotting the audio data\n",
"\n",
@ -603,10 +617,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"### Student Version ###\n",
"\n",
@ -614,8 +628,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"### Step 3: Testing the microphone array\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": {},
"cell_type": "markdown",
"source": [
"*Bonus Tasks - Optional*\n",
"\n",
@ -644,8 +658,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"### Mid-term assessment 2.2 and report\n",
"\n",
@ -672,8 +686,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"## FAQ\n",
"\n",
@ -698,8 +712,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"**I see random numbers from sensors for large distances. Is my sensor damaged ?**\n",
"\n",
@ -707,8 +721,8 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "markdown",
"source": [
"**Are the ultrasonic sensor measurements for the left and the right side done at exactly the same time ?**\n",
"\n",