@@ -100,8 +100,9 @@ def planning(self, sx, sy, gx, gy):
100100 self .calc_grid_position (current_B .y , self .miny ), "xc" )
101101 # for stopping simulation with the esc key.
102102 plt .gcf ().canvas .mpl_connect ('key_release_event' ,
103- lambda event : [exit (
104- 0 ) if event .key == 'escape' else None ])
103+ lambda event :
104+ [exit (0 ) if event .key == 'escape'
105+ else None ])
105106 if len (closed_set_A .keys ()) % 10 == 0 :
106107 plt .pause (0.001 )
107108
@@ -121,61 +122,50 @@ def planning(self, sx, sy, gx, gy):
121122
122123 # expand_grid search grid based on motion model
123124 for i , _ in enumerate (self .motion ):
124- continue_A = False
125- continue_B = False
126125
127- child_node_A = self .Node (current_A .x + self .motion [i ][0 ],
128- current_A .y + self .motion [i ][1 ],
129- current_A .cost + self .motion [i ][2 ],
130- c_id_A )
126+ c_nodes = [self .Node (current_A .x + self .motion [i ][0 ],
127+ current_A .y + self .motion [i ][1 ],
128+ current_A .cost + self .motion [i ][2 ],
129+ c_id_A ),
130+ self .Node (current_B .x + self .motion [i ][0 ],
131+ current_B .y + self .motion [i ][1 ],
132+ current_B .cost + self .motion [i ][2 ],
133+ c_id_B )]
131134
132- child_node_B = self .Node (current_B .x + self .motion [i ][0 ],
133- current_B .y + self .motion [i ][1 ],
134- current_B .cost + self .motion [i ][2 ],
135- c_id_B )
136-
137- n_id_A = self .calc_grid_index (child_node_A )
138- n_id_B = self .calc_grid_index (child_node_B )
135+ n_ids = [self .calc_grid_index (c_nodes [0 ]),
136+ self .calc_grid_index (c_nodes [1 ])]
139137
140138 # If the node is not safe, do nothing
141- if not self .verify_node (child_node_A ):
142- continue_A = True
143-
144- if not self .verify_node (child_node_B ):
145- continue_B = True
146-
147- if n_id_A in closed_set_A :
148- continue_A = True
149-
150- if n_id_B in closed_set_B :
151- continue_B = True
139+ continue_ = self .check_nodes_and_sets (c_nodes , closed_set_A ,
140+ closed_set_B , n_ids )
152141
153- if not continue_A :
154- if n_id_A not in open_set_A :
142+ if not continue_ [ 0 ] :
143+ if n_ids [ 0 ] not in open_set_A :
155144 # discovered a new node
156- open_set_A [n_id_A ] = child_node_A
145+ open_set_A [n_ids [ 0 ]] = c_nodes [ 0 ]
157146 else :
158- if open_set_A [n_id_A ] .cost > child_node_A .cost :
147+ if open_set_A [n_ids [ 0 ]] .cost > c_nodes [ 0 ] .cost :
159148 # This path is the best until now. record it
160- open_set_A [n_id_A ] = child_node_A
149+ open_set_A [n_ids [ 0 ]] = c_nodes [ 0 ]
161150
162- if not continue_B :
163- if n_id_B not in open_set_B :
151+ if not continue_ [ 1 ] :
152+ if n_ids [ 1 ] not in open_set_B :
164153 # discovered a new node
165- open_set_B [n_id_B ] = child_node_B
154+ open_set_B [n_ids [ 1 ]] = c_nodes [ 1 ]
166155 else :
167- if open_set_B [n_id_B ] .cost > child_node_B .cost :
156+ if open_set_B [n_ids [ 1 ]] .cost > c_nodes [ 1 ] .cost :
168157 # This path is the best until now. record it
169- open_set_B [n_id_B ] = child_node_B
158+ open_set_B [n_ids [ 1 ]] = c_nodes [ 1 ]
170159
171160 rx , ry = self .calc_final_bidirectional_path (
172161 meetpointA , meetpointB , closed_set_A , closed_set_B )
173162
174163 return rx , ry
175164
176- def calc_final_bidirectional_path (self , meetnode_A , meetnode_B , closed_set_A , closed_set_B ):
177- rx_A , ry_A = self .calc_final_path (meetnode_A , closed_set_A )
178- rx_B , ry_B = self .calc_final_path (meetnode_B , closed_set_B )
165+ # takes two sets and two meeting nodes and return the optimal path
166+ def calc_final_bidirectional_path (self , n1 , n2 , setA , setB ):
167+ rx_A , ry_A = self .calc_final_path (n1 , setA )
168+ rx_B , ry_B = self .calc_final_path (n2 , setB )
179169
180170 rx_A .reverse ()
181171 ry_A .reverse ()
@@ -198,6 +188,16 @@ def calc_final_path(self, ngoal, closedset):
198188
199189 return rx , ry
200190
191+ def check_nodes_and_sets (self , c_nodes , closedSet_A , closedSet_B , n_ids ):
192+ continue_ = [False , False ]
193+ if not self .verify_node (c_nodes [0 ]) or n_ids [0 ] in closedSet_A :
194+ continue_ [0 ] = True
195+
196+ if not self .verify_node (c_nodes [1 ]) or n_ids [1 ] in closedSet_B :
197+ continue_ [1 ] = True
198+
199+ return continue_
200+
201201 @staticmethod
202202 def calc_heuristic (n1 , n2 ):
203203 w = 1.0 # weight of heuristic
0 commit comments