Started working on the distance sensor code

This commit is contained in:
Thijs Driessen 2025-11-20 15:20:56 +01:00 committed by Nicholas Stănescu
parent bc87f43be8
commit 54892b9c6d
3 changed files with 33 additions and 20 deletions

View File

@ -104,8 +104,14 @@
"### 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",
"status = status.decode('utf-8')\n",
"print(f\"Car status is:\\n\\n{status}\")\n",
"\n",
"# TODO: Close the serial connection"
"# TODO: Close the serial connection\n",
"serial.close()"
]
},
{
@ -149,30 +155,36 @@
"source": [
"### Student Version ###\n",
"\n",
"# TODO: Decode the status response to a string\n",
"def extract_dis ():\n",
" # TODO: Decode the status response to a string\n",
" serial.write(b'S\\n')\n",
" _status = serial.read_until(b'\\n')\n",
" _status = _status.decode('utf-8')\n",
"\n",
"# TODO: Split the status string into lines\n",
"lines = \n",
" # TODO: Split the status string into lines\n",
" lines = _status.splitlines()\n",
"\n",
"# Initialize variables to hold distance values\n",
"dist_L = None\n",
"dist_R = None\n",
" # Initialize variables to hold distance values\n",
" dist_L = None\n",
" dist_R = None\n",
"\n",
"# 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 =\n",
" # Extract distance values based on their positions\n",
" # 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 = lines.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",
" break # Exit the loop after finding the distances\n",
" # Assign dist_L and dist_R accordingly\n",
" dist_L =\n",
" dist_R =\n",
" break # Exit the loop after finding the distances\n",
"\n",
"# Print the extracted distance values\n",
"print(f\"Left Distance: {dist_L}\")\n",
"print(f\"Right Distance: {dist_R}\")"
" # Print the extracted distance values\n",
" print(f\"Left Distance: {dist_L}\")\n",
" print(f\"Right Distance: {dist_R}\")\n",
"\n",
" return dist_L, dist_R"
]
},
{
@ -263,6 +275,7 @@
"\n",
"# TODO: Open the serial connection to KITT, set the motor speed\n",
"\n",
"\n",
"# Initialize a list to store recorded data\n",
"data = []\n",
"\n",

View File