Skip to content

Commit a968a9b

Browse files
committed
Simplifying the Astar Code
1 parent 935276d commit a968a9b

File tree

3 files changed

+71
-39
lines changed

3 files changed

+71
-39
lines changed

python/utils/make_graph.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ def __init__(self,start,goal,obs_node,RPM,grid_size):
1515
self.start = start
1616
self.goal = goal
1717
self.obs_node = obs_node
18-
self.drive = NonHolonomicDrive(RPM,grid_size,send_int_nodes=True)
18+
19+
self.drive = NonHolonomicDrive(RPM,grid_size)
1920

2021
self.fig, self.ax = plt.subplots()
2122

22-
def animate(self,algorithm,visited,path):
23+
def animate(self,algorithm,path):
2324
self.plot_canvas(algorithm)
24-
# self.plot_visited(visited)
25+
self.plot_visited(path)
2526
self.shortest_path(path)
2627
plt.show()
2728

@@ -80,27 +81,48 @@ def plot_canvas(self,algorithm):
8081
def shortest_path(self,path):
8182

8283
for i in reversed(range(1,len(path))):
83-
start = path[i][0]
84-
end = path[i-1][0]
85-
theta = path[i][1]
84+
start = path[i]["vertex"]
85+
end = path[i-1]["vertex"]
86+
theta = path[i]["orientation"]
8687
# theta = np.rad2deg(math.atan2(end.y-start.y,end.x-start.x))%360
87-
neighbours = self.drive.get_neighbours(start,theta)
88+
int_node_x = []
89+
int_node_y = []
90+
for x,y in path[i]["intermediate_nodes"]:
91+
92+
int_node_x.append(round(x,2))
93+
int_node_y.append(round(y,2))
94+
plt.plot(int_node_x, int_node_y,color="r")
95+
plt.pause(0.01)
8896

89-
for key,value in neighbours.items():
97+
plt.scatter(self.start.x,self.start.y,color="magenta")
98+
plt.scatter(self.goal.x,self.goal.y,color="blue")
99+
plt.pause(0.01)
90100

91-
(_,_,int_nodes) = value
101+
def plot_visited(self,path):
102+
103+
for i in reversed(range(1,len(path))):
104+
start = path[i]["vertex"]
105+
end = path[i-1]["vertex"]
106+
theta = path[i]["orientation"]
107+
108+
neighbours = self.drive.get_neighbours(start,theta)
109+
# theta = np.rad2deg(math.atan2(end.y-start.y,end.x-start.x))%360
110+
for _,value in neighbours.items():
111+
112+
int_nodes = value["intermediate_nodes"]
92113

93114
int_node_x = []
94115
int_node_y = []
95116
for i in range(len(int_nodes)):
96117
int_node_x.append(round(int_nodes[i][0],2))
97118
int_node_y.append(round(int_nodes[i][1],2))
98-
plt.plot(int_node_x, int_node_y,color="r")
119+
plt.plot(int_node_x, int_node_y,color="g")
99120
plt.pause(0.01)
100121

101122
plt.scatter(self.start.x,self.start.y,color="magenta")
102123
plt.scatter(self.goal.x,self.goal.y,color="blue")
103124
plt.pause(0.01)
104125

105126

127+
106128

python/utils/non_holonomic.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
class NonHolonomicDrive:
66

7-
def __init__(self,RPM,grid_size,r = 3.8,L = 35,time_run = 1,send_int_nodes=False):
7+
def __init__(self,RPM,grid_size,r = 3.8,L = 35,time_run = 1):
88

99
self.RPM_L = RPM[0]
1010
self.RPM_R = RPM[1]
1111
self.grid_size = grid_size
1212
self.r = r
1313
self.L = L
1414
self.time_run = time_run
15-
self.send_int_nodes = send_int_nodes
1615

1716
def get_neighbours(self,current_node,current_orientation):
1817

@@ -26,13 +25,13 @@ def get_neighbours(self,current_node,current_orientation):
2625

2726
nbr,_,int_nodes = self.MoveRobot(current_node,current_orientation,RPM[0],RPM[1])
2827
robot_twist = self.convert_rpm_to_robospeed(RPM,self.r,self.L)
28+
2929
if self.grid_size[0][0] <= nbr[2].x <= self.grid_size[0][1] and self.grid_size[1][0] <= nbr[2].y <= self.grid_size[1][1]:
30-
31-
if not self.send_int_nodes:
32-
all_neighbours[nbr[2]] = (nbr[0],nbr[1],robot_twist)
33-
else:
34-
all_neighbours[nbr[2]] = (nbr[0],nbr[1],int_nodes)
35-
30+
all_neighbours[nbr[2]] = {"cost":nbr[0],
31+
"orientation":nbr[1],
32+
"twist":robot_twist,
33+
"intermediate_nodes":int_nodes}
34+
3635
return all_neighbours
3736

3837
def MoveRobot(self,current_node,current_orientation,RPM_L,RPM_R):

scripts/SearchBased/HeuristicSearch/Astar.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ def plan(self):
7373

7474
# If the neighbour is not already visited and if not in Obstacle space
7575
if not self.graph.checkObstacleSpace(nbr_node) and not check_NodeIn_list(nbr_node,self.CLOSED):
76-
movement_cost, new_orientation,turtlebot_twist = node_values
77-
new_orientation = new_orientation%360
76+
77+
movement_cost = node_values["cost"]
78+
new_orientation = node_values["orientation"]%360
79+
turtlebot_twist = node_values["twist"]
80+
7881
if self.V[int(2*nbr_node.x)][int(2*nbr_node.y)][self.orientation[new_orientation]] == 0:
7982

