Your question is Debugging Bottlenecks and Edge Cases. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
A robotics team building on General Intuition's simulation stack uses this collision-avoidance check inside the robot's control loop, which runs on every tick. In testing, the robot works fine most of the time, but it crashes whenever it comes to a complete stop at a waypoint, and separately crashes in open areas where the obstacle sensor reports nothing nearby.
import math
def time_to_collision(robot_pos, robot_vel, obstacle_pos):
dx = obstacle_pos[0] - robot_pos[0]
dy = obstacle_pos[1] - robot_pos[1]
distance = math.sqrt(dx ** 2 + dy ** 2)
speed = math.sqrt(robot_vel[0] ** 2 + robot_vel[1] ** 2)
return distance / speed
def find_nearest_obstacle(robot_pos, obstacles):
nearest = obstacles[0]
nearest_dist = math.sqrt((obstacles[0][0] - robot_pos[0]) ** 2 + (obstacles[0][1] - robot_pos[1]) ** 2)
for obstacle in obstacles:
dist = math.sqrt((obstacle[0] - robot_pos[0]) ** 2 + (obstacle[1] - robot_pos[1]) ** 2)
if dist < nearest_dist:
nearest = obstacle
nearest_dist = dist
return nearest
def plan_step(robot_pos, robot_vel, obstacles):
nearest = find_nearest_obstacle(robot_pos, obstacles)
ttc = time_to_collision(robot_pos, robot_vel, nearest)
if ttc < 2.0:
return "brake"
return "continue"
Debug this snippet: identify the bottleneck and the edge cases it doesn't handle, and explain how each one shows up at runtime.