-
Notifications
You must be signed in to change notification settings - Fork 0
/
traj_planner_A_star.py
150 lines (111 loc) · 4.76 KB
/
traj_planner_A_star.py
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
# E206 Motion Planning
# Simple planner
# C Clark
import math
import dubins
import random
import matplotlib.pyplot as plt
from traj_planner_utils import *
import numpy as np
class Node():
def __init__(self, state, parent_node, g_cost, h_cost):
self.state = state
self.parent_node = parent_node
self.g_cost = g_cost
self.h_cost = h_cost
self.f_cost = self.g_cost + self.h_cost
def manhattan_distance_to_node(self, node):
return abs(self.state[1] - node.state[1]) + abs(self.state[2] - node.state[2])
def manhattan_distance_to_state(self, state):
return abs(self.state[1] - state[1]) + abs(self.state[2] - state[2])
def euclidean_distance_to_state(self, state):
return math.sqrt( (self.state[1] - state[1])**2 + (self.state[2] - state[2])**2 )
class A_Star_Planner():
DIST_TO_GOAL_THRESHOLD = 0.5 #m
CHILDREN_DELTAS = [-0.5, -0.25, 0.0, 0.25, 0.5]
DISTANCE_DELTA = 1.5 #m
EDGE_TIME = 10 #s
LARGE_NUMBER = 9999999
def __init__(self):
self.fringe = []
def construct_traj(self, initial_state, desired_state, objects, walls):
""" Construct a trajectory in the X-Y space and in the time-X,Y,Theta space.
Arguments:
traj_point_0 (list of floats): The trajectory's first trajectory point with time, X, Y, Theta (s, m, m, rad).
traj_point_1 (list of floats): The trajectory's last trajectory point with time, X, Y, Theta (s, m, m, rad).
Returns:
traj (list of lists): A list of trajectory points with time, X, Y, Theta (s, m, m, rad).
"""
self.fringe = []
self.desired_state = desired_state
self.objects = objects
self.walls = walls
return []
def add_to_fringe(self, node):
# Add code here.
pass
def get_best_node_on_fringe(self):
return self.fringe.pop(0)
def get_children(self, node_to_expand):
children_list = []
# Add code here.
return children_list
def generate_goal_node(self, node, desired_state):
# Add code here.
return None
def create_node(self, state, parent_node):
# Add code here.
return None #Node(state, parent_node, g_cost, h_cost)
def create_initial_node(self, state):
# Add code here.
return #Node(state, None, g_cost, h_cost)
def calculate_edge_distance(self, state, parent_node):
# Add code here.
return LARGE_NUMBER
def estimate_cost_to_goal(self, state):
return math.sqrt( (self.desired_state[1] - state[1])**2 + (self.desired_state[2] - state[2])**2 )
def build_traj(self, goal_node):
node_list = []
node_to_add = goal_node
while node_to_add != None:
node_list.insert(0, node_to_add)
node_to_add = node_to_add.parent_node
traj = []
for i in range(1,len(node_list)):
node_A = node_list[i-1]
node_B = node_list[i]
traj_point_0 = node_A.state
traj_point_1 = node_B.state
traj_point_1[3] = math.atan2(traj_point_1[2]-traj_point_0[2], traj_point_1[1]-traj_point_0[1])
edge_traj, edge_traj_distance = construct_dubins_traj(traj_point_0, traj_point_1)
traj = traj + edge_traj
return traj
def collision_found(self, node_1, node_2):
""" Return true if there is a collision with the traj between 2 nodes and the workspace
Arguments:
node_1 (Node): A node with the first state of the traj - Time, X, Y, Theta (s, m, m, rad).
node_2 (Node): A node with the second state of the traj - Time, X, Y, Theta (s, m, m, rad).
objects (list of lists): A list of object states - X, Y, radius (m, m, m).
walls (list of lists): A list of walls defined by end points - X0, Y0, X1, Y1, length (m, m, m, m, m).
Returns:
collision_found (boolean): True if there is a collision.
"""
traj, traj_distance = construct_dubins_traj(node_1.state, node_2.state)
return collision_found(traj, self.objects, self.walls)
if __name__ == '__main__':
for i in range(0, 5):
maxR = 10
tp0 = [0, -8, -8, 0]
tp1 = [300, random.uniform(-maxR+1, maxR-1), random.uniform(-maxR+1, maxR-1), 0]
planner = A_Star_Planner()
walls = [[-maxR, maxR, maxR, maxR, 2*maxR], [maxR, maxR, maxR, -maxR, 2*maxR], [maxR, -maxR, -maxR, -maxR, 2*maxR], [-maxR, -maxR, -maxR, maxR, 2*maxR] ]
num_objects = 25
objects = []
for j in range(0, num_objects):
obj = [random.uniform(-maxR+1, maxR-1), random.uniform(-maxR+1, maxR-1), 0.5]
while (abs(obj[0]-tp0[1]) < 1 and abs(obj[1]-tp0[2]) < 1) or (abs(obj[0]-tp1[1]) < 1 and abs(obj[1]-tp1[2]) < 1):
obj = [random.uniform(-maxR+1, maxR-1), random.uniform(-maxR+1, maxR-1), 0.5]
objects.append(obj)
traj = planner.construct_traj(tp0, tp1, objects, walls)
if len(traj) > 0:
plot_traj(traj, traj, objects, walls)