8083
self.V[int(2*nbr_node.x)][int(2*nbr_node.y)][self.orientation[new_orientation]] = 1
@@ -89,7 +92,10 @@ def plan(self):
8992

9093
self.OPEN.insert_pq(self.f[int(2*nbr_node.x)][int(2*nbr_node.y)][self.orientation[new_orientation]],(nbr_node,new_orientation))
9194
self.backtrack_node[nbr_node]={}
92-
self.backtrack_node[nbr_node][self.f[int(2*nbr_node.x)][int(2*nbr_node.y)][self.orientation[new_orientation]]] = (current_vtx[0],current_vtx[1],turtlebot_twist)
95+
self.backtrack_node[nbr_node][self.f[int(2*nbr_node.x)][int(2*nbr_node.y)][self.orientation[new_orientation]]] = {"vertex":current_vtx[0],
96+
"orientation":current_vtx[1],
97+
"twist":turtlebot_twist,
98+
"intermediate_nodes":node_values["intermediate_nodes"]}
9399

94100
if euclidean_heuristic(nbr_node,self.goal) <= 1.5: # Check if goal node is reached
95101
print("The goal node is found")
@@ -99,7 +105,10 @@ def plan(self):
99105
if self.f[int(2*nbr_node.x)][int(2*nbr_node.y)][self.orientation[new_orientation]] > self.f[int(2*current_vtx[0].x)][int(2*current_vtx[0].y)][self.orientation[current_vtx[1]]]:
100106
self.f[int(2*nbr_node.x)][int(2*nbr_node.y)][self.orientation[new_orientation]] = self.f[int(2*current_vtx[0].x)][int(2*current_vtx[0].y)][self.orientation[current_vtx[1]]]
101107
same_nbr_node = getSameNode(nbr_node,list(self.backtrack_node.keys()))
102-
self.backtrack_node[same_nbr_node][self.f[int(2*nbr_node.x)][int(2*nbr_node.y)][self.orientation[new_orientation]]] = (current_vtx[0],current_vtx[1],turtlebot_twist)
108+
self.backtrack_node[same_nbr_node][self.f[int(2*nbr_node.x)][int(2*nbr_node.y)][self.orientation[new_orientation]]] = {"vertex":current_vtx[0],
109+
"orientation":current_vtx[1],
110+
"twist":turtlebot_twist,
111+
"intermediate_nodes":node_values["intermediate_nodes"]}
103112

104113

105114
# If a path Doesn't exit
@@ -114,23 +123,25 @@ def extract_path(self,vertex):
114123
bkt_list -- List of nodes in the shortest path
115124
'''
116125

117-
bkt_list=[]
118-
angle_node_list = []
119-
bkt_list.append(self.goal)
126+
goal_node = {"vertex":self.goal,
127+
"orientation":None,
128+
"twist":None,
129+
"intermediate_nodes":None}
130+
131+
angle_node_list = [goal_node]
120132
node = vertex
121133
# loops till goal is not equal to zero
122134
while node!=0:
123135
for nbr,values in reversed(list(self.backtrack_node.items())):
124136
# if nbr and goal are same
125-
for cost,parent in values.items():
137+
for _,parent in values.items():
126138

127139
if check_nodes(nbr,node):
128-
if not check_NodeIn_list(parent[0],bkt_list):
129-
bkt_list.append(parent[0])
140+
if not check_NodeIn_list(parent["vertex"],[node["vertex"] for node in angle_node_list]):
130141
angle_node_list.append(parent)
131-
node=parent[0]
142+
node=parent["vertex"]
132143

133-
if check_nodes(parent[0],self.start[0]):
144+
if check_nodes(parent["vertex"],self.start[0]):
134145
node=0
135146
return angle_node_list
136147

@@ -189,28 +200,28 @@ def get_grid_size(obstacle_list):
189200

190201
mp_data = motion_planning()
191202

192-
for i in shortest_path:
203+
for i in shortest_path[1:]:
193204

194205
path_pt = Point()
195-
path_pt.x = i[0].x
196-
path_pt.y = i[0].y
206+
path_pt.x = i["vertex"].x
207+
path_pt.y = i["vertex"].y
197208
path_pt.z = 0
198209
mp_data.path.append(path_pt)
199210

200-
mp_data.orientation.append(i[1])
211+
mp_data.orientation.append(i["orientation"])
201212

202213
twist_pt = Twist()
203-
twist_pt.angular.z = i[-1][0]
204-
twist_pt.linear.x = i[-1][1]
214+
twist_pt.angular.z = i["twist"][0]
215+
twist_pt.linear.x = i["twist"][1]
205216

206217
mp_data.turtlebot_twist.append(twist_pt)
207218

208219
mp_pub.publish(mp_data)
209220

210-
# plot_result = Visualize(start[0],goal,obs_locations,RPM,grid_size)
211-
# plot_result.animate("Astar",None,shortest_path)
221+
plot_result = Visualize(start[0],goal,obs_locations,RPM,grid_size)
222+
plot_result.animate("Astar",shortest_path)
212223

213224
for i in shortest_path:
214-
rospy.loginfo(f"The x : {i[0].x} and y : {i[0].y} and orientation {i[1]}")
225+
rospy.loginfo(f"The x coordinate : {i['vertex'].x} and y coordinate : {i['vertex'].y} and orientation : {i['orientation']}")
215226

216227

0 commit comments

Comments
 (0)