Skip to content

Commit 14b49f3

Browse files
committed
minor improvements to graphing
1 parent 596abc4 commit 14b49f3

1 file changed

Lines changed: 35 additions & 32 deletions

File tree

testing/test_hybrid_astar.py

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
import math
1616
import unittest
1717

18-
def gen_obstacle():
19-
pose = ObjectPose(frame=ObjectFrameEnum(3),
20-
t=0, x = 2.5, y=2.5)
21-
dimensions = (1,1,1)
18+
def gen_obstacle(num_obstacles = 1, xrange = 5, yrange=5):
19+
obstacles = []
20+
for _ in range(num_obstacles):
21+
pose = ObjectPose(frame=ObjectFrameEnum(3),
22+
t=0, x = np.random.uniform(0,xrange), y=np.random.uniform(0,yrange))
23+
dimensions = (1,1,1)
24+
obstacles.append(PhysicalObject(pose, dimensions, None))
2225

23-
return [PhysicalObject(pose, dimensions, None)]
26+
return obstacles
2427

2528
class ParkingSolver(AStar):
2629
"""sample use of the astar algorithm. In this exemple we work on a maze made of ascii characters,
@@ -30,11 +33,11 @@ def __init__(self, obstacles):
3033
self.obstacles = obstacles
3134

3235
self.vehicle = DubinsCar() #x = (tx,ty,theta) and u = (fwd_velocity,turnRate).
33-
self.vehicle_sim = DubinsCarIntegrator(self.vehicle, .5, 0.1)
36+
self.vehicle_sim = DubinsCarIntegrator(self.vehicle, 1, 0.1)
3437
self.actions = [(1, -1), (1, -0.5), (1,0), (1, 0.5), (1,1)]
3538

3639
def is_goal_reached(self, current, goal):
37-
return np.linalg.norm(np.array(current) - np.array(goal)) < 0.5
40+
return np.linalg.norm(np.array(current) - np.array(goal)) < .5
3841

3942
def heuristic_cost_estimate(self, n1, n2):
4043
"""computes the 'direct' distance between two (x,y) tuples"""
@@ -67,53 +70,53 @@ def is_valid_neighbor(self, path):
6770
"""
6871
for obstacle in self.obstacles:
6972
for point in path:
70-
print(point)
71-
print(obstacle.polygon_parent())
73+
# print(point)
74+
# print(obstacle.polygon_parent())
7275
if collisions.circle_intersects_polygon_2d(point[:-1], 1, obstacle.polygon_parent()):
7376
return False
74-
7577
return True
7678

7779
def solve():
7880
# generate obstacle
79-
obstacles = gen_obstacle()
81+
obstacles = gen_obstacle(3)
8082

81-
start = (1, 1, 0) # we choose to start at the upper left corner
83+
start = (0, 0, 0) # we choose to start at the upper left corner
8284
goal = (5, 5, 0) # we want to reach the lower right corner
8385

8486
# let's solve it
8587
foundPath = list(ParkingSolver(obstacles).astar(start, goal))
8688

87-
plot_path(obstacles[0], foundPath)
89+
plot_path(obstacles, foundPath)
8890

8991
return list(foundPath)
9092

91-
def plot_path(obstacle, path):
92-
import matplotlib.pyplot as plt
93+
def plot_path(obstacles, path):
9394
x = [point[0] for point in path]
9495
y = [point[1] for point in path]
9596
plt.plot(x,y)
9697

97-
l, w, h = obstacle.dimensions
98-
center = [obstacle.pose.x, obstacle.pose.y]
99-
100-
vertices = np.array([
101-
[center[0] - w / 2, center[1] - l / 2], # Bottom-left
102-
[center[0] + w / 2, center[1] - l / 2], # Bottom-right
103-
[center[0] + w / 2, center[1] + l / 2], # Top-right
104-
[center[0] - w / 2, center[1] + l / 2], # Top-left
105-
[center[0] - w / 2, center[1] - l / 2] # Close the polygon
106-
])
107-
108-
# Plot the polygon
109-
plt.plot(vertices[:, 0], vertices[:, 1], 'b-', linewidth=2)
110-
plt.fill(vertices[:, 0], vertices[:, 1], 'b', alpha=0.3) # Optional fill
111-
plt.scatter(*center, color='red', label="Center") # Mark the center
98+
for obstacle in obstacles:
99+
l, w, h = obstacle.dimensions
100+
center = [obstacle.pose.x, obstacle.pose.y]
101+
102+
vertices = np.array([
103+
[center[0] - w / 2, center[1] - l / 2], # Bottom-left
104+
[center[0] + w / 2, center[1] - l / 2], # Bottom-right
105+
[center[0] + w / 2, center[1] + l / 2], # Top-right
106+
[center[0] - w / 2, center[1] + l / 2], # Top-left
107+
[center[0] - w / 2, center[1] - l / 2] # Close the polygon
108+
])
109+
110+
# Plot the polygon
111+
plt.plot(vertices[:, 0], vertices[:, 1], 'b-', linewidth=2)
112+
plt.fill(vertices[:, 0], vertices[:, 1], 'b', alpha=0.3) # Optional fill
113+
#plt.scatter(*center, color='red', label="Center") # Mark the center
114+
112115
plt.axis('equal')
113-
plt.legend()
116+
#plt.legend()
114117
plt.grid(True)
115118
plt.show()
116119

117120
if __name__ == '__main__':
118121
solution = solve()
119-
print(solve())
122+
print(solution)

0 commit comments

Comments
 (0)