Distance sensor code finished

This commit is contained in:
Thijs Driessen 2025-11-26 15:53:24 +01:00 committed by Nicholas Stănescu
parent cd930318b8
commit 71bca8d47f

View File

@ -325,13 +325,6 @@
"- Also document the motor speed setting (e.g. use this as part of your file name)." "- Also document the motor speed setting (e.g. use this as part of your file name)."
] ]
}, },
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "serial.close()"
},
{ {
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
@ -341,7 +334,6 @@
"### Student Version ###\n", "### Student Version ###\n",
"from pathlib import Path\n", "from pathlib import Path\n",
"\n", "\n",
"# TODO: Open the serial connection to KITT, set the motor speed\n",
"serial = Serial('/dev/rfcomm3', 115200)\n", "serial = Serial('/dev/rfcomm3', 115200)\n",
"serial.write(b'D150\\n')\n", "serial.write(b'D150\\n')\n",
"serial.write(b'M165\\n')\n", "serial.write(b'M165\\n')\n",
@ -360,8 +352,6 @@
" # Read the status response\n", " # Read the status response\n",
" status = serial.read_until(b'\\x04').decode('utf-8')\n", " status = serial.read_until(b'\\x04').decode('utf-8')\n",
" \n", " \n",
" # TODO: Extract the distance values from the status response\n",
"\n",
" dist_L = None\n", " dist_L = None\n",
" dist_R = None\n", " dist_R = None\n",
"\n", "\n",
@ -377,8 +367,6 @@
" dist_R = int(words[5])\n", " dist_R = int(words[5])\n",
" break\n", " break\n",
"\n", "\n",
" # TODO: Record current time and distances\n",
"\n",
" current_time = time.time() - start_time\n", " current_time = time.time() - start_time\n",
" data.append([current_time, dist_L, dist_R])\n", " data.append([current_time, dist_L, dist_R])\n",
" \n", " \n",
@ -396,7 +384,6 @@
"# Close the serial connection\n", "# Close the serial connection\n",
"serial.close()\n", "serial.close()\n",
"\n", "\n",
"# TODO: Write the recorded data to a CSV file\n",
"filepath = Path('./kitt_wall_data_165.csv')\n", "filepath = Path('./kitt_wall_data_165.csv')\n",
"df = pd.DataFrame(data,columns = [\"Time\",\"Distance_L\",\"Distance_R\"])\n", "df = pd.DataFrame(data,columns = [\"Time\",\"Distance_L\",\"Distance_R\"])\n",
"df.to_csv(filepath,index=False)\n", "df.to_csv(filepath,index=False)\n",
@ -446,14 +433,15 @@
"motor_speed_value = 160 # Use the same motor speed as during recording\n", "motor_speed_value = 160 # Use the same motor speed as during recording\n",
"\n", "\n",
"# Load the recorded data from the CSV file\n", "# Load the recorded data from the CSV file\n",
"csv_filename = f'Files/Recordings/kitt_distance_data_{motor_speed_value}.csv'\n", "csv_filename = f'./kitt_wall_data_160.csv'\n",
"data = pd.read_csv(csv_filename)\n", "data = pd.read_csv(csv_filename)\n",
"\n", "\n",
"# Consider discarding the first few readings (inaccurate readings)\n", "# Consider discarding the first few readings (inaccurate readings)\n",
"data = data[2:]\n",
"\n", "\n",
"# TODO: Remove duplicate time stamps and merge L and R data\n",
"# Create a new DataFrame to hold your processed data\n", "# Create a new DataFrame to hold your processed data\n",
"merged_data = []\n", "merged_data = []\n",
"plotting_data = []\n",
"\n", "\n",
"# Iterate over the data\n", "# Iterate over the data\n",
"for index, row in data.iterrows():\n", "for index, row in data.iterrows():\n",
@ -462,16 +450,27 @@
" dist_L = row['Distance_L']\n", " dist_L = row['Distance_L']\n",
" dist_R = row['Distance_R']\n", " dist_R = row['Distance_R']\n",
" \n", " \n",
" # TODO: Decide which distance to use or how to merge them\n", " distance = min(dist_L,dist_R)\n",
" distance = \n",
"\n", "\n",
" merged_data.append([time_stamp, distance])\n", " merged_data.append([time_stamp, distance])\n",
" plotting_data.append([time_stamp, dist_L, dist_R])\n",
"\n", "\n",
"# Convert merged data to DataFrame\n", "# Convert merged data to DataFrame\n",
"merged_df = pd.DataFrame(merged_data, columns=['Time', 'Distance'])\n", "merged_df = pd.DataFrame(merged_data, columns=['Time', 'Distance'])\n",
"plotting_df = pd.DataFrame(plotting_data, columns=['Time', 'Dist_L', 'Dist_R'])\n",
"\n", "\n",
"# TODO: calculate velocity (change in distance over change in time)\n", "# Plotting the distance measured by the left and the right sensor\n",
"merged_df['Velocity'] = \n", "plt.figure()\n",
"plt.plot(plotting_df['Time'], plotting_df['Dist_L'], color='blue', label='Left sensor')\n",
"plt.plot(plotting_df['Time'], plotting_df['Dist_R'], color='red', label='Right sensor')\n",
"plt.xlabel('Time (s)')\n",
"plt.ylabel('Distance (cm)')\n",
"plt.title('Distance from the left and right sensor')\n",
"plt.grid(True)\n",
"plt.legend()\n",
"plt.show()\n",
"\n",
"merged_df['Velocity'] = -merged_df['Distance'].diff().div(merged_df['Time'].diff())\n",
"\n", "\n",
"# Note: Use a negative sign because distance to the wall decreases as KITT moves forward\n", "# Note: Use a negative sign because distance to the wall decreases as KITT moves forward\n",
"\n", "\n",
@ -497,7 +496,9 @@
"plt.title('Velocity of KITT Over Time')\n", "plt.title('Velocity of KITT Over Time')\n",
"plt.grid(True)\n", "plt.grid(True)\n",
"plt.legend()\n", "plt.legend()\n",
"plt.show()" "plt.show()\n",
"\n",
"print(merged_df)"
] ]
}, },
{ {