-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_planner.py
More file actions
150 lines (123 loc) · 4.58 KB
/
Copy pathpath_planner.py
File metadata and controls
150 lines (123 loc) · 4.58 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
Path Planner
A*-based grid path planning using traversability map from TerrainClassifier.
Outputs waypoints for rover to follow toward selected target.
"""
import heapq
import numpy as np
import cv2
import logging
from typing import List, Tuple, Optional
log = logging.getLogger("PathPlanner")
# Grid cell cost = 1 / traversability (lower = easier to traverse)
INF = float("inf")
def _heuristic(a: Tuple[int, int], b: Tuple[int, int]) -> float:
return abs(a[0] - b[0]) + abs(a[1] - b[1])
def astar(cost_grid: np.ndarray,
start: Tuple[int, int],
goal: Tuple[int, int]) -> Optional[List[Tuple[int, int]]]:
"""
A* on a cost grid.
cost_grid[r][c] = traversal cost (INF = blocked).
Returns list of (row, col) or None if no path.
"""
rows, cols = cost_grid.shape
open_heap = []
heapq.heappush(open_heap, (0 + _heuristic(start, goal), 0, start))
came_from = {}
g_score = {start: 0.0}
while open_heap:
_, g, current = heapq.heappop(open_heap)
if current == goal:
# Reconstruct path
path = []
while current in came_from:
path.append(current)
current = came_from[current]
path.append(start)
path.reverse()
return path
r, c = current
for dr, dc in [(-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1)]:
nr, nc = r + dr, c + dc
if not (0 <= nr < rows and 0 <= nc < cols):
continue
cell_cost = cost_grid[nr, nc]
if cell_cost >= INF:
continue
step = 1.414 if dr != 0 and dc != 0 else 1.0
new_g = g + cell_cost * step
if new_g < g_score.get((nr, nc), INF):
g_score[(nr, nc)] = new_g
came_from[(nr, nc)] = current
f = new_g + _heuristic((nr, nc), goal)
heapq.heappush(open_heap, (f, new_g, (nr, nc)))
return None
class PathPlanner:
"""
Builds a cost grid from terrain traversability and finds
the optimal path from rover position to the selected target.
"""
def __init__(self, grid_rows: int = 4, grid_cols: int = 6):
self.grid_rows = grid_rows
self.grid_cols = grid_cols
self._last_path: Optional[List[Tuple[int, int]]] = None
def build_cost_grid(self, terrain_grid) -> np.ndarray:
cost = np.zeros((self.grid_rows, self.grid_cols))
for r, row in enumerate(terrain_grid):
for c, patch in enumerate(row):
t = patch.traversability
cost[r, c] = (1.0 / max(t, 0.01)) if t > 0.05 else INF
return cost
def plan(self, terrain_grid,
target_col: int) -> Optional[List[Tuple[int, int]]]:
"""
Plans path from bottom-center of frame (rover position)
to target column at top of frame.
"""
cost_grid = self.build_cost_grid(terrain_grid)
start = (self.grid_rows - 1, self.grid_cols // 2)
goal = (0, int(np.clip(target_col, 0, self.grid_cols - 1)))
path = astar(cost_grid, start, goal)
self._last_path = path
if path:
log.debug(f"Path found: {len(path)} steps → col {target_col}")
else:
log.warning(f"No feasible path to col {target_col}")
return path
def draw_path(self, frame: np.ndarray,
terrain_grid,
path: Optional[List[Tuple[int, int]]]) -> np.ndarray:
if not path:
return frame
h, w = frame.shape[:2]
ph = h // self.grid_rows
pw = w // self.grid_cols
pts = []
for r, c in path:
px = c * pw + pw // 2
py = r * ph + ph // 2
pts.append((px, py))
for i in range(len(pts) - 1):
cv2.line(frame, pts[i], pts[i + 1], (0, 255, 255), 2)
for pt in pts:
cv2.circle(frame, pt, 4, (0, 200, 255), -1)
# Arrow at start
if len(pts) >= 2:
cv2.arrowedLine(frame, pts[-1], pts[-2], (0, 255, 200), 2,
tipLength=0.4)
return frame
def steering_command(self, path: Optional[List[Tuple[int, int]]]) -> str:
"""
Translates first path step into a steering command.
Returns: 'FORWARD' | 'LEFT' | 'RIGHT' | 'STOP'
"""
if not path or len(path) < 2:
return "STOP"
_, c_now = path[0]
_, c_next = path[1]
if c_next < c_now:
return "LEFT"
elif c_next > c_now:
return "RIGHT"
return "FORWARD"