mirror of
https://gitlab.ewi.tudelft.nl/ee2l1/2025-2026/A.K.03.git
synced 2025-12-12 13:50:57 +01:00
Distance sensor code finished
This commit is contained in:
parent
cd930318b8
commit
71bca8d47f
@ -325,13 +325,6 @@
|
||||
"- 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": {},
|
||||
"cell_type": "code",
|
||||
@ -341,7 +334,6 @@
|
||||
"### Student Version ###\n",
|
||||
"from pathlib import Path\n",
|
||||
"\n",
|
||||
"# TODO: Open the serial connection to KITT, set the motor speed\n",
|
||||
"serial = Serial('/dev/rfcomm3', 115200)\n",
|
||||
"serial.write(b'D150\\n')\n",
|
||||
"serial.write(b'M165\\n')\n",
|
||||
@ -360,8 +352,6 @@
|
||||
" # Read the status response\n",
|
||||
" status = serial.read_until(b'\\x04').decode('utf-8')\n",
|
||||
" \n",
|
||||
" # TODO: Extract the distance values from the status response\n",
|
||||
"\n",
|
||||
" dist_L = None\n",
|
||||
" dist_R = None\n",
|
||||
"\n",
|
||||
@ -377,8 +367,6 @@
|
||||
" dist_R = int(words[5])\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
" # TODO: Record current time and distances\n",
|
||||
"\n",
|
||||
" current_time = time.time() - start_time\n",
|
||||
" data.append([current_time, dist_L, dist_R])\n",
|
||||
" \n",
|
||||
@ -396,7 +384,6 @@
|
||||
"# Close the serial connection\n",
|
||||
"serial.close()\n",
|
||||
"\n",
|
||||
"# TODO: Write the recorded data to a CSV file\n",
|
||||
"filepath = Path('./kitt_wall_data_165.csv')\n",
|
||||
"df = pd.DataFrame(data,columns = [\"Time\",\"Distance_L\",\"Distance_R\"])\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",
|
||||
"\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",
|
||||
"\n",
|
||||
"# Consider discarding the first few readings (inaccurate readings)\n",
|
||||
"data = data[2:]\n",
|
||||
"\n",
|
||||
"# TODO: Remove duplicate time stamps and merge L and R data\n",
|
||||
"# Create a new DataFrame to hold your processed data\n",
|
||||
"merged_data = []\n",
|
||||
"plotting_data = []\n",
|
||||
"\n",
|
||||
"# Iterate over the data\n",
|
||||
"for index, row in data.iterrows():\n",
|
||||
@ -462,16 +450,27 @@
|
||||
" dist_L = row['Distance_L']\n",
|
||||
" dist_R = row['Distance_R']\n",
|
||||
" \n",
|
||||
" # TODO: Decide which distance to use or how to merge them\n",
|
||||
" distance = \n",
|
||||
" distance = min(dist_L,dist_R)\n",
|
||||
"\n",
|
||||
" merged_data.append([time_stamp, distance])\n",
|
||||
" plotting_data.append([time_stamp, dist_L, dist_R])\n",
|
||||
"\n",
|
||||
"# Convert merged data to DataFrame\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",
|
||||
"# TODO: calculate velocity (change in distance over change in time)\n",
|
||||
"merged_df['Velocity'] = \n",
|
||||
"# Plotting the distance measured by the left and the right sensor\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",
|
||||
"# Note: Use a negative sign because distance to the wall decreases as KITT moves forward\n",
|
||||
"\n",
|
||||
@ -497,7 +496,9 @@
|
||||
"plt.title('Velocity of KITT Over Time')\n",
|
||||
"plt.grid(True)\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
"plt.show()\n",
|
||||
"\n",
|
||||
"print(merged_df)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user