-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_map.py
More file actions
58 lines (47 loc) · 1.97 KB
/
Copy pathgrid_map.py
File metadata and controls
58 lines (47 loc) · 1.97 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging
import random
from enum import Enum
from patrol_service import Direction
logger = logging.getLogger(__name__)
class CellStatus(Enum):
UNKNOWN = -1
FREE = 0
OCCUPIED = 1
class GridMap:
def __init__(self):
self.grid_size = 10
self.map = [[CellStatus.UNKNOWN for _ in range(self.grid_size)] for _ in range(self.grid_size)]
self.current_position = [self.grid_size // 2, self.grid_size // 2]
self.map[self.current_position[0]][self.current_position[1]] = CellStatus.FREE
self.directions = { Direction.FRONT: (-1, 0), Direction.BACK: (1, 0),
Direction.LEFT: (0, -1), Direction.RIGHT: (0, 1)}
def is_valid(self, pos):
x, y = pos
return 0 <= x < self.grid_size and 0 <= y < self.grid_size
def update_map(self, reading: dict[Direction, CellStatus]) -> None:
x, y = self.current_position
front = x+1, y
if self.is_valid(front):
self.map[x+1][y] = reading[Direction.FRONT]
right = x, y+1
if self.is_valid(right):
self.map[x][y+1] = reading[Direction.RIGHT]
left = x, y-1
if self.is_valid(left):
self.map[x][y-1] = reading[Direction.LEFT]
back = x-1, y
if self.is_valid(back):
self.map[x-1][y] = reading[Direction.BACK]
def move_robot(self):
for move in [Direction.FRONT, Direction.LEFT, Direction.RIGHT, Direction.BACK]:
dx, dy = self.directions[move]
new_pos = [self.current_position[0] + dx, self.current_position[1] + dy]
if not self.is_valid(new_pos):
print("Hit the boundary! Cannot move.")
continue
if self.map[new_pos[0]][new_pos[1]] == CellStatus.OCCUPIED:
continue
self.current_position = new_pos
self.map[new_pos[0]][new_pos[1]] = CellStatus.FREE
logger.info(f"Moved to {new_pos}")
return move