-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor.py
More file actions
36 lines (31 loc) · 1.17 KB
/
sensor.py
File metadata and controls
36 lines (31 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class LidarSensor:
"""
Simulated 2D LIDAR sensor with a circular field of view.
Uses Manhattan distance to define the sensor footprint,
approximating a diamond-shaped scan pattern.
"""
def __init__(self, radius: int = 4):
"""
Args:
radius: Maximum sensing range in grid steps (Manhattan distance).
"""
self.radius = radius
def scan(self, grid, rover_x: int, rover_y: int) -> dict:
"""
Observe all cells within sensor range.
Args:
grid: Ground-truth Grid object.
rover_x: Current rover x position.
rover_y: Current rover y position.
Returns:
Dict mapping (x, y) -> cell value (0 = free, 1 = obstacle).
"""
observations = {}
for dy in range(-self.radius, self.radius + 1):
for dx in range(-self.radius, self.radius + 1):
if abs(dx) + abs(dy) > self.radius:
continue
nx, ny = rover_x + dx, rover_y + dy
if 0 <= nx < grid.width and 0 <= ny < grid.height:
observations[(nx, ny)] = grid.grid[ny][nx]
return observations